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
|
import os
import sys
import pprint
MAP = {}
MAP_DETAIL = {}
# 将 docx 文件转为 md 文件
def IterateDir(dir):
paths = []
for root, dirs, files in os.walk(dir):
for file in files:
if file.endswith('.doc') or file.endswith('.docx'):
# yield os.path.join(root, file)
paths.append(os.path.join(root, file))
exec("pandoc -s {} -t markdown -o {}".format(os.path.join(root, file), os.path.join(root, file.replace('.doc', '.md'))))
print(paths)
# 处理盘古之白
def panguMd(dir):
for root, dirs, files in os.walk(dir):
for file in files:
try:
# https://github.com/n0vad3v/Tekorrect 盘古之白
os.system("tekorrect -f {}".format(os.path.join(root, file)))
except UnicodeDecodeError:
print("UnicodeDecodeError")
# 增加 md 的 header
def IterateDirHeader(dir):
map = {}
for root, dirs, files in os.walk(dir):
for file in files:
if file.endswith('.md'):
with open(os.path.join(root, file), 'r') as f:
lines = f.readlines()
i = -1
last_line = lines[-1]
while len(last_line) < 6:
i = i - 1
last_line = lines[i]
# 准备时间
time_info = dealMdTime(last_line).strip()
# 准备题目
title_info = file[:-3]
# 深加工 title , 把 数字和文字分隔开
num, i = 0, 3
while num == 0:
try:
num = int(title_info[0:i]) + 1
except ValueError:
i = i - 1
title_info = title_info[0:i] + " " + title_info[i:]
# 准备 category
if file[:3] in MAP:
category_info = MAP[file[:3]][1:]
else:
category_info = "未分类"
# 整理所有 header
info = """---
title: {}
date: {}
tags: ["{}"]
categories: ["{}"]
author: 诸葛
organize: 二花
---
""".format(title_info, time_info, category_info, category_info)
writeHeader2File(os.path.join(root, file), file, info)
# 读一个文件,写一个文件
def writeHeader2File(dir, file, data):
with open(dir, "r") as rf, open("/Users/lifupeng/Downloads/zhuge_md/" + file, "w") as wf:
wf.write(data + '\n\n')
wf.write("<!--more-->")
wf.write('\n')
for line in rf:
wf.write(line)
# 处理不规则的时间
def dealMdTime(data):
# 2022.04.09 2022年4月9号
d1 = data.replace("年", ".")
d2 = d1.replace("月", ".")
d3 = d2.replace("日", "")
d4 = d3.replace("\n", "")
d = d4.split(".")
if len(d) != 3:
return "0000-00-00"
# 补充两位
if len(d[1]) != 2:
d[1] = "0" + d[1]
if len(d[2]) != 2:
d[2] = "0" + d[2]
# print("-".join(d))
return "-".join(d)
# 获取每篇文章的 category
def getCategoryMap(dir):
pathDir = []
for root, dirs, files in os.walk(dir):
for dir in dirs:
pathDir.append(dir)
for file in files:
if file.endswith('.doc') or file.endswith('.docx'):
print(os.path.join(root, file))
for dir in pathDir:
if dir in os.path.join(root, file):
MAP[file[:3]] = dir
if "docx" in file:
MAP_DETAIL[file[:-5]] = dir
if "doc" in file:
MAP_DETAIL[file[-4]] = dir
# pprint.pprint(MAP)
# 写入本地文件
# os.system("echo '{}' >> category.txt".format(MAP))
if __name__ == '__main__':
args = sys.argv
print(args)
if len(args) > 1:
if args[1] == "header":
getCategoryMap('/Users/lifupeng/Downloads/luzhu/category')
IterateDirHeader('/Users/lifupeng/Downloads/luzhu_time')
panguMd('/Users/lifupeng/Downloads/zhuge_md')
if args[1] == "category":
getCategoryMap('/Users/lifupeng/Downloads/luzhu/category')
else:
IterateDir('/Users/lifupeng/Downloads/鲁柱整理/时间为轴')
|