-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
executable file
·164 lines (136 loc) · 4.79 KB
/
tool.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import argparse
import os
import re
import subprocess
from dataclasses import dataclass
from datetime import datetime
from typing import Dict, List, Tuple
from urllib.parse import quote
tags = ["blockchain", "tech", "web", "linux", "other", "algorithm", "golang", "python", "java", "android"]
image_regex = r"!\[(.*?)\]\((.*?)\)"
link_regex = r"\[(.*?)\]\((.*?)\)"
@dataclass
class Post:
title: str
date: str
tags: List[str]
draft: bool = False
contents: Dict[str, Post] = {}
def _run(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
return (
stdout.decode("utf-8").strip() if stdout else "",
stderr.decode("utf-8").strip() if stderr else "",
)
def push():
for file in os.listdir("posts"):
if not file.endswith(".md"):
raise Exception("Can't push, found file which is not md in posts directory")
_run(f"git add .")
_run(f"git commit -m 'Auto commit by script'")
_run(f"git push")
def collect_all():
for md in os.listdir("posts"):
if not md.endswith(".md"):
continue
date, *tail = md.split(" ")
date = datetime.strptime(date, "%Y-%m-%d")
title = " ".join(tail).replace(".md", "").strip()
with open(os.path.join("posts", md)) as f:
lines = f.read().splitlines()
tags, draft = [], False
for line in lines[:8]:
if line.startswith("- tags:"):
matches = re.findall(link_regex, lines[0])
tags = [matches[0][0]] if matches else lines[0].replace("tags:", "").strip().split(",")
elif line.startswith("- draft:"):
draft = line.replace("- draft:", "").strip().lower() == "true"
contents[md] = Post(title, date, tags, draft)
def make_post_li(post: Post):
link = f"/posts/{post.date.strftime('%Y-%m-%d')} {post.title}.md"
date = post.date.strftime("%b %d, %Y")
return f"- [{post.title}]({quote(link)}) *{date}*"
def index():
collect_all()
# Write tags.md
tag_contents, readme_tags = [], []
for tag in tags:
posts = []
for post, post in contents.items():
if tag in post.tags:
posts.append(post)
posts.sort(key=lambda x: x.date, reverse=True)
tag_contents.append(f"## {tag}")
tag_contents.append(f"{len(posts)} posts")
readme_tags.append(f"[{tag}({len(posts)})](/tags.md#{tag})")
for post in posts:
if post.draft:
continue
tag_contents.append(make_post_li(post))
tag_contents.append("\n")
with open("tags.md", "w") as f:
f.write("\n".join(tag_contents))
# Write README.md
posts = list(contents.values())
posts.sort(key=lambda x: x.date, reverse=True)
readme_contents = []
for post in posts:
if post.draft:
continue
readme_contents.append(make_post_li(post))
with open("README.md", "w") as f:
f.write(" ".join(readme_tags) + "\n\n")
f.write(f"### *Posts, since 2013*\n\n")
f.write(f"\n\n")
f.write("\n".join(readme_contents))
def create_post(title):
if not title:
title = input("Please input title: ")
if not title.strip():
raise Exception("Title is empty")
date = datetime.now().strftime("%Y-%m-%d")
md = os.path.join("posts", f"{date} {title}.md")
with open(md, "w") as f:
f.write(f"- tags: \n")
f.write(f"- date: {date}\n")
f.write(f"- draft: true\n\n")
f.write(f"# {title}\n\n")
def publish():
stdout, stderr = _run(f"git diff --name-only")
if stderr:
raise Exception(stderr)
changed_files = stdout.splitlines()
print(changed_files)
for file in changed_files:
if file.startswith("posts/"):
# Corret Image path
print(file)
index()
_run(f"git diff")
char = input("Confirm push (y/n): ")
if char == "y":
push()
def create_args_parser():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
# push = subparsers.add_parser("push", help="Auto commit and push")
# index = subparsers.add_parser("index", help="Generate README.md & tags.md")
new = subparsers.add_parser("new", help="Create a new post")
new.add_argument("-t", "--title", help="Post title", default=None)
publish = subparsers.add_parser("publish", help="Check & Publish all posts")
return parser
if __name__ == "__main__":
parser = create_args_parser()
args = parser.parse_args()
if args.command == "push":
push()
elif args.command == "index":
index()
elif args.command == "new":
create_post(args.title)
elif args.command == "publish":
publish()
else:
parser.print_help()