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