54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import os
|
|
import re
|
|
import shutil
|
|
import yaml
|
|
|
|
SOURCE_DIR = "docs"
|
|
ENGLISH_DIR = "docs_en"
|
|
MKDOCS_YML = "mkdocs.yml"
|
|
|
|
# Kopiera alla filer från docs/ till docs_en/
|
|
if os.path.exists(ENGLISH_DIR):
|
|
shutil.rmtree(ENGLISH_DIR)
|
|
shutil.copytree(SOURCE_DIR, ENGLISH_DIR)
|
|
|
|
def clean_file(path, keep_lang):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
if keep_lang == "sv":
|
|
content = re.sub(r":::\s*en\n.*?\n:::", "", content, flags=re.DOTALL)
|
|
elif keep_lang == "en":
|
|
content = re.sub(r":::\s*sv\n.*?\n:::", "", content, flags=re.DOTALL)
|
|
|
|
content = re.sub(r":::\s*(sv|en)", "", content)
|
|
content = re.sub(r":::", "", content)
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content.strip() + "\n")
|
|
|
|
# Rensa svenska versioner
|
|
for filename in os.listdir(SOURCE_DIR):
|
|
if filename.endswith(".md"):
|
|
clean_file(os.path.join(SOURCE_DIR, filename), keep_lang="sv")
|
|
|
|
# Rensa engelska versioner
|
|
for filename in os.listdir(ENGLISH_DIR):
|
|
if filename.endswith(".md"):
|
|
clean_file(os.path.join(ENGLISH_DIR, filename), keep_lang="en")
|
|
|
|
# Skapa .pages i docs/en/ baserat på nav i 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)
|
|
|
|
os.makedirs(os.path.join(ENGLISH_DIR), exist_ok=True)
|
|
with open(os.path.join(ENGLISH_DIR, ".pages"), "w", encoding="utf-8") as f:
|
|
yaml.dump(english_pages, f, allow_unicode=True)
|