diff options
author | Deon Lackey <dlackey@dlackey.csb> | 2010-08-09 18:26:00 -0400 |
---|---|---|
committer | Deon Lackey <dlackey@dlackey.csb> | 2010-08-09 18:26:00 -0400 |
commit | af7d793377a2bcb2ed202e3c59688a01519aedd2 (patch) | |
tree | b342f8ff8cbcdc1e020eda2c0225ac95e4d00069 | |
download | repo-af7d793377a2bcb2ed202e3c59688a01519aedd2.tar.gz repo-af7d793377a2bcb2ed202e3c59688a01519aedd2.tar.xz repo-af7d793377a2bcb2ed202e3c59688a01519aedd2.zip |
initial xml2man script
-rwxr-xr-x | docbook2nroff.py | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/docbook2nroff.py b/docbook2nroff.py new file mode 100755 index 0000000..ae35773 --- /dev/null +++ b/docbook2nroff.py @@ -0,0 +1,196 @@ +#!/usr/bin/env python +import sys +from xml import sax + +class Variable(object): + def __init__(self): + self.term = None + self.listitem = None + + def __str__(self): + return "%s %s" % (self.term, self.listitem) + +class TextBlock(object): + def __init__(self, text = "", order = 0): + self.text = text + self.order = order + + def __str__(self): + return self.text + +class Para(TextBlock): + def __str__(self): + return "\n.PP\n" + self.text.lstrip() + +class Code(TextBlock): + def __str__(self): + return "\n.nf\n%s\n.fi\n" % self.text.lstrip() + +class DocBookToNroffHandler(sax.ContentHandler): + def __init__(self, out=sys.stdout): + ########################################################################################### + self.out = out + self.stack = [] + ########################################################################################### + + self.refentryinfo = {} + self.refmeta = {} + self.refnamediv = {} + self.synopsis = {} + self.refsection = {} + + self.mypara = None + self.myvar = None + self.myprogramlisting = None + self.elementcount = 0 + self.formatting = [] + self.justformatted = False + + def is_formatting(self, what): + return what in [ "command", "option", "parameter", "replaceable", "filename", "emphasis" ] + + def translate_formatting(self, what): + if what in ["command", "option", "parameter", "filename"]: + return "B" + elif what in ["emphasis", "replaceable"]: + return "I" + return "" + + def startElement(self, name, attrs): + ########################################################################################### + #print >> self.out, "+" + name + name = name.lower() + self.stack.insert(0, name) + self.elementcount += 1 + #print >> self.out, self.stack + ########################################################################################### +# if name == "arg": +# attrs.get("choice") + + if "refsection" == name: + self.refsection["id"] = attrs["id"] + + if "para" == name: + self.mypara = Para(order=self.elementcount) + + if "varlistentry" == name: + self.myvar = Variable() + + if "programlisting" == name: + self.myprogramlisting = Code(order=self.elementcount) + + if self.is_formatting(name): + self.formatting.append(self.translate_formatting(name)) + + def characters(self, content): + ########################################################################################### + #print >> self.out, content + ########################################################################################### + + if "refentryinfo" in self.stack: + if "refentrytitle" in self.stack: + self.refentryinfo["refentrytitle"] = content + if "manvolnum" in self.stack: + self.refentryinfo["manvolnum"] = content + if "productname" in self.stack: + self.refentryinfo["productname"] = content + if "date" in self.stack: + self.refentryinfo["date"] = content + + elif "refmeta" in self.stack: + if "refmeta" in self.stack: + self.refmeta["manvolnum"] = content + + elif "refnamediv" in self.stack: + if "refname" in self.stack: + self.refentryinfo["refname"] = content + if "refpurpose" in self.stack: + self.refentryinfo["refpurpose"] = content + + elif "cmdsynopsis" in self.stack: + if "command" in self.stack: + self.synopsis["command"] = content + elif "arg" in self.stack: + if not self.synopsis.has_key("args"): + self.synopsis["args"] = [] + self.synopsis["args"].append(content) + + elif "refsection" in self.stack: + if "title" in self.stack: + self.refsection["title"] = content + elif "varlistentry" in self.stack: + if "term" in self.stack: + self.myvar.term = content + elif "listitem" in self.stack: + self.myvar.listitem = content + elif "programlisting" in self.stack: + self.myprogramlisting.text += content + elif "para" in self.stack: + if self.formatting: + self.mypara.text += "\n.%s %s\n" % ("".join(self.formatting), content) + self.justformatted = True + else: + if self.justformatted: + self.mypara.text += content.lstrip() + else: + self.mypara.text += content + self.justformatted = False + + + def endElement(self, name): + ########################################################################################### + #print >> self.out, "-" + name + name = name.lower() + del self.stack[0] + #print >> self.out, self.stack + ########################################################################################### + + if "refentryinfo" == name: + print >> self.out, ".TH %(refentrytitle)s %(manvolnum)s \"%(date)s\" \"USER COMMANDS\"" % self.refentryinfo + + if "refnamediv" == name: + print >> self.out, ".SH NAME \n %(refname)s \- %(refpurpose)s" % self.refentryinfo + + if "cmdsynopsis" == name: + self.synopsis["argcombo"] = " ".join(self.synopsis["args"]) + print >> self.out, ".SH Synopsis \n.B %(command)s \n%(argcombo)s" % self.synopsis + + if "refsection" == name: + print >> self.out, ".SH %(title)s \n" % self.refsection + if self.refsection.has_key("varlist"): + for v in self.refsection["varlist"]: + print >> self.out, ".TP \n.B %s\n%s" % (v.term,v.listitem) + + if self.refsection.has_key("textblock"): + self.refsection["textblock"].sort(cmp = lambda a,b: a.order - b.order) + for p in self.refsection["textblock"]: + print >> self.out, str(p) + + self.refsection = {} + + if "varlistentry" == name: + if not self.refsection.has_key("varlist"): + self.refsection["varlist"] = [] + self.refsection["varlist"].append(self.myvar) + self.myvar = None + + if "programlisting" == name: + if not self.refsection.has_key("textblock"): + self.refsection["textblock"] = [] + self.refsection["textblock"].append(self.myprogramlisting) + self.myprogramlisting = None + + if "para" == name: + if not self.refsection.has_key("textblock"): + self.refsection["textblock"] = [] + self.refsection["textblock"].append(self.mypara) + self.mypara = None + + if self.is_formatting(name): + self.formatting.pop() + + + +parser = sax.make_parser() +parser.setContentHandler(DocBookToNroffHandler()) +parser.parse(open(sys.argv[1], "r")) |