Skip to content

Commit

Permalink
initial json output support
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpieters committed Aug 30, 2023
1 parent 1d3da79 commit c527f9f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/html/
/latex/
/json/
/latex_annotated/
__pycache__/
.venv
Expand Down
3 changes: 2 additions & 1 deletion rules_doc_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil

from rules_doc_generator.config import Config
from rules_doc_generator.model.main import standalone_html, standalone_latex, write_to_file
from rules_doc_generator.model.main import standalone_html, standalone_latex, standalone_json, write_to_file
from rules_doc_generator.input.yaml.parser import yaml_to_document
from rules_doc_generator.model.analysis.references import construct_reference_map

Expand All @@ -25,6 +25,7 @@
if config.annotated:
write_to_file('latex_annotated', 'rules_annotated.tex', standalone_latex(document, config, ref_dict))
write_to_file('latex', 'rules.tex', standalone_latex(document, config.not_annotated(), ref_dict))
write_to_file('json', 'rules.json', standalone_json(document, config.not_annotated(), ref_dict))
print("Ready!")

shutil.copyfile(os.path.join('data', 'images', 'credit.svg'), os.path.join('html', 'credit.svg'))
Expand Down
6 changes: 6 additions & 0 deletions rules_doc_generator/model/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def standalone_latex(document: Document, config: Config, id_map: RefDict):
result = document.to_latex(config, id_map)
return result

def standalone_json(document: Document, config: Config, id_map: RefDict):
result = '{\n'
result += document.to_json(config, id_map)
result += '\n}'
return result

def write_to_file(folder: str, filename: str, content: str):
os.makedirs(folder, exist_ok=True)
file = open(os.path.join(folder, filename), 'w')
Expand Down
20 changes: 20 additions & 0 deletions rules_doc_generator/model/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
for elem in self.elements:
result += elem.to_latex_l1(config, id_map, self.bold)
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return 'TODO'

@dataclass
class SubRule:
Expand Down Expand Up @@ -138,6 +141,9 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
result += f'% Example {i}\n'
result += f'\\begin{{adjustwidth}}{{-27pt}}{{0pt}} {example.to_latex(config, id_map)} \end{{adjustwidth}}\n'
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return f'"{self.id}": "{self.format_text.to_json(config, id_map)}"'

@dataclass
class SubSection:
Expand Down Expand Up @@ -204,6 +210,9 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
result += rule.to_latex(config, id_map)
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return f'"{self.id}": "{self.format_text.to_json(config, id_map)}"'

def toc_text(self):
return self.format_text.to_plaintext() if self.toc else ''

Expand Down Expand Up @@ -251,6 +260,10 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
result += '\end{outline}\n'
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return f'"{self.id}": "{self.text.to_json(config, id_map)}",\n' + \
',\n'.join(map(lambda element: element.to_json(config, id_map), self.section_elements))

def toc_text(self):
return self.toc_entry if self.toc_entry else self.text.to_plaintext()

Expand All @@ -274,6 +287,10 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
result += section.to_latex(config, id_map)
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return f'"{self.id}": "{self.text}",\n' + \
',\n'.join(map(lambda section: section.to_json(config, id_map), self.sections))

@dataclass
class Document:
changelog: list[FormatText]
Expand All @@ -297,5 +314,8 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
latex_content = latex_content.replace("%__DOCUMENT_PLACEHOLDER__%", document_content)

return latex_content

def to_json(self, config: Config, id_map: RefDict) -> str:
return ',\n'.join(map(lambda chapter: chapter.to_json(config, id_map), self.chapters[:-1]))

ModelElement = Union[Document, Chapter, Section, SectionElement, SubRule]
39 changes: 39 additions & 0 deletions rules_doc_generator/model/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'\includegraphics[height=8pt]{{{self.text}.png}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return '<img>'

@dataclass
class Text:
Expand All @@ -42,6 +45,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return self.text

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text

@dataclass
class Ref:
Expand All @@ -54,6 +60,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return self.to_text("~", id_map, lambda ref_id, ref_text: fr'\reful{{{ref_id}}}{{{ref_text}}}')

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.to_text(" ", id_map, lambda ref_id, ref_text: ref_text)

def to_text(self, spacer: str, id_map: RefDict, mk_link: Callable[[str, str], str]) -> str:
try:
Expand Down Expand Up @@ -91,6 +100,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'{{\\gameterm{{{self.text}}}}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text.upper()

@dataclass
class SubType:
Expand All @@ -101,6 +113,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'\\textbf{{{self.text}}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text

@dataclass
class Card:
Expand All @@ -111,6 +126,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'\\textit{{{self.text}}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text

@dataclass
class Product:
Expand All @@ -121,6 +139,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'\\textit{{{self.text}}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text

@dataclass
class Link:
Expand All @@ -132,6 +153,9 @@ def to_html(self, config: Config, id_map: RefDict) -> str:

def to_latex(self, config: Config, id_map: RefDict) -> str:
return f'\\hreful{{{self.link}}}{{{self.text}}}'

def to_json(self, config: Config, id_map: RefDict) -> str:
return f'{self.text} ({self.link})'

@dataclass
class NewStart:
Expand All @@ -143,6 +167,9 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
return '\\textbf{\\color{orange}'
else:
return ''

def to_json(self, config: Config, id_map: RefDict) -> str:
return ''

@dataclass
class NewEnd:
Expand All @@ -154,6 +181,9 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
return '}'
else:
return ''

def to_json(self, config: Config, id_map: RefDict) -> str:
return ''

TextElement = Union[Text, Ref, Term, Image, SubType, Product, Card, Link, NewStart, NewEnd]

Expand All @@ -180,6 +210,12 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
result = re.sub(r'\"(.*?)\"', r"``\1''", result)
result = re.sub('&', '\&', result)
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
result = ''
for element in self.textElements:
result += element.to_json(config, id_map).replace('"', '\\"')
return result

@dataclass
class Example:
Expand All @@ -198,3 +234,6 @@ def to_latex(self, config: Config, id_map: RefDict) -> str:
if self.new and config.annotated:
result += r'}'
return result

def to_json(self, config: Config, id_map: RefDict) -> str:
return self.text.to_json(config, id_map).replace('"', '\\"')

0 comments on commit c527f9f

Please sign in to comment.