Skip to content

Commit

Permalink
generalize output type config option
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenpieters committed Nov 23, 2023
1 parent 9d7495c commit 7c8fbdf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/convert.yaml
Expand Up @@ -34,7 +34,7 @@ jobs:
run: pip install -r requirements.txt

- name: Convert input files
run: python3 -m rules_doc_generator -a
run: python3 -m rules_doc_generator -a -t pdf web json

- name: Create pdf output
run: latexmk -pdf -jobname=latex/rules latex/rules.tex
Expand Down
41 changes: 26 additions & 15 deletions rules_doc_generator/__main__.py
Expand Up @@ -2,7 +2,7 @@
import os
import shutil

from rules_doc_generator.config import Config
from rules_doc_generator.config import Config, parse_output_types
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 @@ -13,32 +13,37 @@
parser.add_argument("-y", "--year", default="2023", help="Effective year", action="store")
parser.add_argument("-m", "--month", default="XX", help="Effective month", action="store")
parser.add_argument("-d", "--day", default="XX", help="Effective day", action="store")
parser.add_argument("-p", "--generate-php", default=False, help="Generate php output", action="store_true")
parser.add_argument("-b", "--php-base-path", default="https://example.org/", help="Basepath of php server", action="store")
parser.add_argument("-t", "--output-types", default=["all"], help="Output types", nargs="*", action="store")
args = parser.parse_args()
config = Config(args.annotated, args.year, args.month, args.day, args.generate_php, args.php_base_path)
config = Config(args.annotated, args.year, args.month, args.day, args.php_base_path, parse_output_types(args.output_types))

print("- Config -")
print("- Annotated: " + str(config.annotated))
print("- Output types: " + str(config.output_types))

print("Parsing...")
document = yaml_to_document()

print("Constructing Model...")
ref_dict = construct_reference_map(document)

print("Writing Output...")
write_to_file('html', 'rules.html', standalone_html(document, config.not_php(), ref_dict))
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!")
# PDF Version Output
if "pdf" in config.output_types:
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))

shutil.copyfile(os.path.join('data', 'images', 'credit.svg'), os.path.join('html', 'credit.svg'))
shutil.copyfile(os.path.join('data', 'templates', 'html', 'rules.css'), os.path.join('html', 'rules.css'))
# Web Version Output
if "web" in config.output_types:
write_to_file('html', 'rules.html', standalone_html(document, config, ref_dict, opengraph=False))
shutil.copyfile(os.path.join('data', 'images', 'credit.svg'), os.path.join('html', 'credit.svg'))
shutil.copyfile(os.path.join('data', 'templates', 'html', 'rules.css'), os.path.join('html', 'rules.css'))

# Files for php server
if config.generate_php:
write_to_file('php', 'rules.html', standalone_html(document, config, ref_dict))
# Opengraph Web Version Output
if "opengraph" in config.output_types:
write_to_file('php', 'rules.html', standalone_html(document, config, ref_dict, opengraph=True))
write_to_file('php', 'rules.json', standalone_json(document, config, ref_dict))
shutil.copyfile(os.path.join('data', 'images', 'credit.svg'), os.path.join('php', 'credit.svg'))
shutil.copyfile(os.path.join('data', 'templates', 'html', 'rules.css'), os.path.join('php', 'rules.css'))
Expand All @@ -49,4 +54,10 @@
phpConfigFile = "<?php\n$CONFIG = array (\n"
phpConfigFile += f" 'base_path' => '{config.php_base_path}',\n"
phpConfigFile += ");\n?>\n"
write_to_file('php', 'config.php', phpConfigFile)
write_to_file('php', 'config.php', phpConfigFile)

# Json Output
if "json" in config.output_types:
write_to_file('json', 'rules.json', standalone_json(document, config.not_annotated(), ref_dict))

print("Ready!")
17 changes: 11 additions & 6 deletions rules_doc_generator/config.py
Expand Up @@ -22,14 +22,15 @@ class Config:
effective_year: str
effective_month: str
effective_day: str
generate_php: bool
php_base_path: str
output_types: list[str]

def not_annotated(self):
return Config(False, self.effective_year, self.effective_month, self.effective_day, self.generate_php, self.php_base_path)
return Config(False, self.effective_year, self.effective_month, self.effective_day, self.php_base_path, self.output_types)

def not_php(self):
return Config(self.annotated, self.effective_year, self.effective_month, self.effective_day, False, self.php_base_path)
def without_opengraph(self):
without_opengraph = list(filter(lambda x: x != "opengraph", self.output_types))
return Config(self.annotated, self.effective_year, self.effective_month, self.effective_day, self.php_base_path, without_opengraph)

def version_string(self):
return f'{self.effective_year[2:]}.{self.effective_month}'
Expand All @@ -38,5 +39,9 @@ def effective_date_str(self):
if not self.effective_month in short_month_to_full:
raise Exception(f'Not a valid month string: {self.effective_month}')
return f'{self.effective_day} {short_month_to_full[self.effective_month]} {self.effective_year}'



def parse_output_types(arguments: list[str]):
lowercase_arguments = list(map(lambda x: x.lower(), arguments))
if "all" in lowercase_arguments:
return ["pdf", "web", "opengraph", "json"]
return list(filter(lambda x: x == "pdf" or x == "web" or x == "opengraph" or x == "json", lowercase_arguments))
4 changes: 2 additions & 2 deletions rules_doc_generator/model/main.py
Expand Up @@ -12,12 +12,12 @@ def create_toc_html(id_map: RefDict):
result += f'<li><a href="#{ref_info.id}">{ref_info.reference} {ref_info.text}</a></li>'
return result

def standalone_html(document: Document, config: Config, id_map: RefDict):
def standalone_html(document: Document, config: Config, id_map: RefDict, opengraph: bool):
result = '<?xml encoding="utf-8" ?><!DOCTYPE html><html><head>'
result += '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Atkinson+Hyperlegible">'
result += '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />'
result += '<link rel="stylesheet" href="rules.css">'
if config.generate_php:
if opengraph:
result += '<script src="rules.js" defer></script>'
result += '<link rel="stylesheet" href="extended.css">'
result += '</head><body>'
Expand Down

0 comments on commit 7c8fbdf

Please sign in to comment.