mark.py 43 KB

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