Skip to content

Commit

Permalink
Fix listing-identifier in latex
Browse files Browse the repository at this point in the history
  • Loading branch information
chdemko committed Oct 30, 2024
1 parent 9104d59 commit 9ddb8e8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
45 changes: 35 additions & 10 deletions src/pandoc_numbering/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,20 @@ def _remove_accents(string):
return "".join([c for c in nfkd_form if not unicodedata.combining(c)])

@staticmethod
def _identifier(string):
def identifier(string: str) -> str:
"""
Convert a string to a valid identifier.
Parameters
----------
string
A string to convert.
Returns
-------
str
The corresponding identifier
"""
# replace invalid characters by dash
string = re.sub(
"[^0-9a-zA-Z_-]+", "-", Numbered._remove_accents(string.lower())
Expand Down Expand Up @@ -335,7 +348,7 @@ def _compute_description(self):

def _compute_basic_category(self):
if self._match.group("prefix") is None:
self._basic_category = Numbered._identifier(
self._basic_category = Numbered.identifier(
"".join(map(stringify, self._description))
)
else:
Expand Down Expand Up @@ -421,13 +434,13 @@ def _compute_alias(self):
+ ":"
+ self._section_alias
+ "."
+ Numbered._identifier(stringify(Span(*self._title)))
+ Numbered.identifier(stringify(Span(*self._title)))
)
else:
self._alias = (
self._basic_category
+ ":"
+ Numbered._identifier(stringify(Span(*self._title)))
+ Numbered.identifier(stringify(Span(*self._title)))
)

def _compute_local_number(self):
Expand Down Expand Up @@ -1527,6 +1540,11 @@ def finalize(doc: Doc):
# pylint: disable=consider-using-f-string
if doc.format in {"tex", "latex"}:
latex_category = re.sub("[^a-z]+", "", category)
text = convert_text(
Plain(*definition["listing-title"]),
input_format="panflute",
output_format="latex",
)
latex = (
r"\newlistof{%s}{%s}{%s}"
r"\renewcommand{\cft%stitlefont}{\cfttoctitlefont}"
Expand All @@ -1535,11 +1553,7 @@ def finalize(doc: Doc):
% (
latex_category,
latex_category,
convert_text(
Plain(*definition["listing-title"]),
input_format="panflute",
output_format="latex",
),
text,
latex_category,
latex_category,
latex_category,
Expand All @@ -1548,7 +1562,18 @@ def finalize(doc: Doc):
doc.metadata["header-includes"].append(
MetaInlines(RawInline(latex, "tex"))
)
listof.append(f"\\listof{latex_category}")
if definition["listing-identifier"] is False:
listof.append(f"\\listof{latex_category}")
elif definition["listing-identifier"] is True:
listof.append(
f"\\phantomsection\\label{{{Numbered.identifier(text)}}}"
f"\\listof{latex_category}"
)
else:
listof.append(
f"\\phantomsection\\label{{{definition['listing-identifier']}}}"
f"\\listof{latex_category}"
)
else:
classes = ["pandoc-numbering-listing"] + definition["classes"]

Expand Down
8 changes: 0 additions & 8 deletions tests/test__identifier.py

This file was deleted.

8 changes: 8 additions & 0 deletions tests/test_identifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from unittest import TestCase

from pandoc_numbering import Numbered


class IdentifierTest(TestCase):
def test_identifier(self):
self.assertEqual(Numbered.identifier("0123 Ê à"), "e-a")
12 changes: 6 additions & 6 deletions tests/test_listings_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_listing_latex(self):
`{=tex}
- "`\\usepackage{etoolbox}`{=tex}"
- "`\\newlistof{exercise}{exercise}{List of exercises}\\renewcommand{\\cftexercisetitlefont}{\\cfttoctitlefont}\\setlength{\\cftexercisenumwidth}{\\cftfignumwidth}\\setlength{\\cftexerciseindent}{\\cftfigindent}`{=tex}"
- "`\\ifdef{\\mainmatter}{\\let\\oldmainmatter\\mainmatter\\renewcommand{\\mainmatter}[0]{\\listofexercise\\oldmainmatter}}{}`{=tex}"
- "`\\ifdef{\\mainmatter}{\\let\\oldmainmatter\\mainmatter\\renewcommand{\\mainmatter}[0]{\\phantomsection\\label{list-of-exercises}\\listofexercise\\oldmainmatter}}{}`{=tex}"
pandoc-numbering:
exercise:
general:
Expand All @@ -199,9 +199,9 @@ def test_listing_latex(self):
`\newlistof{exercise}{exercise}{List of exercises}\renewcommand{\cftexercisetitlefont}{\cfttoctitlefont}\setlength{\cftexercisenumwidth}{\cftfignumwidth}\setlength{\cftexerciseindent}{\cftfigindent}`{=tex}
`\ifdef{\mainmatter}{\let\oldmainmatter\mainmatter\renewcommand{\mainmatter}[0]{\listofexercise\oldmainmatter}}{}`{=tex}
`\ifdef{\mainmatter}{\let\oldmainmatter\mainmatter\renewcommand{\mainmatter}[0]{\phantomsection\label{list-of-exercises}\listofexercise\oldmainmatter}}{}`{=tex}
`\ifdef{\mainmatter}{}{\listofexercise}`{=tex}
`\ifdef{\mainmatter}{}{\phantomsection\label{list-of-exercises}\listofexercise}`{=tex}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Exercise}}}`{=tex}[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_listing_latex_format(self):
`{=tex}
- "`\\usepackage{etoolbox}`{=tex}"
- "`\\newlistof{exercise}{exercise}{List of exercises}\\renewcommand{\\cftexercisetitlefont}{\\cfttoctitlefont}\\setlength{\\cftexercisenumwidth}{\\cftfignumwidth}\\setlength{\\cftexerciseindent}{\\cftfigindent}`{=tex}"
- "`\\ifdef{\\mainmatter}{\\let\\oldmainmatter\\mainmatter\\renewcommand{\\mainmatter}[0]{\\listofexercise\\oldmainmatter}}{}`{=tex}"
- "`\\ifdef{\\mainmatter}{\\let\\oldmainmatter\\mainmatter\\renewcommand{\\mainmatter}[0]{\\phantomsection\\label{list-of-exercises}\\listofexercise\\oldmainmatter}}{}`{=tex}"
pandoc-numbering:
exercise:
general:
Expand All @@ -313,9 +313,9 @@ def test_listing_latex_format(self):
`\newlistof{exercise}{exercise}{List of exercises}\renewcommand{\cftexercisetitlefont}{\cfttoctitlefont}\setlength{\cftexercisenumwidth}{\cftfignumwidth}\setlength{\cftexerciseindent}{\cftfigindent}`{=tex}
`\ifdef{\mainmatter}{\let\oldmainmatter\mainmatter\renewcommand{\mainmatter}[0]{\listofexercise\oldmainmatter}}{}`{=tex}
`\ifdef{\mainmatter}{\let\oldmainmatter\mainmatter\renewcommand{\mainmatter}[0]{\phantomsection\label{list-of-exercises}\listofexercise\oldmainmatter}}{}`{=tex}
`\ifdef{\mainmatter}{}{\listofexercise}`{=tex}
`\ifdef{\mainmatter}{}{\phantomsection\label{list-of-exercises}\listofexercise}`{=tex}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Exercise}}}`{=tex}[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ddb8e8

Please sign in to comment.