-
Notifications
You must be signed in to change notification settings - Fork 0
/
build3.py
executable file
·184 lines (144 loc) · 4.34 KB
/
build3.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
## -*- coding: utf-8 -*-
#!/usr/bin/python3
from mako.lookup import Template,TemplateLookup
from sh import cp,rm,pwd,mv,mkdir,uglifyjs,cat,glob,cssoptimizer
import markdown
import logging
import os,sys
import datetime
#Settings
WEBSITE_TITLE = "Ubuntu Faq"
DEFAULT_TAGS = "ubuntu"
YANDEX_SEARCH_ID = ""
SOURCE = "/home/graf/projects/web/ubuntu_bible/content/ubuntu_faq.md"
logging.root.setLevel(logging.DEBUG)
LINKS_ON_PAGE = 20
PAGE_PATTERN = "From%dto%d"
VERSION = 0.2
if (len(sys.argv)>1):
BASE_URL = sys.argv[1]
if BASE_URL[-1]!="/":
BASE_URL+="/"
if len(sys.argv)>2:
YANDEX_SEARCH_ID=sys.argv[2]
else:
BASE_URL = "file://"+str(pwd()).replace("\n","")+"/_site/"
#Code
def materialize_template(template_name,fname,env):
os.chdir("_templates")
env['base_url']=BASE_URL
env['website_title']=WEBSITE_TITLE
env['default_tags']=DEFAULT_TAGS
env['current_url']=BASE_URL+fname+".html"
env['version']=VERSION
env['yandex_search_id']=YANDEX_SEARCH_ID
mylookup = TemplateLookup(directories=['.'],input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace')
result = Template(filename=template_name+".mako",
lookup=mylookup,
default_filters=['decode.utf8']).render(**env)
os.chdir("..")
if fname.count(".")==0:
fname+=".html"
f = open("_site/"+fname,'w')
f.write(result)
f.close()
def valid_fname(fname):
return fname.replace("/","-").replace(".","-").replace("\n","")
def link(title,fname):
link={}
link["title"]=title
link["href"]=url(fname)
return link
def url(fname):
return BASE_URL+valid_fname(fname)+".html"
def bake_note(title, tags, content):
env={
"title": title,
"tags": tags,
"content": markdown.markdown(content)
}
materialize_template("note",valid_fname(title),env)
def bake_toc(headers):
chunks=[headers[x:x+LINKS_ON_PAGE] for x in range(0, len(headers), LINKS_ON_PAGE)]
pages=[]
n=0
for chunk in chunks:
pages.append(link("%d-%d" % (n+1,n+len(chunk)),PAGE_PATTERN % (n,n+LINKS_ON_PAGE)))
n+=LINKS_ON_PAGE
n=0
for chunk in chunks:
links=[]
for header in chunk:
links.append(link(header,header))
env={
"title": "Заметки %d-%d" % (n+1,n+len(chunk)),
"links": links,
"pages": pages,
"start": str(n+1)
}
materialize_template("all",PAGE_PATTERN % (n,n+LINKS_ON_PAGE),env)
n+=LINKS_ON_PAGE
def bake_sitemap(headers):
links = []
for header in headers:
links.append(url(header))
env = {
"date": str(datetime.date.today()),
"links": links
}
materialize_template("sitemap","sitemap.xml",env)
def materialize_notes(source):
raw=open(source, 'r', encoding='utf-8').readlines()
post_buffer=""
tags=""
previous_line=""
previous_title=""
headers=[]
i=0
l=0
for line in raw:
l+=1
#add additional line before code to match markdown parser
if line[0:4]==" " and previous_line[1:4]!=" " and previous_line[0]!="#":
line = "\n" + line
if line[0:2]=="##" and line[2]!="#":
if l!=1:
i+=1
bake_note(previous_title,tags,post_buffer)
if i==1:
bake_note("index",tags,post_buffer)
post_buffer=""
tags=""
previous_title=line[2:]
headers.append(line[2:])
elif l==len(raw):
post_buffer+=line
bake_note(previous_title,tags,post_buffer)
elif line[0:4]=="!tag":
tags=line[6:].replace("\n","")
else:
post_buffer+=line
previous_line=line
bake_toc(headers)
bake_sitemap(headers)
def main():
logging.debug('start')
if YANDEX_SEARCH_ID=="":
logging.warn('to enable seach on your site run\n python3 build3.py "http://website.url/" 123\n where 123 is yandex search id obtainable on http://site.yandex.ru/searches/new/')
#create and clear output directory if necessary
mkdir("-p","_site/")
rm("-Rf",glob("_site/*"))
#copy static contant
cp("-a",glob("_web/*"),"_site/")
mv("_site/dot_htaccess","_site/.htaccess")
#copy optimized css
cssoptimizer(cat(glob("_css/*")),"-i","_site/style.css")
#copy optimized js
uglifyjs(cat(glob("_js/*")),"-o","_site/scripts.js")
#generate content
materialize_notes(SOURCE)
materialize_template("Y_Search","Y_Search",{"title":"Поиск"})
logging.debug('end.')
logging.info('To start copy following url into your browser: \n%sindex.html' % BASE_URL)
if __name__ == "__main__":
main()