Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
R3
howto-cards
Commits
9f7bc620
Verified
Commit
9f7bc620
authored
Feb 17, 2020
by
Laurent Heirendt
✈
Browse files
update script to remove headers
parent
5449d95f
Changes
1
Hide whitespace changes
Inline
Side-by-side
generateIndex.py
0 → 100644
View file @
9f7bc620
import
os
,
re
def
line_prepender
(
filename
,
line
):
with
open
(
filename
,
'r+'
)
as
f
:
content
=
f
.
read
()
f
.
seek
(
0
,
0
)
f
.
write
(
line
.
rstrip
(
'
\r\n
'
)
+
'
\n
'
+
content
)
def
remove_header
(
localroot
,
root
,
filename
,
n
=
5
):
nfirstlines
=
[]
#cwd = os.getcwd()
os
.
chdir
(
localroot
)
#print("Local root folder:" + os.getcwd())
os
.
chdir
(
root
)
#print("Local folder:" + os.getcwd())
# count the number of lines
count
=
0
headerCheck
=
False
with
open
(
filename
,
'r'
)
as
f
:
for
line
in
f
:
count
+=
1
# check if the header is actually a header
if
(
count
==
0
or
count
==
n
)
and
line
[
0
:
3
]
==
"---"
:
print
(
"existing header detected: "
+
line
[
0
:
2
]
)
headerCheck
=
True
# remove the header
if
count
>
n
and
headerCheck
:
with
open
(
filename
)
as
f
,
open
(
"tmp"
+
filename
,
"w"
)
as
out
:
for
_
in
range
(
n
):
nfirstlines
.
append
(
next
(
f
))
for
line
in
f
:
out
.
write
(
line
)
os
.
remove
(
filename
)
os
.
rename
(
"tmp"
+
filename
,
filename
)
print
(
"Header removed: "
+
filename
)
# change back to the local root
os
.
chdir
(
localroot
)
#print("exit folder:" + os.getcwd())
# loop through the entire internal tree
folder
=
"external"
localroot
=
os
.
getcwd
()
for
root
,
dirs
,
files
in
os
.
walk
(
folder
):
for
file
in
files
:
if
file
.
endswith
(
".md"
):
fileName
=
os
.
path
.
join
(
root
,
file
)
#ignore subsections
if
file
[
0
]
!=
"_"
:
#print(localroot)
print
(
"root: "
+
root
+
" file: "
+
file
)
# remove the previous header
remove_header
(
localroot
,
root
,
file
,
8
)
# generate a permalink
permalink
=
"/"
+
root
+
"/"
# generate the shortcut
shortcut
=
re
.
sub
(
folder
,
''
,
root
)
# remove the first /
shortcut
=
shortcut
[
1
:]
shortcut
=
re
.
sub
(
'/'
,
':'
,
shortcut
)
header
=
"---
\n
"
header
+=
"layout: page
\n
"
header
+=
"permalink: "
+
permalink
+
"
\n
"
header
+=
"shortcut: "
+
shortcut
+
"
\n
"
header
+=
"redirect_from:
\n
"
header
+=
" - /cards/"
+
shortcut
+
"
\n
"
header
+=
" - /internal/cards/"
+
shortcut
+
"
\n
"
header
+=
"---"
#print(header)
line_prepender
(
fileName
,
header
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment