-
Notifications
You must be signed in to change notification settings - Fork 1
/
rhweb2.py
executable file
·222 lines (178 loc) · 6.5 KB
/
rhweb2.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
221
222
#!/usr/bin/python3
import os
from flask import Blueprint, Flask, render_template, render_template_string, request, flash, redirect, session, abort, url_for, make_response, g, send_from_directory
from dokuwiki import DokuWiki, DokuWikiError
from bs4 import BeautifulSoup
import rhforum
app = Flask('rhweb2')
app_dir = os.path.dirname(os.path.abspath(__file__))
app.config.from_pyfile(app_dir+"/config.py") # XXX
DOKUUSER = "rhweb"
DOKUPASS = open(app_dir+'/DOKUPASS').read().strip()
DOKUURL = "https://retroherna.org/wiki"
wiki = DokuWiki(DOKUURL, DOKUUSER, DOKUPASS)
rhweb = Blueprint('rhweb', __name__, template_folder='templates', static_folder='static')
def wikipage(name, force=False):
name = name.replace("/", ":")
if not force and not g.purge:
try:
page = open(app_dir+"/cache/"+name+".html").read()
g.caching_comment += "{} read from cache\n".format(name)
return page
except Exception as ex:
g.caching_comment += "{} cache open fail: {}\n".format(name, ex)
page = None
for i in range(3):
try:
page = wiki.pages.html(name)
break
except Exception as ex:
g.caching_comment += "{} get wiki page fail {}: {}\n".format(name, i, ex)
if page:
g.caching_comment += "{} got fresh wiki page\n".format(name)
if not page and force:
try:
return open(app_dir+"/cache/"+page+".html").read().decode('utf-8')
except Exception as ex:
g.caching_comment += "cache open fail w force: {}\n".format(ex)
if not page:
return None
open(app_dir+"/cache/"+name+".html", "w").write(page)
return page
def transform_wikipage(page):
page = page.replace("~CLEAR~", '<div style="clear: both;"></div>')
page = BeautifulSoup(page, "lxml")
for a in page.find_all('a'):
if a.get('href') and "/wiki/doku.php" in a['href']:
a['href'] = a['href'].replace("/wiki/doku.php?id=web:", "/").replace(':', '/')
for img in page.find_all('img'):
if not img['src'].startswith("http"):
img['src'] = img['src'].replace("/wiki/lib/exe/fetch.php", "https://retroherna.org/wiki/lib/exe/fetch.php")
title = img.get('title')
parent = img.parent
if parent.name == "a" and parent['href'].startswith("/wiki"):
parent.name = "div"
del parent['href']
else:
parent = page.new_tag("div")
img.wrap(parent)
classes = img.get('class')
parent['class'] = classes + [" mediawrap"]
if 'mediacenter' in classes and img.get('width'):
# life is too short
parent['style'] = 'width: {}px;'.format(img['width'])
del img['class']
# XXX yes this is necessary, thanks dokuwiki
if title and not any(title.endswith(t) for t in ("png", "jpg", "jpeg", "gif")):
title = page.new_tag("div")
title['class'] = "mediatitle"
if img.get('width'):
title['style'] = "max-width: {}px;".format(img['width'])
title.string = img['title']
parent.append(title)
page = page.html.body
page.name = "section"
#page = "".join(str(page))
return page
def render_wikipage(wikipage, **kwargs):
wikipage = str(transform_wikipage(wikipage))
return render_template_string(wikipage, **kwargs)
@rhweb.before_request
def before_request():
g.caching_comment = ""
g.purge = False
if 'purge' in request.args:
g.purge = True
g.caching_comment += "purging\n"
#g.banner = transform_wikipage(wikipage("web:banner"))
#g.footer = transform_wikipage(wikipage("web:footer"))
if not request.path.startswith("/static"):
g.sidebar = render_wikipage(wikipage("web2:sidebar"), force=True)
g.pagetitle = None
g.dokuwiki_url = DOKUURL
@app.context_processor
def new_template_globals():
urls = {
"facebook": "https://facebook.com/retroherna",
"youtube": "https://www.youtube.com/channel/UCJNNkhuJNO5dujOhy9r-jdA",
"twitter": "https://twitter.com/hernihistorie",
"discord": "https://discord.gg/9jajeqZ",
"instagram": "https://www.instagram.com/retroherna/",
"bankaccount": "https://transparentniucty.moneta.cz/homepage?accountNumber=8686868686",
"forum": "/forum/",
"email": "mailto:[email protected]",
"hernihistorie": "https://hernihistorie.cz/",
"wiki": DOKUURL+"/",
}
return {
'zip': zip,
'urls': urls,
}
@rhweb.route('/', defaults={'path': ''})
@rhweb.route('/<path:path>')
def page(path):
if not path: path = "index"
path = path.rstrip('/')
if ':' in path:
return redirect('/'+path.replace(':', '/'))
#if path not in DOKUPAGES: abort(404)
page = None
#if path.startswith("encyklopedie/"):
# page = wikipage(path.replace("/", ":"))
#else:
page = wikipage("web2:"+path.replace("/", ":"))
if not page: abort(404)
page = transform_wikipage(page)
h1 = page.find('h1')
h2 = page.find('h2')
h3 = page.find('h3')
if h1:
g.pagetitle = h1.string
elif h2:
g.pagetitle = h2.string
elif h3:
g.pagetitle = h3.string
elif path == "index":
g.pagetitle = None
else:
g.pagetitle = path
g.pagedescription = ""
for p in page.find_all('p'):
text = p.get_text().strip()
if text and len(text) > 30:
g.pagedescription = text
break
page = """{% extends '_base.html' %}
{% block content %}
""" + str(page) + """
{% endblock %}
"""
return render_template_string(page, path=path, page=page)
#@app.route("/o-nas")
#def o_nas():
# return redirect("/")
"""
@app.route("/")
def o_nas():
return render_template("o-nas.html")
@app.route("/kontakt")
def kontakt():
return render_template("kontakt.html")
@app.route("/komunita")
def komunita():
return render_template("komunita.html")
@app.route("/vyjezdy")
def vyjezdy():
return render_template("vyjezdy.html")
@app.route("/clanky")
def clanky():
return render_template("clanky.html")
"""
@app.route("/robots.txt")
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
app.register_blueprint(rhweb, url_prefix='')
app.register_blueprint(rhforum.rhforum, url_prefix='/forum')
if __name__ == "__main__":
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(host="", port=9011, debug=True)