Skip to content

Commit

Permalink
add highlighting for rules and format rules text
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpieters committed Jul 19, 2023
1 parent a3a3754 commit 7fa1625
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```
14 changes: 9 additions & 5 deletions rules_doc_generator/input/yaml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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')
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 10 additions & 6 deletions rules_doc_generator/model/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down
18 changes: 17 additions & 1 deletion rules_doc_generator/model/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7fa1625

Please sign in to comment.