FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
git2cpp/docs/create_markdown.py at main · QuantStack/git2cpp · GitHub
QuantStack
git2cpp
Repository navigation
Code
Issues
6
(6)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
git2cpp
/
docs
/
create_markdown.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (74 loc) · 3.35 KB
Breadcrumbs
git2cpp
/
docs
/
create_markdown.py
Copy path
File metadata and controls
93 lines (74 loc) · 3.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import
os
import
re
import
subprocess
from
pathlib
import
Path
def
get_filename
(
args
):
directory
=
Path
(
"created"
).
joinpath
(
*
args
[:
-
1
])
filename
=
args
[
-
1
]
+
".md"
return
directory
/
filename
def
sanitise_line
(
line
):
# Remove trailing whitespace otherwise the markdown parser can insert an extra \n
line
=
line
.
rstrip
()
# Replace angular brackets with HTML equivalents.
line
=
line
.
replace
(
r"&"
,
r"&"
)
line
=
line
.
replace
(
r"<"
,
r"<"
)
line
=
line
.
replace
(
r">"
,
r">"
)
# Replace ansi style codes with <span> elements, only bold is displayed.
# Colour codes are converted to empty <span> elements to match the number of </span>.
line
=
line
.
replace
(
"
\x1b
[1m"
,
r"<span class=git2cpp-bold>"
)
line
=
line
.
replace
(
"
\x1b
[0m"
,
r"</span>"
)
line
=
re
.
sub
(
r"\x1b\[[^m]+m"
,
r"<span>"
,
line
)
# If there are whitespace characters at the start of the line, replace the first with an  
# so that it is not discarded by the markdown parser used by the parsed-literal directive.
line
=
re
.
sub
(
r"^\s"
,
r" "
,
line
)
return
line
# Process a single subcommand, adding new subcommands found to to_process.
def
process
(
args
,
to_process
):
cmd
=
args
+
[
"--help"
]
cmd_string
=
" "
.
join
(
cmd
)
filename
=
get_filename
(
args
)
filename
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
print
(
f"Writing '
{
cmd_string
}
' to file
{
filename
}
"
)
p
=
subprocess
.
run
(
cmd
,
capture_output
=
True
,
text
=
True
,
check
=
True
)
# Write output markdown file, identifying subcommands at the same time to provide
# links to the subcommand markdown files.
subcommands
=
[]
with
open
(
filename
,
"w"
)
as
f
:
f
.
write
(
f"(
{
filename
}
)=
\n
"
)
# Target for links.
f
.
write
(
f"#
{
' '
.
join
(
args
)
}
\n
"
)
f
.
write
(
"
\n
"
)
f
.
write
(
"```{parsed-literal}
\n
"
)
in_subcommand_section
=
False
for
line
in
p
.
stdout
.
splitlines
():
if
in_subcommand_section
:
match
=
re
.
match
(
r"^( )([\w\-_]+)(\s+.*)$"
,
line
)
if
match
:
subcommand
=
match
.
group
(
2
)
subcommand_filename
=
get_filename
(
args
+
[
subcommand
])
line
=
(
match
.
group
(
1
)
+
f"[
{
subcommand
}
](
{
subcommand_filename
}
)"
+
match
.
group
(
3
)
)
subcommands
.
append
(
subcommand
)
elif
line
.
startswith
(
"SUBCOMMANDS:"
):
in_subcommand_section
=
True
f
.
write
(
sanitise_line
(
line
)
+
"
\n
"
)
f
.
write
(
"```
\n
"
)
subcommands
.
sort
()
to_process
.
extend
(
args
+
[
subcommand
]
for
subcommand
in
subcommands
)
if
len
(
subcommands
)
>
0
:
# Hidden table of contents for subcommands of this command/subcommand.
f
.
write
(
"
\n
"
)
f
.
write
(
"```{toctree}
\n
"
)
f
.
write
(
":hidden:
\n
"
)
for
subcommand
in
subcommands
:
f
.
write
(
f"
{
args
[
-
1
]
}
/
{
subcommand
}
\n
"
)
f
.
write
(
"```
\n
"
)
if
__name__
==
"__main__"
:
# Modify the PATH so that git2cpp is found by name, as using a full path will cause the help
# pages to write that full path.
git2cpp_dir
=
Path
(
__file__
).
parent
.
parent
/
"build"
os
.
environ
[
"PATH"
]
=
f"
{
git2cpp_dir
}
{
os
.
pathsep
}
{
os
.
environ
[
'PATH'
]
}
"
to_process
=
[[
"git2cpp"
]]
while
len
(
to_process
)
>
0
:
subcommand
=
to_process
.
pop
(
0
)
process
(
subcommand
,
to_process
)
Back
|
FazBrowse Home
|
New Git URL