Markdown to addon description conversion

Is there any simple way to convert markdown to whatever the format is of the addon web page description?

Thanks

I’ve been writing a Python script to do that. Here is what I’ve done so far:

import sys
import re

from markdown import markdown
from bs4 import BeautifulSoup

allowed_tags = ["img", "a", "b", "i", "code", "ul", "ol", "li", "div"]


def ankiwebify(filename):
    with open(filename, "r", encoding="utf-8") as f:
        html = markdown(f.read(), output_format="html5")
    doc = BeautifulSoup(html, "html.parser")

    # add the "markdown" attribute so that markdown inside html tags in the original text is parsed
    for node in doc():
        node["markdown"] = "1"

    # reparse markdown again
    html = markdown(doc.decode(False), output_format="html5", extensions=["md_in_html"])

    # strip markdown attribute
    html = html.replace(' markdown="1"', "")

    # convert headings to <b> elements
    html = re.sub(
        r"<h(?P<num>\d+)>(.*?)</h(?P=num)>",
        lambda m: "\n<b>" + m.group(2) + f"</b>",
        html,
    )

    # collapse newlines inside paragraphs
    # TODO: do the same for lists?
    html = re.sub(
        r"(<p.*?>)(.*?)(</p.*?>)",
        lambda m: m.group(1) + m.group(2).replace("\n", "\xA0") + m.group(3),
        html,
        flags=re.DOTALL,
    )

    html = re.sub("\n\\s+", "\xA0", html)

    # convert <br>'s to newlines
    html = re.sub(r"(<br>)|(</br>)", "\n", html)

    # remove disallowed tags
    def remove_disallowed_tag(m):
        tag = m.group(2)
        if tag in allowed_tags:
            return m.group(0)
        else:
            return ""

    html = re.sub(r"(<([^/>\s]+).*?>)", remove_disallowed_tag, html, flags=re.DOTALL)
    html = re.sub(r"(</([^>\s]+).*?>)", remove_disallowed_tag, html, flags=re.DOTALL)

    return html


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("no input file given", file=sys.stderr)
        sys.exit(1)
    print(ankiwebify(sys.argv[1]))

It’s ad-hoc. You may need to edit the resulting file a bit.

2 Likes

You can also just use a Markdown-to-HTML converter and edit the resulting HTML file if necessary after testing it with AnkiWeb.

Perfect, thanks.

FWIW I ended up scrapping the beautiful souping because I couldn’t get it to display the way I wanted and just replaced directly in the HTML:

import sys
import re
from markdown import markdown
from htmlmin import minify


def ankiwebify(filename):
    with open(filename, "r", encoding="utf-8") as f:
        html = markdown(f.read(), output_format="html5")
    html = minify(html, remove_empty_space=True)
    html = re.sub(r'</p>', '<br><br>', html)
    html = re.sub(r'<h1>(.*?)</h1>', lambda m: f'<b>{m.group(1).upper()}</b><br>', html)
    html = re.sub(r'<h2>(.*?)</h2>', r'<b>\1</b><br>', html)
    html = re.sub(r'<h3>(.*?)</h3>', r'<i>\1</i><br>', html)
    html = re.sub(r'<[/ ]*(p|pre)[/ ]*>', '', html)
    html = re.sub(r'<[/ ]*br[/ ]*>', r'\n', html)
    return html

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("no input file given", file=sys.stderr)
        sys.exit(1)
    print(ankiwebify(sys.argv[1]))
1 Like

@TRIAEIOU I am looking for someone to build me a script that converts highlighted text into markdown. I am willing to pay for it. If you know how to code, would you mind me giving you more details?

Do you mean in Anki? If so, where do you see the result inserted? Into the clipboard or somewhere else?

Ok, my earlier python script seems to be failing me, I made a simple pandoc writer though (pandoc README.md -t ./anki-addon.lua -o anki-addon-dialog-format.txt).

2 Likes