7 # Assume we only use the "== Section Name" section title syntax
8 sectionheader_re
= re
.compile(r
'^==+\s(.*)\s*$')
10 # Assume we only use the "[[ItemName]]" anchor syntax
11 anchor_re
= re
.compile(r
'^\[\[([^]]+)\]\]')
16 # Initial state is to gather section headers
17 self
.getline
= self
._getsec
20 def _getsec(self
, line
):
21 """Read a section header
23 Prepare to gather anchors from subsequent lines. Don't change
24 state if the line isn't a section header.
26 m
= sectionheader_re
.match(line
)
29 self
.anchors
= anchors
= []
30 self
.d
[m
.group(1)] = anchors
31 self
.getline
= self
._getanchor
33 def _getanchor(self
, line
):
34 """Read an anchor for an item definition
36 Append the anchor names to the list of items in the current
39 m
= anchor_re
.match(line
)
41 return self
._getsec
(line
)
42 self
.anchors
.append(m
.group(1))
44 def diffsort(self
, key
):
45 """Unified diff of unsorted and sorted item lists
47 # Append newlines because difflib works better with them
48 a
= [s
+ '\n' for s
in self
.d
[key
]]
49 b
= sorted(a
, key
=str.lower
)
50 return difflib
.unified_diff(a
, b
, fromfile
=key
+' unsorted',
54 """Diff unsorted and sorted lists of option names in a manpage
56 Use the file named by the first argument, or standard input if
68 for key
in sorted(reader
.d
.keys(), key
=str.lower
):
69 sys
.stdout
.writelines(reader
.diffsort(key
))
71 if __name__
== '__main__':