mark.py 41 KB

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