Skip to content

Commit

Permalink
markdown: separate footnotes and endnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdum committed Nov 30, 2024
1 parent 80bd234 commit 5bd5d4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions odfdo/mixin_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class SplitSpace(NamedTuple):
def _set_global(doc: Any) -> None:
MD_GLOBAL["document"] = doc
MD_GLOBAL["list_level"] = {}
MD_GLOBAL["notes"] = []
MD_GLOBAL["footnote"] = []
MD_GLOBAL["endnote"] = []


def _copy_global() -> dict[str, Any]:
Expand Down Expand Up @@ -239,10 +240,12 @@ def join_fixed_lines(items: list[str]) -> list[str]:

_set_global(self)
md_list = self._md_collect()
joined = join_fixed_lines(md_list)
if MD_GLOBAL["notes"]:
joined.append("\n")
joined.extend(MD_GLOBAL["notes"])
joined: list[str] = join_fixed_lines(md_list)
if MD_GLOBAL["footnote"]:
joined.extend(MD_GLOBAL["footnote"])
joined[-1] += "\n"
if MD_GLOBAL["endnote"]:
joined.extend(MD_GLOBAL["endnote"])
raw_text = "\n".join(x for x in joined if x.strip())
_set_global(None)
return "\n".join(x.rstrip(" ") for x in raw_text.split("\n"))
Expand Down Expand Up @@ -278,7 +281,10 @@ def _md_collect(self) -> list[str]:
class MDNote(MDBase):
def _md_format(self, post_styler: Callable = _as_none) -> str:
citation = f"[{self.citation}]"
MD_GLOBAL["notes"].append(str(self))
if self.note_class == "footnote":
MD_GLOBAL["footnote"].append(str(self))
else:
MD_GLOBAL["endnote"].append(str(self))
return citation + post_styler(self.tail)

def _md_collect(self) -> list[str]:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def test_md_note_text(document_note):
Un paragraphe[1] d'apparence[i] banale.
1. C'est-à-dire l'élément « text:p ».
i. Les apparences sont trompeuses !
"""
).strip()
Expand Down Expand Up @@ -593,6 +594,7 @@ def test_md_dormeur_text(document_dormeur): # FAIL
1. Note dormeur
2. Note next [remote link](https://test.example.com/)
i. End note
"""
Expand Down

0 comments on commit 5bd5d4c

Please sign in to comment.