forked from bobuk/addmeto.cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b3
executable file
·142 lines (121 loc) · 4.47 KB
/
b3
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
#!v/bin/python
# -*- mode: python -*-
import os, sys, shutil, gzip
from datetime import datetime
import markdown, pyatom
import subprocess
TEMPLATE = 'templates/index.html'
def source_constructor(filename):
if not filename.endswith('.md'):
filename = filename + '.md'
return os.path.join('pages', filename)
def source_deconstructor(filename):
if filename.endswith('.md'):
filename = filename[:-3]
if filename.startswith('pages/'):
filename = filename[len('pages/'):]
return filename
def get_git_time(filename):
fluite = "git log --format='format:%%at' '%s'"
res = subprocess.check_output(fluite % filename, shell=True).decode()
res = res.split('\n')[0]
return int(res)
def dest_constructor(filename):
return os.path.join('results', source_deconstructor(filename), 'index.html')
def get_source_list():
sources = []
for fl in os.listdir('pages'):
if fl.endswith('.md') and fl not in ['archive.md', 'about.md']:
sources.append(os.path.join('pages', fl))
return sorted(sources)
def get_updated_list(sources = get_source_list()):
rebuild = []
template_ts = get_git_time(TEMPLATE)
for fl in sources:
dest = dest_constructor(fl)
if not os.path.isfile(dest) or \
os.path.getmtime(dest) < max(get_git_time(fl), template_ts):
rebuild.append( fl )
return rebuild
def load_post(filename):
date = source_deconstructor(filename)
if len(date) > len('0000-00-00'):
year, month, day, subtitle = date.split('-', 3)
date = year + '-' + month + '-' + day
data = open(filename, 'r').read()
title, data = data.split('\n', 1)
if title.startswith('#'):
junk, title = title.split(' ', 1)
return {
'title': title.strip(),
'file': source_deconstructor(filename),
'date': date,
'text': markify(data.strip())
}
class MarkExtension(markdown.extensions.Extension):
MARK_RE = r"(\~\~)(.+?)(\~\~)"
def extendMarkdown(self, md, md_globals):
md.inlinePatterns.add('mark', markdown.inlinepatterns.SimpleTagPattern(self.MARK_RE, 'mark'), '<not_strong')
def markify(text):
return markdown.markdown(text, [MarkExtension(), 'footnotes', 'fenced_code', 'codehilite', 'tables', 'extra', 'nl2br'])
def template(filename):
body = open(TEMPLATE, 'r').read()
dv = load_post(filename)
for key in dv:
body = body.replace('@' + key + '@', dv[key])
return body
def write(src):
dest = dest_constructor(src)
run = template(src)
if not os.path.isdir(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
with open(dest, 'w') as fl:
fl.write(run)
def pages():
write('pages/about.md')
def archive(sources):
res = ['# Архив', '', '']
for post in reversed(sources):
p = load_post(post)
res.append('* [{title}](/{file}) ({date})'.format_map(p))
with open('pages/archive.md', 'w') as fl:
fl.write('\n'.join(res))
write('pages/archive.md')
def rss(sources):
feed = pyatom.AtomFeed(title="Еще один линкблог",
feed_url="http://addmeto.cc/feed",
url="http://addmeto.cc",
author="Григорий Бакунов")
for post in list(reversed(sources))[:10]:
p = load_post(post)
p['updated'] = datetime.fromtimestamp(get_git_time(post))
feed.add(title=p['title'],
content=p['text'],
content_type="html",
author='Бобук',
url="http://addmeto.cc/" + p["file"],
updated=p["updated"]
)
with open('results/feed', 'wb') as fl:
fl.write(gzip.compress(bytes(feed.to_string(), 'utf-8')))
def main(args):
sources = get_source_list()
updated = get_updated_list(sources)
last = sources[-1]
for sfrom in updated:
sys.stdout.write('>> ' + sfrom +' -> ' + dest_constructor(sfrom) + ' \n')
write(sfrom)
if last in updated:
sys.stdout.write('>> Index page updated to ' + last + '\n')
shutil.copy2(dest_constructor(last), 'results/index.html')
if (len(updated) > 1 and updated[0] != last) or 'archive' in args:
archive(sources)
sys.stdout.write('>> Archive updated\n')
if len(updated) > 0 or 'rss' in args:
rss(sources)
sys.stdout.write('>> RSS updated\n')
if 'pages' in args:
pages()
sys.stdout.write('>> Pages updated\n')
if __name__ == '__main__':
main(sys.argv[1:])