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
8afe9e6d
Commit
8afe9e6d
authored
1 year ago
by
Andreas Kraft
Browse files
Options
Downloads
Patches
Plain Diff
Now using absolute filenames
parent
4e5fb541
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
toMkdocs/toMkdocs.py
+62
-6
62 additions, 6 deletions
toMkdocs/toMkdocs.py
with
62 additions
and
6 deletions
toMkdocs/toMkdocs.py
+
62
−
6
View file @
8afe9e6d
...
...
@@ -37,6 +37,8 @@ def analyseMarkdown(filename:str) -> list[Clause]:
The list of clauses.
"""
print
(
f
'
[gray]Analyzing file
"
{
filename
}
"'
)
with
open
(
filename
,
'
r
'
)
as
file
:
inLines
=
file
.
readlines
()
...
...
@@ -165,7 +167,7 @@ def writeClauses(outLines:list[Clause], filename:str, navTitle:str) -> None:
# write to single files
with
open
(
f
'
{
os
.
path
.
dirname
(
filename
)
}
/
{
navTitle
}
/
{
i
}
.md
'
,
'
w
'
)
as
file
:
file
.
writelines
(
f
.
lines
)
print
(
f
'
[green]File
"
{
i
}
.md
"
written
'
)
print
(
f
'
[green]File
"
{
i
}
.md
"
written
-
"
{
f
.
title
}
"
'
)
# write nav.yml file
...
...
@@ -181,6 +183,58 @@ def writeClauses(outLines:list[Clause], filename:str, navTitle:str) -> None:
print
(
f
'
[green]File
"
_nav.yml
"
written
'
)
_markdownLink
=
re
.
compile
(
r
'
\[.*\]\((.*)\)
'
,
re
.
IGNORECASE
)
_htmlLink
=
re
.
compile
(
r
'
<a\s+href=
"
([^
"
\']*)
"
>[^<]*</a>
'
,
re
.
IGNORECASE
)
_anchorLink
=
re
.
compile
(
r
'
<a\s+name=
"
([^
"
]*)
"
>[^<]*</a>
'
,
re
.
IGNORECASE
)
def
updateLinks
(
clauses
:
list
[
Clause
])
->
list
[
Clause
]:
"""
Update the links in the clauses to the new structure.
Args:
clauses: The list of clauses.
Returns:
The list of clauses.
"""
# Build the link target dictionary
linkTargets
=
{}
for
clause
in
clauses
:
for
i
,
line
in
enumerate
(
clause
.
lines
):
if
(
lnk
:
=
_anchorLink
.
findall
(
line
)):
linkTargets
[
lnk
[
0
]]
=
clause
# # Check if the line contains a link
# if not (lnk := _markdownLink.search(line)) and not (lnk := _htmlLink.search(line)) and not (lnk := _anchorLink.search(line)):
# continue
# print(lnk)
# print(lnk.groups()[0])
# Update links in the markdown file
for
title
in
[
c
.
title
for
c
in
clauses
]:
if
title
in
line
:
clause
.
lines
[
i
]
=
line
.
replace
(
title
,
f
'
{
title
}
/
{
title
.
casefold
()
}
.md
'
)
# Create a dictionary with the titles and the corresponding clause
clauseDict
=
{}
for
clause
in
clauses
:
clauseDict
[
clause
.
title
]
=
clause
# Go through the clauses and update the links
for
clause
in
clauses
:
for
i
,
line
in
enumerate
(
clause
.
lines
):
# Update links in the markdown file
for
title
in
clauseDict
.
keys
():
if
title
in
line
:
clause
.
lines
[
i
]
=
line
.
replace
(
title
,
f
'
{
title
}
/
{
title
.
casefold
()
}
.md
'
)
return
clauses
def
copyMediaFiles
(
filename
:
str
,
navTitle
:
str
,
mediaDirectory
:
str
=
'
media
'
)
->
None
:
"""
Copy media files from the source directory to the target directory.
...
...
@@ -193,18 +247,20 @@ def copyMediaFiles(filename:str, navTitle:str, mediaDirectory:str = 'media') ->
targetDirectory
=
f
'
{
os
.
path
.
dirname
(
filename
)
}
/
{
navTitle
}
/
{
mediaDirectory
}
'
if
os
.
path
.
exists
(
sourceDirectory
):
print
(
f
'
[green]Copying media files from
"
{
sourceDirectory
}
"
to
"
{
targetDirectory
}
"'
)
shutil
.
copytree
(
sourceDirectory
,
targetDirectory
,
dirs_exist_ok
=
True
)
print
(
f
'
[green]Copied media files from
"
{
sourceDirectory
}
"
to
"
{
targetDirectory
}
"'
)
else
:
print
(
f
'
[red]Media directory
"
{
sourceDirectory
}
"
does not exist
'
)
def
processDocument
(
args
:
argparse
.
Namespace
)
->
None
:
clauses
=
analyseMarkdown
(
args
.
document
)
document
=
os
.
path
.
abspath
(
args
.
document
)
clauses
=
analyseMarkdown
(
document
)
clauses
=
splitMarkdownDocument
(
clauses
,
[
t
.
casefold
()
for
t
in
args
.
ignore_clause
],
args
.
split_level
)
# clauses = updateLinks(clauses)
clauses
=
prepareForMkdocs
(
clauses
)
writeClauses
(
clauses
,
args
.
document
,
args
.
title
)
copyMediaFiles
(
args
.
document
,
args
.
title
,
args
.
media_directory
)
writeClauses
(
clauses
,
document
,
args
.
title
)
copyMediaFiles
(
document
,
args
.
title
,
args
.
media_directory
)
if
__name__
==
'
__main__
'
:
...
...
@@ -213,7 +269,7 @@ if __name__ == '__main__':
parser
.
add_argument
(
'
--title
'
,
'
-t
'
,
metavar
=
'
title
'
,
required
=
True
,
help
=
'
mkdocs navigation tile
'
)
parser
.
add_argument
(
'
--ignore-clause
'
,
'
-i
'
,
metavar
=
'
clause
'
,
nargs
=
'
+
'
,
default
=
[
'
Contents
'
,
'
History
'
],
help
=
'
ignore headers in the markdown document
'
)
parser
.
add_argument
(
'
--split-level
'
,
'
-sl
'
,
metavar
=
'
level
'
,
type
=
int
,
default
=
2
,
help
=
'
split clauses on which level
'
)
parser
.
add_argument
(
'
--media-directory
'
,
'
-md
'
,
metavar
=
'
media-directory
'
,
default
=
'
media
'
,
help
=
'
directory where media files are stored
'
)
parser
.
add_argument
(
'
--media-directory
'
,
'
-md
'
,
metavar
=
'
media-directory
'
,
default
=
'
media
'
,
help
=
'
directory
name
where media files are stored
'
)
parser
.
add_argument
(
'
document
'
,
type
=
str
,
help
=
'
a oneM2M markdown specification document to process
'
)
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