Skip to content

Commit

Permalink
Add section numbers to pdf toc
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpieters committed Jan 5, 2024
1 parent 602725c commit 2c7c477
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion rules_doc_generator/input/yaml/parser.py
Expand Up @@ -108,9 +108,10 @@ def parse_section(yaml_section: Any) -> Section:

def parse_chapter(yaml_chapter: Any) -> Chapter:
id = parse_id(yaml_chapter, 'chapter')
new = parse_boolean(yaml_chapter, 'new')
text = parse_str_field(yaml_chapter, 'text')
sections = parse_subelements(yaml_chapter, 'sections', parse_section)
return Chapter(id, text, sections)
return Chapter(id, new, text, sections)

def parse_changelog_entry(yaml_changelog_entry: Any) -> FormatText:
text = parse_format_text_field(yaml_changelog_entry, 'text')
Expand Down
30 changes: 24 additions & 6 deletions rules_doc_generator/model/section.py
Expand Up @@ -282,12 +282,19 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
result = f'% Section {self.id}.\n'
if self.toc_entry:
result += f'\subsection[{self.toc_entry}]{{{self.text.to_latex(config, id_map)}}}\n'
elif self.new and config.annotated:
result += f'{{\color{{orange}}\subsection{{{self.text.to_latex(config, id_map)}}}}}\n'
text = self.text.to_latex(config, id_map)
toc_text = self.toc_entry if self.toc_entry else text

result += '\\addtocounter{subsection}{1} \setcounter{subsubsection}{0} '
# Needed for hyperref to jump to the correct place.
# https://tex.stackexchange.com/questions/44088/when-do-i-need-to-invoke-phantomsection
result += '\\phantomsection '
result += f'\\addcontentsline{{toc}}{{subsection}}{{\\arabic{{section}}.\\arabic{{subsection}}~~ {toc_text}}} '
if self.new and config.annotated:
result += f'\subsection*{{\\color{{orange}} \\arabic{{section}}.\\arabic{{subsection}}~~ {text}}}'
else:
result += f'\subsection{{{self.text.to_latex(config, id_map)}}}\n'
result += f'\subsection*{{\\arabic{{section}}.\\arabic{{subsection}}~~ {text}}}'

result += f'\label{{{self.id}}}\n'
if self.snippet:
snippet_lines = self.snippet.to_latex(config, id_map).split('\n')
Expand Down Expand Up @@ -318,6 +325,7 @@ def toc_text(self):
@dataclass
class Chapter:
id: str
new: bool
text: str
sections: list[Section]

Expand All @@ -330,7 +338,17 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
result = f'% Chapter {self.id}.\n'
result += f'\section{{{self.text}}}\n'

result += '\\addtocounter{section}{1} \setcounter{subsection}{0} \setcounter{subsubsection}{0} '
# Needed for hyperref to jump to the correct place.
# https://tex.stackexchange.com/questions/44088/when-do-i-need-to-invoke-phantomsection
result += '\\phantomsection '
result += f'\\addcontentsline{{toc}}{{section}}{{\\arabic{{section}}~~ {self.text}}} '
if self.new and config.annotated:
result += f'\section*{{\\color{{orange}} \\arabic{{section}}~~ {self.text}}}'
else:
result += f'\section*{{\\arabic{{section}}~~ {self.text}}}'

result += f'\label{{{self.id}}}\n'
for section in self.sections:
result += section.to_latex(config, id_map)
Expand Down

0 comments on commit 2c7c477

Please sign in to comment.