-
Notifications
You must be signed in to change notification settings - Fork 6
/
raw_github_to_blog.py
55 lines (38 loc) · 1.28 KB
/
raw_github_to_blog.py
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
# input file
# write file in place
import sys
import os
import re
print("This file is now junk")
if len(sys.argv) != 2:
print(f"Syntax: <filename> {sys.argv}")
exit(-3)
fileName = sys.argv[1]
if not os.path.exists(fileName):
print(f"File {fileName} does not exist")
exit(-3)
lines = []
with open(fileName) as f:
lines = f.readlines()
print(f"Processing {fileName}")
headerTitleMark = "# "
title = [l for l in lines if l.startswith(headerTitleMark)][0]
# Remove Title Mark
title = re.sub(headerTitleMark, "", title)
print(title)
replaceTitleMark = "REPLACETITLE"
def replaceTitle(s):
if s.startswith(replaceTitleMark):
print(f"REPLACED {fileName} to {title}")
s = f"title: {title}"
return s
lines = [replaceTitle(l) for l in lines]
with open(fileName, "w") as f:
f.writelines(lines)
print(f"Done processing {fileName}")
# For each markdown file, prepend the corret header
# fd -e md -exec sed -i '1s;^;---\nlayout: post\nno-render-title: true\n---\n\n_[Copied from my GitHub techdiary](https://github.com/idvorkin/techdiary/blob/master/{})_\n\n;' {}
# Remove the trailing md
# fd -e md -exec sed -i 's;](\(notes/.*\)\.md;](\1;' {}
# Really Hacky (Ha), index is copied from readme, so put that back.
# sed -i 's;blob/master/index.md;;' index.md