diff --git a/lib/markdown2.py b/lib/markdown2.py index 82c496f4..efb0b5ac 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -162,6 +162,7 @@ class MarkdownError(Exception): def markdown_path(path, encoding="utf-8", html4tags=False, tab_width=DEFAULT_TAB_WIDTH, safe_mode=None, extras=None, link_patterns=None, + footnote_title=None, footnote_return_symbol=None, use_file_vars=False): fp = codecs.open(path, 'r', encoding) text = fp.read() @@ -169,15 +170,20 @@ def markdown_path(path, encoding="utf-8", return Markdown(html4tags=html4tags, tab_width=tab_width, safe_mode=safe_mode, extras=extras, link_patterns=link_patterns, + footnote_title=footnote_title, + footnote_return_symbol=footnote_return_symbol, use_file_vars=use_file_vars).convert(text) def markdown(text, html4tags=False, tab_width=DEFAULT_TAB_WIDTH, safe_mode=None, extras=None, link_patterns=None, + footnote_title=None, footnote_return_symbol=None, use_file_vars=False): return Markdown(html4tags=html4tags, tab_width=tab_width, safe_mode=safe_mode, extras=extras, link_patterns=link_patterns, + footnote_title=footnote_title, + footnote_return_symbol=footnote_return_symbol, use_file_vars=use_file_vars).convert(text) @@ -203,7 +209,9 @@ class Markdown(object): _ws_only_line_re = re.compile(r"^[ \t]+$", re.M) def __init__(self, html4tags=False, tab_width=4, safe_mode=None, - extras=None, link_patterns=None, use_file_vars=False): + extras=None, link_patterns=None, + footnote_title=None, footnote_return_symbol=None, + use_file_vars=False): if html4tags: self.empty_element_suffix = ">" else: @@ -233,6 +241,8 @@ def __init__(self, html4tags=False, tab_width=4, safe_mode=None, self._instance_extras = self.extras.copy() self.link_patterns = link_patterns + self.footnote_title = footnote_title + self.footnote_return_symbol = footnote_return_symbol self.use_file_vars = use_file_vars self._outdent_re = re.compile(r'^(\t|[ ]{1,%d})' % tab_width, re.M) @@ -2037,15 +2047,31 @@ def _add_footnotes(self, text): '', ] + + if not self.footnote_title: + self.footnote_title = "Jump back to footnote %d in the text." + if not self.footnote_return_symbol: + self.footnote_return_symbol = "↩" + for i, id in enumerate(self.footnote_ids): if i != 0: footer.append('') footer.append('
  • ' % id) footer.append(self._run_block_gamut(self.footnotes[id])) - backlink = ('' - '↩' % (id, i+1)) + try: + backlink = ('' + + self.footnote_return_symbol + + '') % (id, i+1) + except TypeError: + log.debug("Footnote error. `footnote_title` " + "must include parameter. Using defaults.") + backlink = ('' + '↩' % (id, i+1)) + if footer[-1].endswith("

    "): footer[-1] = footer[-1][:-len("

    ")] \ + ' ' + backlink + "

    " diff --git a/test/test_markdown2.py b/test/test_markdown2.py index 87ec0a02..77b966f4 100644 --- a/test/test_markdown2.py +++ b/test/test_markdown2.py @@ -90,8 +90,7 @@ def _assertMarkdownPath(self, text_path, encoding="utf-8", opts=None, extra["metadata"] = json_loads( codecs.open(metadata_path, 'r', encoding=encoding).read()) extra["metadata_path"] = metadata_path - self._assertMarkdown(text, html, text_path, html_path, opts=opts, - **extra) + self._assertMarkdown(text, html, text_path, html_path, opts=opts, **extra) def _assertMarkdown(self, text, html, text_path=None, html_path=None, opts=None, toc_html=None, toc_html_path=None, metadata=None, @@ -197,9 +196,10 @@ def generate_tests(cls): if not exists(metadata_path): metadata_path = None - test_func = lambda self, t=text_path, o=opts, c=toc_html_path, m=metadata_path: \ - self._assertMarkdownPath(t, opts=o, toc_html_path=c, - metadata_path=m) + test_func = lambda self, t=text_path, o=opts, c=toc_html_path, \ + m=metadata_path: \ + self._assertMarkdownPath(t, opts=o, toc_html_path=c, + metadata_path=m) tags_path = splitext(text_path)[0] + ".tags" if exists(tags_path): diff --git a/test/tm-cases/footnotes_custom.html b/test/tm-cases/footnotes_custom.html new file mode 100644 index 00000000..bd82c880 --- /dev/null +++ b/test/tm-cases/footnotes_custom.html @@ -0,0 +1,27 @@ +

    This is a para with a footnote.1

    + +

    This is another para with a footnote2 in it. Actually it has two3 of +them. No, three4.

    + +
    +
    +
      +
    1. +

      Here is the body of the first footnote. 

      +
    2. + +
    3. +

      And of the second footnote.

      + +

      This one has multiple paragraphs. 

      +
    4. + +
    5. +

      Here is a footnote body that starts on next line. 

      +
    6. + +
    7. +

      quickie "that looks like a link ref if not careful" 

      +
    8. +
    +
    diff --git a/test/tm-cases/footnotes_custom.opts b/test/tm-cases/footnotes_custom.opts new file mode 100644 index 00000000..7b2b5e24 --- /dev/null +++ b/test/tm-cases/footnotes_custom.opts @@ -0,0 +1,3 @@ +{"extras": ["footnotes"], + "footnote_title" : "Yo Dawg, I heard you came from %d." +} diff --git a/test/tm-cases/footnotes_custom.text b/test/tm-cases/footnotes_custom.text new file mode 100644 index 00000000..7d6e7163 --- /dev/null +++ b/test/tm-cases/footnotes_custom.text @@ -0,0 +1,18 @@ +This is a para with a footnote.[^1] + +This is another para with a footnote[^2] in it. Actually it has two[^3] of +them. No, three[^4]. + + +[^1]: Here is the body of the first footnote. + +[^2]: And of the second footnote. + + This one has multiple paragraphs. + +[^3]: + Here is a footnote body that starts on next line. + +[^4]: quickie "that looks like a link ref if not careful" + +