mark.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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('https:\/\/([^/\'" ]*)', 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"]):
  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 = re.sub('<(\/)?([^> ]+)( (?:[^>]+)?)?>', '%H%' + i_list[1] + po.sub('', ot) + '%/H%', data, 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. com = re.compile("{{{((?:(?!{{{|}}}).)*)}}}", re.DOTALL)
  176. while(1):
  177. is_it = com.search(data)
  178. if(is_it):
  179. it_d = is_it.groups()[0]
  180. big_a = re.compile("^\+([1-5])\s(.*)$", re.DOTALL)
  181. big = big_a.search(it_d)
  182. small_a = re.compile("^\-([1-5])\s(.*)$", re.DOTALL)
  183. small = small_a.search(it_d)
  184. color_b = re.compile("^(#(?:[0-9a-f-A-F]{6}|[0-9a-f-A-F]{3}))\s(.*)$", re.DOTALL)
  185. color_2 = color_b.search(it_d)
  186. color_c = re.compile("^#(\w+)\s(.*)$", re.DOTALL)
  187. color_3 = color_c.search(it_d)
  188. back_a = re.compile("^@([0-9a-f-A-F]{6})\s(.*)$", re.DOTALL)
  189. back = back_a.search(it_d)
  190. back_b = re.compile("^@([0-9a-f-A-F]{3})\s(.*)$", re.DOTALL)
  191. back_2 = back_b.search(it_d)
  192. back_c = re.compile("^@(\w+)\s(.*)$", re.DOTALL)
  193. back_3 = back_c.search(it_d)
  194. include_out_a = re.compile("^#!noin\s(.*)$", re.DOTALL)
  195. include_out = include_out_a.search(it_d)
  196. div_a = re.compile("^#!wiki\sstyle=(?:&quot;|&#x27;)((?:(?!&quot;|&#x27;).)*)(?:&quot;|&#x27;)\r\n(.*)$", re.DOTALL)
  197. div = div_a.search(it_d)
  198. html_a = re.compile("^#!html\s(.*)$", re.DOTALL)
  199. html = html_a.search(it_d)
  200. fol_a = re.compile("^#!folding\s((?:(?!\n).)*)\n?\s\n(.*)$", re.DOTALL)
  201. fol = fol_a.search(it_d)
  202. syn_a = re.compile("^#!syntax\s([^\n]*)\r\n(.*)$", re.DOTALL)
  203. syn = syn_a.search(it_d)
  204. if(big):
  205. big_d = big.groups()
  206. data = com.sub('<span style="font-size: ' + str(int(big_d[0]) * 20 + 100) + '%;">' + big_d[1] + '</span>', data, 1)
  207. elif(small):
  208. sm_d = small.groups()
  209. data = com.sub('<span style="font-size: ' + str(100 - int(sm_d[0]) * 10) + '%;">' + sm_d[1] + '</span>', data, 1)
  210. elif(color_2):
  211. c_d_2 = color_2.groups()
  212. data = com.sub('<span style="color: ' + c_d_2[0] + '">' + c_d_2[1] + '</span>', data, 1)
  213. elif(color_3):
  214. c_d_3 = color_3.groups()
  215. data = com.sub('<span style="color: ' + c_d_3[0] + '">' + c_d_3[1] + '</span>', data, 1)
  216. elif(back):
  217. back_d_1 = back.groups()
  218. data = com.sub('<span style="background: #' + back_d_1[0] + '">' + back_d_1[1] + '</span>', data, 1)
  219. elif(back_2):
  220. back_d_2 = back_2.groups()
  221. data = com.sub('<span style="background: #' + back_d_2[0] + '">' + back_d_2[1] + '</span>', data, 1)
  222. elif(back_3):
  223. back_d_3 = back_3.groups()
  224. data = com.sub('<span style="background: ' + back_d_3[0] + '">' + back_d_3[1] + '</span>', data, 1)
  225. elif(div):
  226. div_d = div.groups()
  227. data = com.sub('<div style="' + div_d[0] + '">' + div_d[1] + '</div>', data, 1)
  228. elif(html):
  229. data = com.sub(html.groups()[0], data, 1)
  230. elif(fol):
  231. fol_d = fol.groups()
  232. data = com.sub( "<div> \
  233. " + fol_d[0] + " \
  234. <div id='folding_" + str(fol_num + 1) + "' style='display: inline-block;'> \
  235. [<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + "); folding(" + str(fol_num) + ");'>펼치기</a>] \
  236. </div> \
  237. <div id='folding_" + str(fol_num + 2) + "' style='display: none;'> \
  238. [<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + "); folding(" + str(fol_num) + ");'>접기</a>] \
  239. </div> \
  240. <div id='folding_" + str(fol_num) + "' style='display: none;'> \
  241. <br> \
  242. " + fol_d[1] + " \
  243. </div> \
  244. </div>", data, 1)
  245. fol_num += 3
  246. elif(syn):
  247. syn_d = syn.groups()
  248. 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)
  249. elif(include_out):
  250. if((include or in_c) == 1):
  251. data = com.sub("", data, 1)
  252. else:
  253. data = com.sub(include_out.groups()[0], data, 1)
  254. else:
  255. data = com.sub('<code>' + it_d + '</code>', data, 1)
  256. else:
  257. break
  258. com = re.compile("<code>((?:(?!(?:<code>|<\/code>)).)*)<\/code>", re.DOTALL)
  259. da_com = com.findall(data)
  260. for com_da in da_com:
  261. mid_data = com_da.replace('<', '&lt;').replace('>', '&gt;')
  262. mid_data = re.sub("(?P<in>.)", "#no#\g<in>#/no#", mid_data)
  263. data = com.sub(mid_data, data, 1)
  264. return([data, fol_num])
  265. def toc_pas(data, title, num, toc_y):
  266. i = [0, 0, 0, 0, 0, 0, 0]
  267. last = 0
  268. toc_c = -1
  269. toc_d = -1
  270. span = ''
  271. rtoc = '<div id="toc"><span id="toc-name">목차</span><br><br>'
  272. while(1):
  273. i[0] += 1
  274. m = re.search('(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\r\n', data)
  275. if(m):
  276. result = m.groups()
  277. wiki = len(result[0])
  278. if(last < wiki):
  279. last = wiki
  280. else:
  281. last = wiki
  282. for a in range(wiki + 1, 7):
  283. i[a] = 0
  284. i[wiki] += 1
  285. toc = str(i[1]) + '.' + str(i[2]) + '.' + str(i[3]) + '.' + str(i[4]) + '.' + str(i[5]) + '.' + str(i[6]) + '.'
  286. toc = re.sub("(?P<in>[0-9]0(?:[0]*)?)\.", '\g<in>#.', toc)
  287. toc = re.sub("0\.", '', toc)
  288. toc = re.sub("#\.", '.', toc)
  289. toc = re.sub("\.$", '', toc)
  290. if(toc_c == -1):
  291. margin = 'style="margin-top: 30px;"'
  292. toc_c = toc.count('.')
  293. else:
  294. toc_d = toc.count('.')
  295. if(toc_c == toc_d):
  296. margin = 'style="margin-top: 30px;"'
  297. else:
  298. if(toc_d < toc_c):
  299. margin = 'style="margin-top: 30px;"'
  300. else:
  301. margin = ''
  302. toc_c = toc_d
  303. t = toc.count('.')
  304. span = '<span style="margin-left: 5px;"></span>' * t
  305. rtoc += span + '<a href="#s-' + toc + '">' + toc + '</a>. ' + result[1] + '<br>'
  306. c = re.sub(" $", "", result[1])
  307. d = c
  308. c = re.sub("\[\[(([^|]*)\|)?(?P<in>[^\]]*)\]\]", "\g<in>", c)
  309. edit_d = ''
  310. if(toc_y == 1):
  311. edit_d = ' <span style="font-size:11px;">[<a href="/edit/' + url_pas(title) + '/section/' + str(i[0]) + '">편집</a>]</span>'
  312. 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)
  313. else:
  314. rtoc += '</div>'
  315. break
  316. data = re.sub("\[목차\]", rtoc, data)
  317. return(data)
  318. def backlink_plus(name, link, backtype, num):
  319. if(num == 1):
  320. curs.execute("select title from back where title = ? and link = ? and type = ?", [link, name, backtype])
  321. if(not curs.fetchall()):
  322. try:
  323. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  324. except:
  325. while(1):
  326. try:
  327. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  328. break
  329. except:
  330. time.sleep(1)
  331. def cat_plus(name, link, num):
  332. if(num == 1):
  333. curs.execute("select title from cat where title = ? and cat = ?", [link, name])
  334. if(not curs.fetchall()):
  335. try:
  336. curs.execute("insert into cat (title, cat) values (?, ?)", [link, name])
  337. except:
  338. while(1):
  339. try:
  340. curs.execute("insert into cat (title, cat) values (?, ?)", [link, name])
  341. break
  342. except:
  343. time.sleep(1)
  344. def namumark(title, data, num, in_c, toc_y):
  345. data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", data))
  346. data = html_pas(data)
  347. data = '\r\n' + data + '\r\n'
  348. fol_num = 0
  349. var_d = mid_pas(data, fol_num, 0, in_c)
  350. data = var_d[0]
  351. fol_num = var_d[1]
  352. include = re.compile("\[include\(((?:(?!\)\]|,).)*)((?:(?:,\s?(?:(?!\)\]).)*))+)?\)\]")
  353. while(1):
  354. m = include.search(data)
  355. if(m):
  356. results = m.groups()
  357. if(results[0] == title):
  358. data = include.sub("<b>" + results[0] + "</b>", data, 1)
  359. else:
  360. curs.execute("select data from data where title = ?", [results[0]])
  361. in_con = curs.fetchall()
  362. backlink_plus(title, results[0], 'include', num)
  363. if(in_con):
  364. in_data = in_con[0][0]
  365. in_data = include.sub("", in_data)
  366. in_data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", in_data))
  367. in_data = html_pas(in_data)
  368. var_d = mid_pas(in_data, fol_num, 1, in_c)
  369. in_data = var_d[0]
  370. fol_num = var_d[1]
  371. if(results[1]):
  372. a = results[1]
  373. while(1):
  374. g = re.search("([^= ,]*)\=([^,]*)", a)
  375. if(g):
  376. result = g.groups()
  377. in_data = re.sub("@" + result[0] + "@", result[1], in_data)
  378. a = re.sub("([^= ,]*)\=([^,]*)", "", a, 1)
  379. else:
  380. break
  381. in_data = toc_pas(in_data, results[0], num, toc_y)
  382. data = include.sub('\n<nobr><a href="/w/' + url_pas(results[0]) + '">[' + results[0] + ' 이동]</a><div>' + in_data + '</div><nobr>\n', data, 1)
  383. else:
  384. data = include.sub("<a class=\"not_thing\" href=\"/w/" + url_pas(results[0]) + "\">" + results[0] + "</a>", data, 1)
  385. else:
  386. break
  387. data = re.sub("\r\n##\s?([^\n]*)\r\n", "\r\n", data)
  388. data = re.sub("\[anchor\((?P<in>[^\[\]]*)\)\]", '<span id="\g<in>"></span>', data)
  389. data = savemark(data)
  390. d_re = re.findall('\r\n#(?:redirect|넘겨주기) ([^\n]+)', data)
  391. for d in d_re:
  392. view = d.replace('\\', '')
  393. sh = ''
  394. s_d = re.search('#((?:(?!x27;|#).)+)$', d)
  395. if(s_d):
  396. href = re.sub('#((?:(?!x27;|#).)+)$', '', d)
  397. sh = '#' + s_d.groups()[0]
  398. else:
  399. href = d
  400. 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)
  401. data = re.sub("\[nicovideo\((?P<in>[^,)]*)(?:(?:,(?:[^,)]*))+)?\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>]]", data)
  402. while(1):
  403. m = re.search("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", data)
  404. if(m):
  405. result = m.groups()
  406. blockquote = result[0]
  407. blockquote = re.sub("\n&gt;\s?", "\n", blockquote)
  408. data = re.sub("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", "\n<blockquote>" + blockquote + "</blockquote>", data, 1)
  409. else:
  410. break
  411. if(not re.search('\[목차\]', data)):
  412. if(not re.search('\[목차\(없음\)\]', data)):
  413. data = re.sub("(?P<in>(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "[목차]\n\g<in>", data, 1)
  414. else:
  415. data = re.sub("\[목차\(없음\)\]", "", data)
  416. data = re.sub("(\n)(?P<in>\r\n(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "\g<in>", data)
  417. data = toc_pas(data, title, num, toc_y)
  418. category = ''
  419. while(1):
  420. m = re.search("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", data)
  421. if(m):
  422. g = m.groups()
  423. if(title != g[0]):
  424. cat_plus(title, g[0], num)
  425. if(category == ''):
  426. curs.execute("select title from data where title = ?", [g[0]])
  427. exists = curs.fetchall()
  428. if(exists):
  429. red = ""
  430. else:
  431. red = 'class="not_thing"'
  432. category += '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  433. else:
  434. curs.execute("select title from data where title = ?", [g[0]])
  435. exists = curs.fetchall()
  436. if(exists):
  437. red = ""
  438. else:
  439. red = 'class="not_thing"'
  440. category += ' / ' + '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  441. data = re.sub("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", '', data, 1)
  442. else:
  443. break
  444. data = re.sub("&#x27;&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;&#x27;).)*)&#x27;&#x27;&#x27;", '<b>\g<in></b>', data)
  445. data = re.sub("&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;).)*)&#x27;&#x27;", '<i>\g<in></i>', data)
  446. data = re.sub('(?:~~|--)(?P<in>(?:(?!~~|--).)+)(?:~~|--)', '<s>\g<in></s>', data)
  447. data = re.sub('__(?P<in>.+?)__(?!_)', '<u>\g<in></u>', data)
  448. data = re.sub('\^\^(?P<in>.+?)\^\^(?!\^)', '<sup>\g<in></sup>', data)
  449. data = re.sub(',,(?P<in>.+?),,(?!,)', '<sub>\g<in></sub>', data)
  450. data = re.sub('&lt;math&gt;(?P<in>((?!&lt;math&gt;).)*)&lt;\/math&gt;', '$\g<in>$', data)
  451. data = re.sub('{{\|(?P<in>(?:(?:(?:(?!\|}}).)*)(?:\n?))+)\|}}', '<table> \
  452. <tbody> \
  453. <tr> \
  454. <td> \
  455. \g<in> \
  456. </td> \
  457. </tr> \
  458. </tbody> \
  459. </table>', data)
  460. data = re.sub('\[ruby\((?P<in>[^\,]*)\,\s?(?P<out>[^\)]*)\)\]', '<ruby> \
  461. \g<in> \
  462. <rp>(</rp> \
  463. <rt>\g<out></rt> \
  464. <rp>)</rp> \
  465. </ruby>', data)
  466. test = re.findall('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', data)
  467. if(test):
  468. for wiki in test:
  469. if(wiki[1]):
  470. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[1] + '</a>', data, 1)
  471. else:
  472. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[0] + '</a>', data, 1)
  473. data = re.sub("\[br\]",'<br>', data)
  474. while(1):
  475. com = re.compile("\[youtube\(([^, )]*)(,[^)]*)?\)\]")
  476. m = com.search(data)
  477. if(m):
  478. src = ''
  479. width = '560'
  480. height = '315'
  481. time = '0'
  482. result = m.groups()
  483. if(result[0]):
  484. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-_]*))$', result[0])
  485. if(yudt):
  486. if(yudt.groups()[0]):
  487. src = yudt.groups()[0]
  488. elif(yudt.groups()[1]):
  489. src = yudt.groups()[1]
  490. elif(yudt.groups()[2]):
  491. src = yudt.groups()[2]
  492. else:
  493. src = ''
  494. if(result[1]):
  495. mdata = re.search('width=([0-9%]*)', result[1])
  496. if(mdata):
  497. width = mdata.groups()[0]
  498. mdata = re.search('height=([0-9%]*)', result[1])
  499. if(mdata):
  500. height = mdata.groups()[0]
  501. mdata = re.search('time=([0-9]*)', result[1])
  502. if(mdata):
  503. time = mdata.groups()[0]
  504. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  505. else:
  506. break
  507. data = re.sub("\[\[(?::(?P<in>(?:분류|파일):(?:(?:(?!\]\]).)*)))\]\]", "[[\g<in>]]", data)
  508. a = re.findall('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', data)
  509. for i in a:
  510. b = re.search('(.*)\/', title)
  511. if(b):
  512. m = b.groups()
  513. if(i):
  514. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + i + ']]', data, 1)
  515. else:
  516. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + ']]', data, 1)
  517. else:
  518. if(i):
  519. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + i + ']]', data, 1)
  520. else:
  521. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + ']]', data, 1)
  522. data = re.sub('\[\[(?P<in>\/[^\]|]*)(?P<out>\|(?:[^\]]*))?\]\]', '[[' + title + '\g<in>\g<out>]]', data)
  523. link = re.compile('\[\[((?:(?!\[\[|\]\]|\|).)*)(?:\|((?:(?!\[\[|\]\]).)*))?\]\]')
  524. while(1):
  525. l_d = link.search(data)
  526. if(l_d):
  527. d = l_d.groups()
  528. if(re.search('^(?:파일|외부):', d[0])):
  529. width = ''
  530. height = ''
  531. align = ''
  532. span = ['', '']
  533. try:
  534. w_d = re.search('width=([0-9]+(?:[a-z%]+)?)', d[1])
  535. if(w_d):
  536. width = 'width="' + w_d.groups()[0] + '" '
  537. h_d = re.search('height=([0-9]+(?:[a-z%]+)?)', d[1])
  538. if(h_d):
  539. height = 'height="' + h_d.groups()[0] + '" '
  540. a_d = re.search('align=(center|right)', d[1])
  541. if(a_d):
  542. span[0] = '<span style="display: block; text-align: ' + a_d.groups()[0] + ';">'
  543. span[1] = '</span>'
  544. except:
  545. pass
  546. f_d = re.search('^파일:([^.]+)\.(.+)$', d[0])
  547. if(f_d):
  548. if(not re.search("^파일:([^\n]*)", title)):
  549. backlink_plus(title, d[0], 'file', num)
  550. img = span[0] + '<img src="/image/' + sha224(f_d.groups()[0]) + '.' + f_d.groups()[1] + '" ' + width + height + '>' + span[1]
  551. data = link.sub(img, data, 1)
  552. else:
  553. img = span[0] + '<img src="' + re.sub('^외부:', '', d[0]) + '" ' + width + height + '>' + span[1]
  554. data = link.sub(img, data, 1)
  555. elif(re.search('^https?:\/\/', d[0])):
  556. view = d[0]
  557. try:
  558. if(re.search('(.+)', d[1])):
  559. view = d[1]
  560. except:
  561. pass
  562. data = link.sub('<a class="out_link" rel="nofollow" href="' + d[0] + '">' + view + '</a>', data, 1)
  563. else:
  564. view = d[0].replace('\\', '')
  565. try:
  566. if(re.search('(.+)', d[1])):
  567. view = d[1]
  568. except:
  569. pass
  570. sh = ''
  571. s_d = re.search('#((?:(?!x27;|#).)+)$', d[0])
  572. if(s_d):
  573. href = re.sub('#((?:(?!x27;|#).)+)$', '', d[0])
  574. sh = '#' + s_d.groups()[0]
  575. else:
  576. href = d[0]
  577. if(d[0] == title):
  578. data = link.sub('<b>' + view + '</b>', data, 1)
  579. elif(re.search('^#', d[0])):
  580. data = link.sub('<a href="' + url_pas(href.replace('\\', '')) + sh + '">' + view + '</a>', data, 1)
  581. else:
  582. backlink_plus(title, href.replace('\\', ''), '', num)
  583. curs.execute("select title from data where title = ?", [href.replace('\\', '')])
  584. if(not curs.fetchall()):
  585. no = 'class="not_thing"'
  586. else:
  587. no = ''
  588. data = link.sub('<a ' + no + ' href="/w/' + url_pas(href.replace('\\', '').replace('&#x27;', "'")) + sh + '">' + view + '</a>', data, 1)
  589. else:
  590. break
  591. while(1):
  592. m = re.search("((?:(?:( +)\*\s(?:[^\n]*))\n?)+)", data)
  593. if(m):
  594. result = m.groups()
  595. end = str(result[0])
  596. while(1):
  597. isspace = re.search("( +)\*\s([^\n]*)", end)
  598. if(isspace):
  599. spacebar = isspace.groups()
  600. up = len(spacebar[0]) * 20
  601. end = re.sub("( +)\*\s([^\n]*)", "<li style='margin-left:" + str(up) + "px'>" + spacebar[1] + "</li>", end, 1)
  602. else:
  603. break
  604. end = re.sub("\n", '', end)
  605. data = re.sub("(?:(?:(?:( +)\*\s([^\n]*))\n?)+)", '<ul style="margin-top: 10px;" id="list">' + end + '</ul>', data, 1)
  606. else:
  607. break
  608. now_time = get_time()
  609. data = re.sub('\[date\]', now_time, data)
  610. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  611. time = time_data.groups()
  612. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  613. for age in age_data:
  614. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  615. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  616. e_data = old - will
  617. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  618. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  619. for dday in dday_data:
  620. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  621. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  622. e_data = old - will
  623. if(re.search('^-', str(e_data.days))):
  624. e_day = str(e_data.days)
  625. else:
  626. e_day = '+' + str(e_data.days)
  627. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  628. data = re.sub("-{4,11}", "<hr>", data)
  629. while(1):
  630. b = re.search("(<\/h[0-9]>|\n)( +)", data)
  631. if(b):
  632. result = b.groups()
  633. up = re.sub(' ', '<span id="in"></span>', result[1])
  634. if(re.search('<\/h[0-9]>', result[0])):
  635. data = re.sub("(?P<in>\/h[0-9]>)( +)", '\g<in>' + up, data, 1)
  636. else:
  637. data = re.sub("(?:\n)( +)", '<br>' + up, data, 1)
  638. else:
  639. break
  640. a = 1
  641. tou = "<hr style='margin-top: 30px;' id='footnote'><div><br>"
  642. namu = []
  643. pop_re = re.compile('\[\*([^\s]*)(?:\s((?:(?!\[|\]).)*))?\]')
  644. while(1):
  645. b = pop_re.search(data)
  646. if(b):
  647. results = b.groups()
  648. if(not results[1] and results[0]):
  649. i = 0
  650. while(1):
  651. try:
  652. if(namu[i] == results[0]):
  653. none_this = 0
  654. break
  655. else:
  656. i += 2
  657. except:
  658. none_this = 1
  659. break
  660. if(none_this == 0):
  661. data = pop_re.sub("<sup> \
  662. <a href='javascript:void(0);' onclick='folding(" + str(fol_num) + ");' id='rfn-" + str(a) + "'>[" + results[0] + "]</a> \
  663. </sup> \
  664. <div class='popup' style='display: none;' id='folding_" + str(fol_num) + "'> \
  665. <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] + " \
  666. </div>", data, 1)
  667. else:
  668. data = pop_re.sub("<sup> \
  669. <a href='javascript:void(0);' id='rfn-" + str(a) + "'>#d#" + results[0] + "#/d#</a> \
  670. </sup>", data, 1)
  671. else:
  672. if(results[0]):
  673. namu += [results[0]]
  674. namu += [results[1]]
  675. tou += "<span id='footnote-list'><a href='#rfn-" + str(a) + "' id='fn-" + str(a) + "'>[" + results[0] + "]</a> " + results[1] + "</span><br>"
  676. data = pop_re.sub("<sup> \
  677. <a href='javascript:void(0);' onclick='folding(" + str(fol_num) + ");' id='rfn-" + str(a) + "'>#d#" + results[0] + "#/d#</a> \
  678. </sup> \
  679. <div class='popup' style='display: none;' id='folding_" + str(fol_num) + "'> \
  680. <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] + " \
  681. </div>", data, 1)
  682. else:
  683. tou += "<span id='footnote-list'><a href='#rfn-" + str(a) + "' id='fn-" + str(a) + "'>[" + str(a) + "]</a> " + results[1] + "</span><br>"
  684. data = pop_re.sub('<sup> \
  685. <a href="javascript:void(0);" onclick="folding(' + str(fol_num) + ');" id="rfn-' + str(a) + '">#d#' + str(a) + '#/d#</a> \
  686. </sup> \
  687. <div class="popup" style="display: none;" id="folding_' + str(fol_num) + '"> \
  688. <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] + ' \
  689. </div>', data, 1)
  690. a += 1
  691. fol_num += 2
  692. else:
  693. tou += '</div>'
  694. if(tou == "<hr style='margin-top: 30px;' id='footnote'><div><br></div>"):
  695. tou = ""
  696. else:
  697. tou = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', tou)
  698. break
  699. data = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', data)
  700. data = re.sub("\[각주\](?:(?:<br>| |\r|\n)+)?$", "", data)
  701. data = re.sub("(?:(?:<br>| |\r|\n)+)$", "", data)
  702. data = re.sub("\[각주\]", "<br>" + tou, data)
  703. data += tou
  704. if(category):
  705. data += '<div style="margin-top: 30px;" id="cate">분류: ' + category + '</div>'
  706. data = re.sub("(?:\|\|\r\n)", "#table#<tablenobr>", data)
  707. while(1):
  708. y = re.search("(\|\|(?:(?:(?:(?:(?!\|\|).)*)(?:\n?))+))", data)
  709. if(y):
  710. a = y.groups()
  711. mid_data = re.sub("\|\|", "#table#", a[0])
  712. mid_data = re.sub("\r\n", "<br>", mid_data)
  713. data = re.sub("(\|\|((?:(?:(?:(?!\|\|).)*)(?:\n?))+))", mid_data, data, 1)
  714. else:
  715. break
  716. data = re.sub("#table#", "||", data)
  717. data = re.sub("<tablenobr>", "\r\n", data)
  718. while(1):
  719. m = re.search("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  720. if(m):
  721. results = m.groups()
  722. table = results[0]
  723. while(1):
  724. a = re.search("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  725. if(a):
  726. row = ''
  727. cel = ''
  728. celstyle = ''
  729. rowstyle = ''
  730. alltable = ''
  731. table_d = ''
  732. result = a.groups()
  733. if(result[1]):
  734. table_d = table_p(result[1], result[0])
  735. alltable = table_d[0]
  736. rowstyle = table_d[1]
  737. celstyle = table_d[2]
  738. row = table_d[3]
  739. cel = table_d[4]
  740. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table " + alltable + "> \
  741. <tbody> \
  742. <tr " + rowstyle + "> \
  743. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  744. else:
  745. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  746. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table> \
  747. <tbody> \
  748. <tr> \
  749. <td " + cel + ">", table, 1)
  750. else:
  751. break
  752. table = re.sub("\|\|$", "</td> \
  753. </tr> \
  754. </tbody> \
  755. </table>", table)
  756. while(1):
  757. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  758. if(b):
  759. row = ''
  760. cel = ''
  761. celstyle = ''
  762. rowstyle = ''
  763. table_d = ''
  764. result = b.groups()
  765. if(result[1]):
  766. table_d = table_p(result[1], result[0])
  767. rowstyle = table_d[1]
  768. celstyle = table_d[2]
  769. row = table_d[3]
  770. cel = table_d[4]
  771. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  772. </tr> \
  773. <tr " + rowstyle + "> \
  774. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  775. else:
  776. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  777. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  778. </tr> \
  779. <tr> \
  780. <td " + cel + ">", table, 1)
  781. else:
  782. break
  783. while(1):
  784. c = re.search("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  785. if(c):
  786. row = ''
  787. cel = ''
  788. celstyle = ''
  789. table_d = ''
  790. result = c.groups()
  791. if(result[1]):
  792. table_d = table_p(result[1], result[0])
  793. celstyle = table_d[2]
  794. row = table_d[3]
  795. cel = table_d[4]
  796. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  797. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  798. else:
  799. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  800. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  801. <td " + cel + ">", table, 1)
  802. else:
  803. break
  804. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  805. else:
  806. break
  807. data = re.sub("\r\n(?P<in><h[0-6])", "\g<in>", data)
  808. data = re.sub("(\n<nobr>|<nobr>\n|<nobr>)", "", data)
  809. data = re.sub("#no#(?P<in>.)#\/no#", "\g<in>", data)
  810. data = re.sub("<space>", " ", data)
  811. data = re.sub('<\/blockquote>(?:(?:\r)?\n){2}<blockquote>', '</blockquote><blockquote>', data)
  812. data = re.sub('<\/blockquote>(?:(?:\r)?\n)<br><blockquote>', '</blockquote><blockquote>', data)
  813. data = re.sub('\n', '<br>', data)
  814. data = re.sub('<isbr>', '\r\n', data)
  815. data = re.sub('^(?:<br>|\r|\n| )+', '', data)
  816. data = re.sub('^<div style="margin-top: 30px;" id="cate">', '<div id="cate">', data)
  817. return(data)