-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiserver.py
229 lines (183 loc) · 7.05 KB
/
wikiserver.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
223
224
225
226
227
228
229
from flask import Flask, request, render_template, send_file, make_response, redirect
from markupsafe import Markup
from addnewline import NewLineExtension
from checkboxify import CheckboxifyExtension
from timeago import time_ago
import markdown
from markdown.extensions.toc import TocExtension
import os
app = Flask(__name__)
# !IMPORTANT! if you want to allow editing, set this to true
allow_editing = False or app.debug
enhance_editor = True
no_edit_text = "403 forbidden: editing has been disabled by the host. if this is an error, edit the configuration of the server"
not_found_text = "404 not found: the requested file has not been found"
new_file_template = "---\ntitle:\ntags:\n---\n"
md = markdown.Markdown(extensions=[TocExtension(title="Table of Contents"), 'tables', 'fenced_code', 'meta', 'sane_lists', 'def_list', NewLineExtension(), CheckboxifyExtension()])
@app.get("/favicon.ico")
def favicon():
return send_file("./static/favicon.ico")
@app.get("/")
def index():
text, toc = parseMarkdown("readme.md")
return render_template("home.html", content=Markup(text), toc=Markup(toc))
@app.get("/help.md")
def help():
text, toc = parseMarkdown("help.md")
return render_template("home.html", content=Markup(text), toc=Markup(toc))
@app.get("/sitemap/")
@app.get("/sitemap/<path:subpath>/")
def sitemap(subpath=""):
if(not os.path.exists("./text/" + subpath)):
return make_response(not_found_text, 404)
cont = buildSiteMap(subpath + ("/" if subpath != "" else ""))
path = "| " + subpath
return render_template("home.html", content=cont, path=path)
@app.get("/text/<path:subpath>")
def read(subpath):
if not os.path.exists("." + request.path):
if subpath.endswith(".md"):
return render_template("filecreate.html", path="/edit/" + subpath)
else:
return make_response(not_found_text + request.path, 404)
if not subpath.endswith(".md"):
return send_file("." + request.path)
filetext, toc = parseMarkdown("." + request.path)
return render_template("wiki.html", content=Markup(filetext), path=sitemapToCurrentFolder(subpath), toc=Markup(toc), raw=buildPathToRaw(subpath), edit=buildPathToEdit(subpath), can_edit=allow_editing)
@app.get("/raw/<path:subpath>")
def readPlain(subpath):
path = "./text/" + subpath
if(not os.path.exists(path)):
return make_response(not_found_text, 404)
with open(path, "r", encoding="utf-8") as input_file:
text = input_file.read()
response = make_response(text, 200)
response.mimetype = "text/plain"
return response
@app.get("/changes/")
def changes():
cont = buildChangesView()
return render_template("home.html", content=cont)
@app.get("/edit/<path:subpath>")
def edit(subpath):
if not allow_editing:
return make_response(no_edit_text, 403)
path = "./text/" + subpath
text = new_file_template
if(os.path.exists(path)):
with open(path, "r", encoding="utf-8") as input_file:
text = input_file.read()
return render_template("edit.html", content=text, path=sitemapToCurrentFolder(subpath), target=subpath, enhance_editor=enhance_editor)
@app.post("/edit/<path:subpath>")
def postEdit(subpath):
if not allow_editing:
return make_response(no_edit_text, 403)
path = './text/' + subpath
# folders or file don't exist, make sure they do
if(not os.path.exists(path)):
_dir = "./text/" + os.path.dirname(subpath)
_file = os.path.basename(subpath)
print(f"dir: {_dir}, file: {_file}")
os.makedirs(_dir, exist_ok=True)
open(f"{_dir}/{_file}", "x").close()
with open(path, "bw") as input_file:
input_file.write(request.form['content'].encode())
redir = "/text/" + subpath
return redirect(redir,code=301)
def parseMarkdown(filepath):
md.reset()
with open(filepath, "r", encoding="utf-8") as input_file:
text = input_file.read()
html = md.convert(text)
toc = md.toc
html = buildMetaView(md.Meta) + html
return html, toc
def buildSiteMap(subpath):
res = ['<h2>Sitemap</h2><ul>']
dirs, files = getDirsAndFiles(subpath)
if(subpath != ""):
res.append(buildParentLink(subpath))
for _dir in dirs:
res.append("<li><b><a href='/sitemap/")
res.append(subpath)
res.append(f"{_dir}'>📁{_dir}</a></b></li>")
for _file in files:
res.append("<li><a href='/text/")
res.append(subpath)
res.append(f"{_file}'>📄{_file}</a></li>")
res.append('</ul>')
return Markup(''.join(res))
def getDirsAndFiles(subpath):
"""
generate sorted lists of all directories and files on a path
"""
dirs = []
files = []
combined = "./text/" + subpath
for entry in os.listdir(combined):
path = combined + "/" + entry
if os.path.isdir(path) and not entry.startswith("."):
dirs.append(entry)
elif(os.path.isfile(path) and entry.endswith(".md")):
files.append(entry)
dirs.sort()
files.sort()
return dirs, files
def buildParentLink(subpath):
_dir = os.path.dirname(subpath + "../")
return f"<li><b><a href='/sitemap/{_dir}'>📁..</a></b></li>"
def buildPathToRaw(subpath):
link = f"<a href='/raw/{subpath}'>Raw</a>"
return Markup(link)
def buildPathToEdit(subpath):
link = f"<a href='/edit/{subpath}'>Edit</a>"
return Markup(link)
def sitemapToCurrentFolder(subpath):
_dir = os.path.dirname(subpath)
_file = os.path.basename(subpath)
link = f"<a href='/sitemap/{_dir}/'>{_dir}</a>/{_file}"
return Markup(link)
def getRecentFileChanges():
"""
get a sorted list of the recent changes to files
and how long ago the change was made
"""
files = []
mtime = []
for root, _, contains in os.walk("text\\"):
for item in contains:
if not item.endswith(".md"):
continue
_file = str(os.path.join(root,item))
files.append(_file.replace("\\", "/").replace("text/", ""))
mtime.append(os.path.getmtime(_file))
def sortf(idx):
return -mtime[idx]
def getEntry(idx):
return (str(files[idx]), time_ago(mtime[idx]))
indices = list(range(len(files)))
indices.sort(key=sortf)
indices = indices[0:20]
return list(map(getEntry, indices))
def buildChangesView():
data = getRecentFileChanges()
res = ["<h3>Recent Modifications</h3>"]
res.append("<table><tr><th>file</th><th>last changed</th></tr>")
for path, timeStr in data:
res.append(f"<tr><td><a href='/text/{path}'>{path}</a></td><td>{timeStr}</td></tr>")
res.append("</table>")
return Markup(''.join(res))
def buildMetaView(data: dict):
tags = []
# split and filter tags
if('tags' in data):
split = data['tags'][0].split(' ')
for tag in filter(None, split):
tags.append(tag)
if(len(tags) == 0):
return ""
tagList = ["<ul class='tags'>"]
for tag in tags:
tagList.append(f"<li class='tag'>{tag}</li>")
tagList.append("</ul>")
return ' '.join(tagList)