diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/blog/models.py | 5 | ||||
-rw-r--r-- | app/lib/mdx_attr_list/__init__.py | 1 | ||||
-rw-r--r-- | app/lib/mdx_attr_list/mdx_attr_list.py | 131 | ||||
-rw-r--r-- | app/pages/models.py | 13 |
4 files changed, 145 insertions, 5 deletions
diff --git a/app/blog/models.py b/app/blog/models.py index 2e30ccc..a764c5c 100644 --- a/app/blog/models.py +++ b/app/blog/models.py @@ -79,7 +79,10 @@ class Entry(models.Model): return self.title def get_absolute_url(self): - return "/%s/%s" % (self.pub_date.strftime("%Y/%m").lower(), self.slug) + return "/jrnl/%s/%s" % (self.pub_date.strftime("%Y/%m").lower(), self.slug) + + def get_absolute_url_old(self): + return "/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug) def comment_period_open(self): return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date diff --git a/app/lib/mdx_attr_list/__init__.py b/app/lib/mdx_attr_list/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/app/lib/mdx_attr_list/__init__.py @@ -0,0 +1 @@ + diff --git a/app/lib/mdx_attr_list/mdx_attr_list.py b/app/lib/mdx_attr_list/mdx_attr_list.py new file mode 100644 index 0000000..3b77e3c --- /dev/null +++ b/app/lib/mdx_attr_list/mdx_attr_list.py @@ -0,0 +1,131 @@ +# -*- coding: utf-8 -*- +# from https://gist.githubusercontent.com/richardcrichardc/9520176/raw/a848cd953bf502a3d236ace24bd7f6d3af0d421f/attr_list.py +from __future__ import unicode_literals + +import re + +from markdown.extensions import Extension +from markdown.extensions.attr_list import AttrListTreeprocessor as _AttrListTreeprocessor, isheader +from markdown.util import isBlockLevel + + +class AttrListExtension(Extension): + def extendMarkdown(self, md, md_globals): + md.treeprocessors.add('attr_list', AttrListTreeprocessor(md), '>prettify') + +class AttrListTreeprocessor(_AttrListTreeprocessor): + BASE_RE = r'\{\:([^\}]*)\}' + PARENT_RE = r'\{\^([^\}]*)\}' + HEADER_RE = re.compile(r'[ ]+%s[ ]*$' % BASE_RE) + BLOCK_RE = re.compile(r'\n[ ]*%s[ ]*$' % BASE_RE) + LIST_RE = re.compile(r'\n[ ]*%s[ ]*$' % PARENT_RE) + INLINE_RE = re.compile(r'^%s' % BASE_RE) + + + def _get_last_child(self, elem): + if len(elem): + return self._get_last_child(elem[-1]) + + return elem + + def _is_list(self, elem): + return elem.tag in ['dl', 'ol', 'ul'] + + + def run(self, doc): + for elem in doc.getiterator(): + if isBlockLevel(elem.tag): + if self._is_list(elem): + lc = self._get_last_child(elem) + attr = None + + if lc.tail and lc.tail.strip(): + attr = 'tail' + elif lc.text.strip(): + attr = 'text' + + if attr: + text = getattr(lc, attr) + + m = self.LIST_RE.search(text) + + if m: + self.assign_attrs(elem, m.group(1)) + setattr(lc, attr, text[:m.start()]) + + continue + + # Block level: check for attrs on last line of text + RE = self.BLOCK_RE + + if isheader(elem) or elem.tag == 'dt': + # header or def-term: check for attrs at end of line + RE = self.HEADER_RE + + if len(elem) and elem.tag == 'li': + # special case list items. children may include a ul or ol. + pos = None + # find the ul or ol position + for i, child in enumerate(elem): + if child.tag in ['ul', 'ol']: + pos = i + + break + + if pos is None and elem[-1].tail: + # use tail of last child. no ul or ol. + m = RE.search(elem[-1].tail) + + if m: + self.assign_attrs(elem, m.group(1)) + elem[-1].tail = elem[-1].tail[:m.start()] + elif pos is not None and pos > 0 and elem[pos-1].tail: + # use tail of last child before ul or ol + m = RE.search(elem[pos-1].tail) + + if m: + self.assign_attrs(elem, m.group(1)) + elem[pos-1].tail = elem[pos-1].tail[:m.start()] + elif elem.text: + # use text. ul is first child. + m = RE.search(elem.text) + + if m: + self.assign_attrs(elem, m.group(1)) + elem.text = elem.text[:m.start()] + elif len(elem) and elem[-1].tail: + # has children. Get from tail of last child + m = RE.search(elem[-1].tail) + + if m: + self.assign_attrs(elem, m.group(1)) + elem[-1].tail = elem[-1].tail[:m.start()] + + if isheader(elem): + # clean up trailing #s + elem[-1].tail = elem[-1].tail.rstrip('#').rstrip() + elif elem.text: + # no children. Get from text. + m = RE.search(elem.text) + + if m: + self.assign_attrs(elem, m.group(1)) + elem.text = elem.text[:m.start()] + + if isheader(elem): + # clean up trailing #s + elem.text = elem.text.rstrip('#').rstrip() + else: + # inline: check for attrs at start of tail + if elem.tail: + m = self.INLINE_RE.match(elem.tail) + + if m: + self.assign_attrs(elem, m.group(1)) + elem.tail = elem.tail[m.end():] + +def makeExtension(configs = None): + if configs is None: + configs = {} + + return AttrListExtension(configs) diff --git a/app/pages/models.py b/app/pages/models.py index 62976cd..3ef1a71 100644 --- a/app/pages/models.py +++ b/app/pages/models.py @@ -8,11 +8,16 @@ import yaml from django.conf import settings from django.template.loader import render_to_string from django.template import Context +from mdx_attr_list.mdx_attr_list import AttrListExtension -def markdown_processor(md): - return markdown.markdown(md, ['footnotes'], safe_mode=False) - +def markdown_processor(txt): + md = markdown.Markdown( + extensions=[AttrListExtension(),'footnotes',], + output_format='html5', + safe_mode=False + ) + return md.convert(txt) ''' class Page(models.Model): title = models.CharField(max_length=200) @@ -106,7 +111,7 @@ class _FileLoader(object): for k, v in metadata.items(): setattr(self, k, v) if self.body_markdown: - self.body_html = markdown.markdown(self.body_markdown, ['footnotes'], safe_mode=False) + self.body_html = markdown_processor(self.body_markdown) def read(self): with open(self.filename, "r", encoding="utf-8") as f: |