-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertToHugo.py
138 lines (102 loc) · 4.13 KB
/
ConvertToHugo.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
#!/usr/bin/env python
# coding:utf-8
import os
import re
import yaml
from datetime import datetime
import argparse
__author__ = 'coderzh'
content_regex = re.compile(r'---([\s\S]*?)---([\s\S]*)')
def convert_front_matter(front_data, post_date, url):
if post_date:
front_data['date'] = post_date.strftime('%Y-%m-%d')
front_data['url'] = url
del front_data['layout']
for tag in ['tags', 'categories', 'category']:
if tag in front_data and isinstance(front_data[tag], basestring):
front_data[tag] = front_data[tag].split(' ')
if 'category' in front_data:
front_data['categories'] = front_data['category']
del front_data['category']
replace_regex_list = [
# (re.compile(r'^```(.*?)\n(.*?)\n```', re.DOTALL), r'{{< highlight \1 >}}\n\2\n{{< /highlight >}}'),
(re.compile(r'<!--\smore\s-->'), '<!--more-->'),
(re.compile(r'\{%\sraw\s%\}(.*)\{%\sendraw\s%\}'), r'\1')
]
def convert_body_text(body_text):
result = body_text
for regex, replace_with in replace_regex_list:
result = regex.sub(replace_with, result)
return result
def write_out_file(front_data, body_text, out_file_path):
out_lines = ['---']
front_string = yaml.dump(front_data, width=1000, default_flow_style=False, allow_unicode=True)
out_lines.extend(front_string.splitlines())
out_lines.append('---')
out_lines.extend(body_text.splitlines())
with open(out_file_path, 'w') as f:
f.write('\n'.join(out_lines))
filename_regex = re.compile(r'(\d+-\d+-\d+)-(.*)')
def parse_from_filename(filename):
slug = os.path.splitext(filename)[0]
m = filename_regex.match(slug)
if m:
slug = m.group(2)
post_date = datetime.strptime(m.group(1), '%Y-%m-%d')
return post_date, '/%s/%s' % (post_date.strftime('%Y/%m/%d'), slug)
return None, '/' + slug
def convert_post(file_path, out_dir):
filename = os.path.basename(file_path)
post_date, url = parse_from_filename(filename)
content = ''
with open(file_path, 'r') as f:
content = f.read()
m = content_regex.match(content)
if not m:
print 'Error match content: %s' % file_path
return False
front_data = yaml.load(m.group(1))
if not front_data:
print 'Error load yaml: %s' % file_path
return False
'''
if 'layout' in front_data:
if post_date:
out_dir = os.path.join(out_dir, front_data['layout'], str(post_date.year))
else:
out_dir = os.path.join(out_dir, front_data['layout'])
'''
if not os.path.exists(out_dir):
os.makedirs(out_dir)
out_file_path = os.path.join(out_dir, filename)
convert_front_matter(front_data, post_date, url)
body_text = convert_body_text(m.group(2))
write_out_file(front_data, body_text, out_file_path)
return True
def convert(src_dir, out_dir):
count = 0
error = 0
for root, dirs, files in os.walk(src_dir):
for filename in files:
try:
if os.path.splitext(filename)[1] != '.md' or filename in ['README.md', 'LICENSE.md']:
continue
file_path = os.path.join(root, filename)
common_prefix = os.path.commonprefix([src_dir, file_path])
rel_path = os.path.relpath(os.path.dirname(file_path), common_prefix)
real_out_dir = os.path.join(out_dir, rel_path)
if convert_post(file_path, real_out_dir):
print 'Converted: %s' % file_path
count += 1
else:
error += 1
except Exception as e:
error += 1
print 'Error convert: %s \nException: %s' % (file_path, e)
print '--------\n%d file converted! %s' % (count, 'Error count: %d' % error if error > 0 else 'Congratulation!!!')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert Jekyll blog to GoHugo')
parser.add_argument('src_dir', help='jekyll post dir')
parser.add_argument('out_dir', help='hugo root path')
args = parser.parse_args()
convert(os.path.abspath(args.src_dir), os.path.abspath(args.out_dir))