This repository has been archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto.py
60 lines (48 loc) · 1.74 KB
/
auto.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
#!/usr/bin/env python3
# coding=utf-8
import os
import sys
import datetime
import shutil
def main():
for str in sys.argv[1:]:
process(str)
def process(path):
if os.name == 'nt':
SHELL = "powershell.exe"
PYTHON = "py -3"
else:
SHELL = "/bin/bash"
PYTHON = "python3.6"
with open(path + "/main.md", mode='r', encoding="utf8") as md_file:
title = md_file.readline().rstrip()
head = md_file.readline().rstrip()
content = md_file.read()
title = "_".join(title.title().split(" "))
img_path = "./assets/img/img.auto/{0}".format(title)
if not os.path.exists(img_path):
os.mkdir(img_path)
if os.name == 'nt':
os.system(SHELL + ' -c ' + "mv {0}/*.jpg {1}".format(path, img_path))
os.system(SHELL + ' -c ' + "mv {0}/*.png {1}".format(path, img_path))
else:
print("mv {2}/{0}/*.jpg {2}/{1}/".format(path, img_path, os.getcwd()))
os.system(SHELL + ' -c ' + "mv {2}/{0}/*.jpg {2}/{1}/".format(path, img_path, os.getcwd()))
os.system(SHELL + ' -c ' + "mv {2}/{0}/*.png {2}/{1}/".format(path, img_path, os.getcwd()))
os.system(PYTHON + " ./tinypng.py -r {0}".format(img_path))
imgs = [f for f in os.listdir(img_path)]
article = """---
layout: post
title: "{0}"
date: "{1}"
thumbnail: "{2}"
---
""".format(head, datetime.datetime.now().strftime("%Y/%m/%d"), "/img.auto/{0}/{1}".format(title, imgs[0]))
article += (content + "\n\n")
article += "\n".join(map(
lambda img: "![]({{site.baseurl}}"+"/{0}/{1})".format(img_path[2:], img), imgs[1:]))
with open("_posts/{0}-{1}.md".format(datetime.datetime.now().strftime("%Y-%m-%d"), title), "w", encoding="utf8") as md_out:
md_out.write(article)
shutil.rmtree(path)
if __name__ == "__main__":
main()