-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgen_site.py
More file actions
executable file
·644 lines (557 loc) · 30.4 KB
/
Copy pathgen_site.py
File metadata and controls
executable file
·644 lines (557 loc) · 30.4 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
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
#!/usr/bin/python
#-*- encoding:utf-8 -*-
'''
Created on Apr 14, 2014
@author: sunlt
oopress生成工具。
本程序在Linux Mint 16下运行正常。
'''
import jinja2
import os
import shutil
import codecs
import markdown
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
CONFIG = {
'app_path': '.',
'posts_dir': '/_posts',
'pages_dir': '/_pages',
'static_dir': '/_static',
'img_dir': '/_images',
'extra_dir': '/_extras',
'layout_dir': '/_layouts',
'site_dir': '/_site',
'site_static_dir': '/static', #./_site/static
'site_page_dir': '', #./_site
'site_post_dir': '/post', #./_site/post
'site_img_dir':'/images', #./_site/imgages
'site_tag_dir':'/tag',
'site_category_dir':'/category',
'site_url':'http://blog.letiantian.me',
'site_title':'oopress~',
'site_subtitle':'一个静态博客生成系统'
}
def getTemplate(config, template_file):
'''
config: 保存配置项的字典
'''
print '>>> getTemplate()'
print '\ttemplate file: ',template_file
templateLoader = jinja2.FileSystemLoader( searchpath = config['app_path'] + config['layout_dir'] )
templateEnv = jinja2.Environment( loader=templateLoader )
return templateEnv.get_template( template_file )
def cleanSite(config):
'''
清空CONFIG[site_dir'],重建目录
'''
def try_mkdir(dir_name):
''' '''
try:
os.mkdir(dir_name)
except:
pass
shutil.rmtree(config['app_path'] + config['site_dir'])
os.makedirs(config['app_path'] + config['site_dir'])
try_mkdir(config['app_path'] + config['site_dir'] + config['site_page_dir'])
try_mkdir(config['app_path'] + config['site_dir'] + config['site_post_dir'])
try_mkdir(config['app_path'] + config['site_dir'] + config['site_tag_dir'])
try_mkdir(config['app_path'] + config['site_dir'] + config['site_category_dir'])
#try_mkdir(config['app_path'] + config['site_dir'] + config['site_post_list_dir'])
shutil.copytree(config['app_path'] + config['static_dir'], config['app_path'] + config['site_dir'] + config['site_static_dir'])
shutil.copytree(config['app_path'] + config['img_dir'], config['app_path'] + config['site_dir'] + config['site_img_dir'])
def genStaticHTML(targetDir, file_name, file_content):
''' '''
if not os.path.isdir(targetDir):
os.makedirs(targetDir)
with codecs.open(targetDir+'/'+file_name, 'w', encoding='utf-8') as f:
f.write(file_content)
def getPostList(config):
'''
获取post列表,每个元素是post的路径
例如,返回:['/home/sunlt/Code/eclipse-python/pyJekyll/_posts/\xe5\x93\x88\xe5\x93\x88.md']
'''
file_path = []
for root, dirs, files in os.walk(config['app_path'] + config['posts_dir']):
if len(files) > 0:
for f_name in files:
file_path.append(root + '/' + f_name)
return file_path
def getPageList(config):
''' 获取page列表,每个元素是page的路径 '''
file_path = []
for root, dirs, files in os.walk(config['app_path'] + config['pages_dir']):
if len(files) > 0:
for f_name in files:
file_path.append(root + '/' + f_name)
return file_path
def md2html(str_md):
'''
将markdown语法的str转换为html的str
'''
return markdown.markdown(str_md, extensions=['markdown.extensions.tables'])
def title2fileName(title):
''' hadoop streaming -> hadoop-streaming'''
return title.replace(' ','-')
def getBaseInfoOfPosts(config, post_list):
'''
post文件示例
---
title:关于樂天笔记
layout:page
time:2014-04-07 18:00
author:樂天
cat:云计算
tag:java,hadoop
---
'''
def noLowwerButStrip(s):
return s.strip()
print '>>> getBaseInfoOfPosts()'
for pl in post_list:
print '\t file:', pl
infos = {}
for f_path in post_list:
print '\t', f_path
count = 0
for line in codecs.open(f_path, 'r', encoding='utf-8'):
print '\t>',line
if line.strip() == '---':
if count == 1:
print 'break'
break
if count == 0:
count += 1
print 'continue'
continue
ls = line.split(':', 1)
if ls[0].strip() == 'title':
title = ls[1].strip()
print '\ttitle:',title
elif ls[0].strip() == 'layout':
layout = ls[1].strip()
print '\tlayout',layout
elif ls[0].strip() == 'author':
author = ls[1].strip()
print '\tauthor',author
elif ls[0].strip() == 'time':
create_date = ls[1].strip().split()[0].strip()
create_time = ls[1].strip().split()[1].strip()
print '\tcreate_date',create_date
print '\tcreate_time',create_time
elif ls[0].strip() == 'cat':
if len(ls[1].strip()) == 0:
cat = []
elif ',' not in ls[1].strip():
cat = [ls[1].strip()]
else:
cat = map(noLowwerButStrip, ls[1].split(','))
print '\tcat',cat
elif ls[0].strip() == 'tag':
if len(ls[1].strip()) == 0:
tag = []
elif ',' not in ls[1].strip():
tag = [ls[1].strip()]
else:
tag = map(noLowwerButStrip, ls[1].split(','))
print '\ttag', tag
local_from_path = f_path
local_to_dir = config['app_path'] + config['site_dir'] + config['site_post_dir'] + '/' + create_date.replace('-','/')
url = config['site_post_dir'] + '/' + create_date.replace('-','/') + '/' + title2fileName(title) + '.html'
infos.setdefault((create_date, create_time, title),{})
infos[(create_date, create_time, title)]['layout'] = layout
infos[(create_date, create_time, title)]['author'] = author
infos[(create_date, create_time, title)]['cat'] = cat
infos[(create_date, create_time, title)]['tag'] = tag
infos[(create_date, create_time, title)]['local_from_path'] = local_from_path
infos[(create_date, create_time, title)]['local_to_dir'] = local_to_dir
infos[(create_date, create_time, title)]['url'] = url
infos[(create_date, create_time, title)]['create_date'] = create_date
infos[(create_date, create_time, title)]['create_time'] = create_time
infos[(create_date, create_time, title)]['title'] = title
return infos
def getBaseInfoOfPages(config, page_list):
'''
post文件示例
---
title:关于樂天笔记
layout:page
time:2014-04-07 18:00
---
'''
print '>>> getBaseInfoOfPosts()'
for pl in page_list:
print '\t file:', pl
infos = {}
for f_path in page_list:
print '\t', f_path
count = 0
for line in codecs.open(f_path, 'r', encoding='utf-8'):
print '\t>',line
if line.strip() == '---':
if count == 1:
print 'break'
break
if count == 0:
count += 1
print 'continue'
continue
ls = line.split(':', 1)
if ls[0].strip() == 'title':
title = ls[1].strip() #不做大写变小写的处理
print '\ttitle:',title
elif ls[0].strip() == 'layout':
layout = ls[1].strip()
print '\tlayout',layout
elif ls[0].strip() == 'time':
create_date = ls[1].strip().split()[0].strip()
create_time = ls[1].strip().split()[1].strip()
print '\tcreate_date',create_date
print '\tcreate_time',create_time
local_from_path = f_path
local_to_dir = config['app_path'] + config['site_dir']
url = '/' + title +'.html'
infos.setdefault((create_date, create_time, title),{})
infos[(create_date, create_time, title)]['layout'] = layout
infos[(create_date, create_time, title)]['local_from_path'] = local_from_path
infos[(create_date, create_time, title)]['local_to_dir'] = local_to_dir
infos[(create_date, create_time, title)]['url'] = url
infos[(create_date, create_time, title)]['create_date'] = create_date
infos[(create_date, create_time, title)]['create_time'] = create_time
infos[(create_date, create_time, title)]['title'] = title
return infos
def genSortedInfoOfPages(config, pageInfos):
''' '''
''' 对postInfos进行排序 '''
sorted_pageInfos = sorted(pageInfos.items())[::-1]
return sorted_pageInfos
def getMDContent(local_path):
''' 从文件local_path中提取出markdown的内容 '''
content = ''
count = 0
for line in codecs.open(local_path, 'r', encoding='utf-8'):
if line.strip() == '---' or count < 2:
if line.strip() == '---':
count += 1
else:
content = content + line
return content
def genPosts(config, postInfos, catInfos , tagInfos ,recentPostInfos, sortedPageInfos):
'''
postInfos是getBaseInfoOfPosts(config, post_list)的返回值。
'''
print '>>> genPosts'
for post in postInfos:
print post
template = getTemplate(config, postInfos[post]['layout']+'.html')
print '\tget content from ', postInfos[post]['local_from_path']
post_md_content = getMDContent(postInfos[post]['local_from_path'])
#print post_md_content
#TODO:import
post_html_content = template.render(post_content = md2html(post_md_content),
post_info=postInfos[post],
recent_post_infos = recentPostInfos,
config = config,
tag_infos = tagInfos,
cat_infos = catInfos,
sorted_page_infos = sortedPageInfos)
#print '\tpost: ', post_html_content
print 'write to: ',postInfos[post]['local_to_dir']+'/'+title2fileName(postInfos[post]['title'])+'.html'
genStaticHTML(postInfos[post]['local_to_dir'], title2fileName(postInfos[post]['title']+'.html'), post_html_content)
def genPages(config, pageInfos, catInfos , tagInfos ,recentPostInfos , sortedPageInfos):
'''
pageInfos是getBaseInfoOfPages(config, page_list)的返回值
'''
print '>>> genPages'
for page in pageInfos:
print page
template = getTemplate(config, pageInfos[page]['layout']+'.html')
print '\tget content from ', pageInfos[page]['local_from_path']
page_md_content = getMDContent(pageInfos[page]['local_from_path'])
#print page_md_content
#TODO:import
page_html_content = template.render(page_content = md2html(page_md_content),
page_info=pageInfos[page],
tag_infos = tagInfos,
config = config,
cat_infos = catInfos,
recent_post_infos = recentPostInfos,
sorted_page_infos = sortedPageInfos)
#print '\tpost: ', page_html_content
print 'write to: ',pageInfos[page]['local_to_dir']+'/'+pageInfos[page]['title']+'.html'
genStaticHTML(pageInfos[page]['local_to_dir'], pageInfos[page]['title']+'.html', page_html_content)
def genSortedInfoOfPosts(config, postInfos):
''' 对postInfos进行排序 '''
timePost = {}
for pi in postInfos:
# pi -> (u'2014-04-07', u'18:00', u'title-1')
year_month = '-'.join(pi[0].split('-')[:2])
print '\t year_month: ',year_month
timePost.setdefault(year_month,{})
timePost[year_month].setdefault(pi,{})
timePost[year_month][pi] = postInfos[pi]
for tp in timePost:
timePost[tp] = sorted(timePost[tp].items())[::-1]
sorted_timePost = sorted(timePost.items())
sorted_timePost = sorted_timePost[::-1]
print '\tsorted_timePost:'
for si in sorted_timePost: ##新文章排在前面
print '\t\t', si
##sorted_timePost=
#[
#(u'2014-04', [((u'2014-04-07', u'18:00', u'\u4eceHadoop streaming\u5230mrjob'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/\xe4\xbb\x8eHadoop Streaming\xe5\x88\xb0mrjob.md', 'layout': u'post', 'title': u'\u4eceHadoop streaming\u5230mrjob', 'url': u'http://127.0.0.1/post/2014/04/07/\u4eceHadoop-streaming\u5230mrjob.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'18:00', u'title-1'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/title-1.md', 'layout': u'post', 'title': u'title-1', 'url': u'http://127.0.0.1/post/2014/04/07/title-1.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'17:00', u'\u54c8\u54c8'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/\xe5\x93\x88\xe5\x93\x88.md', 'layout': u'post', 'title': u'\u54c8\u54c8', 'url': u'http://127.0.0.1/post/2014/04/07/\u54c8\u54c8.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'})])
#(u'2013-04', [((u'2013-04-07', u'17:00', u'22'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/22.md', 'layout': u'post', 'title': u'22', 'url': u'http://127.0.0.1/post/2013/04/07/22.html', 'cat': [u'\u7f16\u7a0b'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2013-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2013/04/07'})])
#]
##
return sorted_timePost
def genSortedCatPost(config, postInfos):
'''
'''
catPost = {}
for pi in postInfos:
cats = postInfos[pi]['cat']
for cat in cats:
catPost.setdefault(cat,{})
catPost[cat].setdefault(pi,{})
catPost[cat][pi] = postInfos[pi]
for cp in catPost:
print '\t', cp, catPost[cp]
for cp in catPost:
catPost[cp] = sorted(catPost[cp].items())[::-1]
for cp in catPost:
print '\t',cp, catPost[cp]
sorted_catPost = sorted(catPost.items())
sorted_catPost = sorted_catPost[::-1]
print '\tsorted_catPost:'
for si in sorted_catPost: ##新文章排在前面
print '\t\t', si
##[
#(u'\u7f16\u7a0b', [((u'2013-04-07', u'17:00', u'22'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/22.md', 'layout': u'post', 'title': u'22', 'url': u'http://127.0.0.1/post/2013/04/07/22.html', 'cat': [u'\u7f16\u7a0b'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2013-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2013/04/07'})])
#(u'\u4e91\u8ba1\u7b97', [((u'2014-04-07', u'18:00', u'\u4eceHadoop streaming\u5230mrjob'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/\xe4\xbb\x8eHadoop Streaming\xe5\x88\xb0mrjob.md', 'layout': u'post', 'title': u'\u4eceHadoop streaming\u5230mrjob', 'url': u'http://127.0.0.1/post/2014/04/07/\u4eceHadoop-streaming\u5230mrjob.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'18:00', u'title-1'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/title-1.md', 'layout': u'post', 'title': u'title-1', 'url': u'http://127.0.0.1/post/2014/04/07/title-1.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'17:00', u'\u54c8\u54c8'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/\xe5\x93\x88\xe5\x93\x88.md', 'layout': u'post', 'title': u'\u54c8\u54c8', 'url': u'http://127.0.0.1/post/2014/04/07/\u54c8\u54c8.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'})])
#]
return sorted_catPost
def getAbstractOfPost(post_path):
'''
'''
post_md_content = getMDContent(post_path)
if "<!--more-->" in post_md_content:
md_abstract = post_md_content.split("<!--more-->",1)[0]
return md_abstract
return post_md_content
def genSortedTagPost(config, postInfos):
'''
'''
tagPost = {}
for pi in postInfos:
tags = postInfos[pi]['tag']
for tag in tags:
tagPost.setdefault(tag,{})
tagPost[tag].setdefault(pi,{})
tagPost[tag][pi] = postInfos[pi]
for tp in tagPost:
print '\t', tp, tagPost[tp]
for tp in tagPost:
tagPost[tp] = sorted(tagPost[tp].items())[::-1]
for tp in tagPost:
print '\t', tp, tagPost[tp]
sorted_tagPost = sorted(tagPost.items())
sorted_tagPost = sorted_tagPost[::-1]
print '\tsorted_timePost:'
for si in sorted_tagPost: ##新文章排在前面
print '\t\t', si
##[
#(u'java', [((u'2014-04-07', u'18:00', u'\u4eceHadoop streaming\u5230mrjob'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/\xe4\xbb\x8eHadoop Streaming\xe5\x88\xb0mrjob.md', 'layout': u'post', 'title': u'\u4eceHadoop streaming\u5230mrjob', 'url': u'http://127.0.0.1/post/2014/04/07/\u4eceHadoop-streaming\u5230mrjob.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'18:00', u'title-1'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/title-1.md', 'layout': u'post', 'title': u'title-1', 'url': u'http://127.0.0.1/post/2014/04/07/title-1.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'17:00', u'\u54c8\u54c8'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/\xe5\x93\x88\xe5\x93\x88.md', 'layout': u'post', 'title': u'\u54c8\u54c8', 'url': u'http://127.0.0.1/post/2014/04/07/\u54c8\u54c8.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2013-04-07', u'17:00', u'22'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/22.md', 'layout': u'post', 'title': u'22', 'url': u'http://127.0.0.1/post/2013/04/07/22.html', 'cat': [u'\u7f16\u7a0b'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2013-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2013/04/07'})])
#(u'hadoop', [((u'2014-04-07', u'18:00', u'\u4eceHadoop streaming\u5230mrjob'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/\xe4\xbb\x8eHadoop Streaming\xe5\x88\xb0mrjob.md', 'layout': u'post', 'title': u'\u4eceHadoop streaming\u5230mrjob', 'url': u'http://127.0.0.1/post/2014/04/07/\u4eceHadoop-streaming\u5230mrjob.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'18:00', u'title-1'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/2013/title-1.md', 'layout': u'post', 'title': u'title-1', 'url': u'http://127.0.0.1/post/2014/04/07/title-1.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'18:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2014-04-07', u'17:00', u'\u54c8\u54c8'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/\xe5\x93\x88\xe5\x93\x88.md', 'layout': u'post', 'title': u'\u54c8\u54c8', 'url': u'http://127.0.0.1/post/2014/04/07/\u54c8\u54c8.html', 'cat': [u'\u4e91\u8ba1\u7b97'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2014-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2014/04/07'}), ((u'2013-04-07', u'17:00', u'22'), {'local_from_path': '/home/sunlt/Code/eclipse-python/pyJekyll/_posts/22.md', 'layout': u'post', 'title': u'22', 'url': u'http://127.0.0.1/post/2013/04/07/22.html', 'cat': [u'\u7f16\u7a0b'], 'tag': [u'java', u'hadoop'], 'create_time': u'17:00', 'create_date': u'2013-04-07', 'local_to_dir': u'/home/sunlt/Code/eclipse-python/pyJekyll/_site/post/2013/04/07'})])
#]
return sorted_tagPost
def getRecentPost(config, postInfos, post_top_num):
''' '''
count = 0
result = []
infos = {}
for pi in postInfos:
infos[pi] = postInfos[pi].copy()
infos[pi]['abstract'] = md2html(getAbstractOfPost(infos[pi]['local_from_path']))
sorted_infos = sorted(infos.items())
print '\tsorted_infos:'
for si in sorted_infos[::-1]: ##新文章排在前面
print '\t\t', si
sorted_infos = sorted_infos[::-1]
for si in sorted_infos:
count += 1
if count >= post_top_num:
break
result.append(si)
return result
def genIndexOfSite(config, recentPostInfos, catInfos, tagInfos, layout, sortedPageInfos):
'''
生成site的首页
'''
print '>>>', 'genIndexOfSite'
template = getTemplate(config, layout)
index_html_content = template.render(recent_post_infos = recentPostInfos,
config = config,
cat_infos = catInfos,
tag_infos = tagInfos,
sorted_page_infos = sortedPageInfos)
print config['app_path']+config['site_dir']+'/index.html'
genStaticHTML(config['app_path']+config['site_dir'], 'index.html', index_html_content)
def genCatIndex(config, sortedCatPost, tagInfos, catInfos, layout, recentPostInfos, sortedPageInfos):
'''_site/category/ '''
template = getTemplate(config, layout)
sorted_catPost = sortedCatPost
cat_index_html_content = template.render(config = config,
cat_posts = sorted_catPost,
cat_infos = catInfos,
tag_infos = tagInfos,
recent_post_infos = recentPostInfos,
sorted_page_infos = sortedPageInfos)
print config['app_path']+config['site_category_dir'] + '/index.html'
genStaticHTML(config['app_path']+config['site_dir']+config['site_category_dir'], 'index.html', cat_index_html_content)
def genTagIndex(config, sortedTagPost, tagInfos, catInfos, layout,recentPostInfos, sortedPageInfos):
'''_site/tag/ '''
template = getTemplate(config, layout)
sorted_tagPost = sortedTagPost
tag_index_html_content = template.render(config = config,
tag_posts = sorted_tagPost,
cat_infos = catInfos,
tag_infos = tagInfos,
recent_post_infos = recentPostInfos,
sorted_page_infos = sortedPageInfos)
print config['app_path']+config['site_tag_dir'] + '/index.html'
genStaticHTML(config['app_path']+config['site_dir']+config['site_tag_dir'], 'index.html', tag_index_html_content)
def getTagInfos(config, sortedTagPost):
'''
'name':
{
'anchor': 'tag-1'
'url'
'post_num'
'font_size':8pt
}
'''
def get_font_size(post_num):
if post_num < 5:
return '14pt'
elif post_num < 10:
return '16pt'
elif post_num < 15:
return '17pt'
elif post_num < 20:
return '18pt'
elif post_num < 25:
return '20pt'
else:
return '22pt'
tagInfos = {}
for st in sortedTagPost:
tagInfos.setdefault(st[0], {})
tag_name_no_apace = title2fileName(st[0])
tagInfos[st[0]]['anchor'] = tag_name_no_apace
tagInfos[st[0]]['url'] = config['site_tag_dir']+'/index.html#'+tag_name_no_apace
tagInfos[st[0]]['post_num'] = len(st[1])
tagInfos[st[0]]['font_size'] = get_font_size(len(st[1]))
return tagInfos
def getCatInfos(config, sortedCatPost):
'''
{
'name'
'anchor': 'tag-1'
'url'
'post_num'
'font-size':8pt
}
'''
def get_font_size(post_num):
if post_num < 5:
return '14pt'
elif post_num < 10:
return '16pt'
elif post_num < 15:
return '17pt'
elif post_num < 20:
return '18pt'
elif post_num < 25:
return '20pt'
else:
return '22pt'
catInfos = {}
for sc in sortedCatPost:
catInfos.setdefault(sc[0], {})
cat_name_no_apace = title2fileName(sc[0])
catInfos[sc[0]]['anchor'] = cat_name_no_apace
catInfos[sc[0]]['url'] = config['site_category_dir']+'/index.html#'+cat_name_no_apace
catInfos[sc[0]]['post_num'] = len(sc[1])
catInfos[sc[0]]['font_size'] = get_font_size(len(sc[1]))
return catInfos
def genPostIndex(config, sortedPostInfos, tagInfos, catInfos, layout, recentPostInfos, sortedPageInfos):
''' _site/post/index.html,按日期分类'''
template = getTemplate(config, layout)
post_index_html_content = template.render(config = config,
time_posts = sortedPostInfos,
tag_infos = tagInfos,
cat_infos = catInfos,
recent_post_infos = recentPostInfos,
sorted_page_infos = sortedPageInfos)
print config['app_path']+config['site_post_dir'] + '/index.html'
genStaticHTML(config['app_path']+config['site_dir']+config['site_post_dir'], 'index.html', post_index_html_content)
def genErrorPage(config, sortedPostInfos, tagInfos, catInfos, layout, recentPostInfos, sortedPageInfos):
'''
生成_site/error.html
'''
template = getTemplate(config, layout)
error_html_content = template.render(config = config,
time_posts = sortedPostInfos,
tag_infos = tagInfos,
cat_infos = catInfos,
recent_post_infos = recentPostInfos,
sorted_page_infos = sortedPageInfos )
print config['app_path']+config['site_dir']+ '/error.html'
genStaticHTML(config['app_path']+config['site_dir'], 'error.html', error_html_content)
def copyExtrasToSite(config):
''' '''
from_dir = config['app_path'] + config['extra_dir']
to_dir = config['app_path'] + config['site_dir']
for root, dirs, files in os.walk(from_dir):
print root, dirs, files
for f in files:
print f
shutil.copy(root+'/'+f, to_dir)
if __name__ == '__main__':
cleanSite(CONFIG)
page_infos = getBaseInfoOfPages(CONFIG, getPageList(CONFIG))
sorted_pageInfos = genSortedInfoOfPages(config=CONFIG, pageInfos=page_infos)
post_infos = getBaseInfoOfPosts(CONFIG, getPostList(CONFIG))
recent_post_infos = getRecentPost(config=CONFIG, postInfos=post_infos, post_top_num=16)
sorted_catPost = genSortedCatPost(CONFIG, post_infos)
sorted_tagPost = genSortedTagPost(CONFIG, post_infos)
sorted_postInfos = genSortedInfoOfPosts(config=CONFIG, postInfos=post_infos)
cat_infos = getCatInfos(CONFIG, sorted_catPost)
tag_infos = getTagInfos(CONFIG, sorted_tagPost)
for pi in post_infos:
print pi, post_infos[pi]
genPosts(config = CONFIG, postInfos = post_infos,
catInfos = cat_infos, tagInfos = tag_infos,
recentPostInfos=recent_post_infos,
sortedPageInfos=sorted_pageInfos)
genPages(config = CONFIG, pageInfos = page_infos,
catInfos = cat_infos, tagInfos = tag_infos,
recentPostInfos=recent_post_infos,
sortedPageInfos=sorted_pageInfos)
genIndexOfSite(config=CONFIG, recentPostInfos=recent_post_infos,
catInfos=cat_infos, tagInfos=tag_infos, layout='index.html',
sortedPageInfos=sorted_pageInfos)
genPostIndex(config=CONFIG, sortedPostInfos=sorted_postInfos,
tagInfos = tag_infos, catInfos = cat_infos, layout = 'post_index.html',
recentPostInfos=recent_post_infos,sortedPageInfos=sorted_pageInfos)
genCatIndex(config=CONFIG, sortedCatPost = sorted_catPost,
tagInfos = tag_infos, catInfos = cat_infos, layout='cat_index.html',
recentPostInfos=recent_post_infos,sortedPageInfos=sorted_pageInfos)
genTagIndex(config=CONFIG, sortedTagPost = sorted_tagPost,
tagInfos = tag_infos, catInfos = cat_infos, layout = 'tag_index.html',
recentPostInfos=recent_post_infos,sortedPageInfos=sorted_pageInfos)
genErrorPage(config=CONFIG, sortedPostInfos=sorted_postInfos, tagInfos=tag_infos,
catInfos=cat_infos, layout='error.html', recentPostInfos=recent_post_infos,
sortedPageInfos=sorted_pageInfos)
copyExtrasToSite(config=CONFIG)