From: Mark Hansen Date: Mon, 1 Jun 2020 11:19:12 +0000 (+1000) Subject: Migrate mktypes.sh from ksh to Python/Jinja2 X-Git-Tag: 2.44.1~36^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=902081e6fec2c535432d9a4e1f8be788fed68431;p=graphviz Migrate mktypes.sh from ksh to Python/Jinja2 This is one step towards creating the attrs.html page from one python/jinja2 template. --- diff --git a/doc/infosrc/Makefile b/doc/infosrc/Makefile index ff81a009c..de0ebf3cf 100644 --- a/doc/infosrc/Makefile +++ b/doc/infosrc/Makefile @@ -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 index 000000000..dac565134 --- /dev/null +++ b/doc/infosrc/mktypes.py @@ -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 index 43124e429..000000000 --- a/doc/infosrc/mktypes.sh +++ /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 "

Attribute Type Descriptions

" -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., %f for a double), and regular expressions." -print "For regular expressions, (...)* indicates 0 or more copies of the expression" -print "enclosed in the parentheses, (...)+ indicates 1 or more, and" -print "(...)? denotes 0 or 1 copy." -print "
" - -set -s ${!desc[@]} -for i -do - printf "
%s\n" $i $i - print "
${desc[$i]}" -done - -print "
" -print "\n" - -exit 0 diff --git a/doc/infosrc/templates/types.html.j2 b/doc/infosrc/templates/types.html.j2 new file mode 100644 index 000000000..bb06f0bde --- /dev/null +++ b/doc/infosrc/templates/types.html.j2 @@ -0,0 +1,16 @@ +

Attribute Type Descriptions

+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., %f for a double), and regular expressions. +For regular expressions, (...)* indicates 0 or more copies of the expression +enclosed in the parentheses, (...)+ indicates 1 or more, and +(...)? denotes 0 or 1 copy. +
+{% for t in types %} +
{{t.name}} +
{{t.html_description}} +{% endfor %} +
+ +