]> granicus.if.org Git - graphviz/commitdiff
Extract common Jinja2 environment code
authorMark Hansen <mark@markhansen.co.nz>
Tue, 2 Jun 2020 11:23:26 +0000 (21:23 +1000)
committerMark Hansen <mark@markhansen.co.nz>
Wed, 3 Jun 2020 10:18:26 +0000 (20:18 +1000)
Into one file.

doc/infosrc/mkattrs.py
doc/infosrc/mkoutput.py
doc/infosrc/mkshhtml.py
doc/infosrc/templates.py [new file with mode: 0644]

index 69801c8a3984506a7de6677be08ef9103515cb2a..a8fbddeae4b2081b205e716dc62fbb92e705a67e 100755 (executable)
@@ -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))
index ac350c4ef82f0840a8ca712cbdc4c9c40d9a47b6..f6eb25a673c2fe3c4626989fb3663b4a3a22bdb0 100755 (executable)
@@ -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<params>[^:]+):(?P<format>.*)')
 
@@ -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.
index 7314bd8d318cc125bf505824becb2cd90a1eee2c..3edf6f7c3562033dc36381e574fcf1df1ed311d2 100755 (executable)
@@ -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 (file)
index 0000000..c3639aa
--- /dev/null
@@ -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,
+  )