Skip to content
Permalink
Browse files

Temporary fix

  • Loading branch information...
Depado committed Oct 27, 2015
1 parent b19050c commit 4ae4faceddbcf67c35db070be69588774fc79563
Showing with 62 additions and 59 deletions.
  1. +5 −2 app/models/post.py
  2. +1 −1 app/utils/__init__.py
  3. +56 −56 app/utils/markdown.py
@@ -8,7 +8,7 @@
from flask_login import current_user

from app import app, db
from app.utils import markdown_renderer, ansi_renderer
from app.utils import markdown_renderer, ansi_renderer, renderer


class Post(db.Model):
@@ -63,7 +63,10 @@ def content_as_html(self, index=False):
except Exception as e:
return content
else:
return markdown_renderer.render(self.content)
# renderer.reset_toc()
content = markdown_renderer.render(self.content)
# renderer.render_toc(level=3)
return content

def content_as_ansi(self):
return ansi_renderer.render(self.content)
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

from .markdown import HighlighterRenderer, markdown_renderer, ansi_renderer
from .markdown import HighlighterRenderer, markdown_renderer, ansi_renderer, renderer
@@ -195,62 +195,62 @@ def latex_environment(self, name, text):
def inline_math(self, text):
return '$%s$' % text

def reset_toc(self):
self.toc_tree = []
self.toc_count = 0

def header(self, text, level, raw=None):
rv = '<h%d id="toc-%d">%s</h%d>\n' % (
level, self.toc_count, text, level
)
self.toc_tree.append((self.toc_count, text, level, raw))
self.toc_count += 1
return rv

def render_toc(self, level=3):
"""Render TOC to HTML.
:param level: render toc to the given level
"""
return ''.join(self._iter_toc(level))

def _iter_toc(self, level):
first_level = None
last_level = None

yield '<ul id="table-of-content">\n'

for toc in self.toc_tree:
index, text, l, raw = toc

if l > level:
# ignore this level
continue

if first_level is None:
# based on first level
first_level = l
last_level = l
yield '<li><a href="#toc-%d">%s</a>' % (index, text)
elif last_level == l:
yield '</li>\n<li><a href="#toc-%d">%s</a>' % (index, text)
elif last_level == l - 1:
last_level = l
yield '<ul>\n<li><a href="#toc-%d">%s</a>' % (index, text)
elif last_level > l:
# close indention
yield '</li>'
while last_level > l:
yield '</ul>\n</li>\n'
last_level -= 1
yield '<li><a href="#toc-%d">%s</a>' % (index, text)

# close tags
yield '</li>\n'
while last_level > first_level:
yield '</ul>\n</li>\n'
last_level -= 1

yield '</ul>\n'
# def reset_toc(self):
# self.toc_tree = []
# self.toc_count = 0
#
# def header(self, text, level, raw=None):
# rv = '<h%d id="toc-%d">%s</h%d>\n' % (
# level, self.toc_count, text, level
# )
# self.toc_tree.append((self.toc_count, text, level, raw))
# self.toc_count += 1
# return rv
#
# def render_toc(self, level=3):
# """Render TOC to HTML.
# :param level: render toc to the given level
# """
# return ''.join(self._iter_toc(level))
#
# def _iter_toc(self, level):
# first_level = None
# last_level = None
#
# yield '<ul id="table-of-content">\n'
#
# for toc in self.toc_tree:
# index, text, l, raw = toc
#
# if l > level:
# # ignore this level
# continue
#
# if first_level is None:
# # based on first level
# first_level = l
# last_level = l
# yield '<li><a href="#toc-%d">%s</a>' % (index, text)
# elif last_level == l:
# yield '</li>\n<li><a href="#toc-%d">%s</a>' % (index, text)
# elif last_level == l - 1:
# last_level = l
# yield '<ul>\n<li><a href="#toc-%d">%s</a>' % (index, text)
# elif last_level > l:
# # close indention
# yield '</li>'
# while last_level > l:
# yield '</ul>\n</li>\n'
# last_level -= 1
# yield '<li><a href="#toc-%d">%s</a>' % (index, text)
#
# # close tags
# yield '</li>\n'
# while last_level > first_level:
# yield '</ul>\n</li>\n'
# last_level -= 1
#
# yield '</ul>\n'

renderer = HighlighterRenderer(escape=False)
markdown_renderer = MarkdownWithMath(renderer=renderer)

0 comments on commit 4ae4fac

Please sign in to comment.
You can’t perform that action at this time.