]> granicus.if.org Git - graphviz/commitdiff
Convert mkshhtml from ksh to Python/Jinja2
authorMark Hansen <mark@markhansen.co.nz>
Mon, 1 Jun 2020 12:03:23 +0000 (22:03 +1000)
committerMark Hansen <mark@markhansen.co.nz>
Mon, 1 Jun 2020 12:03:23 +0000 (22:03 +1000)
Soon I'll inline the other parts of shapes.html into this template,
which should give us more flexibility to easily
change the template.

doc/infosrc/Makefile
doc/infosrc/mkshhtml.py [new file with mode: 0755]
doc/infosrc/mkshhtml.sh [deleted file]
doc/infosrc/templates/shapes.html.j2 [new file with mode: 0644]

index 4625154bf69bf61e578da8cffc600b97be37f500..dba36f612e2a7711b5019c9b51b473ee6f843043 100644 (file)
@@ -156,9 +156,9 @@ shapes : shapelist mkshapes.sh
        ./mkshapes.sh
        touch shapes
  
-shapes.html : shapes shapes.1 mkshhtml.sh shapes.2 html.html shapes.3 record.gif record2.gif sdlshapes.png
+shapes.html : shapes shapes.1 mkshhtml.py shapes.2 html.html shapes.3 record.gif record2.gif sdlshapes.png templates/shapes.html.j2
        cat shapes.1 > shapes.html
-       ./mkshhtml.sh >> shapes.html
+       ./mkshhtml.py < shapelist >> shapes.html
        cat shapes.2 >> shapes.html
        cat html.html >> shapes.html
        cat shapes.3 >> shapes.html
@@ -215,7 +215,7 @@ distclean : clean
        (for s in $$(cat shapelist); do rm -f $$s.gif; done)
 
 EXTRA_DIST = $(XGIF) mklang.y mkarrows.sh mkattrs.py mkshapes.sh mkstyles.sh mktapers.sh \
-             mkarrowtbl.sh mkoutput.py mkshhtml.sh \
+             mkarrowtbl.sh mkoutput.py mkshhtml.py \
                   ps_to_png.sh arrow_grammar grammar html_grammar \
              shapelist attrs.1 colors.1 colors.n \
              output.1 output.2 html.1 html.2 html1.dot html.3 \
diff --git a/doc/infosrc/mkshhtml.py b/doc/infosrc/mkshhtml.py
new file mode 100755 (executable)
index 0000000..57f2275
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+from dataclasses import dataclass
+import jinja2
+import markupsafe
+import re
+import sys
+from typing import List, Dict
+
+N_PER_ROW=4
+
+shapes = []
+for line in sys.stdin:
+  shape = line.strip()
+  shapes.append(shape)
+
+# From https://stackoverflow.com/a/312464/171898
+def chunks(lst, n):
+    """Yield successive n-sized chunks from lst."""
+    for i in range(0, len(lst), n):
+        yield lst[i:i + 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')
+print(template.render(rows=chunks(shapes, N_PER_ROW)))
diff --git a/doc/infosrc/mkshhtml.sh b/doc/infosrc/mkshhtml.sh
deleted file mode 100755 (executable)
index e94a91b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#! /bin/ksh
-typeset -i N_PER_ROW=4
-typeset -i cnt=0
-list=
-
-function doLine {
-  echo "  <TR ALIGN=CENTER>"
-  for s in $@
-  do
-    echo "    <TD><IMG SRC=$s.gif>"
-  done
-  echo "  </TR>"
-
-  echo "  <TR ALIGN=CENTER>"
-  for s in $@
-  do
-    echo "    <TD><A NAME=d:$s>$s</A>"
-  done
-  echo "  </TR>"
-}
-
-# Use the shapes name in shape list to create the
-# contents of an HTML array.
-
-for s in $(cat shapelist)
-do
-  list=$list" $s"
-  cnt=$(( $cnt + 1 ))
-  if [[ $cnt = $N_PER_ROW ]]
-  then
-    doLine $list
-    list=
-    cnt=0
-  fi
-done
-
-if [[ -n "$list" ]]
-then
-    doLine $list
-fi
-
-exit 0
diff --git a/doc/infosrc/templates/shapes.html.j2 b/doc/infosrc/templates/shapes.html.j2
new file mode 100644 (file)
index 0000000..17c5e66
--- /dev/null
@@ -0,0 +1,12 @@
+{% for row in rows %}
+  <TR ALIGN=CENTER>
+  {% for shape in row %}
+    <TD><IMG SRC={{shape}}.gif>
+  {% endfor %}
+  </TR>
+  <TR ALIGN=CENTER>
+  {% for shape in row %}
+    <TD><A NAME=d:{{shape}}>{{shape}}</A>
+  {% endfor %}
+  </TR>
+{% endfor %}