checkManpageAlpha.py (2024B)
1 #!/usr/bin/python 2 3 import difflib 4 import re 5 import sys 6 7 # Assume we only use the "== Section Name" section title syntax 8 sectionheader_re = re.compile(r'^==+\s(.*)\s*$') 9 10 # Assume we only use the "[[ItemName]]" anchor syntax 11 anchor_re = re.compile(r'^\[\[([^]]+)\]\]') 12 13 class Reader(object): 14 def __init__(self): 15 self.d = {} 16 # Initial state is to gather section headers 17 self.getline = self._getsec 18 self.section = None 19 20 def _getsec(self, line): 21 """Read a section header 22 23 Prepare to gather anchors from subsequent lines. Don't change 24 state if the line isn't a section header. 25 """ 26 m = sectionheader_re.match(line) 27 if not m: 28 return 29 self.anchors = anchors = [] 30 self.d[m.group(1)] = anchors 31 self.getline = self._getanchor 32 33 def _getanchor(self, line): 34 """Read an anchor for an item definition 35 36 Append the anchor names to the list of items in the current 37 section. 38 """ 39 m = anchor_re.match(line) 40 if not m: 41 return self._getsec(line) 42 self.anchors.append(m.group(1)) 43 44 def diffsort(self, key): 45 """Unified diff of unsorted and sorted item lists 46 """ 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', 51 tofile=key+' sorted') 52 53 def main(): 54 """Diff unsorted and sorted lists of option names in a manpage 55 56 Use the file named by the first argument, or standard input if 57 there is none. 58 """ 59 try: 60 fname = sys.argv[1] 61 f = open(fname, 'r') 62 except IndexError: 63 f = sys.stdin 64 65 reader = Reader() 66 for line in f: 67 reader.getline(line) 68 for key in sorted(reader.d.keys(), key=str.lower): 69 sys.stdout.writelines(reader.diffsort(key)) 70 71 if __name__ == '__main__': 72 main()