source: stylesheets/lfs-xsl/docbook-xsl-1.78.1/extensions/xslt.py@ c158fe6

7.5-systemd 7.6-systemd 7.7-systemd 7.8-systemd 7.9-systemd
Last change on this file since c158fe6 was b1a51ac1, checked in by Krejzi <krejzi@…>, 11 years ago

Import new branch

git-svn-id: http://svn.linuxfromscratch.org/LFS/branches/systemd/BOOK@10389 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#!/usr/bin/python -u
2# $Id: xslt.py 8353 2009-03-17 16:57:50Z mzjn $
3
4import sys
5import libxml2
6import libxslt
7from docbook import adjustColumnWidths
8
9# Check the arguments
10usage = "Usage: %s xmlfile.xml xslfile.xsl [outputfile] [param1=val [param2=val]...]" % sys.argv[0]
11
12xmlfile = None
13xslfile = None
14outfile = "-"
15params = {}
16
17try:
18 xmlfile = sys.argv[1]
19 xslfile = sys.argv[2]
20except IndexError:
21 print usage
22 sys.exit(1)
23
24def quote(astring):
25 if astring.find("'") < 0:
26 return "'" + astring + "'"
27 else:
28 return '"' + astring + '"'
29
30try:
31 outfile = sys.argv[3]
32 if outfile.find("=") > 0:
33 name, value = outfile.split("=", 2)
34 params[name] = quote(value)
35 outfile = None
36
37 count = 4
38 while (sys.argv[count]):
39 try:
40 name, value = sys.argv[count].split("=", 2)
41 if params.has_key(name):
42 print "Warning: '%s' re-specified; replacing value" % name
43 params[name] = quote(value)
44 except ValueError:
45 print "Invalid parameter specification: '" + sys.argv[count] + "'"
46 print usage
47 sys.exit(1)
48 count = count+1
49except IndexError:
50 pass
51
52# ======================================================================
53# Memory debug specific
54# libxml2.debugMemory(1)
55
56# Setup environment
57libxml2.lineNumbersDefault(1)
58libxml2.substituteEntitiesDefault(1)
59libxslt.registerExtModuleFunction("adjustColumnWidths",
60 "http://nwalsh.com/xslt/ext/xsltproc/python/Table",
61 adjustColumnWidths)
62
63# Initialize and run
64styledoc = libxml2.parseFile(xslfile)
65style = libxslt.parseStylesheetDoc(styledoc)
66doc = libxml2.parseFile(xmlfile)
67result = style.applyStylesheet(doc, params)
68
69# Save the result
70if outfile:
71 style.saveResultToFilename(outfile, result, 0)
72else:
73 print result
74
75# Free things up
76style.freeStylesheet()
77doc.freeDoc()
78result.freeDoc()
79
80# Memory debug specific
81#libxslt.cleanup()
82#if libxml2.debugMemory(1) != 0:
83# print "Memory leak %d bytes" % (libxml2.debugMemory(1))
84# libxml2.dumpMemory()
Note: See TracBrowser for help on using the repository browser.