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 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding="utf-8"): 30 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 31 proc = XSLOutput.Processor(stylesheets) 32 template = libxml2dom.parse(template_name) 33 proc.send_output(open(output_name, "wb"), encoding, template) 34 35 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding="utf-8"): 36 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) 37 template = libxml2dom.parse(template_name) 38 proc.send_output(open(output_name, "wb"), encoding, template) 39 40 def ensure_stylesheet(template_name, output_name): 41 if not os.path.exists(output_name) or \ 42 os.path.getmtime(output_name) < os.path.getmtime(template_name): 43 44 make_stylesheet(template_name, output_name) 45 46 def ensure_stylesheet_fragment(template_name, output_name, element_id): 47 if not os.path.exists(output_name) or \ 48 os.path.getmtime(output_name) < os.path.getmtime(template_name): 49 50 make_stylesheet_fragment(template_name, output_name, element_id) 51 52 # vim: tabstop=4 expandtab shiftwidth=4