Skip to content

Commit

Permalink
Add specific classes on numbered elements
Browse files Browse the repository at this point in the history
  • Loading branch information
chdemko committed Nov 10, 2024
1 parent 3e14f0d commit 6acb72f
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 71 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: pipx install hatch
run: |
pipx install hatch
pipx inject --force hatch hatchling==1.25.0
- name: Install dependencies
run: |
wget https://github.com/jgm/pandoc/releases/download/${{ matrix.pandoc-version }}/pandoc-${{ matrix.pandoc-version }}-1-amd64.deb
Expand All @@ -45,7 +47,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: pipx install hatch
run: |
pipx install hatch
pipx inject --force hatch hatchling==1.25.0
- name: Install dependencies
run: |
wget https://github.com/jgm/pandoc/releases/download/${{ matrix.pandoc-version }}/pandoc-${{ matrix.pandoc-version }}-1-amd64.deb
Expand All @@ -67,7 +71,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: pipx install hatch
run: |
pipx install hatch
pipx inject --force hatch hatchling==1.25.0
- name: Install dependencies
run: |
wget https://github.com/jgm/pandoc/releases/download/${{ matrix.pandoc-version }}/pandoc-${{ matrix.pandoc-version }}-1-amd64.deb
Expand All @@ -92,7 +98,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: pipx install hatch
run: |
pipx install hatch
pipx inject --force hatch hatchling==1.25.0
- name: Install dependencies
run: |
wget https://github.com/jgm/pandoc/releases/download/${{ matrix.pandoc-version }}/pandoc-${{ matrix.pandoc-version }}-1-amd64.deb
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Hatch
run: pipx install hatch
run: |
pipx install hatch
pipx inject --force hatch hatchling==1.25.0
- name: Build and publish
env:
HATCH_INDEX_USER: ${{ secrets.PYPI_USERNAME }}
Expand Down
1 change: 1 addition & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build:
python: '3.11'
commands:
- git fetch --unshallow || true
- pip install hatchling==1.25.0
- pip install hatch
- hatch run docs:build $READTHEDOCS_OUTPUT/html/

Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Homepage = "https://github.com/chdemko/pandoc-numbering"
Documentation = "https://pandoc-numbering.readthedocs.io/"
Repository = "https://github.com/chdemko/pandoc-numbering"
Issues = "https://github.com/chdemko/pandoc-numbering/issues"
Coverage = "https://coveralls.io/github/chdemko/pandoc-numbering"

[project.scripts]
pandoc-numbering = "pandoc_numbering:main"
Expand All @@ -72,16 +73,16 @@ dependencies = [
"black[jupyter]~=24.10",
# Style checkers
"doc8~=1.1",
"mypy~=1.12",
"mypy~=1.13",
"flake8~=7.1",
"flake8-pyproject~=1.2",
"flake8-annotations-complexity~=0.0",
"flake8-black~=0.3",
"flake8-blind-except~=0.2",
"flake8-bugbear~=24.8",
"flake8-bugbear~=24.10",
"flake8-builtins~=2.5",
"flake8-cognitive-complexity~=0.1",
"flake8-comprehensions~=3.15",
"flake8-comprehensions~=3.16",
"flake8-docstrings~=1.7",
"flake8-expression-complexity~=0.0",
"flake8-fixme~=1.1",
Expand Down
11 changes: 9 additions & 2 deletions src/pandoc_numbering/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Numbered:
"_description",
"_category",
"_basic_category",
"_classes",
"_first_section_level",
"_last_section_level",
"_leading",
Expand Down Expand Up @@ -272,6 +273,7 @@ def __init__(self, elem: Element, doc: Doc):
self._description = None
self._category = None
self._basic_category = None
self._classes = None
self._first_section_level = None
self._last_section_level = None
self._leading = None
Expand Down Expand Up @@ -402,11 +404,16 @@ def _compute_number(self):
self._number = str(self._doc.count[self._category])

def _compute_tag(self):
self._classes = [
self._category[:-1].replace(":", "-").replace(".", "-") + "-" + self._number
]

# Determine the final tag
if self._match.group("name") is None:
self._tag = self._category + self._number
else:
self._tag = self._basic_category + ":" + self._match.group("name")
self._classes.append(self._basic_category + "-" + self._match.group("name"))

# Compute collections
if self._basic_category not in self._doc.collections:
Expand Down Expand Up @@ -469,7 +476,7 @@ def _compute_data(self):
Span(),
Span(
identifier=self._tag,
classes=["pandoc-numbering-text"] + classes,
classes=["pandoc-numbering-text"] + classes + self._classes,
),
]
)
Expand All @@ -479,7 +486,7 @@ def _compute_data(self):
Span(identifier=self._alias),
Span(
identifier=self._tag,
classes=["pandoc-numbering-text"] + classes,
classes=["pandoc-numbering-text"] + classes + self._classes,
),
]
)
Expand Down
28 changes: 14 additions & 14 deletions tests/test_listings_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def test_listing_classic(self):
- [[Exercise 1]{.pandoc-numbering-entry .exercise}](#exercise:1)
- [[Title]{.pandoc-numbering-entry .exercise}](#exercise:2)
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -68,9 +68,9 @@ def test_listing_identifier_false(self):
- [[Exercise 1]{.pandoc-numbering-entry .exercise}](#exercise:1)
- [[Title]{.pandoc-numbering-entry .exercise}](#exercise:2)
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -104,9 +104,9 @@ def test_listing_identifier(self):
- [[Exercise 1]{.pandoc-numbering-entry .exercise}](#exercise:1)
- [[Title]{.pandoc-numbering-entry .exercise}](#exercise:2)
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -142,9 +142,9 @@ def test_listing_options(self):
- [[Exercise 1]{.pandoc-numbering-entry .exercise}](#exercise:1)
- [[Title]{.pandoc-numbering-entry .exercise}](#exercise:2)
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -203,9 +203,9 @@ def test_listing_latex(self):
`\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}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Exercise}}}`{=tex}[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {2}{\ignorespaces {Title}}}`{=tex}[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {2}{\ignorespaces {Title}}}`{=tex}[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
"latex",
)
Expand Down Expand Up @@ -244,9 +244,9 @@ def test_listing_classic_format(self):
- [[1 Exercise]{.pandoc-numbering-entry .exercise}](#exercise:1)
- [[2 Exercise (Title)]{.pandoc-numbering-entry .exercise}](#exercise:2)
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -317,9 +317,9 @@ def test_listing_latex_format(self):
`\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}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Exercise}}}`{=tex}[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {2}{\ignorespaces {Exercise (Title)}}}`{=tex}[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {2}{\ignorespaces {Exercise (Title)}}}`{=tex}[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
"latex",
)
18 changes: 9 additions & 9 deletions tests/test_numbering_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def test_numbering_classes(self):
- myclass
---
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .myclass}
[**Exercise 1**]{#exercise:1 .pandoc-numbering-text .myclass .exercise-1}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .myclass}
[]{#exercise:title}[**Exercise 2** *(Title)*]{#exercise:2 .pandoc-numbering-text .myclass .exercise-2}
""",
)

Expand Down Expand Up @@ -60,9 +60,9 @@ def test_numbering_text(self):
format-text-title: "**%D %d %n/%c: %T %t**"
---
[**Exercise exercise 1/2**]{#exercise:1 .pandoc-numbering-text .exercise}
[**Exercise exercise 1/2**]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
[]{#exercise:title}[**Exercise exercise 2/2: Title title**]{#exercise:2 .pandoc-numbering-text .exercise}
[]{#exercise:title}[**Exercise exercise 2/2: Title title**]{#exercise:2 .pandoc-numbering-text .exercise .exercise-2}
""",
)

Expand Down Expand Up @@ -111,10 +111,10 @@ def test_numbering_levels(self):
## Second section
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise .exercise-2-2-1}
[]{#exercise:second-chapter.second-section.title}[**Exercise 2.2** *(Title)*]{#exercise:2.2.2 .pandoc-numbering-text .exercise}
""",
[]{#exercise:second-chapter.second-section.title}[**Exercise 2.2** *(Title)*]{#exercise:2.2.2 .pandoc-numbering-text .exercise .exercise-2-2-2}
""",
)

def test_numbering_sectioning(self):
Expand Down Expand Up @@ -160,8 +160,8 @@ def test_numbering_sectioning(self):
## Second section
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise .exercise-2-2-1}
[]{#exercise:second-chapter.second-section.title}[**Exercise 2.2** *(Title)*]{#exercise:2.2.2 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.second-section.title}[**Exercise 2.2** *(Title)*]{#exercise:2.2.2 .pandoc-numbering-text .exercise .exercise-2-2-2}
""",
)
38 changes: 19 additions & 19 deletions tests/test_para.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_para(self):
Example #
""",
r"""
[**Example 1**]{#example:1 .pandoc-numbering-text .example}
[**Example 1**]{#example:1 .pandoc-numbering-text .example .example-1}
[**Example 2**]{#example:2 .pandoc-numbering-text .example}
[**Example 2**]{#example:2 .pandoc-numbering-text .example .example-2}
""",
)

Expand All @@ -35,17 +35,17 @@ def test_para_title(
Example (This is the second title) #
""",
r"""
[]{#example:this-is-the-first-title}[**Example 1** *(This is the first title)*]{#example:1 .pandoc-numbering-text .example}
[]{#example:this-is-the-first-title}[**Example 1** *(This is the first title)*]{#example:1 .pandoc-numbering-text .example .example-1}
[]{#example:this-is-the-second-title}[**Example 2** *(This is the second title)*]{#example:2 .pandoc-numbering-text .example}
[]{#example:this-is-the-second-title}[**Example 2** *(This is the second title)*]{#example:2 .pandoc-numbering-text .example .example-2}
""",
)

def test_para_prefix_single(self):
verify_conversion(
self,
"Example #ex:",
"[**Example 1**]{#ex:1 .pandoc-numbering-text .ex}",
"[**Example 1**]{#ex:1 .pandoc-numbering-text .ex .ex-1}",
)

def test_para_double(self):
Expand All @@ -57,9 +57,9 @@ def test_para_double(self):
Example #
""",
r"""
[**Example 1**]{#example:1 .pandoc-numbering-text .example}
[**Example 1**]{#example:1 .pandoc-numbering-text .example .example-1}
[**Example 2**]{#example:2 .pandoc-numbering-text .example}
[**Example 2**]{#example:2 .pandoc-numbering-text .example .example-2}
""",
)

Expand Down Expand Up @@ -90,7 +90,7 @@ def test_para_sectioning(self):
## Second section
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.second-section.1}[**Exercise 2.1**]{#exercise:2.2.1 .pandoc-numbering-text .exercise .exercise-2-2-1}
""",
)

Expand All @@ -117,17 +117,17 @@ def test_para_numbering_hidden(self):
r"""
# First chapter
[]{#exercise:first-chapter.1}[**Exercise 1**]{#exercise:one .pandoc-numbering-text .exercise}
[]{#exercise:first-chapter.1}[**Exercise 1**]{#exercise:one .pandoc-numbering-text .exercise .exercise-1-1 .exercise-one}
[]{#exercise:first-chapter.2}[**Exercise 2**]{#exercise:1.2 .pandoc-numbering-text .exercise}
[]{#exercise:first-chapter.2}[**Exercise 2**]{#exercise:1.2 .pandoc-numbering-text .exercise .exercise-1-2}
# Second chapter
[]{#exercise:second-chapter.1}[**Exercise 1**]{#exercise:2.1 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.1}[**Exercise 1**]{#exercise:2.1 .pandoc-numbering-text .exercise .exercise-2-1}
[]{#exercise:second-chapter.2}[**Exercise 2**]{#exercise:2.2 .pandoc-numbering-text .exercise}
[]{#exercise:second-chapter.2}[**Exercise 2**]{#exercise:2.2 .pandoc-numbering-text .exercise .exercise-2-2}
[**Exercice 1**]{#exercice:1 .pandoc-numbering-text .exercice}
[**Exercice 1**]{#exercice:1 .pandoc-numbering-text .exercice .exercice-1}
""",
)

Expand All @@ -143,7 +143,7 @@ def test_para_sectioning_unnumbered(self):
r"""
# Unnumbered chapter {#unnumbered-chapter .unnumbered}
[]{#exercise:unnumbered-chapter.1}[**Exercise 0.1**]{#exercise:0.1 .pandoc-numbering-text .exercise}
[]{#exercise:unnumbered-chapter.1}[**Exercise 0.1**]{#exercise:0.1 .pandoc-numbering-text .exercise .exercise-0-1}
""",
)

Expand All @@ -169,19 +169,19 @@ def test_para_numbering_level(self):
Exercise +.+.#
""",
r"""
[**Exercise 0.0.1**]{#exercise:0.0.1 .pandoc-numbering-text .exercise}
[**Exercise 0.0.1**]{#exercise:0.0.1 .pandoc-numbering-text .exercise .exercise-0-0-1}
# First chapter
## First section
[]{#exercise:first-chapter.first-section.1}[**Exercise 1.1.1**]{#exercise:1.1.1 .pandoc-numbering-text .exercise}
[]{#exercise:first-chapter.first-section.1}[**Exercise 1.1.1**]{#exercise:1.1.1 .pandoc-numbering-text .exercise .exercise-1-1-1}
[]{#exercise:first-chapter.first-section.2}[**Exercise 1.1.2**]{#exercise:1.1.2 .pandoc-numbering-text .exercise}
[]{#exercise:first-chapter.first-section.2}[**Exercise 1.1.2**]{#exercise:1.1.2 .pandoc-numbering-text .exercise .exercise-1-1-2}
## Second section
[]{#exercise:first-chapter.second-section.1}[**Exercise 1.2.1**]{#exercise:1.2.1 .pandoc-numbering-text .exercise}
[]{#exercise:first-chapter.second-section.1}[**Exercise 1.2.1**]{#exercise:1.2.1 .pandoc-numbering-text .exercise .exercise-1-2-1}
""",
)

Expand Down Expand Up @@ -224,7 +224,7 @@ def test_numbering_latex(self):
`\ifdef{\mainmatter}{}{}`{=tex}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Equation \(a=b\)}}}`{=tex}[]{#exercise:equation-a-b}[**Exercise 1** *(Equation $a=b$`<!-- -->`{=html})*]{#exercise:1 .pandoc-numbering-text .exercise}
`\phantomsection\addcontentsline{exercise}{exercise}{\protect\numberline {1}{\ignorespaces {Equation \(a=b\)}}}`{=tex}[]{#exercise:equation-a-b}[**Exercise 1** *(Equation $a=b$`<!-- -->`{=html})*]{#exercise:1 .pandoc-numbering-text .exercise .exercise-1}
""",
"latex",
)
Loading

0 comments on commit 6acb72f

Please sign in to comment.