1 #!/usr/bin/env python 2 3 """ 4 Preparation of templating stylesheets. 5 6 Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_stylesheet(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): 38 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 39 proc = XSLOutput.Processor(stylesheets) 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_name="Extract.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 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) 63 template = libxml2dom.parse(template_name) 64 proc.send_output(open(output_name, "wb"), encoding, template) 65 66 def ensure_stylesheet(template_name, output_name): 67 68 """ 69 Ensure the presence of an output stylesheet, preparing it if necessary 70 using the file with the given 'template_name', producing a file with the 71 given 'output_name'. 72 """ 73 74 _ensure_stylesheet(template_name, output_name, make_stylesheet) 75 76 def ensure_stylesheet_fragment(template_name, output_name, element_id): 77 78 """ 79 Ensure the presence of an output stylesheet, preparing it if necessary 80 using the file with the given 'template_name', producing a file with the 81 given 'output_name', capturing the fragment identified by the given 82 'element_id'. 83 """ 84 85 _ensure_stylesheet(template_name, output_name, make_stylesheet_fragment, element_id) 86 87 # Document initialisation functions. 88 89 def make_input_stylesheet(template_name, input_name, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None): 90 91 """ 92 Make an input stylesheet using the file with the given 'template_name', 93 producing a file with the given 'input_name'. Such stylesheets are used to 94 ensure the general structure of an input document. 95 """ 96 97 _make_document(template_name, input_name, stylesheet_names, encoding) 98 99 def ensure_input_stylesheet(template_name, input_name): 100 101 """ 102 Ensure the presence of an input stylesheet, preparing it if necessary 103 using the file with the given 'template_name', producing a file with the 104 given 'input_name'. 105 """ 106 107 _ensure_stylesheet(template_name, input_name, make_input_stylesheet) 108 109 # Schema-related functions. 110 111 def make_schema(template_name, output_name, stylesheet_names=["Schema.xsl"], encoding=None): 112 113 """ 114 Make a schema document using the file with the given 'template_name', 115 producing a file with the given 'output_name'. 116 """ 117 118 _make_document(template_name, output_name, stylesheet_names, encoding) 119 120 # Qt Designer functions. 121 122 def make_qt_fragment(template_name, output_name, widget_name, stylesheet_name="QtDesignerExtract.xsl", encoding=None): 123 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"widget-name" : widget_name}) 124 template = libxml2dom.parse(template_name) 125 proc.send_output(open(output_name, "wb"), encoding, template) 126 127 def ensure_qt_fragment(template_name, output_name, widget_name): 128 _ensure_stylesheet(template_name, output_name, make_qt_fragment, widget_name) 129 130 # Qt Designer Web functions. 131 132 def make_qt_template(template_name, output_name, stylesheet_names=["QtDesigner.xsl"], encoding=None): 133 return make_stylesheet(template_name, output_name, stylesheet_names, encoding) 134 135 def ensure_qt_template(template_name, output_name): 136 _ensure_stylesheet(template_name, output_name, make_qt_template) 137 138 def make_qt_stylesheet(template_name, output_name, stylesheet_names=["QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 139 return make_stylesheet(template_name, output_name, stylesheet_names, encoding) 140 141 def ensure_qt_stylesheet(template_name, output_name): 142 _ensure_stylesheet(template_name, output_name, make_qt_stylesheet) 143 144 # vim: tabstop=4 expandtab shiftwidth=4