-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.py
99 lines (72 loc) · 2.43 KB
/
publish.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
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
94
95
96
97
98
99
import shutil
import os
from pathlib import Path
import subprocess
import datetime
def format_kdb(tmp, _posts):
ref = set(str(tmp).split("/"))
ref.add("kdb")
dest = _posts
if dest.exists():
shutil.rmtree(str(dest.resolve()))
mds = list(tmp.glob("**/*.md"))
dest.mkdir()
for md in mds:
name = "-".join(d for d in str(md).split("/") if d not in ref)
is_main_readme = "-" not in name
categories = " - " + name.split("-")[0]
_set = name.split("-")[0]
date = datetime.datetime.fromtimestamp(int(os.path.getmtime(str(md))))
new_name = "{}-{}-{}-{}".format(date.year, date.month, "01", name)
new_file = dest / new_name
with md.open("r") as f:
lines = f.readlines()
# fields = "title, categories, description, set"
title = "error"
for i, l in enumerate(lines):
if l.startswith("# "):
title = '"{}"'.format(
l.split("# ")[1]
.strip()
.encode("unicode-escape")
.decode("utf-8")
.replace('"', "'")
)
# lines[i] = ""
if not is_main_readme:
break
if is_main_readme and "<img" in l:
lines[i] = ""
break
if is_main_readme:
frontmatter = "---\nmain: true\n---"
else:
frontmatter = "---\ntitle: {}\ncategories:\n{}\nset: {}\n---\n\n".format(
title, categories, _set
)
new_lines = [frontmatter] + lines
with new_file.open("w") as f:
f.writelines(new_lines)
if __name__ == "__main__":
p = Path()
repo = "git@github.com:cc-ai/kdb.git"
assert all(
f in [d.name for d in p.iterdir() if d.is_dir()] for f in {"_site", "_posts"}
)
branches = subprocess.check_output(["git", "branch"]).strip().decode("utf-8")
current = [b for b in branches.split("\n") if "*" in b][0]
assert "source" in current
tmp = p / "tmp"
if tmp.exists():
shutil.rmtree(tmp)
tmp.mkdir()
_posts = p / "_posts"
print("Fetching files...", end="")
os.system("cd {} && git clone {}".format(str(tmp), repo))
print("ok.")
shutil.rmtree(_posts)
_posts.mkdir()
format_kdb(tmp, _posts)
shutil.rmtree(tmp)
os.system("rake publish")
print("\nDone updating.")