2
0

mark.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. from bottle import request, app
  2. from bottle.ext import beaker
  3. from urllib import parse
  4. import json
  5. import sqlite3
  6. import time
  7. import re
  8. import hashlib
  9. import html
  10. json_data = open('set.json').read()
  11. set_data = json.loads(json_data)
  12. conn = sqlite3.connect(set_data['db'] + '.db')
  13. curs = conn.cursor()
  14. session_opts = {
  15. 'session.type': 'file',
  16. 'session.data_dir': './app_session/',
  17. 'session.auto': 1
  18. }
  19. app = beaker.middleware.SessionMiddleware(app(), session_opts)
  20. def get_time():
  21. now = time.localtime()
  22. date = "%04d-%02d-%02d %02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
  23. return(date)
  24. def ip_check():
  25. session = request.environ.get('beaker.session')
  26. if(session.get('Now') == 1):
  27. ip = format(session['DREAMER'])
  28. else:
  29. if(request.environ.get('HTTP_X_FORWARDED_FOR')):
  30. ip = request.environ.get('HTTP_X_FORWARDED_FOR')
  31. else:
  32. ip = request.environ.get('REMOTE_ADDR')
  33. return(ip)
  34. def url_pas(data):
  35. return(parse.quote(data).replace('/','%2F'))
  36. def sha224(data):
  37. return(hashlib.sha224(bytes(data, 'utf-8')).hexdigest())
  38. def savemark(data):
  39. data = re.sub("\[date\(now\)\]", get_time(), data)
  40. if(not re.search("\.", ip_check())):
  41. name = '[[사용자:' + ip_check() + '|' + ip_check() + ']]'
  42. else:
  43. name = ip_check()
  44. data = re.sub("\[name\]", name, data)
  45. return(data)
  46. def send_p(d):
  47. d = html.escape(d)
  48. js_p = re.compile('javascript:', re.I)
  49. d = js_p.sub('', d)
  50. d = re.sub('&lt;a href="(?:[^"]*)"&gt;(?P<in>(?:(?!&lt;).)*)&lt;\/a&gt;', '<a href="' + url_pas('\g<in>') + '">\g<in></a>', d)
  51. return(d)
  52. def table_p(d, d2):
  53. alltable = 'style="'
  54. celstyle = 'style="'
  55. rowstyle = 'style="'
  56. row = ''
  57. cel = ''
  58. q = re.search("&lt;table\s?width=((?:(?!&gt;).)*)&gt;", d)
  59. w = re.search("&lt;table\s?height=((?:(?!&gt;).)*)&gt;", d)
  60. e = re.search("&lt;table\s?align=((?:(?!&gt;).)*)&gt;", d)
  61. if(q):
  62. resultss = q.groups()
  63. alltable += 'width:' + resultss[0] + ';'
  64. if(w):
  65. resultss = w.groups()
  66. alltable += 'height:' + resultss[0] + ';'
  67. if(e):
  68. resultss = e.groups()
  69. if(resultss[0] == 'right'):
  70. alltable += 'float:right;'
  71. elif(resultss[0] == 'center'):
  72. alltable += 'margin:auto;'
  73. ee = re.search("&lt;table\s?textalign=((?:(?!&gt;).)*)&gt;", d)
  74. if(ee):
  75. resultss = ee.groups()
  76. if(resultss[0] == 'right'):
  77. alltable += 'text-align:right;'
  78. elif(resultss[0] == 'center'):
  79. alltable += 'text-align:center;'
  80. r = re.search("&lt;-((?:(?!&gt;).)*)&gt;", d)
  81. if(r):
  82. resultss = r.groups()
  83. cel = 'colspan="' + resultss[0] + '"'
  84. else:
  85. cel = 'colspan="' + str(round(len(d2) / 2)) + '"'
  86. t = re.search("&lt;\|((?:(?!&gt;).)*)&gt;", d)
  87. if(t):
  88. resultss = t.groups()
  89. row = 'rowspan="' + resultss[0] + '"'
  90. ba = re.search("&lt;rowbgcolor=(#[0-9a-f-A-F]{6})&gt;", d)
  91. bb = re.search("&lt;rowbgcolor=(#[0-9a-f-A-F]{3})&gt;", d)
  92. bc = re.search("&lt;rowbgcolor=(\w+)&gt;", d)
  93. if(ba):
  94. resultss = ba.groups()
  95. rowstyle += 'background:' + resultss[0] + ';'
  96. elif(bb):
  97. resultss = bb.groups()
  98. rowstyle += 'background:' + resultss[0] + ';'
  99. elif(bc):
  100. resultss = bc.groups()
  101. rowstyle += 'background:' + resultss[0] + ';'
  102. z = re.search("&lt;table\s?bordercolor=(#[0-9a-f-A-F]{6})&gt;", d)
  103. x = re.search("&lt;table\s?bordercolor=(#[0-9a-f-A-F]{3})&gt;", d)
  104. c = re.search("&lt;table\s?bordercolor=(\w+)&gt;", d)
  105. if(z):
  106. resultss = z.groups()
  107. alltable += 'border:' + resultss[0] + ' 2px solid;'
  108. elif(x):
  109. resultss = x.groups()
  110. alltable += 'border:' + resultss[0] + ' 2px solid;'
  111. elif(c):
  112. resultss = c.groups()
  113. alltable += 'border:' + resultss[0] + ' 2px solid;'
  114. aq = re.search("&lt;table\s?bgcolor=(#[0-9a-f-A-F]{6})&gt;", d)
  115. aw = re.search("&lt;table\s?bgcolor=(#[0-9a-f-A-F]{3})&gt;", d)
  116. ae = re.search("&lt;table\s?bgcolor=(\w+)&gt;", d)
  117. if(aq):
  118. resultss = aq.groups()
  119. alltable += 'background:' + resultss[0] + ';'
  120. elif(aw):
  121. resultss = aw.groups()
  122. alltable += 'background:' + resultss[0] + ';'
  123. elif(ae):
  124. resultss = ae.groups()
  125. alltable += 'background:' + resultss[0] + ';'
  126. j = re.search("&lt;bgcolor=(#[0-9a-f-A-F]{6})&gt;", d)
  127. k = re.search("&lt;bgcolor=(#[0-9a-f-A-F]{3})&gt;", d)
  128. l = re.search("&lt;bgcolor=(\w+)&gt;", d)
  129. if(j):
  130. resultss = j.groups()
  131. celstyle += 'background:' + resultss[0] + ';'
  132. elif(k):
  133. resultss = k.groups()
  134. celstyle += 'background:' + resultss[0] + ';'
  135. elif(l):
  136. resultss = l.groups()
  137. celstyle += 'background:' + resultss[0] + ';'
  138. aa = re.search("&lt;(#[0-9a-f-A-F]{6})&gt;", d)
  139. ab = re.search("&lt;(#[0-9a-f-A-F]{3})&gt;", d)
  140. ac = re.search("&lt;(\w+)&gt;", d)
  141. if(aa):
  142. resultss = aa.groups()
  143. celstyle += 'background:' + resultss[0] + ';'
  144. elif(ab):
  145. resultss = ab.groups()
  146. celstyle += 'background:' + resultss[0] + ';'
  147. elif(ac):
  148. resultss = ac.groups()
  149. celstyle += 'background:' + resultss[0] + ';'
  150. qa = re.search("&lt;width=((?:(?!&gt;).)*)&gt;", d)
  151. qb = re.search("&lt;height=((?:(?!&gt;).)*)&gt;", d)
  152. if(qa):
  153. resultss = qa.groups()
  154. celstyle += 'width:' + resultss[0] + ';'
  155. if(qb):
  156. resultss = qb.groups()
  157. celstyle += 'height:' + resultss[0] + ';'
  158. i = re.search("&lt;\)&gt;", d)
  159. o = re.search("&lt;:&gt;", d)
  160. p = re.search("&lt;\(&gt;", d)
  161. if(i):
  162. celstyle += 'text-align:right;'
  163. elif(o):
  164. celstyle += 'text-align:center;'
  165. elif(p):
  166. celstyle += 'text-align:left;'
  167. alltable += '"'
  168. celstyle += '"'
  169. rowstyle += '"'
  170. return([alltable, rowstyle, celstyle, row, cel])
  171. def html_pas(data):
  172. data = re.sub("%phtml%(?P<in>(?:\/)?(?:a|div|span|embed|iframe)(?:\s[^%]*)?)%phtml%", "<\g<in>>", data)
  173. while(1):
  174. y = re.search("<((div|span|embed|iframe)(?:\s[^>]*))>", data)
  175. if(y):
  176. b = y.groups()
  177. if(re.search("<(\/" + b[1] + ")>", data)):
  178. xss_test = re.search('src=(?:"|\')?(http(s)?:\/\/([^\/]*)\/(?:[^"\' ]*))(?:"|\')?', b[0])
  179. if(xss_test):
  180. check = xss_test.groups()
  181. if(check[2] == "www.youtube.com" or check[2] == "serviceapi.nmv.naver.com" or check[2] == "tv.kakao.com" or check[2] == "tvple.com"):
  182. a = b[0]
  183. else:
  184. a = re.sub('src=(?:"|\')([^"\']*)(?:"|\')', '', b[0])
  185. else:
  186. a = b[0]
  187. try:
  188. if(check[1] != None):
  189. data = re.sub("<((?:\/)?" + b[1] + "(?:\s[^>]*))>", "%phtml%" + a + "%phtml%", data, 1)
  190. data = re.sub("<\/" + b[1] + ">", "%phtml%/" + b[1] + "%phtml%", data, 1)
  191. else:
  192. data = re.sub("<((?:\/)?" + b[1] + "(?:\s[^>]*))>", "[[" + check[0] + "]]", data, 1)
  193. data = re.sub("<\/" + b[1] + ">", "", data, 1)
  194. except:
  195. data = re.sub("<((?:\/)?" + b[1] + "(?:\s[^>]*))>", "%phtml%" + a + "%phtml%", data, 1)
  196. data = re.sub("<\/" + b[1] + ">", "%phtml%/" + b[1] + "%phtml%", data, 1)
  197. else:
  198. data = re.sub("<((?:\/)?" + b[1] + "(?:\s[^>]*))>", '&lt;' + b[0] + '&gt;', data, 1)
  199. break
  200. else:
  201. break
  202. data = html.escape(data)
  203. js_p = re.compile('javascript:', re.I)
  204. data = js_p.sub('', data)
  205. data = re.sub("%phtml%(?P<in>(?:\/)?(?:a|div|span|embed|iframe)(?:\s[^%]*)?)%phtml%", "<\g<in>>", data)
  206. return(data)
  207. def mid_pas(data, fol_num, include, in_c):
  208. while(1):
  209. com = re.compile("{{{((?:(?!{{{)(?!}}}).)*)}}}", re.DOTALL)
  210. y = com.search(data)
  211. if(y):
  212. a = y.groups()
  213. big_a = re.compile("^\+([1-5])\s(.*)$", re.DOTALL)
  214. big = big_a.search(a[0])
  215. small_a = re.compile("^\-([1-5])\s(.*)$", re.DOTALL)
  216. small = small_a.search(a[0])
  217. color_a = re.compile("^(#[0-9a-f-A-F]{6})\s(.*)$", re.DOTALL)
  218. color = color_a.search(a[0])
  219. color_b = re.compile("^(#[0-9a-f-A-F]{3})\s(.*)$", re.DOTALL)
  220. color_2 = color_b.search(a[0])
  221. color_c = re.compile("^#(\w+)\s(.*)$", re.DOTALL)
  222. color_3 = color_c.search(a[0])
  223. back_a = re.compile("^@([0-9a-f-A-F]{6})\s(.*)$", re.DOTALL)
  224. back = back_a.search(a[0])
  225. back_b = re.compile("^@([0-9a-f-A-F]{3})\s(.*)$", re.DOTALL)
  226. back_2 = back_b.search(a[0])
  227. back_c = re.compile("^@(\w+)\s(.*)$", re.DOTALL)
  228. back_3 = back_c.search(a[0])
  229. include_out_a = re.compile("^#!noin\s(.*)$", re.DOTALL)
  230. include_out = include_out_a.search(a[0])
  231. div_a = re.compile("^#!wiki\sstyle=(?:&quot;|&apos;)((?:(?!&quot;|&apos;).)*)(?:&quot;|&apos;)\r\n(.*)$", re.DOTALL)
  232. div = div_a.search(a[0])
  233. html_a = re.compile("^#!html\s(.*)$", re.DOTALL)
  234. html = html_a.search(a[0])
  235. fol_a = re.compile("^#!folding\s((?:(?!\n).)*)\n?\s\n(.*)$", re.DOTALL)
  236. fol = fol_a.search(a[0])
  237. syn_a = re.compile("^#!syntax\s([^\n]*)\r\n(.*)$", re.DOTALL)
  238. syn = syn_a.search(a[0])
  239. if(big):
  240. result = big.groups()
  241. data = com.sub('<span class="font-size-' + result[0] + '">' + result[1] + '</span>', data, 1)
  242. elif(small):
  243. result = small.groups()
  244. data = com.sub('<span class="font-size-small-' + result[0] + '">' + result[1] + '</span>', data, 1)
  245. elif(color):
  246. result = color.groups()
  247. data = com.sub('<span style="color:' + result[0] + '">' + result[1] + '</span>', data, 1)
  248. elif(color_2):
  249. result = color_2.groups()
  250. data = com.sub('<span style="color:' + result[0] + '">' + result[1] + '</span>', data, 1)
  251. elif(color_3):
  252. result = color_3.groups()
  253. data = com.sub('<span style="color:' + result[0] + '">' + result[1] + '</span>', data, 1)
  254. elif(back):
  255. result = back.groups()
  256. data = com.sub('<span style="background:#' + result[0] + '">' + result[1] + '</span>', data, 1)
  257. elif(back_2):
  258. result = back_2.groups()
  259. data = com.sub('<span style="background:#' + result[0] + '">' + result[1] + '</span>', data, 1)
  260. elif(back_3):
  261. result = back_3.groups()
  262. data = com.sub('<span style="background:' + result[0] + '">' + result[1] + '</span>', data, 1)
  263. elif(div):
  264. result = div.groups()
  265. data = com.sub('<div style="' + result[0] + '">' + result[1] + '</div>', data, 1)
  266. elif(html):
  267. result = html.groups()
  268. data = com.sub(result[0], data, 1)
  269. elif(fol):
  270. result = fol.groups()
  271. data = com.sub( "<div> \
  272. " + result[0] + " \
  273. <div id='folding_" + str(fol_num + 1) + "' style='display: inline-block;'> \
  274. [<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + "); folding(" + str(fol_num) + ");'>펼치기</a>] \
  275. </div> \
  276. <div id='folding_" + str(fol_num + 2) + "' style='display: none;'> \
  277. [<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + "); folding(" + str(fol_num) + ");'>접기</a>] \
  278. </div> \
  279. <div id='folding_" + str(fol_num) + "' style='display: none;'> \
  280. <br> \
  281. " + result[1] + " \
  282. </div> \
  283. </div>", data, 1)
  284. fol_num += 3
  285. elif(syn):
  286. result = syn.groups()
  287. data = com.sub('<pre id="syntax"><code class="' + result[0] + '">' + re.sub('\r\n', '<isbr>', re.sub(' ', '<space>', result[1])) + '</code></pre>', data, 1)
  288. elif(html):
  289. result = html.groups()
  290. data = com.sub(result[0], data, 1)
  291. elif(include_out):
  292. if((include or in_c) == 1):
  293. data = com.sub("", data, 1)
  294. else:
  295. result = include_out.groups()
  296. data = com.sub(result[0], data, 1)
  297. else:
  298. data = com.sub('<code>' + a[0] + '</code>', data, 1)
  299. else:
  300. break
  301. while(1):
  302. com = re.compile("<code>(((?!<\/code>).)*)<\/code>", re.DOTALL)
  303. y = com.search(data)
  304. if(y):
  305. a = y.groups()
  306. mid_data = re.sub("<\/span>", "}}}", a[0])
  307. mid_data = re.sub("<\/div>", "}}}", mid_data)
  308. mid_data = re.sub('<span class="font\-size\-(?P<in>[1-6])">', "{{{+\g<in> ", mid_data)
  309. mid_data = re.sub('<span class="font\-size\-small\-(?P<in>[1-6])">', "{{{-\g<in> ", mid_data)
  310. mid_data = re.sub('<span style="color:(?:#)?(?P<in>[^"]*)">', "{{{#\g<in> ", mid_data)
  311. mid_data = re.sub('<span style="background:(?:#)?(?P<in>[^"]*)">', "{{{@\g<in> ", mid_data)
  312. mid_data = re.sub('<div style="(?P<in>[^"]*)">', "{{{#!wiki style=\"\g<in>\"\n", mid_data)
  313. mid_data = re.sub("(?P<in>.)", "<nowiki>\g<in></nowiki>", mid_data)
  314. data = com.sub(mid_data, data, 1)
  315. else:
  316. break
  317. return([data, fol_num])
  318. def toc_pas(data, title):
  319. i = [0, 0, 0, 0, 0, 0, 0]
  320. last = 0
  321. span = ''
  322. rtoc = '<div id="toc"><span id="toc-name">목차</span><br><br>'
  323. while(1):
  324. i[0] += 1
  325. m = re.search('(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n', data)
  326. if(m):
  327. result = m.groups()
  328. wiki = len(result[0])
  329. if(last < wiki):
  330. last = wiki
  331. else:
  332. last = wiki
  333. if(wiki == 1):
  334. i[2] = 0
  335. i[3] = 0
  336. i[4] = 0
  337. i[5] = 0
  338. i[6] = 0
  339. elif(wiki == 2):
  340. i[3] = 0
  341. i[4] = 0
  342. i[5] = 0
  343. i[6] = 0
  344. elif(wiki == 3):
  345. i[4] = 0
  346. i[5] = 0
  347. i[6] = 0
  348. elif(wiki == 4):
  349. i[5] = 0
  350. i[6] = 0
  351. elif(wiki == 5):
  352. i[6] = 0
  353. if(wiki == 1):
  354. i[1] += 1
  355. elif(wiki == 2):
  356. i[2] += 1
  357. elif(wiki == 3):
  358. i[3] += 1
  359. elif(wiki == 4):
  360. i[4] += 1
  361. elif(wiki == 5):
  362. i[5] += 1
  363. else:
  364. i[6] += 1
  365. toc = str(i[1]) + '.' + str(i[2]) + '.' + str(i[3]) + '.' + str(i[4]) + '.' + str(i[5]) + '.' + str(i[6]) + '.'
  366. toc = re.sub("(?P<in>[0-9]0(?:[0]*)?)\.", '\g<in>#.', toc)
  367. toc = re.sub("0\.", '', toc)
  368. toc = re.sub("#\.", '.', toc)
  369. toc = re.sub("\.$", '', toc)
  370. test = re.search('([0-9]*)(\.([0-9]*))?(\.([0-9]*))?(\.([0-9]*))?(\.([0-9]*))?', toc)
  371. if(test):
  372. g = test.groups()
  373. if(g[4]):
  374. span = '<span id="out"></span>' * 4
  375. elif(g[3]):
  376. span = '<span id="out"></span>' * 3
  377. elif(g[2]):
  378. span = '<span id="out"></span>' * 2
  379. elif(g[1]):
  380. span = '<span id="out"></span>'
  381. else:
  382. span = ''
  383. rtoc += span + '<a href="#s-' + toc + '">' + toc + '</a>. ' + result[1] + '<br>'
  384. c = re.sub(" $", "", result[1])
  385. d = c
  386. c = re.sub("\[\[(([^|]*)\|)?(?P<in>[^\]]*)\]\]", "\g<in>", c)
  387. data = re.sub('(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n', '<tablenobr><h' + str(wiki) + ' id="' + c + '"><a href="#toc" id="s-' + toc + '">' + toc + '.</a> ' + d + ' <span style="font-size:11px;">[<a href="/edit/' + url_pas(title) + '/section/' + str(i[0]) + '">편집</a>]</span></h' + str(wiki) + '>', data, 1)
  388. else:
  389. rtoc += '</div>'
  390. break
  391. data = re.sub("\[목차\]", rtoc, data)
  392. return(data)
  393. def backlink_plus(name, link, backtype, num):
  394. if(num == 1):
  395. curs.execute("select title from back where title = ? and link = ? and type = ?", [link, name, backtype])
  396. y = curs.fetchall()
  397. if(not y):
  398. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  399. def cat_plus(name, link, num):
  400. if(num == 1):
  401. curs.execute("select title from cat where title = ? and cat = ?", [link, name])
  402. y = curs.fetchall()
  403. if(not y):
  404. curs.execute("insert into cat (title, cat) values (?, ?)", [link, name])
  405. def namumark(title, data, num, in_c):
  406. data = html_pas(data)
  407. b = 0
  408. a = mid_pas(data, b, 0, in_c)
  409. data = a[0]
  410. b = a[1]
  411. data = re.sub("\[anchor\((?P<in>[^\[\]]*)\)\]", '<span id="\g<in>"></span>', data)
  412. data = savemark(data)
  413. include = re.compile("\[include\(((?:(?!\)\]|,).)*)((?:,\s?(?:[^)]*))+)?\)\]")
  414. while(1):
  415. m = include.search(data)
  416. if(m):
  417. results = m.groups()
  418. if(results[0] == title):
  419. data = include.sub("<b>" + results[0] + "</b>", data, 1)
  420. else:
  421. curs.execute("select data from data where title = ?", [results[0]])
  422. in_con = curs.fetchall()
  423. backlink_plus(title, results[0], 'include', num)
  424. if(in_con):
  425. in_data = in_con[0][0]
  426. in_data = include.sub("", in_data)
  427. in_data = html_pas(in_data)
  428. in_data = mid_pas(in_data, b, 1, in_c)[0]
  429. if(results[1]):
  430. a = results[1]
  431. while(1):
  432. g = re.search("([^= ,]*)\=([^,]*)", a)
  433. if(g):
  434. result = g.groups()
  435. in_data = re.sub("@" + result[0] + "@", result[1], in_data)
  436. a = re.sub("([^= ,]*)\=([^,]*)", "", a, 1)
  437. else:
  438. break
  439. in_data = toc_pas(in_data, results[0])
  440. data = include.sub('\n<nobr><a href="/w/' + url_pas(results[0]) + '">[' + results[0] + ' 이동]</a><div>' + in_data + '</div><nobr>\n', data, 1)
  441. else:
  442. data = include.sub("<a class=\"not_thing\" href=\"" + url_pas(results[0]) + "\">" + results[0] + "</a>", data, 1)
  443. else:
  444. break
  445. while(1):
  446. m = re.search('^#(?:redirect|넘겨주기) ([^\n]*)$', data)
  447. if(m):
  448. results = m.groups()
  449. g = re.sub("\\\#", "<sharp>", results[0])
  450. aa = re.search("^([^\n]*)(#(?:[^\n]*))$", g)
  451. if(aa):
  452. results = aa.groups()
  453. nosharp = re.sub("<sharp>", "#", results[0])
  454. data = re.sub('^#(?:redirect|넘겨주기) ([^\n]*)$', '<meta http-equiv="refresh" content="0;url=/w/' + url_pas(nosharp) + '/from/' + url_pas(title) + results[1] + '" />', data, 1)
  455. else:
  456. nosharp = re.sub("<sharp>", "#", g)
  457. data = re.sub('^#(?:redirect|넘겨주기) ([^\n]*)$', '<meta http-equiv="refresh" content="0;url=/w/' + url_pas(nosharp) + '/from/' + url_pas(title) + '" />', data, 1)
  458. backlink_plus(title, results[0], 'redirect', num)
  459. else:
  460. break
  461. data = '\n' + data + '\n'
  462. data = re.sub("\[nicovideo\((?P<in>[^,)]*)(?:(?:,(?:[^,)]*))+)?\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>]]", data)
  463. while(1):
  464. m = re.search("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", data)
  465. if(m):
  466. result = m.groups()
  467. blockquote = result[0]
  468. blockquote = re.sub("\n&gt;\s?", "\n", blockquote)
  469. data = re.sub("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", "\n<blockquote>" + blockquote + "</blockquote>", data, 1)
  470. else:
  471. break
  472. if(not re.search('\[목차\]', data)):
  473. if(not re.search('\[목차\(없음\)\]', data)):
  474. data = re.sub("(?P<in>(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "[목차]\n\g<in>", data, 1)
  475. else:
  476. data = re.sub("\[목차\(없음\)\]", "", data)
  477. data = re.sub("(\n)(?P<in>\r\n(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "\g<in>", data)
  478. data = toc_pas(data, title)
  479. category = ''
  480. while(1):
  481. m = re.search("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", data)
  482. if(m):
  483. g = m.groups()
  484. if(title != g[0]):
  485. cat_plus(title, g[0], num)
  486. if(category == ''):
  487. curs.execute("select title from data where title = ?", [g[0]])
  488. exists = curs.fetchall()
  489. if(exists):
  490. red = ""
  491. else:
  492. red = 'class="not_thing"'
  493. category += '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  494. else:
  495. curs.execute("select title from data where title = ?", [g[0]])
  496. exists = curs.fetchall()
  497. if(exists):
  498. red = ""
  499. else:
  500. red = 'class="not_thing"'
  501. category += ' / ' + '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  502. data = re.sub("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", '', data, 1)
  503. else:
  504. break
  505. data = re.sub("&#x27;&#x27;&#x27;(?P<in>(?:(?!&#x27;).)*)&#x27;&#x27;&#x27;", '<b>\g<in></b>', data)
  506. data = re.sub("&#x27;&#x27;(?P<in>(?:(?!&#x27;).)*)&#x27;&#x27;", '<i>\g<in></i>', data)
  507. data = re.sub('(?:~~|--)(?P<in>(?:(?!~~|--).)*)(?:~~|--)', '<s>\g<in></s>', data)
  508. data = re.sub('__(?P<in>.+?)__(?!_)', '<u>\g<in></u>', data)
  509. data = re.sub('\^\^(?P<in>.+?)\^\^(?!\^)', '<sup>\g<in></sup>', data)
  510. data = re.sub(',,(?P<in>.+?),,(?!,)', '<sub>\g<in></sub>', data)
  511. data = re.sub('&lt;math&gt;(?P<in>((?!&lt;math&gt;).)*)&lt;\/math&gt;', '$\g<in>$', data)
  512. data = re.sub('{{\|(?P<in>(?:(?:(?:(?!\|}}).)*)(?:\n?))+)\|}}', '<table> \
  513. <tbody> \
  514. <tr> \
  515. <td> \
  516. \g<in> \
  517. </td> \
  518. </tr> \
  519. </tbody> \
  520. </table>', data)
  521. data = re.sub('\[ruby\((?P<in>[^\,]*)\,\s?(?P<out>[^\)]*)\)\]', '<ruby> \
  522. \g<in> \
  523. <rp>(</rp> \
  524. <rt>\g<out></rt> \
  525. <rp>)</rp> \
  526. </ruby>', data)
  527. data = re.sub("##\s?(?P<in>[^\n]*)\n", "<div style='display:none;'>\g<in></div>", data)
  528. test = re.findall('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', data)
  529. if(test):
  530. for wiki in test:
  531. if(wiki[1]):
  532. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[1] + '</a>', data, 1)
  533. else:
  534. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[0] + '</a>', data, 1)
  535. while(1):
  536. m = re.search("\[\[(파일|외부):((?:(?!\]\]|\|).)*)(?:\|((?:(?!\]\]).)*))?\]\]", data)
  537. if(m):
  538. img_d = m.groups()
  539. if(img_d):
  540. if(img_d[0] == '파일' and not re.search("^파일:([^\n]*)", title)):
  541. backlink_plus(title, '파일:' + img_d[1], 'file', num)
  542. width = ''
  543. height = ''
  544. if(img_d[2]):
  545. width_r = re.search("width=([^ \n&]*)", img_d[2])
  546. height_r = re.search("height=([^ \n&]*)", img_d[2])
  547. if(width_r):
  548. width_d = width_r.groups()
  549. width = width_d[0]
  550. if(height_r):
  551. height_d = height_r.groups()
  552. height = height_d[0]
  553. if(img_d[0] == '파일'):
  554. img_e = re.search('^(.+)(\.(?:.+))$', img_d[1]).groups()
  555. data = re.sub("\[\[(파일|외부):((?:(?!\]\]|\|).)*)(?:\|((?:(?!\]\]).)*))?\]\]", '<a href="/w/파일:' + img_d[1] + '"><img src="/image/' + sha224(img_e[0]) + img_e[1] + '" width="' + width + '" height="' + height + '"></a>', data, 1)
  556. else:
  557. data = re.sub("\[\[(파일|외부):((?:(?!\]\]|\|).)*)(?:\|((?:(?!\]\]).)*))?\]\]", '<img src="' + img_d[1] + '" width="' + width + '" height="' + height + '">', data, 1)
  558. else:
  559. break
  560. else:
  561. break
  562. data = re.sub("\[br\]",'<br>', data)
  563. while(1):
  564. com = re.compile("\[youtube\(([^, )]*)(,[^)]*)?\)\]")
  565. m = com.search(data)
  566. if(m):
  567. src = ''
  568. width = '560'
  569. height = '315'
  570. time = '0'
  571. result = m.groups()
  572. if(result[0]):
  573. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-]*))$', result[0])
  574. if(yudt):
  575. if(yudt.groups()[0]):
  576. src = yudt.groups()[0]
  577. elif(yudt.groups()[1]):
  578. src = yudt.groups()[1]
  579. elif(yudt.groups()[2]):
  580. src = yudt.groups()[2]
  581. else:
  582. src = ''
  583. if(result[1]):
  584. mdata = re.search('width=([0-9]*)', result[1])
  585. if(mdata):
  586. width = mdata.groups()[0]
  587. mdata = re.search('height=([0-9]*)', result[1])
  588. if(mdata):
  589. height = mdata.groups()[0]
  590. mdata = re.search('time=([0-9]*)', result[1])
  591. if(mdata):
  592. time = mdata.groups()[0]
  593. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  594. else:
  595. break
  596. data = re.sub("\[\[(?::(?P<in>(?:분류|파일):(?:(?:(?!\]\]).)*)))\]\]", "[[\g<in>]]", data)
  597. a = re.findall('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', data)
  598. for i in a:
  599. b = re.search('(.*)\/', title)
  600. if(b):
  601. m = b.groups()
  602. if(i):
  603. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + i + ']]', data, 1)
  604. else:
  605. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + ']]', data, 1)
  606. else:
  607. if(i):
  608. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + i + ']]', data, 1)
  609. else:
  610. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + ']]', data, 1)
  611. data = re.sub('\[\[(?P<in>\/[^\]|]*)(?P<out>\|(?:[^\]]*))?\]\]', '[[' + title + '\g<in>\g<out>]]', data)
  612. while(1):
  613. m = re.search("\[\[(((?!\]\]).)*)\]\]", data)
  614. if(m):
  615. result = m.groups()
  616. rep = result[0]
  617. rep = re.sub("\\\#", "<sharp>", rep)
  618. a = re.search("^((?:(?!\|).)*)(?:\|(.*))?$", rep)
  619. results = a.groups()
  620. aa = re.search("^([^#]*)(#(?:.*))?$", results[0])
  621. if(results[1]):
  622. g = re.sub("<sharp>", "#", results[1])
  623. else:
  624. g = re.sub("<sharp>", "#", results[0])
  625. results = aa.groups()
  626. if(not results[1]):
  627. sharp = ''
  628. else:
  629. sharp = results[1]
  630. b = re.search("^http(?:s)?:\/\/", results[0])
  631. if(b):
  632. comp = re.compile("(?:\.(?:jpg|png|gif|jpeg))", re.I)
  633. c = comp.search(results[0])
  634. if(c):
  635. data = re.sub('\[\[(((?!\]\]).)*)\]\]', '<a class="out_link" rel="nofollow" target="_blank" href="' + results[0] + sharp + '">' + g + '</a>', data, 1)
  636. else:
  637. data = re.sub('\[\[(((?!\]\]).)*)\]\]', '<a class="out_link" rel="nofollow" target="_blank" href="' + results[0] + sharp + '">' + g + '</a>', data, 1)
  638. else:
  639. if(results[0] == title):
  640. data = re.sub('\[\[(((?!\]\]).)*)\]\]', '<b>' + g + '</b>', data, 1)
  641. else:
  642. nosharp = re.sub("<sharp>", "#", results[0])
  643. curs.execute("select title from data where title = ?", [nosharp])
  644. y = curs.fetchall()
  645. if(y):
  646. clas = ''
  647. else:
  648. clas = 'not_thing'
  649. data = re.sub('\[\[(((?!\]\]).)*)\]\]', '<a title="' + re.sub('#', '\#', nosharp) + sharp + '" class="' + clas + '" href="/w/' + url_pas(nosharp) + sharp + '">' + g + '</a>', data, 1)
  650. backlink_plus(title, results[0], '', num)
  651. else:
  652. break
  653. while(1):
  654. m = re.search("((?:(?:( +)\*\s(?:[^\n]*))\n?)+)", data)
  655. if(m):
  656. result = m.groups()
  657. end = str(result[0])
  658. while(1):
  659. isspace = re.search("( +)\*\s([^\n]*)", end)
  660. if(isspace):
  661. spacebar = isspace.groups()
  662. up = len(spacebar[0]) * 20
  663. end = re.sub("( +)\*\s([^\n]*)", "<li style='margin-left:" + str(up) + "px'>" + spacebar[1] + "</li>", end, 1)
  664. else:
  665. break
  666. end = re.sub("\n", '', end)
  667. data = re.sub("(?:(?:(?:( +)\*\s([^\n]*))\n?)+)", '<ul id="list">' + end + '</ul>', data, 1)
  668. else:
  669. break
  670. now_time = get_time()
  671. data = re.sub('\[date\]', now_time, data)
  672. time_data = re.search('^([0-9]{4})-([0-9]{2})-([0-9]{2})', now_time)
  673. time = time_data.groups()
  674. age_data = re.findall('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', data)
  675. for age in age_data:
  676. year = int(time[0]) - int(age[0])
  677. if(age[1] > time[1]):
  678. year -= 1
  679. elif(age[1] == time[1]):
  680. if(age[2] > time[2]):
  681. year -= 1
  682. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(year), data, 1)
  683. data = re.sub("-{4,11}", "<hr>", data)
  684. while(1):
  685. b = re.search("(<\/h[0-9]>|\n)( +)", data)
  686. if(b):
  687. result = b.groups()
  688. up = re.sub(' ', '<span id="in"></span>', result[1])
  689. if(re.search('<\/h[0-9]>', result[0])):
  690. data = re.sub("(?P<in>\/h[0-9]>)( +)", '\g<in>' + up, data, 1)
  691. else:
  692. data = re.sub("(?:\n)( +)", '<br>' + up, data, 1)
  693. else:
  694. break
  695. a = 1
  696. tou = "<hr id='footnote'><div><br>"
  697. namu = []
  698. while(1):
  699. b = re.search("\[\*([^\s]*)(?:\s(((?!\[|\]).)*))?\]", data)
  700. if(b):
  701. results = b.groups()
  702. if(not results[1] and results[0]):
  703. i = 0
  704. while(1):
  705. try:
  706. if(namu[i] == results[0]):
  707. none_this = 0
  708. break
  709. else:
  710. i += 2
  711. except:
  712. none_this = 1
  713. break
  714. if(none_this == 0):
  715. data = re.sub("\[\*([^\s]*)(?:\s(((?!\[|\]).)*))?\]", "<sup><a title=\"" + namu[i + 1] + "\" id=\"rfn-" + str(a) + "\" href=\"#fn-" + str(a) + "\">[" + results[0] + "]</a></sup>", data, 1)
  716. else:
  717. data = re.sub("\[\*([^\s]*)(?:\s(((?!\[|\]).)*))?\]", "<sup><a id=\"rfn-" + str(a) + "\" href=\"#fn-" + str(a) + "\">[" + results[0] + "]</a></sup>", data, 1)
  718. elif(results[0]):
  719. namu += [results[0]]
  720. namu += [results[1]]
  721. c = results[1]
  722. c = re.sub("<(?:[^>]*)>", '', c)
  723. tou += "<span id='footnote-list'><a href=\"#rfn-" + str(a) + "\" id=\"fn-" + str(a) + "\">[" + results[0] + "]</a> " + results[1] + "</span><br>"
  724. data = re.sub("\[\*([^\s]*)(?:\s(((?!\[|\]).)*))?\]", "<sup><a title=\"" + c + "\" id=\"rfn-" + str(a) + "\" href=\"#fn-" + str(a) + "\">[" + results[0] + "]</a></sup>", data, 1)
  725. a += 1
  726. else:
  727. c = results[1]
  728. c = re.sub("<(?:[^>]*)>", '', c)
  729. tou += "<span id='footnote-list'><a href=\"#rfn-" + str(a) + "\" id=\"fn-" + str(a) + "\">[" + str(a) + "]</a> " + results[1] + "</span><br>"
  730. data = re.sub("\[\*([^\s]*)(?:\s(((?!\[|\]).)*))?\]", '<sup><a title="' + c + '" id="rfn-' + str(a) + '" href="#fn-' + str(a) + '">[' + str(a) + ']</a></sup>', data, 1)
  731. a += 1
  732. else:
  733. tou += '</div>'
  734. if(tou == "<hr id='footnote'><div><br></div>"):
  735. tou = ""
  736. break
  737. data = re.sub("\[각주\](?:(?:<br>| |\r|\n)+)?$", "", data)
  738. data = re.sub("(?:(?:<br>| |\r|\n)+)$", "", data)
  739. data = re.sub("\[각주\]", "<br>" + tou, data)
  740. data += tou
  741. if(category):
  742. data += '<div id="cate">분류: ' + category + '</div>'
  743. data = re.sub("(?:\|\|\r\n)", "#table#<tablenobr>", data)
  744. while(1):
  745. y = re.search("(\|\|(?:(?:(?:(?:(?!\|\|).)*)(?:\n?))+))", data)
  746. if(y):
  747. a = y.groups()
  748. mid_data = re.sub("\|\|", "#table#", a[0])
  749. mid_data = re.sub("\r\n", "<br>", mid_data)
  750. data = re.sub("(\|\|((?:(?:(?:(?!\|\|).)*)(?:\n?))+))", mid_data, data, 1)
  751. else:
  752. break
  753. data = re.sub("#table#", "||", data)
  754. data = re.sub("<tablenobr>", "\r\n", data)
  755. while(1):
  756. m = re.search("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  757. if(m):
  758. results = m.groups()
  759. table = results[0]
  760. while(1):
  761. a = re.search("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  762. if(a):
  763. row = ''
  764. cel = ''
  765. celstyle = ''
  766. rowstyle = ''
  767. alltable = ''
  768. table_d = ''
  769. result = a.groups()
  770. if(result[1]):
  771. table_d = table_p(result[1], result[0])
  772. alltable = table_d[0]
  773. rowstyle = table_d[1]
  774. celstyle = table_d[2]
  775. row = table_d[3]
  776. cel = table_d[4]
  777. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table " + alltable + "> \
  778. <tbody> \
  779. <tr " + rowstyle + "> \
  780. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  781. else:
  782. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  783. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table> \
  784. <tbody> \
  785. <tr> \
  786. <td " + cel + ">", table, 1)
  787. else:
  788. break
  789. table = re.sub("\|\|$", "</td> \
  790. </tr> \
  791. </tbody> \
  792. </table>", table)
  793. while(1):
  794. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  795. if(b):
  796. row = ''
  797. cel = ''
  798. celstyle = ''
  799. rowstyle = ''
  800. table_d = ''
  801. result = b.groups()
  802. if(result[1]):
  803. table_d = table_p(result[1], result[0])
  804. rowstyle = table_d[1]
  805. celstyle = table_d[2]
  806. row = table_d[3]
  807. cel = table_d[4]
  808. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  809. </tr> \
  810. <tr " + rowstyle + "> \
  811. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  812. else:
  813. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  814. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  815. </tr> \
  816. <tr> \
  817. <td " + cel + ">", table, 1)
  818. else:
  819. break
  820. while(1):
  821. c = re.search("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  822. if(c):
  823. row = ''
  824. cel = ''
  825. celstyle = ''
  826. table_d = ''
  827. result = c.groups()
  828. if(result[1]):
  829. table_d = table_p(result[1], result[0])
  830. celstyle = table_d[2]
  831. row = table_d[3]
  832. cel = table_d[4]
  833. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  834. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  835. else:
  836. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  837. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  838. <td " + cel + ">", table, 1)
  839. else:
  840. break
  841. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  842. else:
  843. break
  844. data = re.sub("\r\n(?P<in><h[0-6])", "\g<in>", data)
  845. data = re.sub("(\n<nobr>|<nobr>\n|<nobr>)", "", data)
  846. data = re.sub("<nowiki>(?P<in>.)<\/nowiki>", "\g<in>", data)
  847. data = re.sub("<space>", " ", data)
  848. data = re.sub('<\/blockquote>(?:(?:\r)?\n){2}<blockquote>', '</blockquote><blockquote>', data)
  849. data = re.sub('<\/blockquote>(?:(?:\r)?\n)<br><blockquote>', '</blockquote><blockquote>', data)
  850. data = re.sub('\n', '<br>', data)
  851. data = re.sub('<isbr>', '\r\n', data)
  852. data = re.sub('^<br>', '', data)
  853. data += "<script> \
  854. function folding(num) { \
  855. var fol = document.getElementById('folding_' + num); \
  856. if(fol.style.display == 'inline-block' || fol.style.display == 'block') { \
  857. fol.style.display = 'none'; \
  858. } else { \
  859. if(num % 3 == 0) { \
  860. fol.style.display = 'block'; \
  861. } else { \
  862. fol.style.display = 'inline-block'; \
  863. } \
  864. } \
  865. } \
  866. </script>"
  867. conn.commit()
  868. return(data)