Skip to content

Commit

Permalink
fix: Fix footnotes appearing in all following objects
Browse files Browse the repository at this point in the history
Fixes #186
  • Loading branch information
oprypin authored and pawamoy committed Dec 16, 2020
1 parent 6b9ebe7 commit af24bc2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
21 changes: 20 additions & 1 deletion src/mkdocstrings/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `teardown`, that will teardown all the cached handlers, and then clear the cache.
"""

import functools
import importlib
import re
import textwrap
Expand Down Expand Up @@ -124,6 +125,23 @@ def do_any(seq: Sequence, attribute: str = None) -> bool:
return any(_[attribute] for _ in seq)


def do_convert_markdown(md: Markdown, text: str) -> Markup:
"""
Render Markdown text; for use inside templates.
Arguments:
md: A `markdown.Markdown` instance.
text: The text to convert.
Returns:
An HTML string.
"""
try:
return Markup(md.convert(text))
finally:
md.reset()


class BaseRenderer(ABC):
"""
The base renderer class.
Expand Down Expand Up @@ -198,7 +216,8 @@ def update_env(self, md: Markdown, config: dict) -> None:
"""
# Re-instantiate md: see https://github.com/tomchristie/mkautodoc/issues/14
md = Markdown(extensions=config["mdx"], extension_configs=config["mdx_configs"])
self.env.filters["convert_markdown"] = lambda text: Markup(md.convert(text))

self.env.filters["convert_markdown"] = functools.partial(do_convert_markdown, md)


class BaseCollector(ABC):
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/footnotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def func_a():
"""func_a[^1].
[^1]: Footnote\x20A
"""

def func_b():
"""func_b[^x].
[^x]: Footnote\x20B
"""

def func_c():
"""func_c.
"""
40 changes: 34 additions & 6 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Tests for the extension module."""
from textwrap import dedent

from markdown import Markdown

from mkdocstrings.extension import MkdocstringsExtension
Expand All @@ -14,18 +16,44 @@

def test_render_html_escaped_sequences():
"""Assert HTML-escaped sequences are correctly parsed as XML."""
config = _DEFAULT_CONFIG
md = Markdown(extensions=[MkdocstringsExtension(config, Handlers(config))])
config = dict(_DEFAULT_CONFIG)
config["mdx"].append(MkdocstringsExtension(config, Handlers(config)))
md = Markdown(extensions=config["mdx"])

md.convert("::: tests.fixtures.html_escaped_sequences")


def test_multiple_footnotes():
"""Assert footnotes don't get added to subsequent docstrings."""
config = dict(_DEFAULT_CONFIG, mdx=["footnotes"])
config["mdx"].append(MkdocstringsExtension(config, Handlers(config)))
md = Markdown(extensions=config["mdx"])

output = md.convert(
dedent(
"""
Top.[^aaa]
::: tests.fixtures.footnotes.func_a
::: tests.fixtures.footnotes.func_b
::: tests.fixtures.footnotes.func_c
[^aaa]: Top footnote
"""
)
)
assert output.count("Footnote A") == 1
assert output.count("Footnote B") == 1
assert output.count("Top footnote") == 1


def test_reference_inside_autodoc():
"""Assert cross-reference Markdown extension works correctly."""
config = dict(_DEFAULT_CONFIG)
ext = MkdocstringsExtension(config, Handlers(config))
config["mdx"].append(ext)

md = Markdown(extensions=[ext])
config["mdx"].append(MkdocstringsExtension(config, Handlers(config)))
md = Markdown(extensions=config["mdx"])

output = md.convert("::: tests.fixtures.cross_reference")
snippet = 'Link to <span data-mkdocstrings-identifier="something.Else">something.Else</span>.'
Expand Down

0 comments on commit af24bc2

Please sign in to comment.