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