paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Preparation of templating stylesheets. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import XSLOutput |
paulb@1 | 8 | import libxml2dom |
paulb@1 | 9 | import os |
paulb@1 | 10 | |
paulb@1 | 11 | resource_dir = os.path.join(os.path.split(__file__)[0], "XSL") |
paulb@1 | 12 | |
paulb@1 | 13 | def make_stylesheet(template_name, output_name, stylesheet_name="Prepare.xsl", encoding="utf-8"): |
paulb@1 | 14 | global resource_dir |
paulb@1 | 15 | proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)]) |
paulb@1 | 16 | template = libxml2dom.parse(template_name) |
paulb@1 | 17 | proc.send_output(open(output_name, "wb"), encoding, template) |
paulb@1 | 18 | |
paulb@49 | 19 | def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding="utf-8"): |
paulb@49 | 20 | global resource_dir |
paulb@49 | 21 | proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) |
paulb@49 | 22 | template = libxml2dom.parse(template_name) |
paulb@49 | 23 | proc.send_output(open(output_name, "wb"), encoding, template) |
paulb@49 | 24 | |
paulb@49 | 25 | def ensure_stylesheet(template_name, output_name): |
paulb@49 | 26 | if not os.path.exists(output_name) or \ |
paulb@49 | 27 | os.path.getmtime(output_name) < os.path.getmtime(template_name): |
paulb@49 | 28 | |
paulb@49 | 29 | make_stylesheet(template_name, output_name) |
paulb@49 | 30 | |
paulb@49 | 31 | def ensure_stylesheet_fragment(template_name, output_name, element_id): |
paulb@49 | 32 | if not os.path.exists(output_name) or \ |
paulb@49 | 33 | os.path.getmtime(output_name) < os.path.getmtime(template_name): |
paulb@49 | 34 | |
paulb@49 | 35 | make_stylesheet_fragment(template_name, output_name, element_id) |
paulb@49 | 36 | |
paulb@1 | 37 | # vim: tabstop=4 expandtab shiftwidth=4 |