forked from JetBrains/kotlin-web-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kotlin-website.py
220 lines (165 loc) · 6.11 KB
/
kotlin-website.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
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
import datetime
import json
import os
import sys
from os import path
import yaml
from flask import Flask, render_template, Response, send_from_directory
from flask.helpers import send_file
from flask_frozen import Freezer, walk_directory
from src.Feature import Feature
from src.MyFlatPages import MyFlatPages
from src.Navigaton import Nav
from src.encoder import DateAwareEncoder
from src.grammar import get_grammar
from src.markdown.makrdown import jinja_aware_markdown
from src.pdf import generate_pdf
from src.sitemap import generate_sitemap
app = Flask(__name__, static_folder='_assets')
app.config.from_pyfile('mysettings.py')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
pages = MyFlatPages(app)
freezer = Freezer(app)
ignore_stdlib = "--ignore-stdlib" in sys.argv
root_folder = path.join(os.path.dirname(__file__))
data_folder = path.join(os.path.dirname(__file__), "data")
def get_site_data():
data = {}
for data_file in os.listdir(data_folder):
if data_file.startswith('_'):
continue
data_file_path = path.join(data_folder, data_file)
with open(data_file_path) as stream:
try:
file_name_without_extension = data_file[:-4] if data_file.endswith(".yml") else data_file
data[file_name_without_extension] = yaml.load(stream)
except yaml.YAMLError as exc:
print 'Cant parse data file ' + data_file
print exc.message
except IOError as exc:
print 'Cant open data file ' + data_file
print exc.message
return data
site_data = get_site_data()
def get_nav():
with open(path.join(data_folder, "_nav.yml")) as stream:
nav = Nav(yaml.load(stream))
return nav
nav = get_nav()
def get_kotlin_features():
features_dir = path.join(os.path.dirname(__file__), "kotlin-features")
features = []
for feature_meta in yaml.load(open(path.join(features_dir, "kotlin-features.yml"))):
file_path = path.join(features_dir, feature_meta['content_file'])
with open(file_path) as f:
content = f.read().decode('utf-8')
content = content.replace("\r\n", "\n")
if file_path.endswith(".md"):
content = jinja_aware_markdown(content, pages)
features.append(Feature(content, feature_meta))
return features
@app.context_processor
def add_year_to_context():
return {
'year': datetime.datetime.now().year
}
@app.context_processor
def add_data_to_context():
return {
'nav': nav,
'data': site_data,
'site': {
'pdf_url': app.config['PDF_URL'],
'forum_url': app.config['FORUM_URL'],
'site_github_url': app.config['SITE_GITHUB_URL'],
'data': site_data,
'text_using_gradle': app.config['TEXT_USING_GRADLE']
}
}
@app.route('/data/events.json')
def get_events():
return Response(json.dumps(site_data['events'], cls=DateAwareEncoder), mimetype='application/json')
@app.route('/data/videos.json')
def get_videos():
return Response(json.dumps(site_data['videos'], cls=DateAwareEncoder), mimetype='application/json')
@app.route('/docs/reference/grammar.html')
def grammar():
grammar = get_grammar()
if grammar is None:
return "Grammar file not found", 404
return render_template('pages/grammar.html', kotlinGrammar=grammar)
@app.route('/docs/videos.html')
def videos_page():
return render_template('pages/videos.html')
@app.route('/docs/kotlin-docs.pdf')
def pdf():
return send_file(generate_pdf(pages, nav['reference']))
@app.route('/docs/resources.html')
def resources():
return render_template('resources.html')
@app.route('/search.html')
def search_page():
return render_template('pages/search.html')
@app.route('/')
def index_page():
features = get_kotlin_features()
return render_template('pages/index.html',
is_index_page=True,
features=features
)
def process_page(page_path):
page = pages.get_or_404(page_path)
if 'date' in page.meta and page['date'] is not None:
page.meta['formatted_date'] = page.meta['date'].strftime('%d %B %Y')
if page.meta['formatted_date'].startswith('0'):
page.meta['formatted_date'] = page.meta['formatted_date'][1:]
edit_on_github_url = app.config['EDIT_ON_GITHUB_URL'] + app.config['FLATPAGES_ROOT'] + "/" + page_path + app.config[
'FLATPAGES_EXTENSION']
template = page.meta["layout"] if 'layout' in page.meta else 'default.html'
if not template.endswith(".html"):
template += ".html"
return render_template(
template,
page=page,
baseurl="",
edit_on_github_url=edit_on_github_url,
)
@freezer.register_generator
def page():
for page in pages:
if ignore_stdlib and page.path.startswith("api"):
continue
yield {'page_path': page.path}
@app.route('/<path:page_path>.html')
def page(page_path):
return process_page(page_path)
@app.route('/assets/<path:path>')
def asset(path):
return send_from_directory('assets', path)
@app.route('/assets/images/tutorials/<path:filename>')
def tutorial_img(filename):
return send_from_directory(path.join('assets', 'images', 'tutorials'), filename)
@freezer.register_generator
def asset():
for filename in walk_directory(path.join(root_folder, "assets")):
yield {'path': filename}
@app.route('/<path:page_path>')
def get_index_page(page_path):
"""
Handle requests which urls don't end with '.html' (for example, '/doc/')
We don't need any generator here, because such urls are equivalent to the same urls
with 'index.html' at the end.
:param page_path: str
:return: str
"""
if not page_path.endswith('/'):
page_path += '/'
return process_page(page_path + 'index')
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "build":
urls = freezer.freeze()
if not ignore_stdlib:
generate_sitemap(urls)
else:
app.run(host="0.0.0.0", debug=False, threaded=True)