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