Skip to content

Commit

Permalink
Reorg of render_and_save()
Browse files Browse the repository at this point in the history
Try to create different rendered HTML files in temporary directory.

Use a workdata dictionary to store all necessary information and use it
to create the different HTML files.

Source: https://github.com/GGaayeeG/JinjaTemplatingProj/blob/main/src/buildTemplate.py
  • Loading branch information
tomschr committed Apr 25, 2024
1 parent 09dd213 commit f0b3c10
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/docserv/bih.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def cleanup(self):
cfg['jinja_env'],
# template name
cfg['jinja_template_home'],
# output
# output dir
tmp_dir_nav,
# context:
self,
Expand Down
108 changes: 84 additions & 24 deletions src/docserv/navigation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import json
import logging
from pathlib import Path
Expand Down Expand Up @@ -78,45 +79,104 @@ def build_site_section(bih, stitched_config):
)


def render_and_save(env, template, output, bih) -> None:
def render_and_save(env, template: str, outputdir: str, bih) -> None:
"""Render a Jinja template and save the output to a file"""
servername = bih.config['server']['name']
target = bih.build_instruction['target']
product = bih.product
docset = bih.docset
lang = bih.lang
# Templates
indextmpl = bih.config['targets'][target]['jinja_template_index']
hometmpl = bih.config['targets'][target]['jinja_template_home']
# trdtmpl = bih.config['targets'][target]['jinja_template_trd']
# templatedir = bih.config['targets'][target]['jinja_template_dir']
#
jsondata = bih.config['targets'][target]['jinjacontext_home']
site_sections = bih.config['targets'][target]['site_sections'].split()
default_site_section = bih.config['targets'][target]['default_site_section']
# If valid_languages contains more than one spaces, this doesn't hurt
all_langs = bih.config['server']['valid_languages'].split()
lifecycles = ["supported", "unsuppoted"]
logger.debug("""Useful variables:
docserv config: %r
target: %r
product: %r
docset: %r
lang: %r
outputdir: %r
jsondata: %r
""", servername, target, product, docset, lang, jsondata)

# logger.debug("configfile=%s target=%s", bih.config['server']['name'], target)
contextfile = os.path.join(CACHE_DIR,
servername,
target,
jsondata
)
logger.debug("Loading JSON context from %s", contextfile)
if not os.path.exists(contextfile):
logger.error("JSON context file for rending not found. Expected %s", contextfile)
context = {}
return

with open(contextfile, "r") as fh:
context = json.load(fh)
logger.debug("JSON context successfully loaded.")

logger.debug("contextfile=%s", contextfile)

output = "/tmp/index.html"
site_sections: %r
default_site_section: %r
""", servername, target, product, docset, lang, outputdir, jsondata,
site_sections, default_site_section
)

workdata = {
"products": {
"meta": bih.config['targets'][target]['jinjacontext_home'],
"template": hometmpl,
"render-args": dict(),
"output": "homepage2.html",
},

"smart": {
"meta": "smart_metadata.json", # TODO: introduce a key in
"template": indextmpl,
"render-args": dict(dataSmartDocs=True),
"output": "SmartDocs.html",
},

"sbp": {
"meta": "sdb_metadata.json",
"template": indextmpl,
"render-args": dict(isSBP=True, category="Systems Management"),
"output": "systems-management.html",
},

"trd-ibm": {
"meta": "trd_metadata.json",
"template": indextmpl, # TODO: use correct TRD template
"render-args": dict(isTRD=True, partner='IBM'),
"output": "IBM.html",
},
}

# Load the Jinja template
tmpl = env.get_template(template)
with open(output, "w") as fh:
fh.write(tmpl.render(context))

# Iterate over language and workdata keys:
for lang, item in itertools.product(["en-us"], # TODO: all_langs,
workdata.keys(),
# site_sections,
# lifecycles,
):
meta = os.path.join(CACHE_DIR, servername, target, workdata[item]["meta"])
template = workdata[item]["template"]
args: dict = workdata[item]["render-args"]
output = workdata[item]["output"]

if not os.path.exists(meta):
raise FileNotFoundError(
f"Expected JSON file {meta}, but I couldn't find it."
)

logger.debug("Processing language %s/%s", item, lang)
os.makedirs(f"{outputdir}/{lang}", exist_ok=True)

# Read JSON metadata
with open(meta, "r") as fh:
context = json.load(fh)
logger.debug("JSON context successfully loaded (%r)", meta)

# Render and save rendered HTML
output = os.path.join(outputdir, lang, output)
with open(output, "w") as fh:
content = tmpl.render(data=context, **args)
fh.write(content)
logger.debug("Wrote %s", output)

logger.debug("All languages and products are processed.")


def list_all_products(config: str):
Expand Down

0 comments on commit f0b3c10

Please sign in to comment.