From 799284d7065f8db02e7c8d935def80b7a2dd6f9f Mon Sep 17 00:00:00 2001 From: jwansek Date: Sat, 12 Mar 2022 21:10:13 +0000 Subject: Added contents, made a new template for blog posts --- app.py | 8 ++++---- database.py | 2 +- parser.py | 22 +++++++++++++++++++--- static/style.css | 9 +++++++-- templates/template.jinja | 30 +----------------------------- templates/thought.jinja | 29 +++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 templates/thought.jinja diff --git a/app.py b/app.py index 8e1e703..cd08eb2 100644 --- a/app.py +++ b/app.py @@ -98,12 +98,12 @@ def get_thought(): except TypeError: flask.abort(404) return - return flask.render_template_string( - '{% extends "template.jinja" %}\n{% block content %}\n' + parsed + '\n{% endblock %}', + return flask.render_template( + "thought.jinja", **get_template_items(title, db), - thought = True, + md_html = parsed, + contents_html = headers, 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 42833d5..61ca2f7 100644 --- a/database.py +++ b/database.py @@ -250,4 +250,4 @@ if __name__ == "__main__": import parser with Database() as db: # print(db.get_similar_thoughts("about me", 5)) - print(parser.parse_file("cpus.md")) \ No newline at end of file + print(parser.parse_file("out.md")) diff --git a/parser.py b/parser.py index 4044fb8..c5c8755 100755 --- a/parser.py +++ b/parser.py @@ -13,6 +13,7 @@ import argparse import getpass import houdini import mistune +import jinja2 import app import sys import re @@ -66,20 +67,35 @@ def parse_text(unformatted): plugins = ["strikethrough", "table", "url", "task_lists"] ) html = md(unformatted) + + return html, get_headers(html) + +def get_headers(html): root = lxml.html.fromstring(html) headers = [] + thesmallestlevel = 7 for node in root.xpath('//h1|//h2|//h3|//h4|//h5//h6'): + level = int(node.tag[-1]) + if level < thesmallestlevel: + thesmallestlevel = level headers.append(( # lxml.etree.tostring(node), # "

%s

" % urllib.parse.unquote_plus(node.attrib["id"]), # possibly insecure? urllib.parse.unquote_plus(node.attrib["id"]), - int(node.tag[-1]), # -horrible hack + level, # -horrible hack "#%s" % node.attrib["id"]) ) + + headers = [(i[0], i[1] - thesmallestlevel, i[2]) for i in headers] # print(headers) - - return html, headers + md_template = jinja2.Template(""" +{% for text, depth, link in contents %} +{{ " " * depth }} - [{{ text }}]({{ link }}) +{% endfor %} + """) + + return mistune.html(md_template.render(contents = headers)) def preview_markdown(path, title, category): def startBrowser(): diff --git a/static/style.css b/static/style.css index 89d8814..cc6b460 100644 --- a/static/style.css +++ b/static/style.css @@ -108,11 +108,16 @@ aside { float: right; } -#header_linkers { +#contents { font-size: xx-small; } -#header_linkers li { +#contents ul { + padding-left: 12px; + line-height: 3px; + } + +#contents ul li { list-style: none; } diff --git a/templates/template.jinja b/templates/template.jinja index f28163e..2fd86f9 100644 --- a/templates/template.jinja +++ b/templates/template.jinja @@ -38,41 +38,13 @@ - - {% if thought %} - - {% endif %}
{% block content %} {% endblock %}