-
Notifications
You must be signed in to change notification settings - Fork 0
/
headfmt.py
230 lines (202 loc) · 7 KB
/
headfmt.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#encoding=utf8
import sys
import re, os, sys
sys.path.append("../")
from pythonx.funclib import *
import json
OPENFILE = "openfile" in sys.argv
def regularTitle(fpath):
title = getPostValue(fpath, "title")
categories = getPostValue(fpath, "categories")
print(title, categories)
categories = json.loads(categories)
category = categories[0]
regex = "^.*?{}.*? --( |《)[^《]".format(category)
if not re.findall(regex, title):
openTextFile(fpath)
assert False, regex
def getPostValue(fpath, xkey):
fdata = readfile(fpath, True, "utf8")
if len(fdata.split("---")) <= 1:
print(fpath)
fdata = fdata.split("---")[1]
for line in fdata.split("\n"):
line = line.strip()
if not line: continue
key, value = line.split(":", 1)
if key == xkey:
return value.strip()
return None
def mainxtitle():
print(parsePythonCmdx(__file__))
fnamelist = []
def mainfile(fpath, fname, ftype):
if ftype in ("py",): return
if ftype in ("mdtag",): return
regularTitle(fpath)
# print(fname[11:])
fnamelist.append("%-64s"%fname[11:] + "\t" + getPostValue(fpath, "title"))
searchdir("_posts", mainfile)
fnamelist.sort()
print("\r\n".join(fnamelist))
def formatValue(value):
if not value: return value
if value[0] != "[": return value
try:
json.loads(value)
return value
except:
print(value)
assert value[0] == "[" and value[-1] == "]", value
value = value[1:-1].split(",")
return "[{}]".format(", ".join(["\"{}\"".format(i.strip()) for i in value]))
def analyzehead(fpath, fname, ftype, newmap):
fpath = os.path.relpath(fpath, ".")
print(fpath)
if fpath.startswith("_posts\\"):
pass
elif fpath.startswith("invisible\\"):
pass
else:
assert fpath in ("about.md", "bookshelf.md", "index.md", "disclaimer.md"), fpath
if not newmap["categories"] or not newmap["tags"]:
openTextFile(fpath)
gkvmap = {}
def formatkv(fpath, fname, ftype, fsecli, setkv={}):
li = [line.strip() for line in fsecli.split("\n") if line.strip()]
_posts = "_posts" in fpath.split("\\")
mdkeylist = """
layout title author location
categories tags toc toclistyle
comments visibility
mathjax mermaid glslcanvas
codeprint
""".split()
if not _posts:
mdkeylist.extend("""
permalink
""".split())
igkeylist = """
l2dwidget cluster
sortrefs archived date
layoutclear
""".split()
kvmap = {}
for line in li:
if len(line.split(":", 1)) == 1:
print(fpath)
key, value = line.split(":", 1)
key, value = key.strip(), value.strip()
value = formatValue(value)
if key == "date":
assert re.findall("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \\+[0-9]{4}$", value), value
assert len(fpath.split("invisible")) == 2, fpath
magic = normalPath(fpath).split("invisible\\")[-1].split("\\")
# date: 2021-02-06 15:29:00 +0800
pWordSize = 3
def calcSignHalf(name):
intv = 0
count = 0
while name and count < pWordSize:
intv = (intv << 8) + ord(name[0])
name = name[1:]
count = count + 1
intv = intv >> (4 * pWordSize)
assert(intv <= 0xffffff, "%x" % intv)
return intv & 0xffffff
intsign = 0
while magic:
intsign = (intsign << int(pWordSize * 8 / 2)) + calcSignHalf(magic[0])
magic = magic[1:]
value = "{} +0800".format(formatTimeStamp(intsign))
print(magic, value)
if not key in igkeylist:
assert key in mdkeylist, line
else:
mdkeylist.append(key)
kvmap[key] = value
if not key in gkvmap.keys():
gkvmap[key] = []
def appvalue(value):
if value and not value in gkvmap[key]:
gkvmap[key].append(value)
if value:
if value.startswith("\"") and value.endswith("\""):
value = value[1:-1]
appvalue(value)
elif value.startswith("["):
for val in json.loads(value):
appvalue(val)
elif re.findall("^[a-z]+$", value):
appvalue(value)
elif key in ('title', 'permalink', 'date', 'author'):
appvalue(value)
else:
assert False, (key, value)
newli = []
newmap = {}
for key in mdkeylist:
value = kvmap[key] if key in kvmap else ""
if key in setkv.keys():
value = setkv[key]
line = key + ": " + value.strip()
newli.append(line.strip())
newmap[key] = value.strip()
analyzehead(fpath, fname, ftype, newmap)
return "\r\n".join(newli), newmap
def parseHeadKeyValueRaw(fpath, fname, ftype):
if ftype not in ("md",):
return
fdata = readfile(fpath, True, "utf8")
fsecli = fdata.split("---", 2)
if len(fsecli) <= 2 or len(fsecli[0]) > 3: return
return fsecli
def parseHeadKeyValue(fpath, fname, ftype):
fsecli = parseHeadKeyValueRaw(fpath, fname, ftype)
if not fsecli: return
return formatkv(fpath, fname, ftype, fsecli[1])[1]
gkvconfig = readfileJson("config/headnote.json", "utf8")
gkvconfig = gkvconfig if gkvconfig else {}
def mainxkeyfile(fpath, fname, ftype, depth=-1, setkv={}):
if ftype in ("mdtag",): return
fpath = os.path.relpath(fpath, ".")
fsecli = parseHeadKeyValueRaw(fpath, fname, ftype)
if not fsecli: return
if OPENFILE:
openTextFile(fpath)
mdtagfile = (fpath+".tag").replace(".md.tag", ".mdtag").replace("-", "")
#writefile(mdtagfile, "")
fsecli[1] = "\r\n{}\r\n".format(formatkv(fpath, fname, ftype, fsecli[1], setkv)[0],)
fsecli = "---".join(fsecli)
writefile(fpath, fsecli, "utf8")
if not fpath in gkvconfig.keys():
gkvconfig[fpath] = { "taged": False }
def mainxkey():
print("***" * 30)
searchdir(".", mainxkeyfile, ignorelist=("backup", "_site", "_drafts", "opengl-3rd"))
writefileJson("config/headnote.json", gkvconfig)
nctrl = {
"title": 2,
"date": 2,
}
headnote = {}
for key in gkvmap.keys():
gkvmap[key].sort()
ctrln = 1000
if key in nctrl.keys():
ctrln = nctrl[key]
key, value = key, gkvmap[key]
testset = set()
for tt in value:
testset.add(tt.lower())
assert len(testset) == len(value), (key, value)
key, value = key, gkvmap[key][:ctrln]
print(key, value, "..." if len(gkvmap[key]) > ctrln else "")
headnote[key] = value
# (path, data, encoding="ISO8859-1", ascii=True):
writefileJson("config/headnote.txt", headnote, "utf8", False)
if __name__ == "__main__":
mainxtitle()
mainxkey()
print(parsePythonCmdx(__file__))
#os.system(r"cd invisible & {} tempd.py encrypt".format(getPythonExe(),))