import re def adapt(text, title, layout): text = correct_assets(text, layout) text = add_load_static(text) text = add_meta_data(text) text = correct_title(text, title) text = add_icons(text) text = add_back_to_articles(text) # text = correct_links(text) # text = correct_scripts(text) text = add_mathjax(text) text = add_plantuml(text) text = add_back_to_top_button(text) text = add_scroll_support(text) text = correct_check(text) return text def add_load_static(text): """Add load static on top of the file.""" return "{% load static %}" + text def add_back_to_articles(text): text = text.replace( '
', '
\n\

Back to articles

', ) return text def correct_title(text, title): """Put the true title.""" pattern = re.compile(r"(.*)") wrong_title = re.findall(pattern, text)[0] text = text.replace( f"{wrong_title}", f"{title.capitalize()}" ) return text def add_icons(text): """Add orchid icons in tabs.""" text = text.replace( "", '\n\ \n\ \n\ ', ) return text def correct_links(text): """Correct import links.""" pattern = re.compile(r"href=.assets\/(.*\.css)") for file in re.findall(pattern, text): print(file) text = text.replace( f'href="assets/{file}"', "href=\"{% static 'article/assets/" + file + "' %}\"", ) return text def correct_scripts(text): """Correct import scripts.""" pattern = re.compile(r'src="assets\/(.*\.js)"') for file in re.findall(pattern, text): text = text.replace( "", "\n\ ", ) return text def correct_assets(text, layout): """Correct assets imports.""" # pattern = re.compile(r'([src|href].?=.?[\'|"]assets\/.*\.[js|css].?[\'|"])') pattern = re.compile(r"(assets/.*\.[jc]ss?)") for match in re.findall(pattern, text): substitute = match.replace("assets", layout) substitute = "{% static 'article/" + substitute + "' %}" text = text.replace(match, substitute) return text def add_mathjax(text): """Add mathjax support for latex formulaes rendering.""" text = text.replace( "", '\n\ ', ) return text def add_plantuml(text): """Render plantuml diagrams.""" text = text.replace( "", '\n\ ', ) return text def add_scroll_support(text): """Add scroll support.""" text = text.replace( "", "\n\ \n\ \n\ ", ) return text def correct_check(text): """Replace markdown check to checked buttons.""" text = text.replace("[ ]", '') text = text.replace("[x]", '') return text def add_back_to_top_button(text): """Add back to top button.""" text = text.replace( "", '\n \ ', ) return text def add_meta_data(text): """Add a like button for an article.""" text = text.replace( "", """
{{ view_count }} {{ like_count }} {{ dislike_count }}
{{ created }} {% if update_count > 0 %} V{{ update_count }} {% endif %}
""", ) # text = text.replace('', # """ #
# created: {{ updated }}
# # """) return text