paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Preparation of templating stylesheets. |
paulb@116 | 5 | |
paulb@116 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 7 | |
paulb@116 | 8 | This library is free software; you can redistribute it and/or |
paulb@116 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@116 | 10 | License as published by the Free Software Foundation; either |
paulb@116 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@116 | 12 | |
paulb@116 | 13 | This library is distributed in the hope that it will be useful, |
paulb@116 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@116 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@116 | 16 | Lesser General Public License for more details. |
paulb@116 | 17 | |
paulb@116 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@116 | 19 | License along with this library; if not, write to the Free Software |
paulb@116 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@1 | 21 | """ |
paulb@1 | 22 | |
paulb@202 | 23 | from XSLTools import XSLOutput |
paulb@1 | 24 | import libxml2dom |
paulb@1 | 25 | import os |
paulb@1 | 26 | |
paulb@1 | 27 | resource_dir = os.path.join(os.path.split(__file__)[0], "XSL") |
paulb@1 | 28 | |
paulb@339 | 29 | # Generic functions. |
paulb@339 | 30 | |
paulb@339 | 31 | def _ensure_stylesheet(template_name, output_name, fn, *args, **kw): |
paulb@339 | 32 | if not os.path.exists(output_name) or \ |
paulb@339 | 33 | os.path.getmtime(output_name) < os.path.getmtime(template_name): |
paulb@339 | 34 | |
paulb@339 | 35 | fn(template_name, output_name, *args, **kw) |
paulb@339 | 36 | |
paulb@336 | 37 | # Web template functions. |
paulb@336 | 38 | |
paulb@244 | 39 | def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None): |
paulb@244 | 40 | |
paulb@244 | 41 | """ |
paulb@244 | 42 | Make an output stylesheet using the file with the given 'template_name', |
paulb@244 | 43 | producing a file with the given 'output_name'. |
paulb@244 | 44 | """ |
paulb@244 | 45 | |
paulb@232 | 46 | stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] |
paulb@232 | 47 | proc = XSLOutput.Processor(stylesheets) |
paulb@1 | 48 | template = libxml2dom.parse(template_name) |
paulb@1 | 49 | proc.send_output(open(output_name, "wb"), encoding, template) |
paulb@1 | 50 | |
paulb@244 | 51 | def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding=None): |
paulb@244 | 52 | |
paulb@244 | 53 | """ |
paulb@244 | 54 | Make an output stylesheet using the file with the given 'template_name', |
paulb@244 | 55 | producing a file with the given 'output_name', capturing the fragment |
paulb@244 | 56 | identified by the given 'element_id'. |
paulb@244 | 57 | """ |
paulb@244 | 58 | |
paulb@49 | 59 | proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) |
paulb@49 | 60 | template = libxml2dom.parse(template_name) |
paulb@49 | 61 | proc.send_output(open(output_name, "wb"), encoding, template) |
paulb@49 | 62 | |
paulb@49 | 63 | def ensure_stylesheet(template_name, output_name): |
paulb@244 | 64 | |
paulb@244 | 65 | """ |
paulb@244 | 66 | Ensure the presence of an output stylesheet, preparing it if necessary |
paulb@244 | 67 | using the file with the given 'template_name', producing a file with the |
paulb@244 | 68 | given 'output_name'. |
paulb@244 | 69 | """ |
paulb@244 | 70 | |
paulb@339 | 71 | _ensure_stylesheet(template_name, output_name, make_stylesheet) |
paulb@49 | 72 | |
paulb@49 | 73 | def ensure_stylesheet_fragment(template_name, output_name, element_id): |
paulb@244 | 74 | |
paulb@244 | 75 | """ |
paulb@244 | 76 | Ensure the presence of an output stylesheet, preparing it if necessary |
paulb@244 | 77 | using the file with the given 'template_name', producing a file with the |
paulb@244 | 78 | given 'output_name', capturing the fragment identified by the given |
paulb@244 | 79 | 'element_id'. |
paulb@244 | 80 | """ |
paulb@244 | 81 | |
paulb@339 | 82 | _ensure_stylesheet(template_name, output_name, make_stylesheet_fragment, element_id) |
paulb@49 | 83 | |
paulb@336 | 84 | # Document initialisation functions. |
paulb@336 | 85 | |
paulb@257 | 86 | def make_input_stylesheet(template_name, input_name, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None): |
paulb@244 | 87 | |
paulb@244 | 88 | """ |
paulb@244 | 89 | Make an input stylesheet using the file with the given 'template_name', |
paulb@248 | 90 | producing a file with the given 'input_name'. Such stylesheets are used to |
paulb@244 | 91 | ensure the general structure of an input document. |
paulb@244 | 92 | """ |
paulb@244 | 93 | |
paulb@244 | 94 | stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] |
paulb@244 | 95 | proc = XSLOutput.Processor(stylesheets) |
paulb@244 | 96 | template = libxml2dom.parse(template_name) |
paulb@248 | 97 | proc.send_output(open(input_name, "wb"), encoding, template) |
paulb@248 | 98 | |
paulb@248 | 99 | def ensure_input_stylesheet(template_name, input_name): |
paulb@248 | 100 | |
paulb@248 | 101 | """ |
paulb@248 | 102 | Ensure the presence of an input stylesheet, preparing it if necessary |
paulb@248 | 103 | using the file with the given 'template_name', producing a file with the |
paulb@248 | 104 | given 'input_name'. |
paulb@248 | 105 | """ |
paulb@248 | 106 | |
paulb@339 | 107 | _ensure_stylesheet(template_name, input_name, make_input_stylesheet) |
paulb@244 | 108 | |
paulb@336 | 109 | # Qt Designer functions. |
paulb@336 | 110 | |
paulb@336 | 111 | def make_qt_fragment(template_name, output_name, widget_name, stylesheet_name="QtDesignerExtract.xsl", encoding=None): |
paulb@336 | 112 | proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"widget-name" : widget_name}) |
paulb@336 | 113 | template = libxml2dom.parse(template_name) |
paulb@336 | 114 | proc.send_output(open(output_name, "wb"), encoding, template) |
paulb@336 | 115 | |
paulb@336 | 116 | def ensure_qt_fragment(template_name, output_name, widget_name): |
paulb@339 | 117 | _ensure_stylesheet(template_name, output_name, make_qt_fragment, widget_name) |
paulb@339 | 118 | |
paulb@339 | 119 | # Qt Designer Web functions. |
paulb@339 | 120 | |
paulb@339 | 121 | def make_qt_template(template_name, output_name, stylesheet_names=["QtDesigner.xsl"], encoding=None): |
paulb@339 | 122 | return make_stylesheet(template_name, output_name, stylesheet_names, encoding) |
paulb@336 | 123 | |
paulb@339 | 124 | def ensure_qt_template(template_name, output_name): |
paulb@339 | 125 | _ensure_stylesheet(template_name, output_name, make_qt_template) |
paulb@339 | 126 | |
paulb@339 | 127 | def make_qt_stylesheet(template_name, output_name, stylesheet_names=["QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None): |
paulb@339 | 128 | return make_stylesheet(template_name, output_name, stylesheet_names, encoding) |
paulb@339 | 129 | |
paulb@339 | 130 | def ensure_qt_stylesheet(template_name, output_name): |
paulb@339 | 131 | _ensure_stylesheet(template_name, output_name, make_qt_stylesheet) |
paulb@336 | 132 | |
paulb@1 | 133 | # vim: tabstop=4 expandtab shiftwidth=4 |