#! /usr/bin/python3 # Functions that generate HTML for reports and such. LastEdit = "Last edited on 2025-07-27 11:17:15 by stolfi" import sys, re, subprocess def out(txt): sys.stdout.write(txt) # ---------------------------------------------------------------------- def preamble(wr, title, max_width): wr.write("\n") wr.write("\n") wr.write("
\n") wr.write("\n") wr.write(f"" + txt + "
\n") wr.write(txt) return # ---------------------------------------------------------------------- def fmt_item(st, txt): # Formats a list item. DOES NOT PRINT, returns it instead. st['cur_level'] += 1 txt = fix_markup(txt) txt = indent(st, "" + fix_markup(caption) + "
" ind = " " * st['cur_level'] wr.write(ind); wr.write( "| {image} | \n") wr.write(ind); wr.write( "
| {caption} | \n") wr.write(ind); wr.write( "
\n") wr.write(f"{LastEdit}\n") wr.write("
\n") wr.write("\n") wr.write("\n") wr.write("\n") sys.stdout.flush() return # ---------------------------------------------------------------------- def fix_markup(txt): # Join lines into a single line: txt = re.sub(r"[ ]*[\n][ ]*", " ", txt) # Remover leading and trailing blanks: txt = txt.strip() txt = re.sub(r"([ .,()<>])[/]([^/]+)[/]([ .,()<>])", r"\1\2\3", txt) txt = re.sub(r"([ .,()<>])[*]([^*]+)[*]([ .,()<>])", r"\1\2\3", txt) txt = re.sub(r'["]', r""", txt) return txt # ---------------------------------------------------------------------- def get_text_from_file(fname): rd = open(fname, 'r') lines = rd.readlines() txt = ' '.join(lines) txt = fix_markup(txt) rd.close() return txt # ---------------------------------------------------------------------- def get_image_size(fname): cmd = [ "convert", fname, "-print", "'%[fx:w] %[fx:h]'", "null:-" ] res = str(subprocess.check_output(cmd).strip()) size = re.split(r"[ ]+", res) assert len(size) == 2, f"bad command output '{res}'" size = ( int(x) for x in size ) return size # ----------------------------------------------------------------------