diff --git a/README.md b/README.md index 3708911..4c8d4d2 100644 --- a/README.md +++ b/README.md @@ -175,3 +175,18 @@ The use of the [literal scalar style](https://yaml.org/spec/1.2.2/#literal-style text: |- My text goes here. ``` + +## Highlighting new parts of releases + +Highlight a rule as new. + +``` +- rule: + new: +``` + +Highlight a piece of text as new. + +``` +text: This is old text. {n}This is new text.{\n} +``` diff --git a/rules_doc_generator/input/yaml/parser.py b/rules_doc_generator/input/yaml/parser.py index 873eb8c..3018ad9 100644 --- a/rules_doc_generator/input/yaml/parser.py +++ b/rules_doc_generator/input/yaml/parser.py @@ -2,7 +2,7 @@ from typing import Any, Callable, TypeVar import yaml -from rules_doc_generator.model.text import (FormatText, TextElement, Ref, Image, Text, Term, Example, SubType, Card, Product, Link) +from rules_doc_generator.model.text import (FormatText, TextElement, Ref, Image, Text, Term, Example, SubType, Card, Product, Link, NewStart, NewEnd) from rules_doc_generator.model.section import (Rule, SubRule, SubSection, Section, Chapter, Document, SectionElement, TimingStructureElement) # Parsing model elements. @@ -36,6 +36,10 @@ def parseTextElement(str: str) -> TextElement: text = text_and_link[0] link = text_and_link[1] return Link(text, link) + elif str.startswith('\\n'): + return NewEnd() + elif str.startswith('n'): + return NewStart() else: return Text(str) @@ -61,10 +65,10 @@ def parse_subrule(yaml_sub_rule: Any = False) -> SubRule: def parse_rule(yaml_rule: Any) -> Rule: id = parse_id(yaml_rule, 'rule') - toc = parse_boolean(yaml_rule, 'toc') + new = parse_boolean(yaml_rule, 'new') text = parse_format_text_field(yaml_rule, 'text') examples = parse_subelements(yaml_rule, 'examples', parse_example) - return Rule(id, text, toc, examples) + return Rule(id, new, text, examples) def parse_subsection(yaml_rule: Any) -> SubSection: id = parse_id(yaml_rule, 'subsection') @@ -155,13 +159,13 @@ def parse_with_default(obj: Any, field_type: str, default: A, parse_func: Callab def read_changelog_from_file() -> list[FormatText]: print(f"Parsing changelog") - with open(f'data/input/00_changelog.yaml', "r") as stream: + with open(f'data/input/00_changelog.yaml', "r", encoding="utf8") as stream: yaml_input = load_yaml(stream) return parse_changelog(yaml_input) def read_chapter_from_file(section_file: str) -> Chapter: print(f"Parsing {section_file}") - with open(f'data/input/{section_file}.yaml', "r") as stream: + with open(f'data/input/{section_file}.yaml', "r", encoding="utf8") as stream: yaml_input = load_yaml(stream) return parse_chapter(yaml_input) diff --git a/rules_doc_generator/model/section.py b/rules_doc_generator/model/section.py index 4dff209..06aa0b5 100644 --- a/rules_doc_generator/model/section.py +++ b/rules_doc_generator/model/section.py @@ -81,8 +81,8 @@ def to_latex(self, id_map: RefDict) -> str: @dataclass class Rule: id: Union[str, None] + new: bool format_text: FormatText - toc: bool examples: list[Example] def to_html(self, id_map: RefDict) -> str: @@ -93,12 +93,16 @@ def to_html(self, id_map: RefDict) -> str: def to_latex(self, id_map: RefDict) -> str: result = f'% Rule {self.id}\n' + if self.new: + result += '\\color{orange}' result += '\\1 ' - if self.toc: - result += '\\phantomsection ' - result += '\\addtocounter{subsubsection}{1} ' - result += '\\addcontentsline{toc}{subsubsection}{\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}~~ ' + self.format_text.to_latex(id_map) + '} ' - result += f'\\refstepcounter{{manual_refs}} \label{{{self.id}}} {self.format_text.to_latex(id_map)}\n' + result += f'\\refstepcounter{{manual_refs}} \label{{{self.id}}} ' + if self.new: + result += '\\textbf{' + result += f'{self.format_text.to_latex(id_map)}' + if self.new: + result += '} \\color{black}' + result += '\n' for i, example in enumerate(self.examples): result += f'% Example {i}\n' result += f'\\begin{{adjustwidth}}{{-27pt}}{{0pt}} {example.to_latex(id_map)} \end{{adjustwidth}}\n' diff --git a/rules_doc_generator/model/text.py b/rules_doc_generator/model/text.py index 12ae732..d4bcb79 100644 --- a/rules_doc_generator/model/text.py +++ b/rules_doc_generator/model/text.py @@ -131,7 +131,23 @@ def to_html(self, id_map: RefDict) -> str: def to_latex(self, id_map: RefDict) -> str: return f'\\hreful{{{self.link}}}{{{self.text}}}' -TextElement = Union[Text, Ref, Term, Image, SubType, Product, Card, Link] +@dataclass +class NewStart: + def to_html(self, id_map: RefDict) -> str: + return '' + + def to_latex(self, id_map: RefDict) -> str: + return '{\\color{orange}' + +@dataclass +class NewEnd: + def to_html(self, id_map: RefDict) -> str: + return '' + + def to_latex(self, id_map: RefDict) -> str: + return '}' + +TextElement = Union[Text, Ref, Term, Image, SubType, Product, Card, Link, NewStart, NewEnd] @dataclass class FormatText: