From: Mark Hansen Date: Mon, 1 Jun 2020 12:03:23 +0000 (+1000) Subject: Convert mkshhtml from ksh to Python/Jinja2 X-Git-Tag: 2.44.1~34^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6bd716163e9f4cd5bfdf6c59e1f0f71b1612bb63;p=graphviz Convert mkshhtml from ksh to Python/Jinja2 Soon I'll inline the other parts of shapes.html into this template, which should give us more flexibility to easily change the template. --- diff --git a/doc/infosrc/Makefile b/doc/infosrc/Makefile index 4625154bf..dba36f612 100644 --- a/doc/infosrc/Makefile +++ b/doc/infosrc/Makefile @@ -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 index 000000000..57f227534 --- /dev/null +++ b/doc/infosrc/mkshhtml.py @@ -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 index e94a91b21..000000000 --- a/doc/infosrc/mkshhtml.sh +++ /dev/null @@ -1,42 +0,0 @@ -#! /bin/ksh -typeset -i N_PER_ROW=4 -typeset -i cnt=0 -list= - -function doLine { - echo " " - for s in $@ - do - echo " " - done - echo " " - - echo " " - for s in $@ - do - echo " $s" - done - echo " " -} - -# 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 index 000000000..17c5e66ef --- /dev/null +++ b/doc/infosrc/templates/shapes.html.j2 @@ -0,0 +1,12 @@ +{% for row in rows %} + + {% for shape in row %} + + {% endfor %} + + + {% for shape in row %} + {{shape}} + {% endfor %} + +{% endfor %}