2
0

mark.py 42 KB

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