From c23d783c8f55be1471c90c51b9bbab4dd302e7c9 Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Sun, 6 Jan 2013 17:13:13 +0100 Subject: [PATCH] enhance meta info in generated html - set title - add git favicon - set generator url - add a footer - date - language Signed-off-by: Scito Signed-off-by: Olivier Matz --- diff2html.py | 84 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/diff2html.py b/diff2html.py index dc000ee..92bbf36 100644 --- a/diff2html.py +++ b/diff2html.py @@ -35,7 +35,7 @@ # and display those directly. -import sys, re, htmlentitydefs, getopt, StringIO, codecs +import sys, re, htmlentitydefs, getopt, StringIO, codecs, datetime # minimum line size, we add a zero-sized breakable space every # LINESIZE characters @@ -43,32 +43,48 @@ linesize = 20 tabsize = 8 show_CR = False encoding = "utf-8" +lang = "en" +desc = "File comparison" +dtnow = datetime.datetime.now() +modified_date = "%s+01:00"%dtnow.isoformat() html_hdr = """ - - - - HTML Diff - - - + + + + + + HTML Diff{0} + + + + + + + + + """ html_footer = """ + """ @@ -295,14 +311,15 @@ def empty_buffer(output_file): buf = [] -def parse_input(input_file, output_file, +def parse_input(input_file, output_file, input_file_name, output_file_name, exclude_headers, show_hunk_infos): global add_cpt, del_cpt global line1, line2 global hunk_off1, hunk_size1, hunk_off2, hunk_size2 if not exclude_headers: - output_file.write(html_hdr.format(encoding).encode(encoding)) + title_suffix = ' ' + input_file_name + output_file.write(html_hdr.format(title_suffix, encoding, desc, "", modified_date, lang).encode(encoding)) output_file.write(table_hdr.encode(encoding)) while True: @@ -363,7 +380,7 @@ def parse_input(input_file, output_file, empty_buffer(output_file) output_file.write(table_footer.encode(encoding)) if not exclude_headers: - output_file.write(html_footer.encode(encoding)) + output_file.write(html_footer.format("", dtnow.strftime("%d.%m.%Y")).encode(encoding)) def usage(): @@ -391,8 +408,8 @@ def main(): global show_CR global encoding - input_file = sys.stdin - output_file = sys.stdout + input_file_name = '' + output_file_name = '' exclude_headers = False show_hunk_infos = False @@ -415,8 +432,10 @@ def main(): encoding = a elif o in ("-i", "--input"): input_file = codecs.open(a, "r", encoding) + input_file_name = a elif o in ("-o", "--output"): output_file = codecs.open(a, "w") + output_file_name = a elif o in ("-x", "--exclude-html-headers"): exclude_headers = True elif o in ("-t", "--tabsize"): @@ -429,14 +448,23 @@ def main(): show_hunk_infos = True else: assert False, "unhandled option" - parse_input(input_file, output_file, + + # Use stdin if not input file is set + if not ('input_file' in locals()): + input_file = codecs.getreader(encoding)(sys.stdin) + + # Use stdout if not output file is set + if not ('output_file' in locals()): + output_file = codecs.getwriter(encoding)(sys.stdout) + + parse_input(input_file, output_file, input_file_name, output_file_name, exclude_headers, show_hunk_infos) def parse_from_memory(txt, exclude_headers, show_hunk_infos): " Parses diff from memory and returns a string with html " input_stream = StringIO.StringIO(txt) output_stream = StringIO.StringIO() - parse_input(input_stream, output_stream, exclude_headers, show_hunk_infos) + parse_input(input_stream, output_stream, '', '', exclude_headers, show_hunk_infos) return output_stream.getvalue() -- 2.20.1