From f88c8961cc34c9511b64b973fac8bddc712fdadc Mon Sep 17 00:00:00 2001 From: ankraft <an.kraft@gmail.com> Date: Tue, 10 Oct 2023 16:37:17 +0200 Subject: [PATCH] Added --level / -l CLA to limit the number of printed TOC levels. --- generateTOC/README.md | 6 ++++-- generateTOC/generateTOC.py | 43 +++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/generateTOC/README.md b/generateTOC/README.md index 4907235..ac34a43 100644 --- a/generateTOC/README.md +++ b/generateTOC/README.md @@ -19,7 +19,7 @@ $ python generateTOC.py <document path> ## Command Line Options ``` -usage: generateTOC.py [-h] [--add-content] [--indent <indent>] [--contents] document +usage: generateTOC.py [-h] [--add-content] [--contents] [--indent <indent>] [--level LEVELS] document positional arguments: document document to parse @@ -27,7 +27,9 @@ positional arguments: options: -h, --help show this help message and exit --add-content, -a add TOC to "# Content" section in the document (default: False) + --contents, -c add link to "Contents" section in the generated TOC (default: False) --indent <indent>, -i <indent> indent spaces for each level (default: 4) - --contents, -c add link to "Contents" section in the generated TOC (default: False) + --level LEVELS, -l LEVELS + limit the TOC levels; 0 means no limit (default: 0) ``` \ No newline at end of file diff --git a/generateTOC/generateTOC.py b/generateTOC/generateTOC.py index ef65376..5f63622 100644 --- a/generateTOC/generateTOC.py +++ b/generateTOC/generateTOC.py @@ -25,8 +25,21 @@ def backupFile(filename:str) -> None: def processDocument(args:argparse.Namespace) -> None: + """ Process the document and generate the TOC. + + Args: + args: The command line arguments. + """ + def prepareTOClink(line:str) -> str: - """Prepare a link for the TOC""" + """ Prepare a link for the TOC. + + Args: + line: The line to prepare. + + Returns: + The prepared line. + """ # Remove HTML tags line = re.sub('<[^<]+?>', '', line) @@ -51,6 +64,12 @@ def processDocument(args:argparse.Namespace) -> None: _l = line.strip() if _l.startswith('#'): level = len(_l) - len(_l.lstrip('#')) - 1 # level is number of # - 1 + + # Skip if level is to large + if args.levels and level >= args.levels: + continue + + # Skip the Contents headline if necessary if (headline := _l.lstrip('#').strip()) == 'Contents' and not args.contents: continue headers.append((headline, level)) @@ -91,11 +110,29 @@ def processDocument(args:argparse.Namespace) -> None: if __name__ == '__main__': + def nonNegativeInt(value:str) -> int: + """Check if a value is a non-negative integer. + + Args: + value: The value to check. + + Returns: + The value if it is a non-negative integer. + + Raises: + argparse.ArgumentTypeError: If the value is not a non-negative integer. + """ + ivalue = int(value) + if ivalue < 0: + raise argparse.ArgumentTypeError("%s is an invalid non-negative value" % value) + return ivalue + # Parse command line arguments parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--add-content', '-a', action='store_true', dest='addContent', default = False, help = 'add TOC to "# Content" section in the document') - parser.add_argument('--indent', '-i', action='store', dest='indent', default = 4, metavar = '<indent>', help = 'indent spaces for each level') - parser.add_argument('--contents', '-c', action='store_true', dest='contents', default = False, help = 'add link to "Contents" section in the generated TOC') + parser.add_argument('--contents', '-c', action='store_true', dest='contents', default = False, help = 'add link to "Contents" section in the generated TOC') + parser.add_argument('--indent', '-i', action='store', dest='indent', type = nonNegativeInt, default = 4, metavar = '<indent>', help = 'indent spaces for each level') + parser.add_argument('--level', '-l', action='store', dest='levels', type = nonNegativeInt, default = 0, help = 'limit the TOC levels; 0 means no limit') parser.add_argument('document', help = 'document to parse') args = parser.parse_args() -- GitLab