diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2022-03-12 19:50:03 +0000 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2022-03-12 19:50:03 +0000 |
commit | c7fea1d2c78e77654f098f9ac7409f97ad171f44 (patch) | |
tree | c637935478b24bd3d89490148eb8528ec1e39d2a | |
parent | ae079307387d1e97a7ae425dc9c110cafd873116 (diff) | |
download | eda.gay-c7fea1d2c78e77654f098f9ac7409f97ad171f44.tar.gz eda.gay-c7fea1d2c78e77654f098f9ac7409f97ad171f44.zip |
switched to mistune (over misaka) for markdown parsing, added table of contents
-rw-r--r-- | app.py | 6 | ||||
-rw-r--r-- | database.py | 3 | ||||
-rwxr-xr-x | parser.py | 47 | ||||
-rw-r--r-- | requirements.txt | 2 | ||||
-rw-r--r-- | static/style.css | 8 | ||||
-rw-r--r-- | templates/template.jinja | 6 |
6 files changed, 52 insertions, 20 deletions
@@ -57,7 +57,7 @@ def index(): return flask.render_template( "index.jinja", **get_template_items("eden's site :3", db), - markdown = parser.parse_text(f.read()), + markdown = parser.parse_text(f.read())[0], featured_thoughts = db.get_featured_thoughts(), tweets = db.get_cached_tweets(7) + [("view all tweets...", db.get_my_twitter())], commits = db.get_cached_commits(since = datetime.datetime.now() - datetime.timedelta(days = 7)) @@ -93,7 +93,8 @@ def get_thought(): thought_id = flask.request.args.get("id", type=int) with database.Database() as db: try: - category_name, title, dt, parsed = parser.get_thought_from_id(db, thought_id) + category_name, title, dt, parsed, headers = parser.get_thought_from_id(db, thought_id) + print(headers) except TypeError: flask.abort(404) return @@ -102,6 +103,7 @@ def get_thought(): **get_template_items(title, db), thought = True, dt = "published: " + str(dt), + headers = headers, category = category_name, othercategories = db.get_categories_not(category_name), related = db.get_similar_thoughts(category_name, thought_id) diff --git a/database.py b/database.py index 3dd2d9a..42833d5 100644 --- a/database.py +++ b/database.py @@ -247,6 +247,7 @@ def request_recent_commits(since = datetime.datetime.now() - datetime.timedelta( return sorted(out, key = lambda a: a["datetime"], reverse = True) if __name__ == "__main__": + import parser with Database() as db: # print(db.get_similar_thoughts("about me", 5)) - print(db.get_iso_cd_options())
\ No newline at end of file + print(parser.parse_file("cpus.md"))
\ No newline at end of file @@ -6,17 +6,19 @@ from pygments.formatters import HtmlFormatter, ClassNotFound from pygments.lexers import get_lexer_by_name import urllib.parse import webbrowser +import lxml.etree +import lxml.html import database import argparse import getpass import houdini -import misaka +import mistune import app import sys import re import os -class HighlighterRenderer(misaka.SaferHtmlRenderer): +class EdawebRenderer(mistune.HTMLRenderer): def blockcode(self, text, lang): try: lexer = get_lexer_by_name(lang, stripall=True) @@ -29,42 +31,55 @@ class HighlighterRenderer(misaka.SaferHtmlRenderer): # default return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip())) - def blockquote(self, content): + def block_quote(self, content): content = content[3:-5] # idk why this is required... out = '\n<blockquote>' for line in houdini.escape_html(content.strip()).split("\n"): out += '\n<span class="quote">{}</span><br>'.format(line) return out + '\n</blockquote>' - def image(self, link, title, alt): + def image(self, link, text, title): return "<a href='%s' target='_blank'><img alt='%s' src='%s'></a>" % ( - urlparse(link)._replace(query='').geturl(), alt, link + urlparse(link)._replace(query='').geturl(), text, link ) - def header(self, content, level): - # if level > 1: - hash_ = urllib.parse.quote_plus(content) + def heading(self, text, level): + hash_ = urllib.parse.quote_plus(text) return "<h%d id='%s'>%s <a class='header_linker' href='#%s'>[#]</a></h%d>" % ( - level, hash_, content, hash_, level + level, hash_, text, hash_, level ) - # else: - # return "<h1>%s</h1>" % content def get_thought_from_id(db, id_): category_name, title, dt, markdown = db.get_thought(id_) - return category_name, title, dt, parse_text(markdown) + html, headers = parse_text(markdown) + return category_name, title, dt, html, headers def parse_file(path): with open(path, "r") as f: unformatted = f.read() - return parse_text(unformatted) + return parse_text(unformatted)[0] def parse_text(unformatted): - renderer = HighlighterRenderer() - md = misaka.Markdown(renderer, extensions=('fenced-code', 'quote')) + md = mistune.create_markdown( + renderer = EdawebRenderer(), + plugins = ["strikethrough", "table", "url", "task_lists"] + ) + html = md(unformatted) + root = lxml.html.fromstring(html) + + headers = [] + for node in root.xpath('//h1|//h2|//h3|//h4|//h5//h6'): + headers.append(( + # lxml.etree.tostring(node), + # "<p>%s</p>" % urllib.parse.unquote_plus(node.attrib["id"]), # possibly insecure? + urllib.parse.unquote_plus(node.attrib["id"]), + int(node.tag[-1]), # -horrible hack + "#%s" % node.attrib["id"]) + ) + # print(headers) - return md(unformatted) + return html, headers def preview_markdown(path, title, category): def startBrowser(): diff --git a/requirements.txt b/requirements.txt index c896a79..90f647f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,4 @@ PasteScript==3.2.0 waitress houdini.py Pygments -misaka +mistune diff --git a/static/style.css b/static/style.css index 8fcfa6f..89d8814 100644 --- a/static/style.css +++ b/static/style.css @@ -108,6 +108,14 @@ aside { float: right; } +#header_linkers { + font-size: xx-small; +} + +#header_linkers li { + list-style: none; +} + #tags { font-size: xx-small; } diff --git a/templates/template.jinja b/templates/template.jinja index 77bf38c..f28163e 100644 --- a/templates/template.jinja +++ b/templates/template.jinja @@ -42,6 +42,12 @@ {% if thought %} <aside> <h4>{{dt}}</h4> + <h5>contents:</h5> + <ul id="header_linkers"> + {% for text, depth, link in headers %} + <li>{{ '#'*depth }} <a href="{{ link }}">{{ text }}</a></li> + {% endfor %} + </ul> <h5>this category:</h5> <ul> <li><b><a href="{{'/thoughts#'+category.replace(' ', '_')}}">{{category}}</a></b></li> |