]> pere.pagekite.me Git - text-free-culture-lessig.git/blob - scripts/verify-indexterm-range
Fix a spelling error.
[text-free-culture-lessig.git] / scripts / verify-indexterm-range
1 #!/usr/bin/python
2 #
3 # Verify the <indexterm> ranges of a docbook document, ensuring
4 # endofrange come after startofrange, and the IDs used by startofrange
5 # are unique.
6
7 from lxml import etree
8 import subprocess
9
10 def main():
11 filename = 'freeculture.xml'
12
13 # make sure entities are looked up / available
14 # Based on idea from
15 # http://stackoverflow.com/questions/14731633/how-to-resolve-external-entities-with-xml-etree-like-lxml-etree
16 proc = subprocess.Popen(['xmllint','--noent',filename],stdout=subprocess.PIPE)
17 output = proc.communicate()[0]
18 doc = ElementTree.parse(StringIO.StringIO(output))
19
20 ids = {}
21 order = 0
22 for it in doc.getroot().xpath('//indexterm'):
23 order = order + 1
24 indextermclass = None
25 id = None
26 if 'class' in it.attrib:
27 indextermclass = it.attrib['class']
28 if 'startofrange' == indextermclass:
29 id=it.attrib['id']
30 if id in ids:
31 print "error: non-unique indexterm id: %s" % id
32 ids[id] = order
33 if 'endofrange' == indextermclass:
34 id = it.attrib['startref']
35 if id not in ids:
36 print "error: indexterm id=\"%s\" not listed before endofrange" % id
37 print indextermclass, id
38
39 if __name__ == "main":
40 main()