Loading generateTOC/README.md +4 −2 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 generateTOC/generateTOC.py +40 −3 Original line number Diff line number Diff line Loading @@ -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) Loading @@ -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)) Loading Loading @@ -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('--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() Loading Loading
generateTOC/README.md +4 −2 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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
generateTOC/generateTOC.py +40 −3 Original line number Diff line number Diff line Loading @@ -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) Loading @@ -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)) Loading Loading @@ -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('--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() Loading