-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoBlog.py
More file actions
150 lines (128 loc) · 4.65 KB
/
Copy pathautoBlog.py
File metadata and controls
150 lines (128 loc) · 4.65 KB
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
######################################
## `_______ `__ __ `_______ ##
## | __ \ | | / / |___ / ##
## | | \ \| |/ / / / ##
## | | | || _ \ / / ##
## | |__| || | \ \ / /____ ##
## |________/|__| \__\/________| ##
######################################
# 2016/01/28 by DKZ https://davidkingzyb.github.io
import os
import json
import misaka
import re
import urllib
import blogconfig
import html
files=os.listdir('./blogmd');
def read(file):
with open(file,'r', encoding='UTF-8') as f:
return f.read()
def write(file,str):
with open(file,'w',encoding='UTF-8') as f:
f.write(str)
def render(o,html):
for x in o.keys():
html=html.replace('{{'+x+'}}',o[x])
return html
head=read('template/head.html')
foot=read('template/foot.html')
def packhtml(htmlbody,id):
#if id<len(blogconfig.issuearr):
# o={'discussurl':blogconfig.issueurl+str(id)}#blogconfig.issuearr[id]}
#else:
# o={'discussurl':'http://davidkingzyb.github.io/blogmd/0.html'}
o={'discussurl':'https://github.com/davidkingzyb/davidkingzyb.github.io/issues/1'};
html=render(o,head)+'<article class="markdown-body">'+htmlbody+'</article>'+render(o,foot)
return html
reH=re.compile(r'(<h\d>(.*?)</h\d>)')
reImg=re.compile(r'<img.*?/>')
def addGithubAnchor(htmlstr):
html=htmlstr
hdata=re.findall(reH,html)
for x in hdata:
pattern=x[0]
data=x[1]
hrefstr=' name="'+urllib.parse.quote(data.replace(' ','-').lower())+'"'
astr=pattern[:4]+'<a class="anchor"'+hrefstr+'><span class="octicon octicon-link"></span></a>'+data+pattern[-5:]
html=html.replace(pattern,astr)
return html
def MDtoHTML(mdfile):
print('md to html:'+mdfile)
md=read('./blogmd/'+mdfile)
html=misaka.html(md,extensions=['tables','fenced-code'])
html=addGithubAnchor(html)
html=packhtml(html,int(mdfile.split('.')[0]))
write('./blogmd/'+mdfile[:-3]+'.html',html)
def doBlogs(files):
for f in files:
if(f!='blogImg' and f!='blogImg_src'):
t=f.split('.')[1]
if t=='md':
MDtoHTML(f)
gRSStemplate="""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>DKZ's Blog</title>
<link>http://davidkingzyb.github.io/blog.html</link>
<description>Tech Program Design 造物</description>
<copyright>(c)2015-2026 by DKZ</copyright>
<image>
<url>http://davidkingzyb.github.io/res/img/cubehead.png</url>
<title>DKZ</title>
<link>http://davidkingzyb.github.io/blog.html</link>
</image>
"""
RSStemplate=''
def doRSS(o,lines):
global RSStemplate
ll=list(filter(lambda l:l[0:2]!='![',lines))
article=''.join(ll[8:30])+'...\n\n\nlink: http://davidkingzyb.github.io/blogmd/'+html.escape(str(o['index']))+'.html'
RSStemplate='<item><title>'+html.escape(o['title'])+'</title><link>http://davidkingzyb.github.io/blogmd/'+html.escape(str(o['index']))+'.html</link><description>'+html.escape(o['info'])+'\n'+html.escape(article)+'</description></item>'
def MDtoJson(file):
print('md to json:'+file)
index=int(file.split('.')[0])
with open('./blogmd/'+file,'r', encoding='UTF-8') as f:
lines=f.readlines()
title=lines[0][1:-1]
info=lines[2][2:-3]
key=lines[4][:-1]
img=lines[6]
img=img[img.find('(')+1:-2]
result={"index":index,"title":title,"info":info,"key":key,"img":img}
doRSS(result,lines)
return result
def createBlogJson(files,jsonfile,bottom,top):
blog=[]
for f in files:
if(f!='blogImg' and f!='blogImg_src'):
t=f.split('.')[1]
if t=='md':
x=int(f.split('.')[0])
if bottom<=x and x<=top:
j=MDtoJson(f)
blog.append(j)
blogJson={"blog":blog}
with open(jsonfile+'.json','w', encoding='UTF-8') as bjf:
bjf.write(json.dumps(blogJson))
def createRSSfile(filename):
global gRSStemplate
global RSStemplate
RSStemplate+='</channel></rss>'
with open(filename+'.xml','w',encoding='UTF-8') as rssf:
rssf.write(gRSStemplate+RSStemplate)
print('RSS ok')
doBlogs(files)
createBlogJson(files,'blog2015',0,10)
createBlogJson(files,'blog2016',11,24)
createBlogJson(files,'blog2017',25,27)
createBlogJson(files,'blog2018',28,30)
createBlogJson(files,'blog2019',31,34)
createBlogJson(files,'blog2020',35,36)
createBlogJson(files,'blog2021',37,37)
createBlogJson(files,'blog2022',38,39)
createBlogJson(files,'blog2023',40,43)
createBlogJson(files,'blog2024',44,51)
createBlogJson(files,'blog2025',52,52)
createBlogJson(files,'blog2026',53,99)
createRSSfile('rss')