"""
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, ModuleNotFoundError) as e:
    print("Pandoc not found on your system. Using mistune (python-only)")
    import mistune

    class IDRenderer(mistune.HTMLRenderer):
        def header(self, text, level, raw=None):
            text = "<h{} id={}>{}</h{}>".format(
                level,
                (text.replace(" ", "-")).lower(),
                text,
                level
            )

            return text

    renderer = IDRenderer(escape=False)
    markdown = mistune.create_markdown(renderer=renderer)

    def convert_text(input_text):
        return markdown(input_text)