Skip to content

Commit

Permalink
update timing structure model
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpieters committed Sep 5, 2023
1 parent b56c59b commit a4698f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
14 changes: 9 additions & 5 deletions rules_doc_generator/input/yaml/parser.py
Expand Up @@ -3,7 +3,7 @@
import yaml

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)
from rules_doc_generator.model.section import (Rule, SubRule, SubSection, Section, Chapter, Document, SectionElement, TimingStructure, TimingStructureElement)

# Parsing model elements.

Expand Down Expand Up @@ -53,11 +53,15 @@ def parse_example(yaml_example: Any) -> Example:
new = parse_boolean(yaml_example, 'new')
return Example(text, new)

def parse_timing_structure(yaml_timing_structure: Any) -> TimingStructureElement:
text = parse_with_default(yaml_timing_structure, 'text', None, parse_format_text_field)
def parse_timing_structure_element(yaml_timing_structure_element: Any) -> TimingStructureElement:
text = parse_format_text_field(yaml_timing_structure_element, 'text')
elements = parse_subelements(yaml_timing_structure_element, 'elements', parse_timing_structure_element)
return TimingStructureElement(text, elements)

def parse_timing_structure(yaml_timing_structure: Any) -> TimingStructure:
bold = parse_boolean(yaml_timing_structure, 'bold')
elements = parse_subelements(yaml_timing_structure, 'elements', parse_timing_structure)
return TimingStructureElement(text, bold, elements)
elements = parse_subelements(yaml_timing_structure, 'elements', parse_timing_structure_element)
return TimingStructure(bold, elements)

def parse_subrule(yaml_sub_rule: Any = False) -> SubRule:
id = parse_id(yaml_sub_rule, 'rule')
Expand Down
27 changes: 16 additions & 11 deletions rules_doc_generator/model/section.py
Expand Up @@ -8,7 +8,6 @@
@dataclass
class TimingStructureElement:
text: FormatText
bold: bool
elements: list[TimingStructureElement]

def to_html_l1(self, config: Config, id_map: RefDict, bold: bool) -> str:
Expand All @@ -33,13 +32,6 @@ def to_html_l2(self, config: Config, id_map: RefDict) -> str:
def to_html_l3(self, config: Config, id_map: RefDict) -> str:
return f'<li class="TimingStructureL3 TimingStructureNormal">{self.text.to_html(config, id_map)}</li>'

def to_html(self, config: Config, id_map: RefDict) -> str:
result = '<ol class="TimingStructureList">'
for elem in self.elements:
result += elem.to_html_l1(config, id_map, self.bold)
result += '</ol>'
return result

def to_latex_l1(self, config: Config, id_map: RefDict, bold: bool) -> str:
result = '\\1 '
if bold:
Expand All @@ -61,6 +53,19 @@ def to_latex_l2(self, config: Config, id_map: RefDict) -> str:
def to_latex_l3(self, config: Config, id_map: RefDict) -> str:
return f' \\3 {self.text.to_latex(config, id_map)}\n'


@dataclass
class TimingStructure:
bold: bool
elements: list[TimingStructureElement]

def to_html(self, config: Config, id_map: RefDict) -> str:
result = '<ol class="TimingStructureList">'
for elem in self.elements:
result += elem.to_html_l1(config, id_map, self.bold)
result += '</ol>'
return result

def to_latex(self, config: Config, id_map: RefDict) -> str:
if self.bold:
result = '\setlist[enumerate,1]{label=\\textbf{\\arabic*)}}\n'
Expand Down Expand Up @@ -246,7 +251,7 @@ def to_json(self, config: Config, id_map: RefDict) -> str:
def toc_text(self):
return self.format_text.to_plaintext() if self.toc else ''

SectionElement = Union[Rule, SubSection, TimingStructureElement]
SectionElement = Union[Rule, SubSection, TimingStructure]

@dataclass
class Section:
Expand All @@ -265,8 +270,8 @@ def to_html(self, config: Config, id_map: RefDict) -> str:
for elem in self.section_elements:
match elem:
case Rule(): result += f'<li class="Rule" id="{elem.id}">{elem.to_html(config, id_map)}</li>'
case SubSection(): result += f'<li class="Rule" id="{elem.id}">{elem.to_html(config, id_map)}'
case TimingStructureElement(): result += elem.to_html(config, id_map)
case SubSection(): result += f'<li class="Rule" id="{elem.id}">{elem.to_html(config, id_map)}</li>'
case TimingStructure(): result += elem.to_html(config, id_map)
result += '</ol>'
return result

Expand Down
4 changes: 3 additions & 1 deletion rules_doc_generator/model/text.py
Expand Up @@ -194,7 +194,9 @@ class FormatText:
def to_plaintext(self) -> str:
result = ''
for element in self.textElements:
result += element.to_plaintext()
match element:
case Text(): result += element.to_plaintext()
case _: raise Exception(f'Unexpected element type for plaintext: {element}')
return result

def to_html(self, config: Config, id_map: RefDict) -> str:
Expand Down

0 comments on commit a4698f8

Please sign in to comment.