Rebuilt documentation generators

This commit is contained in:
Manuel Cortez 2021-11-01 17:18:49 -06:00
parent ed80558273
commit ab44ce6fcb
2 changed files with 89 additions and 70 deletions

View File

@ -1,28 +1,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from codecs import open
""" This script converts the hold documentation (saved in markdown files) in a python file with a list of strings to translate it using gettext.""" """ This script converts the hold documentation (saved in markdown files) in a python file with a list of strings to translate it using gettext."""
def prepare_documentation_in_file(fileSource, fileDest): def prepare_documentation_in_file(fileSource, fileDest):
""" This takes documentation written in a markdown file and put all the contents in a python file, to create a translatable documentation. """ This takes documentation written in a markdown file and put all the contents in a python file, to create a translatable documentation.
@fileSource str: A markdown(.md) file. @fileSource str: A markdown(.md) file.
@fileDest str: A file where this will put the new strings""" @fileDest str: A file where this will put the new strings"""
f1 = open(fileSource, "r", encoding="utf-8") f1 = open(fileSource, "r", encoding="utf-8")
f2 = open(fileDest, "w", encoding="utf-8") f2 = open(fileDest, "w", encoding="utf-8")
lns = f1.readlines() lns = f1.readlines()
f2.write("# -*- coding: utf-8 -*-\n") f2.write("# -*- coding: utf-8 -*-\n")
f2.write("documentation = [\n") f2.write("documentation = [\n")
for i in lns: for i in lns:
if "\n" == i: if "\n" == i:
newvar = "\"\"," newvar = "\"\",\n"
elif "\n" == i[-1]: elif "\n" == i[-1]:
newvar = "\"\"\"%s\"\"\",\n" % (i[:-1]) newvar = "_(\"\"\"%s\"\"\"),\n" % (i[:-1])
else: else:
newvar = "\"\"\"%s\"\"\",\n" % (i) newvar = "_(\"\"\"%s\"\"\"),\n" % (i)
f2.write(newvar) f2.write(newvar)
f1.close() f1.close()
f2.write("]") f2.write("]")
f2.close() f2.close()
prepare_documentation_in_file("manual.md", "strings.py") prepare_documentation_in_file("manual.md", "strings.py")

View File

@ -5,63 +5,83 @@ import locale
import paths import paths
import markdown import markdown
import shutil import shutil
from codecs import open as _open
from importlib import reload from importlib import reload
def get_translation_function(name, language): # Languages already translated or translating the documentation.
if language == "en": documentation_languages = ["en", "es", "fr", "de", "it", "gl", "ja", "ru", "ro", "eu", "ca", "da", "sr"]
return gettext.NullTranslations()
translation_function = gettext.translation(name, os.path.join(paths.app_path(), "locales"), languages=[language])
return translation_function
# the list of supported language codes of TW Blue
languages = ["en", "es", "fr", "de", "it", "gl", "ja", "ru", "ro", "eu", "ca", "da", "sr"]
def generate_document(language, document_type="documentation"): # Changelog translated languages.
if document_type == "documentation": changelog_languages = ["en", "ca", "de", "es", "eu", "fr", "gl", "ja", "ro", "ru", "sr"]
translation_file = "twblue-documentation"
translation_function = get_translation_function(translation_file, language) # this function will help us to have both strings.py and changelog.py without issues by installing a global dummy translation function.
markdown_file = markdown.markdown("\n".join([translation_function.gettext(s[:-1]) if s != "\n" else s for s in strings.documentation[1:]]), extensions=["markdown.extensions.toc"]) def install_null_translation(name):
title = translation_function.gettext(strings.documentation[0][:-1]) _ = gettext.NullTranslations()
filename = "manual.html" _.install()
elif document_type == "changelog": return
translation_file = "twblue-changelog"
translation_function = get_translation_function(translation_file, language) def get_translations(name):
markdown_file = markdown.markdown("\n".join([translation_function.gettext(s[:-1]) if s != "\n" else s for s in changelog.documentation[1:]]), extensions=["markdown.extensions.toc"]) """ Create translation instances for every language of the translated document. """
title = translation_function.gettext(changelog.documentation[0][:-1]) translations = {}
filename = "changelog.html" if "documentation" in name:
first_html_block = """<!doctype html> langs = documentation_languages
<html lang="%s"> else:
<head> langs = changelog_languages
<title>%s</title> for l in langs:
<meta charset="utf-8"> if l != "en":
</head> _ = gettext.translation(name, os.path.join(paths.app_path(), "locales"), languages=[l])
<body> translations[l] = _
<header><h1>%s</h1></header> else:
""" % (language, title, title) _ = gettext.NullTranslations()
first_html_block = first_html_block+ markdown_file translations[l] = _
first_html_block = first_html_block + "\n</body>\n</html>" return translations
if not os.path.exists(os.path.join("documentation", language)):
os.mkdir(os.path.join("documentation", language)) def generate_document(lang, lang_name, document_type="documentation"):
mdfile = _open(os.path.join("documentation", language, filename), "w", encoding="utf-8") """ Generates a document by using the provided lang object, which should be a translation, and lang_name, which should be the two letter code representing the language. """
mdfile.write(first_html_block) if document_type == "documentation":
mdfile.close() translation_file = "twblue-documentation"
markdown_file = markdown.markdown("\n".join([lang.gettext(s) if s != "" else s for s in strings.documentation[1:]]), extensions=["markdown.extensions.toc"])
title = lang.gettext(strings.documentation[0])
filename = "manual.html"
elif document_type == "changelog":
translation_file = "twblue-changelog"
markdown_file = markdown.markdown("\n".join([lang.gettext(s) if s != "" else s for s in changelog.documentation[1:]]), extensions=["markdown.extensions.toc"])
title = lang.gettext(changelog.documentation[0])
filename = "changelog.html"
first_html_block = """<!doctype html>
<html lang="%s">
<head>
<title>%s</title>
<meta charset="utf-8">
</head>
<body>
<header><h1>%s</h1></header>
""" % (lang_name, title, title)
first_html_block = first_html_block+ markdown_file
first_html_block = first_html_block + "\n</body>\n</html>"
if not os.path.exists(os.path.join("documentation", lang_name)):
os.mkdir(os.path.join("documentation", lang_name))
mdfile = open(os.path.join("documentation", lang_name, filename), "w", encoding="utf-8")
mdfile.write(first_html_block)
mdfile.close()
def create_documentation(): def create_documentation():
print("Creating documentation in the supported languages...\n") changelog_translations = get_translations("twblue-changelog")
if not os.path.exists("documentation"): documentation_translations = get_translations("twblue-documentation")
os.mkdir("documentation") print("Creating documentation in the supported languages...\n")
if os.path.exists(os.path.join("documentation", "license.txt")) == False: if not os.path.exists("documentation"):
shutil.copy(os.path.join("..", "license.txt"), os.path.join("documentation", "license.txt")) os.mkdir("documentation")
for i in languages: if os.path.exists(os.path.join("documentation", "license.txt")) == False:
print("Creating documentation for: %s" % (i,)) shutil.copy(os.path.join("..", "license.txt"), os.path.join("documentation", "license.txt"))
try: for i in documentation_languages:
generate_document(i) print("Creating documentation for: %s" % (i,))
generate_document(i, "changelog") generate_document(lang_name=i, lang=documentation_translations.get(i))
except: for i in changelog_languages:
continue print("Creating changelog for: %s" % (i,))
print("Done") generate_document(lang_name=i, lang=changelog_translations.get(i), document_type="changelog")
print("Done")
install_null_translation("twblue-documentation")
import strings import strings
import changelog import changelog
create_documentation() create_documentation()