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 # Web template functions. 38 39 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 40 41 """ 42 Make an output stylesheet using the file with the given 'template_name', 43 producing a file with the given 'output_name'. 44 """ 45 46 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 47 proc = XSLOutput.Processor(stylesheets) 48 template = libxml2dom.parse(template_name) 49 proc.send_output(open(output_name, "wb"), encoding, template) 50 51 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding=None): 52 53 """ 54 Make an output stylesheet using the file with the given 'template_name', 55 producing a file with the given 'output_name', capturing the fragment 56 identified by the given 'element_id'. 57 """ 58 59 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) 60 template = libxml2dom.parse(template_name) 61 proc.send_output(open(output_name, "wb"), encoding, template) 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_stylesheet(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_stylesheet(template_name, output_name, make_stylesheet_fragment, element_id) 83 84 # Document initialisation functions. 85 86 def make_input_stylesheet(template_name, input_name, 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 94 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 95 proc = XSLOutput.Processor(stylesheets) 96 template = libxml2dom.parse(template_name) 97 proc.send_output(open(input_name, "wb"), encoding, template) 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 # Qt Designer functions. 110 111 def make_qt_fragment(template_name, output_name, widget_name, stylesheet_name="QtDesignerExtract.xsl", encoding=None): 112 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"widget-name" : widget_name}) 113 template = libxml2dom.parse(template_name) 114 proc.send_output(open(output_name, "wb"), encoding, template) 115 116 def ensure_qt_fragment(template_name, output_name, widget_name): 117 _ensure_stylesheet(template_name, output_name, make_qt_fragment, widget_name) 118 119 # Qt Designer Web functions. 120 121 def make_qt_template(template_name, output_name, stylesheet_names=["QtDesigner.xsl"], encoding=None): 122 return make_stylesheet(template_name, output_name, stylesheet_names, encoding) 123 124 def ensure_qt_template(template_name, output_name): 125 _ensure_stylesheet(template_name, output_name, make_qt_template) 126 127 def make_qt_stylesheet(template_name, output_name, stylesheet_names=["QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 128 return make_stylesheet(template_name, output_name, stylesheet_names, encoding) 129 130 def ensure_qt_stylesheet(template_name, output_name): 131 _ensure_stylesheet(template_name, output_name, make_qt_stylesheet) 132 133 # vim: tabstop=4 expandtab shiftwidth=4