diff --git a/about.py b/about.py index 5aadec982fc96cfea5d7506448342ade38f75305..db61e91053efd86ffa5c54f2f7c738dcbfaddd73 100644 --- a/about.py +++ b/about.py @@ -1,13 +1,14 @@ -""" This file contains the routines used to compile the 'about' page's text. - This text comes to us in three parts, which are stored in three markdown - files in the data directory, along with an about_meta.yaml file describing - how they are to be described at the top of the page. +""" +This file contains the routines used to compile the 'about' page's text. +This text comes to us in three parts, which are stored in three markdown +files in the data directory, along with an about_meta.yaml file describing +how they are to be described at the top of the page. - The markdown files are then compiled to two pieces of HTML, one for the - sidebar and one for the main part of the text, as well as a small - dictionary that describes the content of the cards at the top of the page. +The markdown files are then compiled to two pieces of HTML, one for the +sidebar and one for the main part of the text, as well as a small +dictionary that describes the content of the cards at the top of the page. - Created 16-08-2017 by Josh Borrow (joshua.borrow@durham.ac.uk) +Created 16-08-2017 by Josh Borrow (joshua.borrow@durham.ac.uk) """ #Disable errors associated with un-overwritten methods in HTMLParser. @@ -15,7 +16,7 @@ from html.parser import HTMLParser -import pypandoc +from mdcomp import convert_text import yaml @@ -33,8 +34,8 @@ def compile_markdown(data, data_dir="../data"): for item in data["files"]: with open(f"{data_dir}/{item['name']}", "r") as handle: input_text = handle.read() - - compiled_text = pypandoc.convert_text(input_text, 'html', format='md') + + compiled_text = convert_text(input_text) output_text += f"<div id=\"{item['slug']}\">{compiled_text}</div>" return output_text diff --git a/mdcomp.py b/mdcomp.py new file mode 100644 index 0000000000000000000000000000000000000000..a70fee13ddd7c0e0fb9252832d240a7ecdac18a4 --- /dev/null +++ b/mdcomp.py @@ -0,0 +1,51 @@ +""" +Allows for us to switch between pandoc and mistune nicely. + +If the user does not have pandoc installed, we want to use mistune. The +problem there is that mistune does not have the automatic id plugin +for headings that we use to generate sidebars, i.e. + +## HEAD + +gets converted to + +<h2>HEAD</h2> + +not + +<h2 id="head">HEAD</h2> + +like it does within pandoc. + +Created 22-09-2017 by Josh Borrow (joshua.borrow@durham.ac.uk) +""" + +try: + import pypandoc + + test = pypandoc.convert_text("hello", "html", format="md") + + def convert_text(input_text): + return pypandoc.convert_text(input_text, "html", format="md") + +except OSError: + print("Pandoc not found on your system. Using mistune (python-only)") + import mistune + + class IDRenderer(mistune.Renderer): + def header(self, text, level, raw=None): + text = "<h{} id={}>{}</h{}>".format( + level, + (text.replace(" ", "-")).lower(), + text, + level + ) + + return text + + renderer = IDRenderer() + markdown = mistune.Markdown(renderer=renderer) + + def convert_text(input_text): + return markdown(input_text) + diff --git a/requirements.txt b/requirements.txt index 489f4d2ab89243b68ca482bfa1b47dfb8445f8f1..331437896ead8dfc906764d43909039b70f34a63 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyyaml jinja2 pypandoc +mistune