]> granicus.if.org Git - graphviz/commitdiff
Migrate mktypes.sh from ksh to Python/Jinja2
authorMark Hansen <mark@markhansen.co.nz>
Mon, 1 Jun 2020 11:19:12 +0000 (21:19 +1000)
committerMark Hansen <mark@markhansen.co.nz>
Mon, 1 Jun 2020 11:19:12 +0000 (21:19 +1000)
This is one step towards creating the attrs.html page from one
python/jinja2 template.

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

index ff81a009c1ed4cebef7149d6025660aa0410db0d..de0ebf3cf80d55a8e305c446897396ed04e16ac5 100644 (file)
@@ -119,9 +119,9 @@ sdlshapes.ps : sdlshapes.dot
 sdlshapes.png : sdlshapes.ps
        ./ps_to_png.sh sdlshapes.ps sdlshapes.png
 
-attrs.html : attrs types mkattrs.py mktypes.sh templates/attrs.html.j2
+attrs.html : attrs types mkattrs.py mktypes.py templates/attrs.html.j2
        ./mkattrs.py < attrs > $@
-       ./mktypes.sh < types >> $@
+       ./mktypes.py < types >> $@
 
 colors.html : colors.1 colors.n ../../lib/common/color_names ../../lib/common/svgcolor_names ../../lib/common/brewer_colors mkcolors.awk brewer.awk svg.awk
        mkdir -p colortmp
@@ -216,7 +216,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 \
-             mktypes.sh mkarrowtbl.sh mkoutput.py mkshhtml.sh \
+             mktypes.py mkarrowtbl.sh mkoutput.py mkshhtml.sh \
                   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/mktypes.py b/doc/infosrc/mktypes.py
new file mode 100755 (executable)
index 0000000..dac5651
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+from dataclasses import dataclass
+import jinja2
+import markupsafe
+import sys
+from typing import List
+
+
+@dataclass
+class Type:
+    name: str
+    html_description: str
+
+
+types: List[Type] = []
+
+for line in sys.stdin:
+    # Skip comment lines.
+    if line.startswith('#'):
+        continue
+    if line.startswith(':'):
+        # This is a header line. Grab out the values.
+        types.append(Type(
+            name=line[1:].rstrip(),
+            html_description=''
+        ))
+    else:
+        # This is an HTML line, possibly a continuation of a previous HTML line.
+        t = types[-1]
+        # This is purely so the diffs look the same porting from ksh, could
+        # delete this later.
+        if t.html_description != '':
+            t.html_description += '  '
+        t.html_description += line
+
+types.sort(key=lambda t: t.name)
+
+for t in types:
+    t.html_description = markupsafe.Markup(t.html_description)
+
+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('types.html.j2')
+print(template.render(
+    types=types,
+))
diff --git a/doc/infosrc/mktypes.sh b/doc/infosrc/mktypes.sh
deleted file mode 100755 (executable)
index 43124e4..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-#! /bin/ksh
-typeset -A desc
-name=
-
-# Read in each type description. Store the descriptions
-# in the desc array, indexed by the typename.
-
-OLDIFS="$IFS"
-IFS=
-while read line
-do
-  c=${line:0:1}
-  if [[ $c == '#' ]]
-  then continue
-  elif [[ $c == ':' ]]
-  then
-    if [[ -n "$name" ]]
-    then
-      desc[$name]=$txt
-      txt=""
-    fi
-    name=${line#:}
-  else
-    if [[ -z "$txt" ]]
-    then
-      txt="${line}\n"
-    else
-      txt="$txt  ${line}\n"
-    fi
-  fi
-done
-IFS="$OLDIFS"
-
-if [[ -n "$name" ]]
-then
-  desc[$name]=$txt
-  txt=""
-fi
-
-#print ${!desc[@]}
-#print ${desc[@]}
-#exit
-
-# Output type descriptions as an HTML list
-
-print "<H1>Attribute Type Descriptions</H1>"
-print "The following list gives the legal strings corresponding to values of"
-print "the given types."
-print "The syntax for describing legal type strings is a mixture of literal strings,"
-print "stdio encodings (e.g., <TT>%f</TT> for a double), and regular expressions."
-print "For regular expressions, <TT>(...)*</TT> indicates 0 or more copies of the expression"
-print "enclosed in the parentheses,  <TT>(...)+</TT> indicates 1 or more, and"
-print "<TT>(...)?</TT> denotes 0 or 1 copy."
-print "<DL>"
-
-set -s ${!desc[@]}
-for i
-do
-  printf "<DT><A NAME=k:%s><STRONG>%s</STRONG></A>\n" $i $i
-  print "<DD>${desc[$i]}"
-done
-
-print "</DL>"
-print "</BODY>\n</HTML>"
-
-exit 0
diff --git a/doc/infosrc/templates/types.html.j2 b/doc/infosrc/templates/types.html.j2
new file mode 100644 (file)
index 0000000..bb06f0b
--- /dev/null
@@ -0,0 +1,16 @@
+<H1>Attribute Type Descriptions</H1>
+The following list gives the legal strings corresponding to values of
+the given types.
+The syntax for describing legal type strings is a mixture of literal strings,
+stdio encodings (e.g., <TT>%f</TT> for a double), and regular expressions.
+For regular expressions, <TT>(...)*</TT> indicates 0 or more copies of the expression
+enclosed in the parentheses,  <TT>(...)+</TT> indicates 1 or more, and
+<TT>(...)?</TT> denotes 0 or 1 copy.
+<DL>
+{% for t in types %}
+<DT><A NAME=k:{{t.name}}><STRONG>{{t.name}}</STRONG></A>
+<DD>{{t.html_description}}
+{% endfor %}
+</DL>
+</BODY>
+</HTML>