-
Notifications
You must be signed in to change notification settings - Fork 3
/
posts.py
65 lines (47 loc) · 1.63 KB
/
posts.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from os import listdir
from os.path import isfile, join
print("update posts list in readme")
postsDir = "content/posts"
onlyfiles = [f for f in listdir(postsDir) if isfile(join(postsDir, f))]
postTitleAndDate = {}
# 博客标题占位符
TITLE = "title: "
# 博客日期占位符
DATE = "date: "
# 最终生成的posts列表
res = os.linesep
total = 0
for f in onlyfiles:
tmp = postsDir + "/" + f
with open(tmp, "r", encoding="UTF-8") as f:
content = f.readlines()
title = ""
date = ""
entry = ""
for line in content:
if(line.startswith(TITLE)):
title = line[line.index(TITLE) + len(TITLE):]
title = title[1:len(title)-2]
entry = entry + "[" + title + "](" + tmp + ")"
if(line.startswith(DATE)):
date = line[line.index(DATE) + len(DATE):]
entry = entry + " " + date
break
total = total + 1
res = res + entry + os.linesep
# print(res)
res = "**截止目前输出了 " + str(total) + " 篇文章, 海到尽头天作岸,山登绝顶我为峰!**" + os.linesep + res
readmeFile = "README.md"
originContent=""
with open(readmeFile, "r", encoding="UTF-8") as f:
originContent = f.read()
# print(originContent)
left = originContent[:originContent.index("## 源文件")] + os.linesep
left = left + "## 源文件" + os.linesep
right = originContent[originContent.index("## 约定"):len(originContent)]
all = left + os.linesep + res + right
with open(readmeFile, "w", encoding="UTF-8") as f:
f.write(all)