Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Specification Tools
Scripts
Commits
f88c8961
Commit
f88c8961
authored
1 year ago
by
Andreas Kraft
Browse files
Options
Downloads
Patches
Plain Diff
Added --level / -l CLA to limit the number of printed TOC levels.
parent
f66a2c55
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
generateTOC/README.md
+4
-2
4 additions, 2 deletions
generateTOC/README.md
generateTOC/generateTOC.py
+40
-3
40 additions, 3 deletions
generateTOC/generateTOC.py
with
44 additions
and
5 deletions
generateTOC/README.md
+
4
−
2
View file @
f88c8961
...
@@ -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>] [--
contents
] document
usage: generateTOC.py [-h] [--add-content]
[--contents]
[--indent <indent>] [--
level LEVELS
] document
positional arguments:
positional arguments:
document document to parse
document document to parse
...
@@ -27,7 +27,9 @@ positional arguments:
...
@@ -27,7 +27,9 @@ positional arguments:
options:
options:
-h, --help show this help message and exit
-h, --help show this help message and exit
--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)
--contents, -c add link to "Contents" section in the generated TOC (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)
--level LEVELS, -l LEVELS
limit the TOC levels; 0 means no limit (default: 0)
```
```
\ No newline at end of file
This diff is collapsed.
Click to expand it.
generateTOC/generateTOC.py
+
40
−
3
View file @
f88c8961
...
@@ -25,8 +25,21 @@ def backupFile(filename:str) -> None:
...
@@ -25,8 +25,21 @@ def backupFile(filename:str) -> None:
def
processDocument
(
args
:
argparse
.
Namespace
)
->
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
:
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
# Remove HTML tags
line
=
re
.
sub
(
'
<[^<]+?>
'
,
''
,
line
)
line
=
re
.
sub
(
'
<[^<]+?>
'
,
''
,
line
)
...
@@ -51,6 +64,12 @@ def processDocument(args:argparse.Namespace) -> None:
...
@@ -51,6 +64,12 @@ 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
# 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
:
if
(
headline
:
=
_l
.
lstrip
(
'
#
'
).
strip
())
==
'
Contents
'
and
not
args
.
contents
:
continue
continue
headers
.
append
((
headline
,
level
))
headers
.
append
((
headline
,
level
))
...
@@ -91,11 +110,29 @@ def processDocument(args:argparse.Namespace) -> None:
...
@@ -91,11 +110,29 @@ def processDocument(args:argparse.Namespace) -> None:
if
__name__
==
'
__main__
'
:
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
# Parse command line arguments
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
(
'
--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
'
)
parser
.
add_argument
(
'
document
'
,
help
=
'
document to parse
'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment