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 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 proc.send_output(open(output_name, "wb"), encoding, input) 41 42 # Web template functions. 43 44 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 45 46 """ 47 Make an output stylesheet using the file with the given 'template_name', 48 producing a file with the given 'output_name'. 49 """ 50 51 _make_document(template_name, output_name, stylesheet_names, encoding) 52 53 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_names=["Extract.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 54 55 """ 56 Make an output stylesheet using the file with the given 'template_name', 57 producing a file with the given 'output_name', capturing the fragment 58 identified by the given 'element_id'. 59 """ 60 61 _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"element-id" : element_id}) 62 63 def ensure_stylesheet(template_name, output_name): 64 65 """ 66 Ensure the presence of an output stylesheet, preparing it if necessary 67 using the file with the given 'template_name', producing a file with the 68 given 'output_name'. 69 """ 70 71 _ensure_document(template_name, output_name, make_stylesheet) 72 73 def ensure_stylesheet_fragment(template_name, output_name, element_id): 74 75 """ 76 Ensure the presence of an output stylesheet, preparing it if necessary 77 using the file with the given 'template_name', producing a file with the 78 given 'output_name', capturing the fragment identified by the given 79 'element_id'. 80 """ 81 82 _ensure_document(template_name, output_name, make_stylesheet_fragment, element_id) 83 84 # Document initialisation functions. 85 86 def make_input_stylesheet(template_name, input_name, init_enumerations=1, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None): 87 88 """ 89 Make an input stylesheet using the file with the given 'template_name', 90 producing a file with the given 'input_name'. Such stylesheets are used to 91 ensure the general structure of an input document. 92 93 The optional 'init_enumerations' (defaulting to true) may be used to 94 indicate whether enumerations are to be initialised from external documents. 95 """ 96 97 if init_enumerations: 98 init_enumerations_str = "yes" 99 else: 100 init_enumerations_str = "no" 101 _make_document(template_name, input_name, stylesheet_names, encoding, parameters={"init-enumerations" : init_enumerations_str}) 102 103 def ensure_input_stylesheet(template_name, input_name, init_enumerations=1): 104 105 """ 106 Ensure the presence of an input stylesheet, preparing it if necessary 107 using the file with the given 'template_name', producing a file with the 108 given 'input_name'. 109 110 The optional 'init_enumerations' (defaulting to true) may be used to 111 indicate whether enumerations are to be initialised from external documents. 112 """ 113 114 _ensure_document(template_name, input_name, make_input_stylesheet, init_enumerations) 115 116 # Schema-related functions. 117 118 def make_schema(template_name, output_name, stylesheet_names=["Schema.xsl"], encoding=None): 119 120 """ 121 Make a schema document using the file with the given 'template_name', 122 producing a file with the given 'output_name'. 123 """ 124 125 _make_document(template_name, output_name, stylesheet_names, encoding) 126 127 # Template repair functions. 128 129 def fix_namespaces(template_name, output_name, stylesheet_names=["FixNamespace.xsl"], encoding=None): 130 131 """ 132 Fix damage done to document namespaces by various editing tools, taking the 133 document identified by 'template_name' and producing a new document with the 134 given 'output_name'. 135 """ 136 137 _make_document(template_name, output_name, stylesheet_names, encoding) 138 139 # Qt Designer functions. 140 141 def make_qt_fragment(template_name, output_name, widget_name, stylesheet_names=["QtDesignerExtract.xsl"], encoding=None): 142 _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name}) 143 144 def ensure_qt_fragment(template_name, output_name, widget_name): 145 _ensure_document(template_name, output_name, make_qt_fragment, widget_name) 146 147 # Qt Designer Web functions. 148 149 def make_qt_template(template_name, output_name, stylesheet_names=["QtDesigner.xsl"], encoding=None): 150 _make_document(template_name, output_name, stylesheet_names, encoding) 151 152 def ensure_qt_template(template_name, output_name): 153 _ensure_document(template_name, output_name, make_qt_template) 154 155 def make_qt_stylesheet(template_name, output_name, stylesheet_names=["QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 156 _make_document(template_name, output_name, stylesheet_names, encoding) 157 158 def ensure_qt_stylesheet(template_name, output_name): 159 _ensure_document(template_name, output_name, make_qt_stylesheet) 160 161 # Qt Designer Web functions for fragments. 162 163 def make_qt_template_fragment(template_name, output_name, widget_name, stylesheet_names=["QtDesignerExtract.xsl", "QtDesigner.xsl"], encoding=None): 164 _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name}) 165 166 def ensure_qt_template_fragment(template_name, output_name, widget_name): 167 _ensure_document(template_name, output_name, make_qt_template_fragment, widget_name) 168 169 def make_qt_stylesheet_fragment(template_name, output_name, widget_name, 170 stylesheet_names=["QtDesignerExtract.xsl", "QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 171 172 _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name}) 173 174 def ensure_qt_stylesheet_fragment(template_name, output_name, widget_name): 175 _ensure_document(template_name, output_name, make_qt_stylesheet_fragment, widget_name) 176 177 # vim: tabstop=4 expandtab shiftwidth=4