diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:48:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 06:48:59 +0000 |
commit | d835b2cae8abc71958b69362162e6a70c3d7ef63 (patch) | |
tree | 81052e3d2ce3e1bcda085f73d925e9d6257dec15 /doc/sort-doc.py | |
parent | Initial commit. (diff) | |
download | crmsh-upstream.tar.xz crmsh-upstream.zip |
Adding upstream version 4.6.0.upstream/4.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'doc/sort-doc.py')
-rw-r--r-- | doc/sort-doc.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/doc/sort-doc.py b/doc/sort-doc.py new file mode 100644 index 0000000..87c35a2 --- /dev/null +++ b/doc/sort-doc.py @@ -0,0 +1,82 @@ +# Tool to sort the documentation alphabetically +# Makes a lot of assumptions about the structure of the document it edits +# Looks for special markers that indicate structure + +# prints output to stdout + +# print lines until in a cmdhelp_<section> +# collect all cmdhelp_<section>_<subsection> subsections +# sort and print + +import sys +import re + + +class Sorter(object): + def __init__(self): + self.current_section = None + self.current_subsection = None + self.subsections = [] + self.re_section = re.compile(r'^\[\[cmdhelp_([^_,]+),') + self.re_subsection = re.compile(r'^\[\[cmdhelp_([^_]+)_([^,]+),') + + def beginsection(self, line): + m = self.re_section.match(line) + name = m.group(1) + self.current_section = [name, line] + self.current_subsection = None + self.subsections = [] + return self.insection + + def insection(self, line): + if line.startswith('[[cmdhelp_%s_' % (self.current_section[0])): + return self.beginsubsection(line) + elif line.startswith('[['): + self.finishsection() + return self.preprint(line) + else: + self.current_section[1] += line + return self.insection + + def beginsubsection(self, line): + m = self.re_subsection.match(line) + name = m.group(2) + self.current_subsection = [name, line] + return self.insubsection + + def insubsection(self, line): + if line.startswith('[['): + self.subsections.append(self.current_subsection) + self.current_subsection = None + return self.insection(line) + self.current_subsection[1] += line + return self.insubsection + + def finishsection(self): + if self.current_section: + print self.current_section[1], + for name, text in sorted(self.subsections, key=lambda x: x[0]): + print text, + self.current_section = None + self.subsections = [] + + def preprint(self, line): + if self.re_section.match(line): + return self.beginsection(line) + print line, + return self.preprint + + def run(self, lines): + action = self.preprint + for line in lines: + prevaction = action + action = action(line) + if action is None: + print prevaction + print self.current_section + print self.current_subsection + sys.exit(1) + if self.current_section: + self.finishsection() + +Sorter().run(open(sys.argv[1]).readlines()) |