Created
March 11, 2020 07:48
-
-
Save mentiflectax/0cf6187332bc8b2284c1101650f0a47e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exec(open("py/cm.py").read()) | |
import toml | |
import sys | |
# toml_file_name = sys.argv[1] | |
def add_text_fragment(toml_file_name): | |
f = open(toml_file_name, "r") | |
pt = toml.load(f) | |
f.close() | |
if (pt["type"] == "TA"): | |
target_file_name_ctr = compose_target_filename("TA") | |
target_file_name, target_file_nr = compose_target_filename("TA") | |
template_file_name = "templates/ta.md" | |
# Read template | |
template = read_file(template_file_name) | |
# Substitute placeholders in the template | |
txt = template | |
# TODO: Substitute all placeholders | |
# TODO: Substitute @TITLE | |
txt = txt.replace("@TITLE", pt["title"]) | |
# Substitute @ENGLISH | |
engParts = [] | |
if (pt["original"]["lang"] == "ru"): | |
original_section_name = "Original Russian Text" | |
# English section | |
if (pt["english"]["translation_type"] == "Gist"): | |
engParts.append("Below, in section \"Gist\" you can find a summary (not a line-by-line translation) of the text in section \"" + original_section_name + "\".\n") | |
engParts.append("\n## ") | |
engParts.append(pt["english"]["translation_type"]) | |
engParts.append("\n\n") | |
engParts.append(pt["english"]["content"]) | |
eng = arr2str(engParts) | |
txt = txt.replace("@ENGLISH", eng) | |
# Substitute @ORIGINAL | |
origParts = [] | |
origParts.append("## ") | |
origParts.append(original_section_name) | |
origParts.append("\n") | |
origParts.append(pt["original"]["content"]) | |
orig = arr2str(origParts) | |
txt = txt.replace("@ORIGINAL", orig) | |
# Substitute @SRC | |
print("txt:") | |
print(txt) | |
print("pt[source]:") | |
print(pt["source"]) | |
print("pt[source][taSrc]:") | |
print(pt["source"]["taSrc"]) | |
src = pt["source"]["taSrc"] | |
txt = txt.replace("@SRC", ", " + src) | |
# Write file | |
write_file(target_file_name, txt) | |
# Insert a link into the main page | |
main_page = "../content/docs/austrocrimes/thalerhof/_index.md" | |
old_main_page = read_file(main_page) | |
parts = [] | |
parts.append(" * [") | |
parts.append(pt["title"]) | |
parts.append("]({{< relref \"ta_") | |
parts.append(str(target_file_nr)) | |
parts.append("\" >}})\n") | |
parts.append("<!-- @THALERHOF_ALMANAC_MATERIALS_END -->") | |
text_to_append = "".join(parts) | |
new_main_page = old_main_page.replace("<!-- @THALERHOF_ALMANAC_MATERIALS_END -->", text_to_append) | |
write_file(main_page, new_main_page) | |
# Write the file with substitutions to the right location | |
f = open(target_file_name, "w") | |
f.write(txt) | |
f.close() | |
# Write out the name of the input file to the list of processed files | |
f = open("PROCESSED_TOML_FILES", "a+") | |
f.write(toml_file_name + "\n") | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment