i18n
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
56213ebc03
commit
183bbfb780
|
@ -1,58 +1,63 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
|
import yaml
|
||||||
|
|
||||||
SOURCE_DIR = "docs"
|
SOURCE_DIR = "docs"
|
||||||
ENGLISH_DIR = "docs/en"
|
ENGLISH_DIR = os.path.join(SOURCE_DIR, "en")
|
||||||
|
MKDOCS_YML = "mkdocs.yml"
|
||||||
|
|
||||||
|
# 1. Kopiera alla filer från docs/ till docs/en/
|
||||||
if os.path.exists(ENGLISH_DIR):
|
if os.path.exists(ENGLISH_DIR):
|
||||||
shutil.rmtree(ENGLISH_DIR)
|
shutil.rmtree(ENGLISH_DIR)
|
||||||
shutil.copytree(SOURCE_DIR, ENGLISH_DIR)
|
shutil.copytree(SOURCE_DIR, ENGLISH_DIR)
|
||||||
|
|
||||||
# Hantera block i formatet:
|
|
||||||
# :sv
|
|
||||||
# text...
|
|
||||||
# :::
|
|
||||||
def remove_other_language_blocks(content, keep_lang):
|
|
||||||
block_pattern = re.compile(r":(sv|en)\n(.*?)\n:::", re.DOTALL)
|
|
||||||
|
|
||||||
result = []
|
|
||||||
pos = 0
|
|
||||||
for match in block_pattern.finditer(content):
|
|
||||||
start, end = match.span()
|
|
||||||
lang, block = match.groups()
|
|
||||||
|
|
||||||
# Behåll neutral text mellan block
|
|
||||||
result.append(content[pos:start])
|
|
||||||
if lang == keep_lang:
|
|
||||||
result.append(block.strip() + "\n\n")
|
|
||||||
pos = end
|
|
||||||
result.append(content[pos:])
|
|
||||||
return ''.join(result)
|
|
||||||
|
|
||||||
# Inline-taggar, t.ex. [:sv]text
|
|
||||||
def remove_other_language_inline(content, keep_lang):
|
|
||||||
def replacer(match):
|
|
||||||
lang, text = match.groups()
|
|
||||||
return text if lang == keep_lang else ""
|
|
||||||
return re.sub(r"\[:(sv|en)](.*?)(?=(\[:|$))", replacer, content)
|
|
||||||
|
|
||||||
def clean_file(path, keep_lang):
|
def clean_file(path, keep_lang):
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
content = remove_other_language_blocks(content, keep_lang)
|
# 2. Rensa block: :sv ... ::: eller :en ... :::
|
||||||
|
if keep_lang == "sv":
|
||||||
|
content = re.sub(r":en\n.*?\n:::", "", content, flags=re.DOTALL)
|
||||||
|
elif keep_lang == "en":
|
||||||
|
content = re.sub(r":sv\n.*?\n:::", "", content, flags=re.DOTALL)
|
||||||
|
|
||||||
|
# 3. Ta bort alla språk-taggar som :sv, :en och :::
|
||||||
|
content = re.sub(r":(sv|en)", "", content)
|
||||||
|
content = re.sub(r":::", "", content)
|
||||||
|
|
||||||
|
# 4. Rensa inline-översättningar
|
||||||
|
def remove_other_language_inline(text, keep_lang):
|
||||||
|
def replacer(match):
|
||||||
|
lang = match.group(1)
|
||||||
|
txt = match.group(2)
|
||||||
|
return txt if lang == keep_lang else ""
|
||||||
|
return re.sub(r"\[:(sv|en)](.*?)(?=\[:|$)", replacer, text)
|
||||||
|
|
||||||
content = remove_other_language_inline(content, keep_lang)
|
content = remove_other_language_inline(content, keep_lang)
|
||||||
|
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
f.write(content.strip() + "\n")
|
f.write(content.strip() + "\n")
|
||||||
|
|
||||||
# Rensa svenska filer
|
# 5. Rensa svenska versioner
|
||||||
for filename in os.listdir(SOURCE_DIR):
|
for filename in os.listdir(SOURCE_DIR):
|
||||||
if filename.endswith(".md"):
|
if filename.endswith(".md"):
|
||||||
clean_file(os.path.join(SOURCE_DIR, filename), keep_lang="sv")
|
clean_file(os.path.join(SOURCE_DIR, filename), keep_lang="sv")
|
||||||
|
|
||||||
# Rensa engelska filer
|
# 6. Rensa engelska versioner
|
||||||
for filename in os.listdir(ENGLISH_DIR):
|
for filename in os.listdir(ENGLISH_DIR):
|
||||||
if filename.endswith(".md"):
|
if filename.endswith(".md"):
|
||||||
clean_file(os.path.join(ENGLISH_DIR, filename), keep_lang="en")
|
clean_file(os.path.join(ENGLISH_DIR, filename), keep_lang="en")
|
||||||
|
|
||||||
|
# 7. Skapa .pages i docs/en/ från mkdocs.yml
|
||||||
|
def extract_nav_from_mkdocs_yml(path):
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f)
|
||||||
|
return {
|
||||||
|
"title": data.get("site_name", "SA6ANW"),
|
||||||
|
"nav": data.get("nav", [])
|
||||||
|
}
|
||||||
|
|
||||||
|
english_pages = extract_nav_from_mkdocs_yml(MKDOCS_YML)
|
||||||
|
with open(os.path.join(ENGLISH_DIR, ".pages"), "w", encoding="utf-8") as f:
|
||||||
|
yaml.dump(english_pages, f, allow_unicode=True)
|
||||||
|
|
Loading…
Reference in New Issue