mark.py 42 KB

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