-
Notifications
You must be signed in to change notification settings - Fork 48
/
hexo2hugo.py
275 lines (244 loc) · 8.15 KB
/
hexo2hugo.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
#!/usr/bin/env python3
#==================================
# move zorng's hexo blog to hugo
#
# 2019-06-11 init
# 2019-06-20 build flash content
#==================================
from pathlib import Path
import os
import re
import yaml
import toml
from datetime import datetime, timezone, timedelta
pwddir = Path(__file__).parent.resolve()
hexo_source = pwddir.parent.joinpath('blog/source/')
hexo_posts = pwddir.parent.joinpath('blog/source/_posts')
hugo_posts = pwddir.joinpath('content/post')
hugo_articles = pwddir.joinpath('content/article')
def replace_body_flash(body_text):
""" dispose the {% flash %} xxx {% endflash %}
in multiline
"""
re_flash = re.compile(r'{% flash %\}(.*?)\{% endflash %\}', re.DOTALL)
match_list = re_flash.finditer(body_text)
repl_list = []
for matchobj in match_list:
flash_params = matchobj.group(1).strip()
# print('flash_params', flash_params)
lines = flash_params.split('\n')
flash_pobj = {}
for line in lines:
# print('line', line)
line_list = line.strip().split(':')
key = line_list[0].strip()
value = line_list[1].strip()
if key == 'useexpressinstall' or key == 'menu' or key == 'fversion':
value = value.replace("'", '').replace('"', '')
elif key == 'width' or key == 'height':
value = value.replace("'", '').replace('"', '')
value = int(value)
else:
value = str(value)
flash_pobj[key] = value
new_param_line = ['{0}="{1}"'.format(k, v) for k, v in flash_pobj.items()]
repl_list.append('{{{{< flash {0} >}}}}'.format(' '.join(new_param_line)))
# print(repl_list)
for repl in repl_list:
body_text = re_flash.sub(repl, body_text, 1)
# print('flash_params ', flash_params)
# print('flash_pobj ', flash_pobj)
# print(body_text)
return body_text
def replace_body_download(body_text):
""" dispose the
{% download %}
id:
- 1
- 2
{% enddownload %}
in multiline
"""
re_flash = re.compile(r'{% download %\}(.*?)\{% enddownload %\}', re.DOTALL)
match_list = re_flash.finditer(body_text)
repl_list = []
for matchobj in match_list:
dl_params = matchobj.group(1).strip()
lines = dl_params.split('\n')
dl_ids = []
for line in lines:
print('line', line)
if line.startswith('id'):
continue
lead = line.find('-')
if lead == -1:
continue
value = line[lead+1:].strip().replace("'", '').replace('"', '')
dl_ids.append(value)
repl_list.append('{{{{< download {0} >}}}}'.format(' '.join(dl_ids)))
# print(repl_list)
for repl in repl_list:
# print(body_text)
body_text = re_flash.sub(repl, body_text, 1)
return body_text
def replace_body_label(body_text):
""" dispose the shortcode
{% label 'text' info/danger/warning/success %}
"""
re_label = re.compile(r'\{% label +\'(.*?)\' +(\w+) +%\}')
match_list = re_label.finditer(body_text)
repl_list = []
for matchobj in match_list:
label_name = matchobj.group(1)
label_color = matchobj.group(2)
repl_list.append('{{{{< label {0} {1} >}}}}'.format(label_name, label_color))
for repl in repl_list:
# print(body_text)
body_text = re_label.sub(repl, body_text, 1)
return body_text
def replace_body_alert(body_text):
re_alert = re.compile(r'\{% alert +(\w+) +%\}(.*?)\{% endalert %\}', re.DOTALL)
match_list = re_alert.finditer(body_text)
repl_list = []
for matchobj in match_list:
alert_color = matchobj.group(1)
alert_text = matchobj.group(2).strip()
repl_list.append('{{{{% alert {0} %}}}}\n{1}\n{{{{% /alert %}}}}'.format(alert_color, alert_text))
for repl in repl_list:
body_text = re_alert.sub(repl, body_text, 1)
return body_text
def replace_bodies(body_text):
body_text = replace_body_flash(body_text)
body_text = replace_body_download(body_text)
body_text = replace_body_alert(body_text)
body_text = replace_body_label(body_text)
return body_text
def read_content(f):
front_matter = []
body = []
with f.open() as pf:
fm = True
line = pf.readline()
while line:
if line.startswith('---'):
if len(front_matter) == 0:
line = pf.readline()
continue
else:
fm = False
else:
if fm:
front_matter.append(line)
else:
body.append(line)
line = pf.readline()
body_text = replace_bodies(''.join(body))
return {
'front_matter': yaml.load(''.join(front_matter), Loader=yaml.SafeLoader),
'body': body_text
}
def mege_content(p, type_):
ofm = p['front_matter']
datefmt = '%Y-%m-%d %H:%M:%S'
tzinfo = timezone(timedelta(hours=8))
dt = ofm['date']
if isinstance(dt, str):
dt = datetime.strptime(ofm['date'], datefmt)
dt = datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, tzinfo=tzinfo)
upt = ofm.get('updated')
if isinstance(upt, str):
upt = datetime.strptime(upt, datefmt)
tags = ofm.get('tags')
nicename = ofm.get('nicename')
postid = ofm.get('postid')
slug = str(nicename) if nicename else str(postid)
categories = ofm.get('categories')
toc = p.get('toc', False)
aliases = None
url = None
if type_ == 'post':
aliases = ['/post/{0}.html'.format(postid)]
if type_ == 'article':
url = '/{0}/'.format(slug)
nfm = {
'title': ofm['title'],
'postid': int(postid),
'date': dt,
'isCJKLanguage': True,
'toc': toc,
'type': type_,
'slug': slug,
}
if aliases:
nfm['aliases'] = aliases
if url:
nfm['url'] = url
# use singular, not plural
# so write follows in config.toml of hugo project
"""
[taxonomies]
category = "category"
tag = "tag"
"""
if categories is not None:
# nfm['categories'] = [categories]
nfm['category'] = [categories]
if tags is not None:
# use singular, not plural
# nfm['tag'] = tags
nfm['tag'] = tags
if upt is not None:
upt = datetime(upt.year, upt.month, upt.day, upt.hour, upt.minute, upt.second, tzinfo=tzinfo)
nfm['lastmod'] = upt
if ofm.get('attachments'):
nfm['attachments'] = ofm['attachments']
front_matter_toml = toml.dumps(nfm)
return '+++\n%s+++\n\n%s' % (front_matter_toml, p['body'])
def build_post(p):
""" build a post
"""
f = hexo_posts.joinpath(p)
post_data = read_content(f)
s = mege_content(post_data, 'post')
hugef = hugo_posts.joinpath(p)
hugef.write_text(s, encoding='utf8')
def build_posts():
""" build all posts in hexo/source/_posts
"""
i = 0
error_posts = []
for p in os.listdir(hexo_posts):
if not p.endswith('.md'):
continue
print('perform ', i, p)
try:
build_post(p)
except Exception as e:
error_posts.append({'name': p, 'error': e})
print('%s error %s' %(p, e))
continue
i += 1
print(len(error_posts))
def build_pages():
i = 0
error_pages = []
for p in os.listdir(hexo_source):
if p.startswith('.') or p.startswith('_') or p in ('search', 'uploads', 'tag', 'category', 'link'):
continue
page_file = hexo_source.joinpath(p, 'index.md')
page_data = read_content(page_file)
print('perform ', i, page_file)
try:
s = mege_content(page_data, 'article')
hugef = hugo_articles.joinpath(p+'.md')
hugef.write_text(s, encoding='utf8')
except Exception as e:
error_pages.append({'name': p, 'error': e})
print('%s error %s' %(p, e))
continue
i += 1
print(len(error_pages))
if __name__ == '__main__':
# build_post('2631.md')
build_posts()
build_pages()