2 # vi:si:et:sw=4:sts=4:ts=4
5 use the output from gst-xmlinspect.py to mangle tmpl/*.sgml and
6 insert/overwrite Short Description and Long Description
9 # FIXME: right now it uses pygst and scans on its own;
10 # we really should use inspect/*.xml instead since the result of
11 # gst-xmlinspect.py is committed by the docs maintainer, who can be
12 # expected to have pygst, but this step should be done for every docs build,
15 # read in inspect/*.xml
16 # for every tmpl/element-(name).xml: mangle with details from element
18 from __future__ import print_function, unicode_literals
26 def __init__(self, filename):
27 self.filename = filename
33 Read and parse the sections from the given file.
35 lines = open(self.filename).readlines()
36 matcher = re.compile("<!-- ##### SECTION (\S+) ##### -->\n")
40 match = matcher.search(line)
42 id = match.expand("\\1")
43 self._sectionids.append(id)
44 self._sections[id] = []
48 "WARNING: line before a SECTION header: %s" % line)
50 self._sections[id].append(line)
52 def get_section(self, id):
54 Get the content from the given section.
56 return self._sections[id]
58 def set_section(self, id, content):
60 Replace the given section id with the given content.
62 self._sections[id] = content
66 Return the output of the current template in the tmpl/*.sgml format.
69 for id in self._sectionids:
70 lines.append("<!-- ##### SECTION %s ##### -->\n" % id)
71 for line in self._sections[id]:
76 def write(self, backup=False):
78 Write out the template file again, backing up the previous one.
81 target = self.filename + ".mangle.bak"
82 os.rename(self.filename, target)
84 handle = open(self.filename, "w")
85 handle.write(self.output())
88 import xml.dom.minidom
90 def get_elements(file):
92 doc = xml.dom.minidom.parse(file)
95 for e in doc.childNodes:
96 if e.nodeType == e.ELEMENT_NODE and e.localName == 'plugin':
103 for e in elem.childNodes:
104 if e.nodeType == e.ELEMENT_NODE and e.localName == 'elements':
112 for e in elem.childNodes:
113 if e.nodeType == e.ELEMENT_NODE and e.localName == 'element':
117 for e2 in e.childNodes:
118 if e2.nodeType == e2.ELEMENT_NODE and e2.localName == 'name':
119 name = e2.childNodes[0].nodeValue.encode("UTF-8")
120 elif e2.nodeType == e2.ELEMENT_NODE and e2.localName == 'description':
122 description = e2.childNodes[0].nodeValue.encode("UTF-8")
124 description = 'No description'
126 if name != None and description != None:
127 elements[name] = {'description': description}
132 if not len(sys.argv) == 3:
133 sys.stderr.write('Please specify the inspect/ dir and the tmpl/ dir')
136 inspectdir = sys.argv[1]
137 tmpldir = sys.argv[2]
139 # parse all .xml files; build map of element name -> short desc
140 #for file in glob.glob("inspect/plugin-*.xml"):
142 for file in glob.glob("%s/plugin-*.xml" % inspectdir):
143 elements.update(get_elements(file))
145 for file in glob.glob("%s/element-*.sgml" % tmpldir):
146 base = os.path.basename(file)
147 element = base[len("element-"):-len(".sgml")]
150 if element in elements.keys():
151 description = elements[element]['description']
152 tmpl.set_section("Short_Description", "%s\n\n" % description)
154 # put in an include if not yet there
155 line = '<include xmlns="http://www.w3.org/2003/XInclude" href="' + \
156 'element-' + element + '-details.xml">' + \
157 '<fallback xmlns="http://www.w3.org/2003/XInclude" />' + \
159 section = tmpl.get_section("Long_Description")
160 if not section[0] == line:
161 section.insert(0, line)
162 tmpl.set_section("Long_Description", section)