Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: split into files #452

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Move calculate_toc_html to utils
Einenlum committed Jun 21, 2022

Verified

This commit was signed with the committer’s verified signature.
commit 1b1543e645c726849b861db5fe391902c719f265
37 changes: 1 addition & 36 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@
from collections import defaultdict

from lib.errors import MarkdownError
from lib.utils import slugify
from lib.utils import slugify, calculate_toc_html

# ---- globals

@@ -2462,41 +2462,6 @@ class MarkdownWithExtras(Markdown):

# ---- internal support functions


def calculate_toc_html(toc):
"""Return the HTML for the current TOC.
This expects the `_toc` attribute to have been set on this instance.
"""
if toc is None:
return None

def indent():
return ' ' * (len(h_stack) - 1)
lines = []
h_stack = [0] # stack of header-level numbers
for level, id, name in toc:
if level > h_stack[-1]:
lines.append("%s<ul>" % indent())
h_stack.append(level)
elif level == h_stack[-1]:
lines[-1] += "</li>"
else:
while level < h_stack[-1]:
h_stack.pop()
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
lines.append("%s</ul></li>" % indent())
lines.append('%s<li><a href="#%s">%s</a>' % (
indent(), id, name))
while len(h_stack) > 1:
h_stack.pop()
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
lines.append("%s</ul>" % indent())
return '\n'.join(lines) + '\n'


class UnicodeWithAttrs(str):
"""A subclass of unicode used for the return value of conversion to
possibly attach some attributes. E.g. the "toc_html" attribute when
35 changes: 35 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -20,4 +20,39 @@ def slugify(value):
value = _slugify_strip_re.sub("", value).strip().lower()
return _slugify_hyphenate_re.sub("-", value)


## end of http://code.activestate.com/recipes/577257/ }}}


def calculate_toc_html(toc):
"""Return the HTML for the current TOC.
This expects the `_toc` attribute to have been set on this instance.
"""
if toc is None:
return None

def indent():
return " " * (len(h_stack) - 1)

lines = []
h_stack = [0] # stack of header-level numbers
for level, id, name in toc:
if level > h_stack[-1]:
lines.append("%s<ul>" % indent())
h_stack.append(level)
elif level == h_stack[-1]:
lines[-1] += "</li>"
else:
while level < h_stack[-1]:
h_stack.pop()
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
lines.append("%s</ul></li>" % indent())
lines.append('%s<li><a href="#%s">%s</a>' % (indent(), id, name))
while len(h_stack) > 1:
h_stack.pop()
if not lines[-1].endswith("</li>"):
lines[-1] += "</li>"
lines.append("%s</ul>" % indent())
return "\n".join(lines) + "\n"