forked from flowerinthenight/flowerinthenight.github.io
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmdrstrip.py
1292 lines (1118 loc) · 48.3 KB
/
mdrstrip.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#encoding=utf8
import re, os, sys
sys.path.append("../")
import datetime, time
import traceback
from coderstrip import *
from pythonx.funclib import *
from pythonx.kangxi import TranslateKangXi
from pythonx.pelib import getLuckFileMd5, get_local_ipinfo
__file__ = os.path.abspath(__file__)
if __name__ == "__main__":
MYCACHE = LocalLimitedDict(os.path.join("tempdir", "mycache", os.path.split(__file__)[-1], 'cache.db'), max_size=5000)
else:
MYCACHE = None
from PIL import Image
# AttributeError: module 'PIL.Image' has no attribute 'Resampling'
PIL_IMAGE_SAMPLE = Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.ANTIALIAS
# Paranoid text spacing in Python
# https://github.com/vinta/pangu.py
OPENFILE = "openfile" in sys.argv
AUTOFORMAT = "format" in sys.argv
REBUILD = "rebuild" in sys.argv
COPYRES = "copyres" in sys.argv
CLEARIMG = "clearimg" in sys.argv
IGNOREERR = "ignoreerr" in sys.argv
OPENRESENT = "openresent" in sys.argv or "openresentx" in sys.argv
OPENRESENTx = "openresentx" in sys.argv
NETFAKE = "netfake" in sys.argv
DEBUG = "debug" in sys.argv
# 名称,域名正则。
LINKTAGARRAY = (("bili", "bilibili.com"),
("zhihu", "zhihu.com"),
("cnblogs", "cnblogs.com"),
("csdn", "csdn.net"),
("github", "github.com|github.io"),
("jianshu", "jianshu.com"),
("wiki", "wikipedia.org"),
("weixin", "weixin.qq.com"),
("keqq", "ke.qq.com"),
("scriptol", "scriptol.com"),
("khronos", "khronos.org"),
("gluon", "gluon.ai"),
)
SPACEBACKFILE_TAIL = ".spaceback.json"
NEWLINE_CHAR = "\r\n" if IS_WINDOWS else "\n"
PAUSE_CMD = "pause" if IS_WINDOWS else "read -p 'Press [Enter] key to continue...'"
def getLinkTagSrc(name):
return "{% include relref_"+name+".html %}"
def isHostIgnoreStat(hostk):
for name, host in LINKTAGARRAY:
if refindall("^({})$".format(host), hostk):
return True
if refindall("\\.({})$".format(host), hostk):
return True
for host in ("sunocean.life", "hawkhai.com",):
if refindall("^({})$".format(host), hostk):
return True
if refindall("\\.({})$".format(host), hostk):
return True
return False
G_CACHE_IGLIST = {}
def readfileIglist(fpath):
hash = "{},{},{}".format(getMd5(fpath), os.path.getsize(fpath), os.path.getmtime(fpath))
if hash in G_CACHE_IGLIST:
return G_CACHE_IGLIST[hash]
li = readfile(fpath, True, "utf8").split("\n")
li = [i.strip().split(" #")[0].strip() for i in li if i.strip().split(" #")[0].strip()]
li = [i.strip().split("# ")[0].strip() for i in li if i.strip().split("# ")[0].strip()]
if not IGNOREERR:
assert li, fpath
G_CACHE_IGLIST[hash] = li
return li
# copyfrom E:\kSource\blog\checkcache.py
G_CHECKPAGE = []
def checkpage(fdata):
if not G_CHECKPAGE:
itag0 = bytesToString("ERR_CONNECTION_TIMED_OUT".encode("utf8"))
itag1 = bytesToString("无法访问此网站".encode("utf8"))
itag2 = bytesToString('<div class="Qrcode-title">扫码登录</div>'.encode("utf8")) # 知乎的问题
itag3 = bytesToString('未注册手机验证后自动登录,注册即代表同意'.encode("utf8")) # 知乎的问题
itag4 = bytesToString("其他登录方式".encode("utf8"))
itag5 = bytesToString("其他方式登录".encode("utf8"))
itag6 = bytesToString('name="passport_iframe" src="https://passport.csdn.net/account/login?'.encode("utf8"))
itag7 = bytesToString('www.zhihu.com/api/v3/account/api/login/qrcode/'.encode("utf8"))
# 垃圾广告。动力广告
itag8 = bytesToString('pp.chuzushijian.cn'.encode("utf8"))
itag9 = bytesToString('动力广告'.encode("utf8"))
itag10 = bytesToString('<title>404 Not Found</title>'.encode("utf8"))
itag11 = bytesToString('<title>隐私设置错误</title>'.encode("utf8"))
itag12 = bytesToString('网站在更新其安全设置期间可能会经常显示警告。此问题应该很快就会得到改进。'.encode("utf8"))
itag13 = bytesToString('<h1>您的连接不是私密连接</h1>'.encode("utf8"))
itag14 = bytesToString('出现此问题的原因可能是配置有误或您的连接被拦截了。'.encode("utf8"))
G_CHECKPAGE.extend((itag0, itag1, itag2, itag3, itag4, itag5, itag6, itag7, itag8, itag9,
itag10, itag11, itag12, itag13, itag14))
for itag in G_CHECKPAGE:
if fdata.find(itag) != -1:
return itag
return None
def backupUrlContent(fpath, md5src, url):
for urlz in readfileIglist("config/mdrstrip_url_ignore_ends.txt"):
if url.endswith(urlz):
return
if not IGNOREERR:
assert not url.endswith(".exe"), url
assert not url.endswith(".zip"), url
# 有可能挂掉的网站,都稍微做一下备份。
fname = os.path.split(fpath)[-1]
urligstarts = readfileIglist("config/mdrstrip_url_ignore_starts.txt")
urligstarts.extend(readfileIglist("invisible/config/mdrstrip_url_ignore_starts.txt"))
for urlz in urligstarts:
if url.startswith(urlz):
return
if "[{}]{}".format(fname, url).startswith(urlz):
return
print(fpath, url)
chrome = True # 可能有 js 代码,所以必须都用 Chrome 进行缓存
chromeDialog = False
for urlz in readfileIglist("config/mdrstrip_url_chrome_dialog.txt"):
if url.startswith(urlz):
chromeDialog = True
mdname = os.path.split(fpath)[-1]
urlhostsrc = calcHost(url)
urlhostdir = urlhostsrc.replace(":", "/")
urlmd5 = getMd5(url)[:8]
invdir = isInvisibleDir(fpath)
if mdname in ("wechatdl.md",):
return
ttype = ".html"
ttype = calcType(ttype, url.split(urlhostsrc)[1])
if ttype.endswith(".md"): # 不能是这个,否则会被 Jekyll 自动格式化。
ttype = ".html"
if ttype in (".action",):
ttype = ".html"
if ttype.endswith(".pdf"): # pdf 下载
chrome = False
def buildlocal(ftype):
flocal = os.path.join("backup", mdname, urlhostdir, urlmd5 + ftype)
if invdir:
flocal = os.path.join("invisible", flocal)
return flocal
mdxfile = False
flocal = buildlocal(ttype)
if chrome and urlhostsrc in readfileIglist("config/mdrstrip_host_jekyll.txt"):
mdxfile = True
ttype = ".md" # 借用 Jekyll 格式化
newlocal = buildlocal(ttype)
if os.path.exists(flocal):
os.rename(flocal, newlocal)
flocal = newlocal
ttype = ".html" # 太多了,严重影响速度,改回 html。
newlocal = buildlocal(ttype)
if os.path.exists(flocal):
os.rename(flocal, newlocal)
flocal = newlocal
shotpath = flocal + SELENIUM
fdata = querySnapCache(urlmd5)
if fdata:
writefile(flocal, fdata)
fdatalocal = True
else:
if NETFAKE:
fdata = b""
else:
fdata = netgetCacheLocal(url, cacheTimeout=60*60*24*1000, chrome=chrome, local=flocal, shotpath=shotpath, chromeDialog=chromeDialog)
fdatalocal = False
idata = bytesToString(fdata)
if not url in readfileIglist("config/mdrstrip_url_ignore.txt"):
result = checkpage(idata)
if result:
print("无法访问此网站", fpath, url, result)
if not fdatalocal: os.system(PAUSE_CMD)
removeSnapCache(urlmd5)
osremove(flocal)
osremove(shotpath)
return backupUrlContent(fpath, md5src, url)
def addmdhead(fdata):
xtime = formatTimeStamp(time.time())
xurl = url
fdata = """---
title : %(title)s
---
* TIME: %(time)s
* URL: <%(url)s>
-----
""" % { "time": xtime, "url": xurl, "title": "自动快照存档", } + fdata
return fdata
def ismdhead(fdata):
return fdata and fdata.startswith("---")
def html2md(fdata):
import html2text
h = html2text.HTML2Text()
h.ignore_links = False
fdata = h.handle(fdata)
return fdata.replace("{{", "{ {").replace("}}", "} }")
# html 过大,自动切换到 mdfile
if len(fdata) >= 1024*1000*1 and fdata.lower().find(b"<body") != -1 and fdata.lower().find(b"<html") != -1:
mdxfile = True
if mdxfile:
fdata = bytesToString(fdata, "utf8")
if fdata.lower().find("<body") != -1 and fdata.lower().find("<html") != -1:
fdata = html2md(fdata)
fdata = addmdhead(fdata)
if not NETFAKE:
writefile(flocal, fdata, "utf8")
elif not ismdhead(fdata):
fdata = addmdhead(fdata)
if not NETFAKE:
writefile(flocal, fdata, "utf8")
if urlhostsrc == "www.shadertoy.com":
li = refindall(r"""\r?\n\r?\n[0-9]+\r?\n\r?\n \r?\n \r?\n """, fdata)
for i in li: fdata = fdata.replace(i, NEWLINE_CHAR+" ")
if not NETFAKE:
writefile(flocal, fdata, "utf8")
fmd5 = getFileSrcMd5z(flocal, mycache=MYCACHE) # 大文件,错误已经铸成,改不了了。
invdir2 = isInvisibleDir(flocal) # invdir = isInvisibleDir(fpath)
mdrstripBigfile = os.path.join("invisible" if invdir2 else ".", "config/mdrstrip_bigfiles.txt")
igbigfiles = readfileIglist(mdrstripBigfile)
if not fmd5 in igbigfiles and not flocal in igbigfiles:
if len(fdata) >= 1024*1000*1 and not IGNOREERR:
print(getFileSrcMd5z(flocal, mycache=MYCACHE), "#", flocal, "#", "%.1f MB"%(len(fdata) / 1024 / 1024))
assert False, (len(fdata) / 1024.0 / 1000.0, url, flocal)
remote = buildlocal(".html" if mdxfile else ttype).replace("\\", "/")
touchSnapCache(urlmd5, flocal)
# protocol :// hostname[:port] / path / [:parameters][?query]#fragment
remotename = url.split("?")[0].split("#")[0].split("/")[-1]
ignorenamefile = "config/mdrstrip_url_ignore_nametype.txt"
if remotename in readfileIglist(ignorenamefile):
return remote
# 外链类型 断言...
if not remote.split(".")[-1] in ("pdf", "html", "git", "php", "c", "phtml", "cpp", "cxx", "htm", "shtm", "xml",
"ipynb", "py", "asp", "shtml", "aspx", "xhtml", "txt", "mspx", "sh",):
print(fpath, url)
openTextFile(ignorenamefile)
assert False, remote
return remote
G_IMG_TAGED = set() # 图片资源等。
def tidyupImgClear():
for key in G_IMG_TAGED:
osremove(key)
def tidyupImgCollect(rootdir):
def mainfile(fpath, fname, ftype):
if fname.endswith(THUMBNAIL):
if not os.path.exists(fpath[:-len(THUMBNAIL)]):
osremove(fpath)
else:
G_IMG_TAGED.add(os.path.relpath(fpath, ".").lower())
searchdir(rootdir, mainfile) # tidyupImgCollect
# 本地图片缓存路径。
def tidyupImg(imglocal, fpath, line, imgthumb=True):
fakeimgfiles = readfileIglist("config/mdrstrip_fake_image_files.txt")
if imglocal in fakeimgfiles:
return line
imgdir, imgfname = os.path.split(imglocal)
imgnocopy = os.path.join(imgdir, "imgnocopy.txt")
if imgfname.find(".") == -1:
imgfname = imgfname + ".jpg"
imgtype = imgfname.split(".")[-1].lower()
if imgtype == "mp4":
ffmpegConvert(imglocal)
if not COPYRES:
assert os.path.exists(imglocal), fpath +" "+ imglocal
return line
invdir = isInvisibleDir(fpath)
fname = os.path.split(fpath)[-1]
if fname.lower().endswith(".md"):
fname = fname[:-3]
if refindall("^[0-9]{4}-[0-9]{2}-[0-9]{2}-", fname):
fname = fname[:10].replace("-", "")[-6:]+"-"+fname[11:]
if len(fname) > 32:
fname = fname[:30]+"~"+getMd5(fname)[:2]
tpath = os.path.join("assets", "images", fname.lower(), imgfname) #.lower() 不能转小写,因为服务器是大小写敏感的。
if invdir:
tpath = os.path.join("invisible", "images", fname.lower(), imgfname) #.lower() 不能转小写,因为服务器是大小写敏感的。
if os.path.exists(imgnocopy):
tpath = imglocal
if not os.path.exists(imglocal) and os.path.exists(tpath): # 貌似已经剪切过去了。
copyfile(tpath, imglocal)
count = 0
while not os.path.exists(imglocal):
if os.path.exists(imgnocopy):
return line
print("文件不存在", imglocal)
if not imgthumb: # 特殊图片,不用检测。
return line
count = count + 1
if count >= 3:
assert False, "重试次数过多..."
os.system(PAUSE_CMD)
fakeimgfiles = readfileIglist("config/mdrstrip_fake_image_files.txt")
if imglocal in fakeimgfiles:
return line
iscopy = copyfile(imglocal, tpath) # 是否图片挪窝了。
imglocalnail = imglocal + THUMBNAIL
tpathnail = tpath + THUMBNAIL
isnailcopy = False
if os.path.exists(imglocalnail):
isnailcopy = copyfile(imglocalnail, tpathnail) # 是否缩略图挪窝了。
if os.path.abspath(imglocal) != os.path.abspath(tpath):
G_IMG_TAGED.add(os.path.relpath(imglocal, ".").lower())
if os.path.relpath(tpath, ".").lower() in G_IMG_TAGED:
G_IMG_TAGED.remove(os.path.relpath(tpath, ".").lower())
# 同样大小的小图片先占位... lazyload
thumbPath = tpath + THUMBNAIL
from PIL import Image
# 1. 明确指出不需要缩略图的情况。
if not imgthumb:
osremove(thumbPath)
# 2. 创建缩略图。
elif not os.path.exists(thumbPath) and imgtype in ("png", "jpg", "gif", "jpeg", "webp", "bmp", "jfif",):
try:
img = Image.open(tpath)
except RuntimeError as ex: # could not create decoder object
print("Image.open RuntimeError", tpath)
raise ex
width, height = img.size
widthctrl = 64
if width > widthctrl:
try:
# DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.
imgtmp = img.resize((widthctrl, round(widthctrl*height/width)), PIL_IMAGE_SAMPLE)
img = imgtmp.convert("RGB")
except OSError as ex: # broken data stream when reading image file
print("Image.resize OSError", tpath)
raise ex
# DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.
img = img.resize((width, height), PIL_IMAGE_SAMPLE) # 恢复到原来大小,便于客户端排版。
from PIL import ImageFont, ImageDraw # 导入模块
draw = ImageDraw.Draw(img, "RGBA") # 修改图片
font = ImageFont.truetype(r"assets/logos/方正楷体_GB2312.ttf", size = 20)
draw.rectangle(((0, 0), (width, 40)), fill=(0,0,0,127))
draw.text((10, 10), u'图片加载中, 请稍候....', fill="#ffffff", font=font)
#img.show()
#exit(0)
# 小于 100K...
img = img.convert("RGB") #.convert("L")
img.save(thumbPath)
appendfile(thumbPath, getFileMd5(tpath, mycache=MYCACHE))
# 3. 无法创建缩略图(矢量图)。
elif not os.path.exists(thumbPath) and imgtype in ("svg",):
osremove(thumbPath)
# 4. 检查缩略图。
elif os.path.exists(thumbPath):
srcmd5 = readfileLast(thumbPath, True, size=32)
if getLuckFileMd5(tpath, srcmd5) != srcmd5: # 原图变化了。
osremove(thumbPath)
return tidyupImg(imglocal, fpath, line, imgthumb=imgthumb)
#img = Image.open(tpath)
#width, height = img.size
#try:
# img = Image.open(thumbPath)
#except RuntimeError as ex: # could not create decoder object
# print("Image.open RuntimeError", thumbPath)
# osremove(thumbPath)
# return tidyupImg(imglocal, fpath, line) # 存在问题,重新创建。
#if img.size != (width, height): # 尺寸不对,重新创建。
# img.close()
# osremove(thumbPath)
# return tidyupImg(imglocal, fpath, line)
imgtype = imgfname.split(".")[-1].lower()
if not imgtype in ("pdf", "png", "jpg", "gif", "jpeg", "webp", "mp4", "zip", "bmp", "svg", "jfif", "webm", "avif",):
print(imglocal, fpath, line)
assert False, imglocal
if iscopy: osremove(imglocal)
if isnailcopy: osremove(imglocalnail)
return line.replace(imglocal, tpath.replace("\\", "/"))
G_HOSTSET = {}
def collectHost(fpath, md5src, line, imgthumb):
reflist = []
linesrc = line[:]
regex = "(?:\"/(.*?)\")|(?:'/(.*?)')"
li = refindall(regex, line)
for imglocal in li:
imglocal = "".join(imglocal)
if imglocal.endswith("/"):
continue
if len(imglocal) <= 2:
continue
kignore = False
for src in ("/player.bilibili.com/",
"source/", "blog/", "source/shader/", "assets/glslEditor-0.0.20/",
"images/photo.jpg",):
if imglocal.startswith(src):
kignore = True
if kignore: continue
if os.path.isdir(imglocal):
continue
line = tidyupImg(imglocal, fpath, line, imgthumb=imgthumb)
regex = r"""(
(https?)://
([a-z0-9\.-]+\.[a-z]{2,8})
(:[0-9]{1,4})?
(/[a-z0-9\&%_\./~=+:@–-]*)?
(\?[a-z0-9\&%_\./~=+:\[\]-]*)?
(#[a-z0-9\&%_\./~=:?-]*)?
)"""
regex = "".join(regex.split())
li = refindall(regex, line, re.IGNORECASE)
if not li: return reflist, line
iglist = readfileIglist("config/mdrstrip_url_quote.txt")
iglist.extend(readfileIglist("invisible/config/mdrstrip_url_quote.txt"))
for tx in li:
url = tx[0]
host = tx[2]
checkz = line.split(url)
for iline in checkz[1:]: # 检查网址的后继标记。
checkli = ["", ")", "]", ">", " ", "*", "$"]
for urlz in iglist:
if url.startswith(urlz) and urlz:
checkli.append(";")
checkli.append("\"")
checkli.append("\'")
checkli.append("\\")
checkli.append("`")
if iline[:2] in ("{{",):
continue
if not iline[:1] in checkli:
print(line)
print(url)
assert False, (url, checkz)
assert not url.endswith("."), fpath +" "+ url
remote = backupUrlContent(fpath, md5src, url)
if remote:
reflist.append([url, remote])
if isHostIgnoreStat(host):
continue
if not host in G_HOSTSET:
G_HOSTSET[host] = 0
G_HOSTSET[host] += 1
xline = line[:]
for name, host in LINKTAGARRAY:
tak = getLinkTagSrc(name)
xline = xline.replace(tak+"]", name+"]")
li = refindall("<.*?>", xline)
for tx in li:
xline = xline.replace(tx, "")
for name, host in LINKTAGARRAY:
# 视频要特别标注域名。
li1 = refindall(host, xline, re.IGNORECASE)
li2 = refindall(name+"\\]", xline, re.IGNORECASE)
if len(li1) == len(li2):
continue
if xline.find("[")==-1 and xline.find("<")==-1 and xline.find("(")==-1:
continue
print(xline)
print(li1)
print(li2)
openTextFile(fpath)
assert False, linesrc
return reflist, line
# 语法高亮的 tag 检查。
def loadRougifyList():
ROUGIFY_LIST_FILE = "config/rougify_list_json.txt"
ROUGIFY_LIST = readfileJson(ROUGIFY_LIST_FILE)
if not ROUGIFY_LIST:
ROUGIFY_LIST_SRC = readfile("config/rougify_list.txt", True)
ROUGIFY_LIST = refindall("\n([^\\s:]+):", ROUGIFY_LIST_SRC, re.MULTILINE)
ROUGIFY_LIST2 = refindall("\\[\\s*aliases\\s*:(.*?)\\]", ROUGIFY_LIST_SRC)
for temp in ROUGIFY_LIST2:
temp = temp.strip().split(",")
for itemp in temp:
itemp = itemp.strip()
if not itemp: continue
ROUGIFY_LIST.append(itemp)
assert len(ROUGIFY_LIST) == 366, len(ROUGIFY_LIST)
writefileJson(ROUGIFY_LIST_FILE, ROUGIFY_LIST)
for i in ROUGIFY_LIST:
assert refindall("^([0-9a-z_#+-]+)$", i, re.IGNORECASE), i
return ROUGIFY_LIST
G_CNCHAR = []
G_CSCHAR = [] # 中文符号集合
G_ENCHAR = []
G_TYPESET = set()
G_MDKEYSET = set()
SNAPSHOT_HTML = "<font class='ref_snapshot'>参考资料快照</font>"
REVIEW_REGEX = "^<p class='reviewtip'><script type='text/javascript' src='{% include relrefx?.html url=\".*?\" %}'></script></p>$"
REVIEW_FORMAT = "<p class='reviewtip'><script type='text/javascript' src='{%% include relref.html url=\"/%s.js\" %%}'></script></p>"
REVIEW_LINE = "<hr class='reviewline'/>"
REVIEW_JS_PATH = "%s.js"
ROUGIFY_LIST = loadRougifyList()
def removeRefs(fpath, lines):
lineCount = len(lines)
headIndex = -1
for index in range(lineCount):
i = lineCount-1 - index
if not lines[i] or not lines[i].strip():
continue
if refindall("^- \\[{}\\]\\({}\\)$".format(".*?", ".*?"), lines[i]): # \\[[0-9]+\\]
continue
if lines[i] == SNAPSHOT_HTML:
headIndex = i
break
break
if headIndex != -1:
assert lines[headIndex-1] == "" or refindall(REVIEW_REGEX, lines[headIndex-1]), "%r"%lines[headIndex-1]
assert lines[headIndex-2] in ("-----", REVIEW_LINE), "%r"%lines[headIndex-2]
assert lines[headIndex-3] == "", "%r"%lines[headIndex-3]
lines = lines[:headIndex-3]
else:
while lines and (lines[-1] in ("", "-----", REVIEW_LINE) or
refindall(REVIEW_REGEX, lines[-1])):
lines = lines[:-1]
return lines
def appendRefs(fpath, md5src, lines, imgthumb):
reflist = []
for index, line in enumerate(lines):
ireflist, line = collectHost(fpath, md5src, line, imgthumb)
lines[index] = line
if ireflist:
reflist.extend(ireflist)
invdir = isInvisibleDir(fpath)
fpath = os.path.relpath(fpath, ".")
frelgit = fpath
if os.path.exists(fpath+".tempd"): # 存在加密版本。
frelgit = fpath+".tempd"
def getDateStr(frelgit):
# 获取 md 文件的最后修改时间。
cmdx = 'git log -n 1 --pretty=format:"%ad" --date=short -- "{}"'.format(frelgit)
if invdir:
cmdx = 'cd {} & git log -n 1 --pretty=format:"%ad" --date=short -- "{}"'.format(*frelgit.split(os.sep, 1))
if invdir and IS_MACOS:
cmdx = 'cd {} && git log -n 1 --pretty=format:"%ad" --date=short -- "{}" && cd ..'.format(*frelgit.split(os.sep, 1))
try:
datestr = popenCmd(cmdx)
datestr = bytesToString(datestr)
except FileNotFoundError: # [Errno 2] No such file or directory: 'cd invisible && git log -n 1 --pretty=format:"%ad" --date=short -- "ncnn/onnxruntime-for-win7/README_EN.md" && cd ..'
datestr = None
if not datestr:
datestr = datetime.datetime.now().date()
return datestr
if fpath.startswith("_posts"+os.sep):
fpath = os.path.join("blogs", fpath.split(os.sep)[-1])
if invdir:
fpath = "invisible"+os.sep+"reviewjs"+os.sep+ fpath[len("invisible"+os.sep):]
else:
fpath = "assets"+os.sep+"reviewjs"+os.sep + fpath
reviewjs = REVIEW_JS_PATH % (fpath)
fcode = readfile(reviewjs, True)
fcheck = refindall("[0-9a-f]{32}", fcode)
if fcheck and md5src == fcheck[0]:
pass
else:
fcode = """document.write("%s: review"); // md5src=%s%s""" % (
getDateStr(frelgit), md5src, NEWLINE_CHAR)
writefile(reviewjs, fcode)
review = REVIEW_FORMAT % (fpath.replace("\\", "/"))
assert refindall(REVIEW_REGEX, review), review
if "sortrefs: true" in lines:
reflist = sorted(reflist, key=lambda x: x[1], reverse=False)
if reflist:
lines.append("")
lines.append("")
lines.append(REVIEW_LINE)
lines.append(review)
lines.append(SNAPSHOT_HTML)
lines.append("")
lines.append("")
urlset = set()
count = 0
for url, remote in reflist:
if url in urlset: continue
urlset.add(url)
count = count + 1
from urllib.parse import unquote
remote = "{% " + ("include relrefx.html url=\"/%s\"" % (remote,)) + " %}"
lines.append("- [{}]({})".format(url, remote)) # count
lines.append("")
else:
lines.append("")
lines.append("")
lines.append(REVIEW_LINE)
lines.append(review)
lines.append("")
return lines
def mainfile(fpath, fname, ftype, fdepth=0):
if fpath.endswith(SPACEBACKFILE_TAIL):
fjson = readfileJson(fpath, "utf8")
writefileJson(fpath, fjson, ascii=False, encoding="utf8")
return
iglist = readfileIglist("config/mdrstrip_file_ignore.txt")
if fname in iglist:
return
fpathsrc, fnamesrc, ftypesrc = fpath, fname, ftype
checkfilesize(fpath, fname, ftype)
ftype = ftype.lower()
errcnt = 0
warnCnEnSpace = ftype in ("md", "php", "html", "htm", "vsh", "fsh",) # 英文中文空符检查
warnTitleSpace = ftype in ("md",) # 标题前后空行检查
warnIndentSpace = ftype in ("md", "php", "scss", "vsh", "fsh",) # 缩进检查
isMdFile = ftype in ("md",)
isSrcFile = ftype in ("md", "php", "html", "htm", "js", "css", "scss", "svg", "py", "vsh", "fsh",)
keepStripFile = ftype in ("svg",) or fname in ("gitsrc.html",) or refindall("^relref[a-z_]*\\.html$", fname)
keepFileTypeList = ("rar", "zip", "pdf", "doc", "mp4",) # 中英文间隔,容易造成失误的列表。
if fpath.find(os.sep+"winfinder"+os.sep) != -1:
isSrcFile = isSrcFile or ftype in ("h", "cpp", "cxx", "rc", "c",)
if not isSrcFile:
if fpath.find(os.sep+"_site"+os.sep) != -1: # 必须在 _site 文件夹?
G_TYPESET.add(ftype)
return
if isMdFile:
# 收集 Jekyll 头定义 key 集合。
fdata = readfile(fpath, True).strip()
if fdata.startswith("---"):
kvlist = fdata.split("---")[1].strip().split("\n")
for kv in kvlist:
kv = kv.strip()
key, value = kv.split(":", 1)
key = key.strip()
value = value.strip()
G_MDKEYSET.add(key)
if fpath.find(os.sep+"_site"+os.sep) != -1: # _site 文件夹
return
def linerstrip(line):
if isMdFile:
for name, host in LINKTAGARRAY:
tak = getLinkTagSrc(name)
# 移除多余空格
line = line.replace(" "+tak+"]", tak+"]")
line = line.replace(" "+tak+"]", tak+"]")
# 格式化。
line = line.replace(tak+"]", " "+tak+"]")
line = line.replace(name+"]", tak+"]")
line = line.replace("[ "+tak+"]", "["+name+" "+tak+"]")
line = line.replace(" ——", "——").replace(" ——", "——")
line = line.replace("—— ", "——").replace("—— ", "——")
line = line.replace("——", " —— ")
return line.rstrip()
print(fpath)
if IS_MACOS or IS_LINUX:
fdata = readfile(fpath)
fdata = fdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n").replace(b"\n", b"\r\n")
writefile(fpath, fdata)
md5src = getFileMd5(fpath, mycache=MYCACHE) # mainfile
try:
lines = readfileLines(fpath, False, False, "utf8")
except Exception as ex:
openTextFile(fpath)
raise ex
lines = removeRefs(fpath, lines)
lines = [linerstrip(line) for line in lines]
lines.append("")
lines.append("")
while len(lines) >= 2 and not lines[-1] and not lines[-2]:
lines = lines[:-1]
while len(lines) >= 1 and not lines[0]:
lines = lines[1:]
if keepStripFile:
while len(lines) >= 1 and not lines[-1]:
lines = lines[:-1]
if isMdFile:
imgthumb = True
lineCount = 0
for line in lines:
if line.strip() == "---":
lineCount = lineCount + 1
if lineCount >= 2: break
if "".join(line.strip().lower().split()) == "imgthumb:false":
imgthumb = False
break
# 避免格式化的文件。
if "".join(line.strip().lower().split()) == "codeformat:false":
return
try:
lines = appendRefs(fpath, md5src, lines, imgthumb)
except AssertionError as ex:
if fdepth >= 5:
raise ex
openTextFile(fpath)
traceback.print_exc()
print("断言错误 {}".format(ex,))
os.system(PAUSE_CMD)
return mainfile(fpathsrc, fnamesrc, ftypesrc, fdepth+1)
# .spaceback.json
spacebackfile = (os.path.splitext(fpath)[0] + SPACEBACKFILE_TAIL)
spacebackjson = {}
if not os.path.exists(spacebackfile):
spacebackfile = os.path.join(os.path.split(fpath)[0], "k"+os.path.splitext(fname)[0]+SPACEBACKFILE_TAIL)
if os.path.exists(spacebackfile):
spacebackjson = readfileJson(spacebackfile, "utf8")
codestate = False
chartstate = False
for index, line in enumerate(lines):
for kftype in keepFileTypeList:
line = line.replace(" ."+kftype, "."+kftype)
lines[index] = line
if isMdFile and line.count("**") >= 2 and line.count("**") % 2 == 0 and line.count("***") == 0 and not codestate:
dotlines = line.split("**")
newline = ""
for idx, dot in enumerate(dotlines):
if idx % 2 == 1:
if not newline.strip():
newline = newline + "**" + dot.strip() + "** "
else:
newline = newline.rstrip() + " **" + dot.strip() + "** "
else:
if idx >= 2:
newline += dot.lstrip()
else:
newline += dot
newline = newline.rstrip()
line = newline
lines[index] = line
line = line.replace("````", "```").replace("````", "```")
linxCount = line.count("`")
if isMdFile and linxCount >= 2 and linxCount % 2 == 0:
newline = ""
linxCount = 0
for idx, ch in enumerate(line):
if ch == '`':
linxCount = linxCount + 1
if linxCount % 2 == 1:
if newline and not newline.endswith(" "):
if not newline.endswith(":"):
newline += " "
newline += "`"
else:
if newline.endswith(": "):
newline = newline[:-1]
newline += "`"
else:
newline += "`"
if idx+1 < len(line) and line[idx+1] != " ":
newline += " "
else:
newline += ch
line = newline
lines[index] = line
preline = lines[index - 1] if index > 0 else ""
nextline = lines[index + 1] if index < len(lines)-1 else ""
# ```java
# {% highlight ruby %}
# https://github.com/rouge-ruby/rouge/wiki/List-of-supported-languages-and-lexers
li1 = refindall("```\\s*([0-9a-z_#+-]+)", line, re.IGNORECASE)
li2 = refindall("\\{%\\s*highlight\\s*([0-9a-z_#+-]+)", line, re.IGNORECASE)
li1.extend(li2)
for i in li1:
if not i in ROUGIFY_LIST:
openTextFile(fpath)
print("代码语言无法识别 {}:{} \"{}\"".format(fpath, index+1, i))
os.system(PAUSE_CMD)
return mainfile(fpathsrc, fnamesrc, ftypesrc, fdepth+1)
tagregex = "^\\s*[#]+\\s"
prelinetag = refindall(tagregex, preline)
nextlinetag = refindall(tagregex, nextline)
if warnTitleSpace and not codestate:
tagregexk = "^\\s*[#]+\\s{2,}" # md 文件标题后接的空格 只能是一个。
if refindall(tagregexk, preline):
openTextFile(fpath)
assert False, preline
if refindall("^\\s*[*-]+\\s", line):
idtcnt = 2 # 如果在列表里面,缩进检查 2 个为单位。
else:
idtcnt = 4
cnsign = "‘’“”" # 中文符号
cnregex = "\u4e00-\u9fa5" # 中文汉字
# 统计出现的字符。
for ch in line:
ordch = ord(ch)
regch = "\\u%04x"%(ordch)
if ordch <= 0x7F or isDiacritic(ch):
G_ENCHAR.append(ch) # 英文
continue
if ordch >= 0x4e00 and ordch <= 0x9fa5:
if cnregex.find(regch) == -1:
cnregex += regch # 中文汉字
if G_CNCHAR.count(ch) == 0:
G_CNCHAR.append(ch)
else:
if cnsign.find(regch) == -1:
cnsign += regch # 中文符号
if G_CSCHAR.count(ch) == 0:
G_CSCHAR.append(ch)
cnregexc = cnregex[:]
cnregex += cnsign # 中文汉字符号都来起。
# 1.不间断空格\u00A0,主要用在office中,让一个单词在结尾处不会换行显示,快捷键ctrl+shift+space;
# 2.半角空格(英文符号)\u0020,代码中常用的;
# 3.全角空格(中文符号)\u3000,中文文章中使用;
# 不能出现全角的空格。
# utf8:20 c2a0 e38080 "\xa0"
if line.find("\u00A0") != -1 and line.find("\u3000") != -1 and not fname in ("glslEditor.min.js",):
print("xspace", fpath, line)
errcnt += 1
#liw = refindall("[{}]+".format(cnregex,), line, re.IGNORECASE)
#lia = refindall("[^{}]+".format(cnregex,), line, re.IGNORECASE)
linec = line
for itmp in refindall("\\$\\$.*?\\$\\$", line): # 忽略数学公式
linec = linec.replace(itmp, " ") # "$$$$")
for itmp in refindall("“.*?”", line): # 忽略双引号
linec = linec.replace(itmp, " ") # "“”")
for itmp in refindall("`.*?`", line): # 忽略代码部分
linec = linec.replace(itmp, " ") # "“”")
# 忽略特殊的 tag 标记。
for itmp in ('"WEB前端"',):
linec = linec.replace(itmp, "\"\"")
# 图片 caption 不校验空格。
linec = linec.replace('caption="', 'caption=" ')
linec = linec.replace('caption2="', 'caption2=" ')
linec = linec.replace('title="', 'title=" ')
lix1 = refindall("[{}][^{} *]".format(cnregex, cnregex), linec, re.IGNORECASE)
lix2 = refindall("[^{} *][{}]".format(cnregex, cnregex), linec, re.IGNORECASE)
lix = []
lix.extend(lix1)
lix.extend(lix2)
cnsignregex = "[{}]".format(cnsign)
for ix in lix:
cx, cy = ix
# 其中一个是中文符号。
if refindall(cnsignregex, cy) or refindall(cnsignregex, cx):
continue
if cy in "-<]~" or cx in "->[~":
continue
if chartstate:
continue
if cx in ("\"", "[") and (" "+line).count(" "+ix) == 1:
continue
if cy in ("\"", "]", ",") and (line+" ").count(ix+" ") == 1:
continue
if cx in ("(", ) and (" \\"+line).count(" \\"+ix) == 1:
continue
if cy in ("\\", ) and (line+") ").count(ix+") ") == 1:
continue
if cx in ("\"",) and ("["+line).count("["+ix) == 1:
continue
if cy in ("\"",) and ((line+"]").count(ix+"]") == 1 or (line+",").count(ix+",") == 1):
continue
tagcontinue = False
for kftype in keepFileTypeList:
if cy in (".",) and (line.count(ix+kftype) == 1):
tagcontinue = True
if tagcontinue: continue
if not warnCnEnSpace:
continue
if codestate:
if cy in "\":" or cx in "\":":
continue
if line.startswith("print ("):
continue
print("[%d]"%(index+1), ix, cx, cy, "\t", line)
errcnt += 1
if AUTOFORMAT:
line = line.replace(ix, cx+" "+cy)
lines[index] = line
# 检查中文问本里面不应该出现的英文符号。
if isMdFile:
lixyx = refindall("[{}] [,()] [{}]".format(cnregex, cnregex), linec, re.IGNORECASE)
lixyx.extend(refindall("[{}] [,()]$".format(cnregex), linec, re.IGNORECASE))
lixyx.extend(refindall("[{}][,;] [{}]".format(cnregexc, cnregexc), linec, re.IGNORECASE))
if lixyx:
openTextFile(fpath)
print(lixyx)
print("中文符号问题 {}:{} \"{}\"".format(fpath, index+1, linec))
os.system(PAUSE_CMD)
return mainfile(fpathsrc, fnamesrc, ftypesrc, fdepth+1)
fxline = "".join(line.split())
if fxline.startswith("<divclass=\"mermaid\"") and not chartstate:
chartstate = True
if fxline.startswith("</div>") and chartstate:
chartstate = False
if fxline.startswith("{%highlight"):
codestate = True
continue
if fxline.startswith("{%endhighlight%}"):
codestate = False
continue
if fxline.startswith("```") and not codestate:
codestate = True