paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paul@688 | 4 | Preparation of templating stylesheets and related resources. |
paulb@116 | 5 | |
paul@688 | 6 | Copyright (C) 2005, 2006, 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 7 | |
paulb@600 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 11 | later version. |
paulb@116 | 12 | |
paulb@600 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 16 | details. |
paulb@116 | 17 | |
paulb@600 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@1 | 20 | """ |
paulb@1 | 21 | |
paulb@202 | 22 | from XSLTools import XSLOutput |
paulb@1 | 23 | import libxml2dom |
paulb@1 | 24 | import os |
paulb@1 | 25 | |
paulb@1 | 26 | resource_dir = os.path.join(os.path.split(__file__)[0], "XSL") |
paulb@1 | 27 | |
paulb@339 | 28 | # Generic functions. |
paulb@339 | 29 | |
paulb@372 | 30 | def _ensure_document(template_name, output_name, fn, *args, **kw): |
paulb@339 | 31 | if not os.path.exists(output_name) or \ |
paulb@339 | 32 | os.path.getmtime(output_name) < os.path.getmtime(template_name): |
paulb@339 | 33 | |
paulb@339 | 34 | fn(template_name, output_name, *args, **kw) |
paulb@339 | 35 | |
paulb@364 | 36 | def _make_document(input_name, output_name, stylesheet_names, encoding=None, parameters=None): |
paulb@356 | 37 | stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] |
paulb@364 | 38 | proc = XSLOutput.Processor(stylesheets, parameters=parameters or {}) |
paulb@356 | 39 | input = libxml2dom.parse(input_name) |
paulb@659 | 40 | if hasattr(input, "xinclude"): |
paulb@659 | 41 | input.xinclude() |
paulb@356 | 42 | proc.send_output(open(output_name, "wb"), encoding, input) |
paulb@356 | 43 | |
paulb@336 | 44 | # Web template functions. |
paulb@336 | 45 | |
paulb@244 | 46 | def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None): |
paulb@244 | 47 | |
paulb@244 | 48 | """ |
paulb@244 | 49 | Make an output stylesheet using the file with the given 'template_name', |
paulb@244 | 50 | producing a file with the given 'output_name'. |
paulb@244 | 51 | """ |
paulb@244 | 52 | |
paulb@356 | 53 | _make_document(template_name, output_name, stylesheet_names, encoding) |
paulb@1 | 54 | |
paulb@479 | 55 | def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_names=["Extract.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): |
paulb@244 | 56 | |
paulb@244 | 57 | """ |
paulb@244 | 58 | Make an output stylesheet using the file with the given 'template_name', |
paulb@244 | 59 | producing a file with the given 'output_name', capturing the fragment |
paulb@244 | 60 | identified by the given 'element_id'. |
paulb@244 | 61 | """ |
paulb@244 | 62 | |
paulb@364 | 63 | _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"element-id" : element_id}) |
paulb@49 | 64 | |
paulb@49 | 65 | def ensure_stylesheet(template_name, output_name): |
paulb@244 | 66 | |
paulb@244 | 67 | """ |
paulb@244 | 68 | Ensure the presence of an output stylesheet, preparing it if necessary |
paulb@244 | 69 | using the file with the given 'template_name', producing a file with the |
paulb@244 | 70 | given 'output_name'. |
paulb@244 | 71 | """ |
paulb@244 | 72 | |
paulb@372 | 73 | _ensure_document(template_name, output_name, make_stylesheet) |
paulb@49 | 74 | |
paulb@49 | 75 | def ensure_stylesheet_fragment(template_name, output_name, element_id): |
paulb@244 | 76 | |
paulb@244 | 77 | """ |
paulb@244 | 78 | Ensure the presence of an output stylesheet, preparing it if necessary |
paulb@244 | 79 | using the file with the given 'template_name', producing a file with the |
paulb@244 | 80 | given 'output_name', capturing the fragment identified by the given |
paulb@244 | 81 | 'element_id'. |
paulb@244 | 82 | """ |
paulb@244 | 83 | |
paulb@372 | 84 | _ensure_document(template_name, output_name, make_stylesheet_fragment, element_id) |
paulb@49 | 85 | |
paulb@336 | 86 | # Document initialisation functions. |
paulb@336 | 87 | |
paulb@364 | 88 | def make_input_stylesheet(template_name, input_name, init_enumerations=1, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None): |
paulb@244 | 89 | |
paulb@244 | 90 | """ |
paulb@244 | 91 | Make an input stylesheet using the file with the given 'template_name', |
paulb@248 | 92 | producing a file with the given 'input_name'. Such stylesheets are used to |
paulb@244 | 93 | ensure the general structure of an input document. |
paulb@364 | 94 | |
paulb@364 | 95 | The optional 'init_enumerations' (defaulting to true) may be used to |
paulb@364 | 96 | indicate whether enumerations are to be initialised from external documents. |
paulb@244 | 97 | """ |
paulb@244 | 98 | |
paulb@364 | 99 | if init_enumerations: |
paulb@364 | 100 | init_enumerations_str = "yes" |
paulb@364 | 101 | else: |
paulb@364 | 102 | init_enumerations_str = "no" |
paulb@364 | 103 | _make_document(template_name, input_name, stylesheet_names, encoding, parameters={"init-enumerations" : init_enumerations_str}) |
paulb@248 | 104 | |
paulb@364 | 105 | def ensure_input_stylesheet(template_name, input_name, init_enumerations=1): |
paulb@248 | 106 | |
paulb@248 | 107 | """ |
paulb@248 | 108 | Ensure the presence of an input stylesheet, preparing it if necessary |
paulb@248 | 109 | using the file with the given 'template_name', producing a file with the |
paulb@248 | 110 | given 'input_name'. |
paulb@364 | 111 | |
paulb@364 | 112 | The optional 'init_enumerations' (defaulting to true) may be used to |
paulb@364 | 113 | indicate whether enumerations are to be initialised from external documents. |
paulb@248 | 114 | """ |
paulb@248 | 115 | |
paulb@372 | 116 | _ensure_document(template_name, input_name, make_input_stylesheet, init_enumerations) |
paulb@244 | 117 | |
paulb@356 | 118 | # Schema-related functions. |
paulb@356 | 119 | |
paulb@356 | 120 | def make_schema(template_name, output_name, stylesheet_names=["Schema.xsl"], encoding=None): |
paulb@356 | 121 | |
paulb@356 | 122 | """ |
paulb@356 | 123 | Make a schema document using the file with the given 'template_name', |
paulb@356 | 124 | producing a file with the given 'output_name'. |
paulb@356 | 125 | """ |
paulb@356 | 126 | |
paulb@356 | 127 | _make_document(template_name, output_name, stylesheet_names, encoding) |
paulb@356 | 128 | |
paulb@533 | 129 | # Template repair functions. |
paulb@533 | 130 | |
paulb@533 | 131 | def fix_namespaces(template_name, output_name, stylesheet_names=["FixNamespace.xsl"], encoding=None): |
paulb@533 | 132 | |
paulb@533 | 133 | """ |
paulb@533 | 134 | Fix damage done to document namespaces by various editing tools, taking the |
paulb@533 | 135 | document identified by 'template_name' and producing a new document with the |
paulb@533 | 136 | given 'output_name'. |
paulb@533 | 137 | """ |
paulb@533 | 138 | |
paulb@533 | 139 | _make_document(template_name, output_name, stylesheet_names, encoding) |
paulb@533 | 140 | |
paul@688 | 141 | # Translation-related functions. |
paulb@336 | 142 | |
paul@688 | 143 | def find_translation_keys(template_name, output_name, stylesheet_names=["FindTranslationKeys.xsl"], encoding=None): |
paulb@339 | 144 | |
paul@688 | 145 | """ |
paul@688 | 146 | Find translation keys in the document identified by 'template_name', |
paul@688 | 147 | producing a translations document with the given 'output_name'. |
paul@688 | 148 | """ |
paulb@336 | 149 | |
paulb@372 | 150 | _make_document(template_name, output_name, stylesheet_names, encoding) |
paulb@339 | 151 | |
paulb@1 | 152 | # vim: tabstop=4 expandtab shiftwidth=4 |