Skip to content
Snippets Groups Projects
Commit c07d6b29 authored by Josh Borrow's avatar Josh Borrow
Browse files

added mistune as a possible markdown renderer (it's python only)

parent 5a96bdb0
No related branches found
No related tags found
No related merge requests found
""" 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
......
"""
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment