mark.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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("delete from back where title = ? and link = ? and type = ?", [link, name, backtype])
  320. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  321. def cat_plus(name, link, num):
  322. if(num == 1):
  323. curs.execute("delete from cat where title = ? and cat = ?", [link, name])
  324. curs.execute("insert into cat (title, cat) values (?, ?)", [link, name])
  325. def namumark(title, data, num, in_c, toc_y):
  326. data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", data))
  327. data = html_pas(data)
  328. data = '\r\n' + data + '\r\n'
  329. fol_num = 0
  330. var_d = mid_pas(data, fol_num, 0, in_c)
  331. data = var_d[0]
  332. fol_num = var_d[1]
  333. include = re.compile("\[include\(((?:(?!\)\]|,).)*)((?:(?:,\s?(?:(?!\)\]).)*))+)?\)\]")
  334. while(1):
  335. m = include.search(data)
  336. if(m):
  337. results = m.groups()
  338. if(results[0] == title):
  339. data = include.sub("<b>" + results[0] + "</b>", data, 1)
  340. else:
  341. curs.execute("select data from data where title = ?", [results[0]])
  342. in_con = curs.fetchall()
  343. backlink_plus(title, results[0], 'include', num)
  344. if(in_con):
  345. in_data = in_con[0][0]
  346. in_data = include.sub("", in_data)
  347. in_data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", in_data))
  348. in_data = html_pas(in_data)
  349. var_d = mid_pas(in_data, fol_num, 1, in_c)
  350. in_data = var_d[0]
  351. fol_num = var_d[1]
  352. if(results[1]):
  353. a = results[1]
  354. while(1):
  355. g = re.search("([^= ,]*)\=([^,]*)", a)
  356. if(g):
  357. result = g.groups()
  358. in_data = re.sub("@" + result[0] + "@", result[1], in_data)
  359. a = re.sub("([^= ,]*)\=([^,]*)", "", a, 1)
  360. else:
  361. break
  362. in_data = toc_pas(in_data, results[0], num, toc_y)
  363. data = include.sub('\n<nobr><a href="/w/' + url_pas(results[0]) + '">[' + results[0] + ' 이동]</a><div>' + in_data + '</div><nobr>\n', data, 1)
  364. else:
  365. data = include.sub("<a class=\"not_thing\" href=\"/w/" + url_pas(results[0]) + "\">" + results[0] + "</a>", data, 1)
  366. else:
  367. break
  368. data = re.sub("\r\n##\s?([^\n]*)\r\n", "\r\n", data)
  369. data = re.sub("\[anchor\((?P<in>[^\[\]]*)\)\]", '<span id="\g<in>"></span>', data)
  370. data = savemark(data)
  371. d_re = re.findall('\r\n#(?:redirect|넘겨주기) ([^\n]+)', data)
  372. for d in d_re:
  373. view = d.replace('\\', '')
  374. sh = ''
  375. s_d = re.search('#((?:(?!x27;|#).)+)$', d)
  376. if(s_d):
  377. href = re.sub('#((?:(?!x27;|#).)+)$', '', d)
  378. sh = '#' + s_d.groups()[0]
  379. else:
  380. href = d
  381. 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)
  382. data = re.sub("\[nicovideo\((?P<in>[^,)]*)(?:(?:,(?:[^,)]*))+)?\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>]]", data)
  383. while(1):
  384. m = re.search("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", data)
  385. if(m):
  386. result = m.groups()
  387. blockquote = result[0]
  388. blockquote = re.sub("\n&gt;\s?", "\n", blockquote)
  389. data = re.sub("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", "\n<blockquote>" + blockquote + "</blockquote>", data, 1)
  390. else:
  391. break
  392. if(not re.search('\[목차\]', data)):
  393. if(not re.search('\[목차\(없음\)\]', data)):
  394. data = re.sub("(?P<in>(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "[목차]\n\g<in>", data, 1)
  395. else:
  396. data = re.sub("\[목차\(없음\)\]", "", data)
  397. data = re.sub("(\n)(?P<in>\r\n(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "\g<in>", data)
  398. data = toc_pas(data, title, num, toc_y)
  399. category = ''
  400. while(1):
  401. m = re.search("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", data)
  402. if(m):
  403. g = m.groups()
  404. if(title != g[0]):
  405. cat_plus(title, g[0], num)
  406. if(category == ''):
  407. curs.execute("select title from data where title = ?", [g[0]])
  408. exists = curs.fetchall()
  409. if(exists):
  410. red = ""
  411. else:
  412. red = 'class="not_thing"'
  413. category += '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  414. else:
  415. curs.execute("select title from data where title = ?", [g[0]])
  416. exists = curs.fetchall()
  417. if(exists):
  418. red = ""
  419. else:
  420. red = 'class="not_thing"'
  421. category += ' / ' + '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  422. data = re.sub("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", '', data, 1)
  423. else:
  424. break
  425. data = re.sub("&#x27;&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;&#x27;).)*)&#x27;&#x27;&#x27;", '<b>\g<in></b>', data)
  426. data = re.sub("&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;).)*)&#x27;&#x27;", '<i>\g<in></i>', data)
  427. data = re.sub('(?:~~|--)(?P<in>(?:(?!~~|--).)+)(?:~~|--)', '<s>\g<in></s>', data)
  428. data = re.sub('__(?P<in>.+?)__(?!_)', '<u>\g<in></u>', data)
  429. data = re.sub('\^\^(?P<in>.+?)\^\^(?!\^)', '<sup>\g<in></sup>', data)
  430. data = re.sub(',,(?P<in>.+?),,(?!,)', '<sub>\g<in></sub>', data)
  431. data = re.sub('&lt;math&gt;(?P<in>((?!&lt;math&gt;).)*)&lt;\/math&gt;', '$\g<in>$', data)
  432. data = re.sub('{{\|(?P<in>(?:(?:(?:(?!\|}}).)*)(?:\n?))+)\|}}', '<table> \
  433. <tbody> \
  434. <tr> \
  435. <td> \
  436. \g<in> \
  437. </td> \
  438. </tr> \
  439. </tbody> \
  440. </table>', data)
  441. data = re.sub('\[ruby\((?P<in>[^\,]*)\,\s?(?P<out>[^\)]*)\)\]', '<ruby> \
  442. \g<in> \
  443. <rp>(</rp> \
  444. <rt>\g<out></rt> \
  445. <rp>)</rp> \
  446. </ruby>', data)
  447. test = re.findall('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', data)
  448. if(test):
  449. for wiki in test:
  450. if(wiki[1]):
  451. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[1] + '</a>', data, 1)
  452. else:
  453. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[0] + '</a>', data, 1)
  454. data = re.sub("\[br\]",'<br>', data)
  455. while(1):
  456. com = re.compile("\[youtube\(([^, )]*)(,[^)]*)?\)\]")
  457. m = com.search(data)
  458. if(m):
  459. src = ''
  460. width = '560'
  461. height = '315'
  462. time = '0'
  463. result = m.groups()
  464. if(result[0]):
  465. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-_]*))$', result[0])
  466. if(yudt):
  467. if(yudt.groups()[0]):
  468. src = yudt.groups()[0]
  469. elif(yudt.groups()[1]):
  470. src = yudt.groups()[1]
  471. elif(yudt.groups()[2]):
  472. src = yudt.groups()[2]
  473. else:
  474. src = ''
  475. if(result[1]):
  476. mdata = re.search('width=([0-9%]*)', result[1])
  477. if(mdata):
  478. width = mdata.groups()[0]
  479. mdata = re.search('height=([0-9%]*)', result[1])
  480. if(mdata):
  481. height = mdata.groups()[0]
  482. mdata = re.search('time=([0-9]*)', result[1])
  483. if(mdata):
  484. time = mdata.groups()[0]
  485. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  486. else:
  487. break
  488. data = re.sub("\[\[(?::(?P<in>(?:분류|파일):(?:(?:(?!\]\]).)*)))\]\]", "[[\g<in>]]", data)
  489. a = re.findall('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', data)
  490. for i in a:
  491. b = re.search('(.*)\/', title)
  492. if(b):
  493. m = b.groups()
  494. if(i):
  495. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + i + ']]', data, 1)
  496. else:
  497. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + ']]', data, 1)
  498. else:
  499. if(i):
  500. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + i + ']]', data, 1)
  501. else:
  502. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + ']]', data, 1)
  503. data = re.sub('\[\[(?P<in>\/[^\]|]*)(?P<out>\|(?:[^\]]*))?\]\]', '[[' + title + '\g<in>\g<out>]]', data)
  504. link = re.compile('\[\[((?:(?!\[\[|\]\]|\|).)*)(?:\|((?:(?!\[\[|\]\]).)*))?\]\]')
  505. while(1):
  506. l_d = link.search(data)
  507. if(l_d):
  508. d = l_d.groups()
  509. if(re.search('^(?:파일|외부):', d[0])):
  510. width = ''
  511. height = ''
  512. align = ''
  513. span = ['', '']
  514. try:
  515. w_d = re.search('width=([0-9]+(?:[a-z%]+)?)', d[1])
  516. if(w_d):
  517. width = 'width="' + w_d.groups()[0] + '" '
  518. h_d = re.search('height=([0-9]+(?:[a-z%]+)?)', d[1])
  519. if(h_d):
  520. height = 'height="' + h_d.groups()[0] + '" '
  521. a_d = re.search('align=(center|right)', d[1])
  522. if(a_d):
  523. span[0] = '<span style="display: block; text-align: ' + a_d.groups()[0] + ';">'
  524. span[1] = '</span>'
  525. except:
  526. pass
  527. f_d = re.search('^파일:([^.]+)\.(.+)$', d[0])
  528. if(f_d):
  529. if(not re.search("^파일:([^\n]*)", title)):
  530. backlink_plus(title, d[0], 'file', num)
  531. img = span[0] + '<img src="/image/' + sha224(f_d.groups()[0]) + '.' + f_d.groups()[1] + '" ' + width + height + '>' + span[1]
  532. data = link.sub(img, data, 1)
  533. else:
  534. img = span[0] + '<img src="' + re.sub('^외부:', '', d[0]) + '" ' + width + height + '>' + span[1]
  535. data = link.sub(img, data, 1)
  536. elif(re.search('^https?:\/\/', d[0])):
  537. view = d[0]
  538. try:
  539. if(re.search('(.+)', d[1])):
  540. view = d[1]
  541. except:
  542. pass
  543. data = link.sub('<a class="out_link" rel="nofollow" href="' + d[0] + '">' + view + '</a>', data, 1)
  544. else:
  545. view = d[0].replace('\\', '')
  546. try:
  547. if(re.search('(.+)', d[1])):
  548. view = d[1]
  549. except:
  550. pass
  551. sh = ''
  552. s_d = re.search('#((?:(?!x27;|#).)+)$', d[0])
  553. if(s_d):
  554. href = re.sub('#((?:(?!x27;|#).)+)$', '', d[0])
  555. sh = '#' + s_d.groups()[0]
  556. else:
  557. href = d[0]
  558. if(d[0] == title):
  559. data = link.sub('<b>' + view + '</b>', data, 1)
  560. elif(re.search('^#', d[0])):
  561. data = link.sub('<a href="' + url_pas(href.replace('\\', '')) + sh + '">' + view + '</a>', data, 1)
  562. else:
  563. backlink_plus(title, href.replace('\\', ''), '', num)
  564. curs.execute("select title from data where title = ?", [href.replace('\\', '')])
  565. if(not curs.fetchall()):
  566. no = 'class="not_thing"'
  567. else:
  568. no = ''
  569. data = link.sub('<a ' + no + ' href="/w/' + url_pas(href.replace('\\', '').replace('&#x27;', "'")) + sh + '">' + view + '</a>', data, 1)
  570. else:
  571. break
  572. while(1):
  573. m = re.search("((?:(?:( +)\*\s(?:[^\n]*))\n?)+)", data)
  574. if(m):
  575. result = m.groups()
  576. end = str(result[0])
  577. while(1):
  578. isspace = re.search("( +)\*\s([^\n]*)", end)
  579. if(isspace):
  580. spacebar = isspace.groups()
  581. up = len(spacebar[0]) * 20
  582. end = re.sub("( +)\*\s([^\n]*)", "<li style='margin-left:" + str(up) + "px'>" + spacebar[1] + "</li>", end, 1)
  583. else:
  584. break
  585. end = re.sub("\n", '', end)
  586. data = re.sub("(?:(?:(?:( +)\*\s([^\n]*))\n?)+)", '<ul style="margin-top: 10px;" id="list">' + end + '</ul>', data, 1)
  587. else:
  588. break
  589. now_time = get_time()
  590. data = re.sub('\[date\]', now_time, data)
  591. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  592. time = time_data.groups()
  593. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  594. for age in age_data:
  595. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  596. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  597. e_data = old - will
  598. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  599. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  600. for dday in dday_data:
  601. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  602. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  603. e_data = old - will
  604. if(re.search('^-', str(e_data.days))):
  605. e_day = str(e_data.days)
  606. else:
  607. e_day = '+' + str(e_data.days)
  608. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  609. data = re.sub("-{4,11}", "<hr>", data)
  610. while(1):
  611. b = re.search("(<\/h[0-9]>|\n)( +)", data)
  612. if(b):
  613. result = b.groups()
  614. up = re.sub(' ', '<span id="in"></span>', result[1])
  615. if(re.search('<\/h[0-9]>', result[0])):
  616. data = re.sub("(?P<in>\/h[0-9]>)( +)", '\g<in>' + up, data, 1)
  617. else:
  618. data = re.sub("(?:\n)( +)", '<br>' + up, data, 1)
  619. else:
  620. break
  621. a = 1
  622. tou = "<hr style='margin-top: 30px;' id='footnote'><div><br>"
  623. namu = []
  624. pop_re = re.compile('\[\*([^\s]*)(?:\s((?:(?!\[|\]).)*))?\]')
  625. while(1):
  626. b = pop_re.search(data)
  627. if(b):
  628. results = b.groups()
  629. if(not results[1] and results[0]):
  630. i = 0
  631. while(1):
  632. try:
  633. if(namu[i] == results[0]):
  634. none_this = 0
  635. break
  636. else:
  637. i += 2
  638. except:
  639. none_this = 1
  640. break
  641. if(none_this == 0):
  642. data = pop_re.sub("<sup> \
  643. <a href='javascript:void(0);' onclick='folding(" + str(fol_num) + ");' id='rfn-" + str(a) + "'>[" + results[0] + "]</a> \
  644. </sup> \
  645. <div class='popup' style='display: none;' id='folding_" + str(fol_num) + "'> \
  646. <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] + " \
  647. </div>", data, 1)
  648. else:
  649. data = pop_re.sub("<sup> \
  650. <a href='javascript:void(0);' id='rfn-" + str(a) + "'>#d#" + results[0] + "#/d#</a> \
  651. </sup>", data, 1)
  652. else:
  653. if(results[0]):
  654. namu += [results[0]]
  655. namu += [results[1]]
  656. tou += "<span id='footnote-list'><a href='#rfn-" + str(a) + "' id='fn-" + str(a) + "'>[" + results[0] + "]</a> " + results[1] + "</span><br>"
  657. data = pop_re.sub("<sup> \
  658. <a href='javascript:void(0);' onclick='folding(" + str(fol_num) + ");' id='rfn-" + str(a) + "'>#d#" + results[0] + "#/d#</a> \
  659. </sup> \
  660. <div class='popup' style='display: none;' id='folding_" + str(fol_num) + "'> \
  661. <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] + " \
  662. </div>", data, 1)
  663. else:
  664. tou += "<span id='footnote-list'><a href='#rfn-" + str(a) + "' id='fn-" + str(a) + "'>[" + str(a) + "]</a> " + results[1] + "</span><br>"
  665. data = pop_re.sub('<sup> \
  666. <a href="javascript:void(0);" onclick="folding(' + str(fol_num) + ');" id="rfn-' + str(a) + '">#d#' + str(a) + '#/d#</a> \
  667. </sup> \
  668. <div class="popup" style="display: none;" id="folding_' + str(fol_num) + '"> \
  669. <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] + ' \
  670. </div>', data, 1)
  671. a += 1
  672. fol_num += 2
  673. else:
  674. tou += '</div>'
  675. if(tou == "<hr style='margin-top: 30px;' id='footnote'><div><br></div>"):
  676. tou = ""
  677. else:
  678. tou = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', tou)
  679. break
  680. data = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', data)
  681. data = re.sub("\[각주\](?:(?:<br>| |\r|\n)+)?$", "", data)
  682. data = re.sub("(?:(?:<br>| |\r|\n)+)$", "", data)
  683. data = re.sub("\[각주\]", "<br>" + tou, data)
  684. data += tou
  685. if(category):
  686. data += '<div style="margin-top: 30px;" id="cate">분류: ' + category + '</div>'
  687. data = re.sub("(?:\|\|\r\n)", "#table#<tablenobr>", data)
  688. while(1):
  689. y = re.search("(\|\|(?:(?:(?:(?:(?!\|\|).)*)(?:\n?))+))", data)
  690. if(y):
  691. a = y.groups()
  692. mid_data = re.sub("\|\|", "#table#", a[0])
  693. mid_data = re.sub("\r\n", "<br>", mid_data)
  694. data = re.sub("(\|\|((?:(?:(?:(?!\|\|).)*)(?:\n?))+))", mid_data, data, 1)
  695. else:
  696. break
  697. data = re.sub("#table#", "||", data)
  698. data = re.sub("<tablenobr>", "\r\n", data)
  699. while(1):
  700. m = re.search("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  701. if(m):
  702. results = m.groups()
  703. table = results[0]
  704. while(1):
  705. a = re.search("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  706. if(a):
  707. row = ''
  708. cel = ''
  709. celstyle = ''
  710. rowstyle = ''
  711. alltable = ''
  712. table_d = ''
  713. result = a.groups()
  714. if(result[1]):
  715. table_d = table_p(result[1], result[0])
  716. alltable = table_d[0]
  717. rowstyle = table_d[1]
  718. celstyle = table_d[2]
  719. row = table_d[3]
  720. cel = table_d[4]
  721. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table " + alltable + "> \
  722. <tbody> \
  723. <tr " + rowstyle + "> \
  724. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  725. else:
  726. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  727. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table> \
  728. <tbody> \
  729. <tr> \
  730. <td " + cel + ">", table, 1)
  731. else:
  732. break
  733. table = re.sub("\|\|$", "</td> \
  734. </tr> \
  735. </tbody> \
  736. </table>", table)
  737. while(1):
  738. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  739. if(b):
  740. row = ''
  741. cel = ''
  742. celstyle = ''
  743. rowstyle = ''
  744. table_d = ''
  745. result = b.groups()
  746. if(result[1]):
  747. table_d = table_p(result[1], result[0])
  748. rowstyle = table_d[1]
  749. celstyle = table_d[2]
  750. row = table_d[3]
  751. cel = table_d[4]
  752. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  753. </tr> \
  754. <tr " + rowstyle + "> \
  755. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  756. else:
  757. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  758. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  759. </tr> \
  760. <tr> \
  761. <td " + cel + ">", table, 1)
  762. else:
  763. break
  764. while(1):
  765. c = re.search("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  766. if(c):
  767. row = ''
  768. cel = ''
  769. celstyle = ''
  770. table_d = ''
  771. result = c.groups()
  772. if(result[1]):
  773. table_d = table_p(result[1], result[0])
  774. celstyle = table_d[2]
  775. row = table_d[3]
  776. cel = table_d[4]
  777. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  778. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  779. else:
  780. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  781. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  782. <td " + cel + ">", table, 1)
  783. else:
  784. break
  785. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  786. else:
  787. break
  788. data = re.sub("\r\n(?P<in><h[0-6])", "\g<in>", data)
  789. data = re.sub("(\n<nobr>|<nobr>\n|<nobr>)", "", data)
  790. data = re.sub("#no#(?P<in>.)#\/no#", "\g<in>", data)
  791. data = re.sub("<space>", " ", data)
  792. data = re.sub('<\/blockquote>(?:(?:\r)?\n){2}<blockquote>', '</blockquote><blockquote>', data)
  793. data = re.sub('<\/blockquote>(?:(?:\r)?\n)<br><blockquote>', '</blockquote><blockquote>', data)
  794. data = re.sub('\n', '<br>', data)
  795. data = re.sub('<isbr>', '\r\n', data)
  796. data = re.sub('^(?:<br>|\r|\n| )+', '', data)
  797. data = re.sub('^<div style="margin-top: 30px;" id="cate">', '<div id="cate">', data)
  798. conn.commit()
  799. return(data)