-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInky.py
executable file
·234 lines (211 loc) · 8.57 KB
/
Inky.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
230
231
232
233
234
################################################################################
#----------------InkyWiki---by-Brian-Evans---------------------------------------#
################################################################################
from bottle import route, run, template, static_file, post, request, get, redirect
import os.path, os
import re
import inkyconfig as cfg
#####---------------------Main-page-view-functions-------------------------#####
@route('/')
def home():
pagename = 'main'
with open('./content/main', 'r') as textfile:
bodytext = textfile.read()
return template('core', pagename=pagename, bodytext=bodytext, options=cfg.inky)
@route('/wiki/<pagename>')
def build(pagename='main'):
#Serves either an edit dialog or an existing page
if not os.path.isfile('./content/' + str(pagename)):
return template('editcreate', pagename=pagename)
else:
if not re.search(cfg.inky['name_re'],pagename):
pagename = 'main'
with open('./content/' + pagename, 'r') as textfile:
bodytext = textfile.read()
return template('core', pagename=pagename, bodytext=bodytext, options=cfg.inky)
@route('/edit/<pagename>', method='POST')
def createedit(pagename):
#Commits the edits supplied to it from 'build' or 'editExisting'
pagedata = request.forms.get('PageData') #comes in as a string
pagedata = stripHTML(pagedata)
pagedata = alinkbuild(pagedata)
pagedata = swapScript(pagedata,'ToHTML')
pagename = pagename.lower()
fo = open('./content/' + pagename, 'w')
fo.write(str.strip(pagedata))
fo.close()
return template('editcomplete', pagename=pagename)
@route('/edit/existing/<pagename>')
def editExisting(pagename):
#Serves an edit page for a pre-existing article
with open('./content/' + pagename, 'r') as textfile:
bodytext = textfile.read()
bodytext = alinkunbuild(bodytext)
bodytext = swapScript(bodytext,'FromHTML')
bodytext = str.strip(bodytext)
return template('editexisting', pagename=pagename, bodytext=bodytext)
@route('/rename/<pagename>', method='POST')
def renameExisting(pagename):
#Renames an existing page and is fed from a function titled 'renameEntry'
newName = request.forms.get('newpagename').lower()
if re.search(cfg.inky['name_re'],newName):
newNameForURL = newName.replace(' ','_')
os.rename('./content/' + pagename, './content/' + newNameForURL)
return template('renamecomplete', pagename=pagename, newName=newName, newNameForURL=newNameForURL)
else:
with open('./content/' + pagename, 'r') as textfile:
bodytext = textfile.read()
return template('core', pagename=pagename, bodytext=bodytext)
@route('/rename/existing/<pagename>')
def renameEntry(pagename):
#Serves an edit page to change an article's name
return template('rename', pagename=pagename)
@route('/delete/<pagename>')
def deleteArticle(pagename):
if os.path.isfile('./content/' + pagename) and re.search(cfg.inky['name_re'],pagename):
os.remove('./content/' + pagename)
deletestatus = 'was successful.'
return template('deletecomplete', pagename=pagename, deletestatus=deletestatus)
else:
deletestatus = 'failed. The article did not exist or was not a valid target for deletion.'
return template('deletecomplete', pagename=pagename, deletestatus=deletestatus)
@route('/styles/<css>')
def servestyle(css):
#This serves the CSS to the templates
return static_file(css,root='./styles/')
@route('/js/<js>')
def servestyle(js):
#This serves the js to the templates
return static_file(js,root='./js/')
@route('/wiki/search', method='POST')
def search():
pagename = request.forms.get('pagename').lower()
if len(pagename) > 0:
if re.search(cfg.inky['name_re'],pagename):
pagename = pagename.replace(' ','_')
results = findPages(pagename)
found = False
for item in results:
if item == pagename:
with open('./content/' + pagename, 'r') as textfile:
bodytext = textfile.read()
return template('core', pagename=pagename, bodytext=bodytext, options=cfg.inky)
else:
found = False
return template('search-results', results=results, pagename=pagename, found=found, options=cfg.inky)
else:
pagename = 'main'
with open('./content/' + pagename, 'r') as textfile:
bodytext = textfile.read()
return template('core', pagename=pagename, bodytext=bodytext)
else:
return redirect(request.environ['HTTP_REFERER'])
#
##
###
####
#####-------------Functions-for-editor(s)/String-Functions-----------------#####
##--~~
##--~~
def alinkbuild(string):
#Will build hyperlinks from WikiCode entered into the edit textareabox
#At present this allows for properly nested wikiCode within '[F: ... :F]' tags
instances = string.count('[F: ')
if instances > 0:
for x in range(instances):
start = string.find('[F: ') + 4
end = string.find(' :F]')
linktext = string[start:end]
if linktext.count(':') > 1 and linktext.count(':') % 2 == 0:
counter = linktext.count(':')/2
linkname = linktext[4*counter:len(linktext)-4*counter]
linkname = linkname.replace(' ','_')
else:
linkname = linktext.replace(' ','_')
#deal with aliased links
original = linktext
if linkname.find('=') >= 1:
split = linkname.find('=')
linkname = linkname[:split]
linktext = linktext[split+1:]
linkname = linkname.lower()
string = string.replace('[F: ' + original + ' :F]', '<a href="/wiki/' + linkname + '">' + linktext + '</a>')
return string
else:
return string
##--~~
##--~~
def alinkunbuild(string):
#Deconstructs hyperlinks back into Wikicode for the edit textarea box
instances = string.lower()
instances = instances.count('<a href=')
marker = 0
if instances > 0:
for x in range(instances):
start = string.lower().find('<a href="/wiki/',marker)
end = string.find('">',start + 1) + 1
aliasStart = end + 1
aliasEnd = string.find('<',end)
if string[start+15:end-1] == string[aliasStart:aliasEnd]:
string = string.replace(string[start:end+1],'[F: ')
else:
#deal with aliased links
replacee = string[start:end+1]
replacement = string[start+15:end-1]
string = string.replace(replacee,'[F: ' + replacement + '=')
marker = end + 1
string = string.replace('</a>',' :F]')
return string
else:
return string
##--~~
##--~~
def stripHTML(string):
#Will strip all html tags from a string
instances = string.count('<')
if instances > 0:
for x in range(instances):
start = string.find('<')
end = string.find('>') + 1
linktext = string[start:end]
string = string.replace(linktext,'').replace(' ',' ')
return string
else:
return string
##--~~
##--~~
def swapScript(string,direction='FromHTML'):
#Direction takes: 'FromHTML' and any other value (to go ToHTML)
#Swaps html for wikiCode and vice versa
tags = [
['<b>','[b: '],['</b>',' :b]'],
['<i>','[i: '],['</i>',' :i]'],
['<sub>','[s: '],['</sub>',' :s]'],
['<sup>','[S: '],['</sup>',' :S]'],
['<blockquote>','[q: '],['</blockquote>',' :q]'],
['<p>','[p: '],['</p>',' :p]'],
['<h3>','[h: '],['</h3><hr>',' :h]'],
['<ul>','[L: '],['</ul',' :L]'],
['<li>','[l: '],['</li>',' :l]'],
['<br>','[n: ']
]
for x in tags:
if direction == 'FromHTML':
string = string.replace(x[0],x[1])
else:
string = string.replace(x[1],x[0])
return string
##--~~
##--~~
def findPages(string):
#Used in serach
matches = []
for item in os.listdir('./content/'):
if re.search(r''+string,item,re.I):
matches.append(item)
return matches
##--~~
##--~~
#####---------------------------Run-the-server-----------------------------#####
if __name__ == '__main__':
run(host='localhost', port=8080)