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