-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
231 lines (196 loc) · 5.44 KB
/
app.py
File metadata and controls
231 lines (196 loc) · 5.44 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
#!/usr/bin/python
import os, sys
from flask import (
abort,
Flask,
render_template,
url_for,
render_template_string
)
from flask_flatpages import (
FlatPages,
pygmented_markdown,
pygments_style_defs
)
from flask_frozen import Freezer
import yaml
from markdown import markdown
CONFIG = 'configs'
PAGE = 'pages'
POST = 'posts'
STATIC = 'static'
TEMPLATE = 'templates'
FLATPAGES_AUTO_RELOAD = True
FLATPAGES_EXTENSION = '.md'
FLATPAGES_ROOT = '.'
FLATPAGES_MARKDOWN_EXTENSIONS = [
'attr_list', # {: #someid .someclass somekey='some value' }
'def_list', # Apple
# : Pomaceous fruit of plants of the genus
# Malus in the family Rosaceae.
'codehilite', # syntax highlight
'fenced_code', # github style syntax highlighting
'smarty',
'tables', # Table
]
app = Flask(__name__)
pages = FlatPages(app)
freezer = Freezer(app)
app.config.from_object(__name__)
def hexnumber():
"""
hasilkan angka hexadesimal 0 - 255
dengan format: 0x00 s/d 0xFF
"""
st = ''
res = []
for x in range(256):
if x < 0x10:
st = '0{}'.format(str(hex(x))[-1])
else:
st = '{}'.format(hex(x))[2:]
res.append('{}{}'.format('0x', st.upper()))
return res
def get_config(filename = 'meta.yaml'):
filename = os.path.join(CONFIG, filename)
if not os.path.exists(filename):
return
with open(filename) as fd:
return yaml.load(fd.read(), Loader=yaml.Loader)
# override @app.route("/<tut>/<cab>/<item>")
@app.route('/static/<path:dirname>/<path:filename>')
def statis(dirname, filename):
statik_file = '{}/{}'.format(dirname, filename)
return app.send_static_file(statik_file)
@app.route('/')
def home():
"""
Tampilkan daftar tutorial
dengan maksimal 3 cabangnya
"""
template = 'index.html'
site_config = get_config()
tutorial = {}
for conf in site_config:
if conf in ["title", "desc", "baseurl"]:
continue
# konfigurasi cabang
tut_list = site_config[conf]
if not tut_list['rilis']:
continue
tutorial[conf] = tut_list
tutorial[conf]['konten'] = get_config("{}.yaml".format(conf))
ref = get_config(filename='ref-code.yaml')
return render_template(template,
ref=ref,
site=site_config,
tutorial=tutorial)
@app.route('/tentang/')
def tentang():
template = "_base.html"
path = '{}/{}'.format(PAGE, "tentang")
page = pages.get_or_404(path)
return render_template(template,
site = get_config(),
page=page)
@app.route("/404.html")
def not_found():
template = "_base.html"
path = '{}/{}'.format(PAGE, "404")
page = pages.get_or_404(path)
site_config = get_config()
return render_template(template,
site = site_config,
page = page)
@app.route("/<item>/")
def tutorial(item):
template = "tutorial.html"
site_config = get_config()
konfigurasi_tutorial = get_config(
filename="{}.yaml".format(item))
if not konfigurasi_tutorial:
abort(404)
return render_template(template,
site = site_config,
konfigurasi_meta = site_config[item],
nama_tutorial = item,
konfigurasi_tutorial = konfigurasi_tutorial )
#@app.route("/<tut>/<item>")
@app.route("/<tut>/<item>/")
def cabang(tut, item):
template = "cabang.html"
site_config = get_config()
konf_cabang = get_config(filename="{}/{}.yaml".format(tut, item))
if not konf_cabang:
abort(404)
path = '{}/{}/{}'.format(PAGE, tut, item)
page = pages.get_or_404(path)
daftar_bab, daftar_item = daftar_menu_cabang(
site_config, konf_cabang, tut, item)
return render_template(template,
page = page,
site = site_config,
konfigurasi_meta = site_config[tut],
konfigurasi_tutorial = konf_cabang,
daftar_bab = daftar_bab,
daftar_item = daftar_item,
nama_tutorial = tut,
nama_cabang = item,
nama_item = item,
#url_item = url_for('item_cabang', tut=tut, cab=item, item=item)
)
#@app.route("/<tut>/<cab>/<item>")
@app.route("/<tut>/<cab>/<item>/")
def item_cabang(tut, cab, item):
template = "cabang.html"
site_config = get_config()
konf_cabang = get_config(filename="{}/{}.yaml".format(tut, cab))
if not konf_cabang:
abort(404)
path = '{}/{}/{}/{}'.format(PAGE, tut, cab, item)
page = pages.get_or_404(path)
daftar_bab, daftar_item = daftar_menu_cabang(
site_config, konf_cabang, tut, cab)
return render_template(template,
page = page,
site = site_config,
konfigurasi_meta = site_config[tut],
konfigurasi_tutorial = konf_cabang,
daftar_bab = daftar_bab,
daftar_item = daftar_item,
nama_tutorial = tut,
nama_cabang = cab,
nama_item = item,
url_item = url_for('item_cabang', tut=tut, cab=cab, item=item)
)
def daftar_menu_cabang(konf_site, konf_cabang, tutorial, cabang):
"""
return (bab: [daftar item dari cabang]) # link ke hex
"""
konf_daftar_bab = konf_cabang['bab']
daftar_bab = {}
daftar_item = {}
# Supaya gampang diurutkan di kode html
for bab in konf_daftar_bab:
daftar_bab[konf_daftar_bab[bab]['nomor']] = bab
daftar_item[bab] = []
for laman in pages:
path = '{}/{}/{}'.format(PAGE, tutorial, cabang)
if not laman.path.startswith(path) \
or laman.path == path: # != <cabang>.md
continue
if 'bab' not in laman.meta:
continue
elif laman.meta.get('bab') not in konf_daftar_bab:
sys.stderr.write("[!] PERINGATAN: bab tidak terdaftar\n")
sys.stderr.write(" bab : {}".format(laman.meta['bab']))
sys.stderr.write(" file: {}".format(laman.path))
continue
daftar_item[laman.meta['bab']].append(laman)
return daftar_bab, daftar_item
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
#app.run(host='0.0.0.0', debug=True)
app.run(host='127.0.0.1', debug=True)