This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
/
shot.py
125 lines (113 loc) · 3.96 KB
/
shot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
import os, getopt
import sys
from shutil import move, copyfile
from collections import OrderedDict
from pyatom import AtomFeed
from datetime import datetime
from libs.tools import *
from libs.mdown import Convert
from libs.templ import Render
from libs.dump import Dump
config = load_config()
md = Convert()
tpl = Render(config['templates']['folder'])
def work(the_file, dmp = None):
result = md.go(the_file)
if result == None:
print("File %s has wrong head." % the_file)
return None
else:
result['site'] = config['site']
result['author'] = config['author']
result['feed'] = config['feed']
result['disqus'] = config['disqus']
content = tpl.render(config['templates']['article'], result)
if dmp != None:
save(os.path.join(config['output'], result['date'] + '.html'), content)
dmp.archive[result['date'] + '.html'] = result
return content
def archive_page(dmp):
result = {}
result['site'] = config['site']
result['author'] = config['author']
result['feed'] = config['feed']
result['disqus'] = config['disqus']
result['content'] = OrderedDict(sorted(dmp.archive.items(), reverse=True))
content = tpl.render(config['templates']['archive'], result)
save(os.path.join(config['output'], 'archive.html'), content)
idx = os.path.join(config['output'], result['content'].items()[0][0])
copyfile(idx, os.path.join(config['output'], 'index.html'))
def static_page():
flist = files(config['nav_pages'])
for the_file in flist:
content = work(the_file)
if content:
save(os.path.join(config['output'],\
os.path.basename(the_file)[:-3] + '.html'), content)
def rss(dmp):
url = config['site'][:-1] if config['site'].endswith('/') else config['site']
feed = AtomFeed(title=config['title'],
feed_url=url + "/feed",
url=url,
author=config['author'])
feed_dict = OrderedDict(sorted(dmp.archive.items(), reverse=True))
#current_date = datetime.now().strftime('%Y-%m-%d')
try:
for x in xrange(10):
feed.add(title=feed_dict.items()[x][1]['title'],
content=feed_dict.items()[x][1]['content'],
content_type="html",
author=config['author'],
url=url + feed_dict.items()[x][0],
updated=datetime.strptime(feed_dict.items()[x][1]['date'], '%Y-%m-%d')
)
except IndexError:
pass
save(os.path.join(config['output'], config['feed']), feed.to_string())
def regen_mode(): # режим полной перегенирации файлов
clear(config['output'])
save('dump.db', '{}')
dmp = Dump()
flist = files(config['source'])
for the_file in flist:
work(the_file, dmp)
archive_page(dmp)
rss(dmp)
dmp.sync()
static_page()
def draft_mode(): # Режим генерации из черновиков
dmp = Dump()
flist = files(config['draft'])
for the_file in flist:
if work(the_file, dmp):
move(the_file, config['source'])
archive_page(dmp)
rss(dmp)
dmp.sync()
def main(args):
try:
if not args:
raise getopt.GetoptError('Error')
opt, arg = getopt.getopt(args, 'hirds', ['help', 'init', 'regen', 'draft', 'server'])
except getopt.GetoptError, e:
usage()
if arg:
usage()
for o, a in opt:
if o in ('-h', '--help'):
usage()
elif o in ('-i', '--init'):
initial(**config)
elif o in ('-r', '--regen'):
regen_mode()
elif o in ('-d', '--draft'):
draft_mode()
elif o in ('-s', '--server'):
server(config['output'], config['port'])
else:
usage()
if __name__ == '__main__':
main(sys.argv[1:])