reticulum_website/build.py

234 lines
6.5 KiB
Python
Raw Normal View History

2022-07-01 09:47:15 -04:00
import markdown
import os
SOURCES_PATH = "./source"
BUILD_PATH = "./build"
INPUT_ENCODING = "utf-8"
OUTPUT_ENCODING = "utf-8"
2022-07-01 09:47:15 -04:00
document_start = """
<!doctype html>
<html>
<head>
2023-12-25 06:08:56 -05:00
<link rel="stylesheet" href="css/water.css?v=6">
2022-07-01 09:47:15 -04:00
<meta charset="utf-8"/>
<title>Reticulum Network</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2022-07-01 09:47:15 -04:00
</head>
<body>
"""
document_end = """
</body>
</html>
"""
2022-11-14 05:26:59 -05:00
langs_md = """<div class="top_menu lang_menu">{LANGS}</div>"""
menu_md = """<div class="top_menu">[{RETICULUM}](index{LANG_EXT}.html) | [{START}](start{LANG_EXT}.html) | [{HARDWARE}](hardware{LANG_EXT}.html) | [{TESTNET}](connect{LANG_EXT}.html) | [{MANUAL}](docs{LANG_EXT}.html) | [{CRYPTO}](crypto{LANG_EXT}.html) | [{CREDITS}](credits{LANG_EXT}.html) | [{SOURCE}](https://github.com/markqvist/reticulum) | [{DONATE}](donate{LANG_EXT}.html)</div>"""
2022-07-01 09:47:15 -04:00
primary_lang = "en"
langs = [
{"name": "Deutsch", "ext": "de"},
{"name": "English", "ext": "en"},
2024-01-15 07:04:59 -05:00
{"name": "Nederlands", "ext": "nl"},
2022-11-29 11:12:07 -05:00
{"name": "Polski", "ext": "pl"},
{"name": "Español", "ext": "es"},
{"name": "Português", "ext": "pt-br"},
2023-12-25 06:03:23 -05:00
{"name": "Türkçe", "ext": "tr"},
{"name": "日本語", "ext": "jp"},
2023-12-25 14:01:44 -05:00
{"name": "简体中文", "ext": "zh-cn"},
]
menu_translations = {
"en": {
"RETICULUM": "Reticulum",
"START": "Start",
"HARDWARE": "Hardware",
"TESTNET": "Testnet",
"MANUAL": "Manual",
"CRYPTO": "Crypto",
"CREDITS": "Credits",
"SOURCE": "Source",
"DONATE": "Donate",
},
2023-12-24 22:12:26 -05:00
"tr": {
"RETICULUM": "Reticulum",
"START": "Başlat",
"HARDWARE": "Donanım",
"TESTNET": "Test Ağı",
"MANUAL": "Kılavuz",
"CRYPTO": "Şifreleme",
"CREDITS": "Hakkında",
"SOURCE": "Kaynak",
2023-12-24 22:20:35 -05:00
"DONATE": "Destekle",
2023-12-24 22:12:26 -05:00
},
2024-01-14 22:14:10 -05:00
"nl": {
"RETICULUM": "Reticulum",
"START": "Start",
"HARDWARE": "Hardware",
"TESTNET": "Testnet",
"MANUAL": "Handleiding",
"CRYPTO": "Crypto",
"CREDITS": "Dankwoord",
"SOURCE": "Broncode",
2024-01-15 14:01:00 -05:00
"DONATE": "Doneren",
2024-01-14 22:14:10 -05:00
},
2023-12-24 22:12:26 -05:00
"jp": {
"RETICULUM": "レチキュラム",
"START": "開始",
"HARDWARE": "ハードウェア",
"TESTNET": "テストネット",
"MANUAL": "マニュアル",
"CRYPTO": "暗号",
"CREDITS": "クレジット",
"SOURCE": "ソース",
"DONATE": "寄付",
},
"de": {
"RETICULUM": "Reticulum",
"START": "Los Geht's",
"HARDWARE": "Hardware",
"TESTNET": "Testnetz",
"MANUAL": "Handbuch",
"CRYPTO": "Krypto",
"CREDITS": "Credits",
"SOURCE": "Quellcode",
"DONATE": "Unterstützen",
},
"pl": {
2022-11-29 11:12:07 -05:00
"RETICULUM": "Reticulum",
2022-11-30 15:02:45 -05:00
"START": "Jak Zacząć",
2022-11-29 11:12:07 -05:00
"HARDWARE": "Hardware",
"TESTNET": "Testnet",
2022-11-29 12:03:28 -05:00
"MANUAL": "Podręcznik",
"CRYPTO": "Krypto",
"CREDITS": "Zasługi",
2022-11-30 15:02:45 -05:00
"SOURCE": "Kod Źródłowy",
"DONATE": "Darowizna",
},
"pt-br": {
"RETICULUM": "Reticulum",
"START": "Começar",
"HARDWARE": "Hardware",
"TESTNET": "Rede de Teste",
"MANUAL": "Manual",
"CRYPTO": "Cripto",
"CREDITS": "Créditos",
"SOURCE": "Código",
"DONATE": "Ajude",
2023-12-25 14:01:44 -05:00
},
"es": {
"RETICULUM": "Reticulum",
"START": "Empezas",
"HARDWARE": "Hardware",
"TESTNET": "Testnet",
"MANUAL": "Manual",
"CRYPTO": "Crypto",
"CREDITS": "Creditos",
"SOURCE": "Código fuente",
"DONATE": "Donar",
},
2023-12-25 14:01:44 -05:00
"zh-cn": {
"RETICULUM": "Reticulum",
"START": "开始使用",
"HARDWARE": "硬件要求",
"TESTNET": "测试网络",
"MANUAL": "阅读手册",
"CRYPTO": "密码学",
"CREDITS": "致谢",
"SOURCE": "源代码",
"DONATE": "支持开发",
},
}
def get_page_lang(page):
page_lang = primary_lang
for lang in langs:
base_name = mdf.replace(".md", "")
if lang["ext"] != primary_lang:
if base_name.endswith(lang["ext"]):
page_lang = lang["ext"]
return page_lang
def get_languages_md(page):
page = page.replace(SOURCES_PATH, ".")
current_page_lang = get_page_lang(page)
if current_page_lang != primary_lang:
page_base_name = page.replace("_" + current_page_lang + ".md", "")
else:
page_base_name = page.replace(".md", "")
lang_list = ""
for lang_entry in langs:
lang = lang_entry["name"]
lang_ext = lang_entry["ext"]
if lang_ext != primary_lang:
lang_ext_str = "_" + lang_ext
else:
lang_ext_str = ""
link_target = page_base_name + lang_ext_str + ".html"
link_md = "[" + lang + "](" + link_target + ") | "
lang_list += link_md
2022-11-12 12:20:10 -05:00
return langs_md.replace("{LANGS}", lang_list[:-3])
def get_menu_md(lang):
local_menu_md = menu_md
for entry in menu_translations[lang]:
local_menu_md = local_menu_md.replace(
"{" + entry + "}", menu_translations[lang][entry]
)
return local_menu_md
2022-07-01 09:47:15 -04:00
def scan_pages(base_path):
files = [
file
for file in os.listdir(base_path)
if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "."
]
directories = [
file
for file in os.listdir(base_path)
if os.path.isdir(os.path.join(base_path, file)) and file[:1] != "."
]
2022-07-01 09:47:15 -04:00
page_sources = []
for file in files:
if file.endswith(".md"):
page_sources.append(base_path + "/" + file)
2022-07-01 09:47:15 -04:00
for directory in directories:
page_sources.append(scan_pages(base_path + "/" + directory))
2022-07-01 09:47:15 -04:00
return page_sources
2022-07-01 09:47:15 -04:00
source_files = scan_pages(SOURCES_PATH)
for mdf in source_files:
with open(mdf, "rb") as f:
page_lang = get_page_lang(mdf)
if page_lang != primary_lang:
page_lang_ext = "_" + page_lang
else:
page_lang_ext = ""
2022-07-01 09:47:15 -04:00
md = f.read().decode(INPUT_ENCODING)
page_md = "<center>" + get_languages_md(mdf) + "" + get_menu_md(page_lang).replace("{LANG_EXT}", page_lang_ext) + "</center>\n\n" + md
html = markdown.markdown(
page_md, extensions=["markdown.extensions.fenced_code"]
)
2022-07-01 09:47:15 -04:00
html = document_start + html + document_end
of = BUILD_PATH + mdf.replace(SOURCES_PATH, "").replace(".md", ".html")
2022-07-01 09:47:15 -04:00
with open(of, "wb") as wf:
wf.write(html.encode(OUTPUT_ENCODING))