From: Mark Hansen Date: Tue, 2 Jun 2020 11:23:26 +0000 (+1000) Subject: Extract common Jinja2 environment code X-Git-Tag: 2.44.1~23^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ee67e637bed864e1a84e321d2db5ab545e4718f7;p=graphviz Extract common Jinja2 environment code Into one file. --- diff --git a/doc/infosrc/mkattrs.py b/doc/infosrc/mkattrs.py index 69801c8a3..a8fbddeae 100755 --- a/doc/infosrc/mkattrs.py +++ b/doc/infosrc/mkattrs.py @@ -6,11 +6,10 @@ # See `attrs` and `types` files for format documentation. from dataclasses import dataclass -import jinja2 import markupsafe -import re import sys from typing import List, Dict +import templates ## # Parse `attrs` file @@ -141,16 +140,5 @@ for t in types: # Output HTML ## -env = jinja2.Environment( - # Load template files from ./templates/ - loader=jinja2.FileSystemLoader('templates'), - # Auto-HTML-escape any html or xml files. - autoescape=jinja2.select_autoescape(['html', 'xml', 'html.j2', 'xml.j2']), - # Whitespace control - trim_blocks=True, - lstrip_blocks=True, - # Raise exception on any attempt to access undefined variables. - undefined=jinja2.StrictUndefined, -) -template = env.get_template('attrs.html.j2') +template = templates.env().get_template('attrs.html.j2') print(template.render(attrs=attrs, types=types)) diff --git a/doc/infosrc/mkoutput.py b/doc/infosrc/mkoutput.py index ac350c4ef..f6eb25a67 100755 --- a/doc/infosrc/mkoutput.py +++ b/doc/infosrc/mkoutput.py @@ -3,11 +3,11 @@ # Uses `templates/output.html.j2` # See `outputs` file for format documentation. -import jinja2 import markupsafe import re import sys from typing import Dict, Tuple +import templates HEADER_RE = re.compile(r'^:(?P[^:]+):(?P.*)') @@ -42,18 +42,7 @@ for line in sys.stdin: html_descriptions[params] += line -env = jinja2.Environment( - # Load template files from ./templates/ - loader=jinja2.FileSystemLoader('templates'), - # Auto-HTML-escape any html or xml files. - autoescape=jinja2.select_autoescape(['html', 'xml', 'html.j2', 'xml.j2']), - # Whitespace control - trim_blocks=True, - lstrip_blocks=True, - # Raise exception on any attempt to access undefined variables. - undefined=jinja2.StrictUndefined, -) -template = env.get_template('output.html.j2') +template = templates.env().get_template('output.html.j2') print(template.render( formats=formats, # Vouch for the HTML descriptions as being safe and not needing auto-HTML-escaping. diff --git a/doc/infosrc/mkshhtml.py b/doc/infosrc/mkshhtml.py index 7314bd8d3..3edf6f7c3 100755 --- a/doc/infosrc/mkshhtml.py +++ b/doc/infosrc/mkshhtml.py @@ -2,10 +2,9 @@ # Generates shapes.html. Takes path to an html.html file to include as argv[1], # and a shapelist on stdin. -import jinja2 import markupsafe import sys -from typing import List +import templates N_PER_ROW = 4 @@ -20,18 +19,7 @@ def chunks(lst, n): # Use the shapes name in shape list to create the # contents of an HTML array. -env = jinja2.Environment( - # Load template files from ./templates/ - loader=jinja2.FileSystemLoader('templates'), - # Auto-HTML-escape any html or xml files. - autoescape=jinja2.select_autoescape(['html', 'xml', 'html.j2', 'xml.j2']), - # Whitespace control - trim_blocks=True, - lstrip_blocks=True, - # Raise exception on any attempt to access undefined variables. - undefined=jinja2.StrictUndefined, -) -template = env.get_template('shapes.html.j2') +template = templates.env().get_template('shapes.html.j2') print(template.render( html=markupsafe.Markup(open(sys.argv[1]).read()), rows=chunks(shapes, N_PER_ROW), diff --git a/doc/infosrc/templates.py b/doc/infosrc/templates.py new file mode 100644 index 000000000..c3639aa4b --- /dev/null +++ b/doc/infosrc/templates.py @@ -0,0 +1,14 @@ +import jinja2 + +def env(): + return jinja2.Environment( + # Load template files from ./templates/ + loader=jinja2.FileSystemLoader('templates'), + # Auto-HTML-escape any html or xml files. + autoescape=jinja2.select_autoescape(['html', 'xml', 'html.j2', 'xml.j2']), + # Whitespace control + trim_blocks=True, + lstrip_blocks=True, + # Raise exception on any attempt to access undefined variables. + undefined=jinja2.StrictUndefined, + )