Skip to content
Snippets Groups Projects
Commit f66a2c55 authored by Andreas Kraft's avatar Andreas Kraft
Browse files

Added -contents / -c CLA. Printing the link to the "Contents" section is now...

Added -contents / -c CLA. Printing the link to the "Contents" section is now not added by default, but can be enabled
parent 761d5802
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ $ python generateTOC.py <document path> ...@@ -19,7 +19,7 @@ $ python generateTOC.py <document path>
## Command Line Options ## Command Line Options
``` ```
usage: generateTOC.py [-h] [--add-content] [--indent <indent>] document usage: generateTOC.py [-h] [--add-content] [--indent <indent>] [--contents] document
positional arguments: positional arguments:
document document to parse document document to parse
...@@ -29,4 +29,5 @@ options: ...@@ -29,4 +29,5 @@ options:
--add-content, -a add TOC to "# Content" section in the document (default: False) --add-content, -a add TOC to "# Content" section in the document (default: False)
--indent <indent>, -i <indent> --indent <indent>, -i <indent>
indent spaces for each level (default: 4) indent spaces for each level (default: 4)
--contents, -c add link to "Contents" section in the generated TOC (default: False)
``` ```
\ No newline at end of file
...@@ -51,7 +51,9 @@ def processDocument(args:argparse.Namespace) -> None: ...@@ -51,7 +51,9 @@ def processDocument(args:argparse.Namespace) -> None:
_l = line.strip() _l = line.strip()
if _l.startswith('#'): if _l.startswith('#'):
level = len(_l) - len(_l.lstrip('#')) - 1 # level is number of # - 1 level = len(_l) - len(_l.lstrip('#')) - 1 # level is number of # - 1
headers.append((_l.lstrip('#').strip(), level)) if (headline := _l.lstrip('#').strip()) == 'Contents' and not args.contents:
continue
headers.append((headline, level))
# Prepare and Print the table of contents # Prepare and Print the table of contents
to = '# Contents\n\n' to = '# Contents\n\n'
...@@ -93,6 +95,7 @@ if __name__ == '__main__': ...@@ -93,6 +95,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) 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('--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('--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('document', help = 'document to parse') parser.add_argument('document', help = 'document to parse')
args = parser.parse_args() args = parser.parse_args()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment