-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
148 lines (117 loc) · 3.76 KB
/
generator.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
from abc import ABC, abstractmethod
import argparse
import subprocess
import os
from datetime import datetime
class GenFilePipelineFactory(ABC):
FILE_PATH = None
@abstractmethod
def get_filename(self):
return "unknown"
@abstractmethod
def get_content(self):
return "unknown"
@classmethod
def create_file(cls, filename: str, content: str = "", overwrite: bool = False):
if not os.path.exists(filename) or overwrite:
with open(filename, "w") as f:
f.write(content)
else:
raise Exception(f"{filename} existed, "
f"enable --overwrite flag to overwrite existed file")
def post_run(self):
pass
def run(self):
filename = self.get_filename()
content = self.get_content()
if not self.FILE_PATH:
raise NotImplemented("undefined FILE_PATH")
GenFilePipelineFactory.create_file(
"/".join((self.FILE_PATH, filename, )),
content,
getattr(self, "overwrite", False)
)
class GenPostPipeline(GenFilePipelineFactory):
FILE_PATH = "_posts"
def __init__(self, args: object) -> None:
self.title = args.title
self.published_date = datetime.now().strftime("%Y-%m-%d")
self.tags = args.tags
self.category = args.category
self.author = subprocess.run(
['git', 'config', 'user.name'], capture_output=True, text=True).stdout.strip('\n')
self.overwrite = args.overwrite
def get_filename(self):
return f"{self.published_date}-{self.title}.md".replace(" ", "-").lower()
def get_content(self):
return f"""
---
layout: post
title: "{self.title}"
date: {self.published_date}
categories: {self.category}
author: {self.author}
tags: {" ".join(self.tags)}
---
""".strip()
class GenTagPipeline(GenFilePipelineFactory):
FILE_PATH = "tag"
def __init__(self, args: object) -> None:
self.name = args.name
self.overwrite = args.overwrite
def get_filename(self):
return f"{self.name}.md".replace(" ", "-")
def get_content(self):
return f"""
---
layout: tagpage
title: "Tag: {self.name}"
tag: {self.name}
robots: noindex
---
""".strip()
def gen_new_post(args):
pipeline = GenPostPipeline(args)
pipeline.run()
def gen_new_tag(args):
pipeline = GenTagPipeline(args)
pipeline.run()
def setup_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.required = True
# setup new post command parser
new_post_cmd_parser = subparsers.add_parser(
"post", description="to generate new post with defined template",
)
new_post_cmd_parser.set_defaults(func=gen_new_post)
new_post_cmd_parser.add_argument(
"--title", help="post title", required=True
)
new_post_cmd_parser.add_argument(
"--tags", help="post tags", nargs="+", default=tuple()
)
new_post_cmd_parser.add_argument(
"--category", help="post category", default="unknown"
)
new_post_cmd_parser.add_argument(
"--overwrite", help="replace existed post", action=argparse.BooleanOptionalAction
)
# setup new tag command parser
new_tag_cmd_parser = subparsers.add_parser(
"tag", description="to generate new tag with defined template",
)
new_tag_cmd_parser.set_defaults(func=gen_new_tag)
new_tag_cmd_parser.add_argument(
"--name", help="tag name", required=True
)
new_tag_cmd_parser.add_argument(
"--overwrite", help="replace existed tag", action=argparse.BooleanOptionalAction
)
return parser.parse_args()
def main():
# run with args
args = setup_args()
args.func(args)
if __name__ == "__main__":
main()