diff --git a/src/pandoc_numbering/_main.py b/src/pandoc_numbering/_main.py index fa88597..f9e619f 100644 --- a/src/pandoc_numbering/_main.py +++ b/src/pandoc_numbering/_main.py @@ -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()) @@ -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: @@ -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): @@ -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}" @@ -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, @@ -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"] diff --git a/tests/test__identifier.py b/tests/test__identifier.py deleted file mode 100644 index fcda5ea..0000000 --- a/tests/test__identifier.py +++ /dev/null @@ -1,8 +0,0 @@ -from unittest import TestCase - -from pandoc_numbering import Numbered - - -class IdentifierTest(TestCase): - def test__identifier(self): - self.assertEqual(Numbered._identifier("0123 Ê à"), "e-a") diff --git a/tests/test_identifier.py b/tests/test_identifier.py new file mode 100644 index 0000000..2148165 --- /dev/null +++ b/tests/test_identifier.py @@ -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") diff --git a/tests/test_listings_meta.py b/tests/test_listings_meta.py index cc86420..2a5d654 100644 --- a/tests/test_listings_meta.py +++ b/tests/test_listings_meta.py @@ -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: @@ -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} @@ -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: @@ -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} diff --git a/uv.lock b/uv.lock index 7954700..02172ef 100644 --- a/uv.lock +++ b/uv.lock @@ -24,7 +24,7 @@ wheels = [ [[package]] name = "pandoc-numbering" -version = "3.4.2.2.post1.dev10" +version = "3.4.2.3.post1.dev1" source = { editable = "." } dependencies = [ { name = "panflute" },