~mbuechse/gemtext-to-html

071e58879b94afc97bc86bfe7069c73ecdcc85e7 — Matthias B├╝chse 2 years ago 450a31f master
Fix defect (somewhat): escape certain characters as HTML entities
1 files changed, 10 insertions(+), 3 deletions(-)

M convert_dir.py
M convert_dir.py => convert_dir.py +10 -3
@@ 23,7 23,7 @@ r"""<!DOCTYPE HTML>
  <title>%%title%%</title>
  <link rel="alternate" type="application/rss+xml" title="rss" href="rss.xml">
  <meta name=viewport content="width=device-width, initial-scale=1">
  <style>body {font-family:sans-serif;max-width:40em;margin:auto;} html {overflow-y:scroll;}
  <style>body {font-family:sans-serif;max-width:40em;margin:auto;} html {overflow-y:scroll;} h1 + p {font-size:120%;}
  @media (max-width:50rem) {body {margin:0px 20px;}}</style>
</head>
<body>


@@ 89,6 89,12 @@ def convert_gem_link(meat):
    return href, inner


def escape_html(text):
    if text is None:
        return
    return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")


def convert_gemtext(lines):
    title = None



@@ 101,8 107,9 @@ def convert_gemtext(lines):
                preformat = not preformat
                yield ("</pre>", "<pre>")[int(preformat)]
                continue
            # phew, this is not very good
            if preformat:
                yield gmi_line
                yield escape_html(gmi_line)
                continue
            # skip empty line except in preformat
            if not gmi_line:


@@ 125,7 132,7 @@ def convert_gemtext(lines):
            if ("<li>" in pattern) != in_list:
                in_list = not in_list
                yield ("</ul>", "<ul>")[int(in_list)]
            yield pattern.format(inner=inner, href=href)
            yield pattern.format(inner=escape_html(inner), href=escape_html(href))
        if in_list:
            yield "</ul>"