mark.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. table_class = 'class="'
  59. alltable = 'style="'
  60. celstyle = 'style="'
  61. rowstyle = 'style="'
  62. row = ''
  63. cel = ''
  64. table_w = re.search("&lt;table\s?width=((?:(?!&gt;).)*)&gt;", d)
  65. table_h = re.search("&lt;table\s?height=((?:(?!&gt;).)*)&gt;", d)
  66. table_a = re.search("&lt;table\s?align=((?:(?!&gt;).)*)&gt;", d)
  67. if(table_w):
  68. alltable += 'width: ' + table_w.groups()[0] + ';'
  69. if(table_h):
  70. alltable += 'height: ' + table_h.groups()[0] + ';'
  71. if(table_a):
  72. if(table_a.groups()[0] == 'right'):
  73. alltable += 'float: right;'
  74. elif(table_a.groups()[0] == 'center'):
  75. alltable += 'margin: auto;'
  76. table_t_a = re.search("&lt;table\s?textalign=((?:(?!&gt;).)*)&gt;", d)
  77. if(table_t_a):
  78. if(table_t_a.groups()[0] == 'right'):
  79. alltable += 'text-align: right;'
  80. elif(table_t_a.groups()[0] == 'center'):
  81. alltable += 'text-align: center;'
  82. row_t_a = re.search("&lt;row\s?textalign=((?:(?!&gt;).)*)&gt;", d)
  83. if(row_t_a):
  84. if(row_t_a.groups()[0] == 'right'):
  85. rowstyle += 'text-align: right;'
  86. elif(row_t_a.groups()[0] == 'center'):
  87. rowstyle += 'text-align: center;'
  88. else:
  89. rowstyle += 'text-align: left;'
  90. table_cel = re.search("&lt;-((?:(?!&gt;).)*)&gt;", d)
  91. if(table_cel):
  92. cel = 'colspan="' + table_cel.groups()[0] + '"'
  93. else:
  94. cel = 'colspan="' + str(round(len(d2) / 2)) + '"'
  95. table_row = re.search("&lt;\|((?:(?!&gt;).)*)&gt;", d)
  96. if(table_row):
  97. row = 'rowspan="' + table_row.groups()[0] + '"'
  98. row_bgcolor_2 = re.search("&lt;rowbgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})&gt;", d)
  99. row_bgcolor_3 = re.search("&lt;rowbgcolor=(\w+)&gt;", d)
  100. if(row_bgcolor_2):
  101. rowstyle += 'background: ' + row_bgcolor_2.groups()[0] + ';'
  102. elif(row_bgcolor_3):
  103. rowstyle += 'background: ' + row_bgcolor_3.groups()[0] + ';'
  104. table_border_2 = re.search("&lt;table\s?bordercolor=(#(?:[0-9a-f-A-F]{3}){1,2})&gt;", d)
  105. table_border_3 = re.search("&lt;table\s?bordercolor=(\w+)&gt;", d)
  106. if(table_border_2):
  107. alltable += 'border: ' + table_border_2.groups()[0] + ' 2px solid;'
  108. elif(table_border_3):
  109. alltable += 'border: ' + table_border_3.groups()[0] + ' 2px solid;'
  110. table_bgcolor_2 = re.search("&lt;table\s?bgcolor=(#(?:[0-9a-f-A-F]{3}){1,2})&gt;", d)
  111. table_bgcolor_3 = re.search("&lt;table\s?bgcolor=(\w+)&gt;", d)
  112. if(table_bgcolor_2):
  113. alltable += 'background: ' + table_bgcolor_2.groups()[0] + ';'
  114. elif(table_bgcolor_3):
  115. alltable += 'background: ' + table_bgcolor_3.groups()[0] + ';'
  116. bgcolor_2 = re.search("&lt;(?:bgcolor=)?(#(?:[0-9a-f-A-F]{3}){1,2})&gt;", d)
  117. bgcolor_3 = re.search("&lt;(?:bgcolor=)?(\w+)&gt;", d)
  118. if(bgcolor_2):
  119. celstyle += 'background: ' + bgcolor_2.groups()[0] + ';'
  120. elif(bgcolor_3):
  121. celstyle += 'background: ' + bgcolor_3.groups()[0] + ';'
  122. n_width = re.search("&lt;width=((?:(?!&gt;).)*)&gt;", d)
  123. n_height = re.search("&lt;height=((?:(?!&gt;).)*)&gt;", d)
  124. if(n_width):
  125. celstyle += 'width: ' + n_width.groups()[0] + ';'
  126. if(n_height):
  127. celstyle += 'height: ' + n_height.groups()[0] + ';'
  128. text_right = re.search("&lt;\)&gt;", d)
  129. text_center = re.search("&lt;:&gt;", d)
  130. text_left = re.search("&lt;\(&gt;", d)
  131. if(text_right):
  132. celstyle += 'text-align: right;'
  133. elif(text_center):
  134. celstyle += 'text-align: center;'
  135. elif(text_left):
  136. celstyle += 'text-align: left;'
  137. text_class = re.search("&lt;table\s?class=((?:(?!&gt;).)+)&gt;", d)
  138. if(text_class):
  139. d = text_class.groups()
  140. table_class += d[0]
  141. alltable += '"'
  142. celstyle += '"'
  143. rowstyle += '"'
  144. table_class += '"'
  145. return([alltable, rowstyle, celstyle, row, cel, table_class])
  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;">\n', data, 1)
  332. else:
  333. rtoc += '</div>'
  334. break
  335. data = re.sub("\[목차\]", rtoc, data)
  336. return(data)
  337. def link(title, data, num, category):
  338. while(1):
  339. m = re.search("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", data)
  340. if(m):
  341. g = m.groups()
  342. if(title != g[0]):
  343. backlink_plus(title, g[0], 'cat', num)
  344. if(category == ''):
  345. curs.execute("select title from data where title = ?", [g[0]])
  346. exists = curs.fetchall()
  347. if(exists):
  348. red = ""
  349. else:
  350. red = 'class="not_thing"'
  351. category += '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  352. else:
  353. curs.execute("select title from data where title = ?", [g[0]])
  354. exists = curs.fetchall()
  355. if(exists):
  356. red = ""
  357. else:
  358. red = 'class="not_thing"'
  359. category += ' / ' + '<a ' + red + ' href="/w/' + url_pas(g[0]) + '">' + re.sub("분류:", "", g[0]) + '</a>'
  360. data = re.sub("\[\[(분류:(?:(?:(?!\]\]).)*))\]\]", '', data, 1)
  361. else:
  362. break
  363. test = re.findall('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', data)
  364. if(test):
  365. for wiki in test:
  366. if(wiki[1]):
  367. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[1] + '</a>', data, 1)
  368. else:
  369. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + wiki[0] + '</a>', data, 1)
  370. data = re.sub("\[\[(?::(?P<in>(?:분류|파일):(?:(?:(?!\]\]).)*)))\]\]", "[[\g<in>]]", data)
  371. a = re.findall('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', data)
  372. for i in a:
  373. b = re.search('(.*)\/', title)
  374. if(b):
  375. m = b.groups()
  376. if(i):
  377. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + i + ']]', data, 1)
  378. else:
  379. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + m[0] + ']]', data, 1)
  380. else:
  381. if(i):
  382. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + i + ']]', data, 1)
  383. else:
  384. data = re.sub('\[\[\.\.\/(\|(?:[^\]]*))?\]\]', '[[' + title + ']]', data, 1)
  385. data = re.sub('\[\[(?P<in>\/[^\]|]*)(?P<out>\|(?:[^\]]*))?\]\]', '[[' + title + '\g<in>\g<out>]]', data)
  386. link = re.compile('\[\[((?:(?!\[\[|\]\]|\|).)*)(?:\|((?:(?!\[\[|\]\]).)*))?\]\]')
  387. while(1):
  388. l_d = link.search(data)
  389. if(l_d):
  390. d = l_d.groups()
  391. if(re.search('^(?:파일|외부):', d[0])):
  392. width = ''
  393. height = ''
  394. align = ''
  395. span = ['', '']
  396. try:
  397. w_d = re.search('width=([0-9]+(?:[a-z%]+)?)', d[1])
  398. if(w_d):
  399. width = 'width="' + w_d.groups()[0] + '" '
  400. h_d = re.search('height=([0-9]+(?:[a-z%]+)?)', d[1])
  401. if(h_d):
  402. height = 'height="' + h_d.groups()[0] + '" '
  403. a_d = re.search('align=(center|right)', d[1])
  404. if(a_d):
  405. span[0] = '<span style="display: block; text-align: ' + a_d.groups()[0] + ';">'
  406. span[1] = '</span>'
  407. except:
  408. pass
  409. f_d = re.search('^파일:([^.]+)\.(.+)$', d[0])
  410. if(f_d):
  411. if(not re.search("^파일:([^\n]*)", title)):
  412. backlink_plus(title, d[0], 'file', num)
  413. img = span[0] + '<img src="/image/' + sha224(f_d.groups()[0]) + '.' + f_d.groups()[1] + '" ' + width + height + '>' + span[1]
  414. data = link.sub(img, data, 1)
  415. else:
  416. img = span[0] + '<img src="' + re.sub('^외부:', '', d[0]) + '" ' + width + height + '>' + span[1]
  417. data = link.sub(img, data, 1)
  418. elif(re.search('^https?:\/\/', d[0])):
  419. view = d[0]
  420. try:
  421. if(re.search('(.+)', d[1])):
  422. view = d[1]
  423. except:
  424. pass
  425. data = link.sub('<a class="out_link" rel="nofollow" href="' + d[0] + '">' + view + '</a>', data, 1)
  426. else:
  427. view = d[0].replace('\\', '')
  428. try:
  429. if(re.search('(.+)', d[1])):
  430. view = d[1]
  431. except:
  432. pass
  433. sh = ''
  434. s_d = re.search('#((?:(?!x27;|#).)+)$', d[0])
  435. if(s_d):
  436. href = re.sub('#((?:(?!x27;|#).)+)$', '', d[0])
  437. sh = '#' + s_d.groups()[0]
  438. else:
  439. href = d[0]
  440. if(d[0] == title):
  441. data = link.sub('<b>' + view + '</b>', data, 1)
  442. elif(re.search('^#', d[0])):
  443. data = link.sub('<a href="' + url_pas(href.replace('\\', '')) + sh + '">' + view + '</a>', data, 1)
  444. else:
  445. backlink_plus(title, href.replace('\\', ''), '', num)
  446. curs.execute("select title from data where title = ?", [href.replace('\\', '')])
  447. if(not curs.fetchall()):
  448. no = 'class="not_thing"'
  449. backlink_plus(title, href.replace('\\', ''), 'no', num)
  450. else:
  451. no = ''
  452. a = href.replace('\\', '').replace('&#x27;', "'").replace('&quot;', '"')
  453. data = link.sub('<a ' + no + ' title="' + a + sh + '" href="/w/' + url_pas(a) + sh + '">' + view + '</a>', data, 1)
  454. else:
  455. break
  456. return([data, category])
  457. def backlink_plus(name, link, backtype, num):
  458. if(num == 1):
  459. curs.execute("select title from back where title = ? and link = ? and type = ?", [link, name, backtype])
  460. d = curs.fetchall()
  461. if(not d):
  462. try:
  463. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  464. except:
  465. while(1):
  466. try:
  467. curs.execute("insert into back (title, link, type) values (?, ?, ?)", [link, name, backtype])
  468. break
  469. except:
  470. time.sleep(1)
  471. def namumark(title, data, num, in_c, toc_y):
  472. data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", data))
  473. data = html_pas(data)
  474. data = '\r\n' + data + '\r\n'
  475. fol_num = 0
  476. var_d = mid_pas(data, fol_num, 0, in_c)
  477. data = var_d[0]
  478. fol_num = var_d[1]
  479. category = ''
  480. include = re.compile("\[include\(((?:(?!\)\]|,).)*)((?:(?:,\s?(?:(?!\)\]).)*))+)?\)\]")
  481. m = include.findall(data)
  482. for results in m:
  483. if(results[0] == title):
  484. data = include.sub("<b>" + results[0] + "</b>", data, 1)
  485. else:
  486. curs.execute("select data from data where title = ?", [results[0]])
  487. in_con = curs.fetchall()
  488. backlink_plus(title, results[0], 'include', num)
  489. if(in_con):
  490. in_data = in_con[0][0]
  491. in_data = include.sub("", in_data)
  492. in_data = re.sub("\n", "\r\n", re.sub("\r\n", "\n", in_data))
  493. in_data = html_pas(in_data)
  494. var_d = mid_pas(in_data, fol_num, 1, in_c)
  495. var_d2 = link(title, var_d[0], 0, category)
  496. in_data = var_d2[0]
  497. category = var_d2[1]
  498. fol_num = var_d[1]
  499. if(results[1]):
  500. a = results[1]
  501. while(1):
  502. g = re.search("([^= ,]*)\=([^,]*)", a)
  503. if(g):
  504. result = g.groups()
  505. in_data = re.sub("@" + result[0] + "@", result[1], in_data)
  506. a = re.sub("([^= ,]*)\=([^,]*)", "", a, 1)
  507. else:
  508. break
  509. in_data = toc_pas(in_data, results[0], num, toc_y)
  510. data = include.sub('\n<nobr><a href="/w/' + url_pas(results[0]) + '">[' + results[0] + ' 이동]</a><div>' + in_data + '</div><nobr>\n', data, 1)
  511. else:
  512. data = include.sub("<a class=\"not_thing\" href=\"/w/" + url_pas(results[0]) + "\">" + results[0] + "</a>", data, 1)
  513. data = re.sub("\r\n##\s?([^\n]*)\r\n", "\r\n", data)
  514. data = re.sub("\[anchor\((?P<in>[^\[\]]*)\)\]", '<span id="\g<in>"></span>', data)
  515. data = savemark(data)
  516. d_re = re.findall('\r\n#(?:redirect|넘겨주기) ((?:(?!\r|\n|%0D).)+)', data)
  517. for d in d_re:
  518. view = d.replace('\\', '')
  519. sh = ''
  520. s_d = re.search('#((?:(?!x27;|#).)+)$', d)
  521. if(s_d):
  522. href = re.sub('#((?:(?!x27;|#).)+)$', '', d)
  523. sh = '#' + s_d.groups()[0]
  524. else:
  525. href = d
  526. data = re.sub('\r\n#(?:redirect|넘겨주기) ((?:(?!\r|\n|%0D).)+)', '<meta http-equiv="refresh" content="0;url=/w/' + url_pas(href.replace('\\', '').replace('&#x27;', "'").replace('&quot;', '"')) + '/from/' + url_pas(title) + sh + '" />', data, 1)
  527. data = re.sub("\[nicovideo\((?P<in>[^,)]*)(?:(?:,(?:[^,)]*))+)?\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>]]", data)
  528. while(1):
  529. m = re.search("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", data)
  530. if(m):
  531. result = m.groups()
  532. blockquote = result[0]
  533. blockquote = re.sub("\n&gt;\s?", "\n", blockquote)
  534. data = re.sub("\n&gt;\s?((?:[^\n]*)(?:(?:(?:(?:\n&gt;\s?)(?:[^\n]*))+)?))", "\n<blockquote>" + blockquote + "</blockquote>", data, 1)
  535. else:
  536. break
  537. if(not re.search('\[목차\]', data)):
  538. if(not re.search('\[목차\(없음\)\]', data)):
  539. data = re.sub("(?P<in>(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "[목차]\n\g<in>", data, 1)
  540. else:
  541. data = re.sub("\[목차\(없음\)\]", "", data)
  542. data = re.sub("(\n)(?P<in>\r\n(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "\g<in>", data)
  543. data = toc_pas(data, title, num, toc_y)
  544. data = re.sub("&#x27;&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;&#x27;).)*)&#x27;&#x27;&#x27;", '<b>\g<in></b>', data)
  545. data = re.sub("&#x27;&#x27;(?P<in>(?:(?!&#x27;&#x27;).)*)&#x27;&#x27;", '<i>\g<in></i>', data)
  546. data = re.sub('(?:~~|--)(?P<in>(?:(?!~~|--).)+)(?:~~|--)', '<s>\g<in></s>', data)
  547. data = re.sub('__(?P<in>.+?)__(?!_)', '<u>\g<in></u>', data)
  548. data = re.sub('\^\^(?P<in>.+?)\^\^(?!\^)', '<sup>\g<in></sup>', data)
  549. data = re.sub(',,(?P<in>.+?),,(?!,)', '<sub>\g<in></sub>', data)
  550. data = re.sub('&lt;math&gt;(?P<in>((?!&lt;math&gt;).)*)&lt;\/math&gt;', '[math]\g<in>[/math]', data)
  551. data = re.sub('{{\|(?P<in>(?:(?:(?:(?!\|}}).)*)(?:\n?))+)\|}}', '<table><tbody><tr><td>\g<in></td></tr></tbody></table>', data)
  552. data = re.sub('\[ruby\((?P<in>[^\,]*)\,\s?(?P<out>[^\)]*)\)\]', '<ruby>\g<in><rp>(</rp><rt>\g<out></rt><rp>)</rp></ruby>', data)
  553. data = re.sub("\[br\]", '<br>', data)
  554. while(1):
  555. com = re.compile("\[youtube\(([^, )]*)(,[^)]*)?\)\]")
  556. m = com.search(data)
  557. if(m):
  558. src = ''
  559. width = '560'
  560. height = '315'
  561. time = '0'
  562. result = m.groups()
  563. if(result[0]):
  564. yudt = re.search('(?:\?v=(.*)|\/([^/?]*)|^([a-zA-Z0-9\-_]*))$', result[0])
  565. if(yudt):
  566. if(yudt.groups()[0]):
  567. src = yudt.groups()[0]
  568. elif(yudt.groups()[1]):
  569. src = yudt.groups()[1]
  570. elif(yudt.groups()[2]):
  571. src = yudt.groups()[2]
  572. else:
  573. src = ''
  574. if(result[1]):
  575. mdata = re.search('width=([0-9%]*)', result[1])
  576. if(mdata):
  577. width = mdata.groups()[0]
  578. mdata = re.search('height=([0-9%]*)', result[1])
  579. if(mdata):
  580. height = mdata.groups()[0]
  581. mdata = re.search('time=([0-9]*)', result[1])
  582. if(mdata):
  583. time = mdata.groups()[0]
  584. data = com.sub('<iframe width="' + width + '" height="' + height + '" src="https://www.youtube.com/embed/' + src + '?start=' + time + '" frameborder="0" allowfullscreen></iframe><br>', data, 1)
  585. else:
  586. break
  587. var_d2 = link(title, data, 1, category)
  588. data = var_d2[0]
  589. category = var_d2[1]
  590. while(1):
  591. m = re.search("(\n(?:(?:( +)\*\s(?:[^\n]*))\n?)+)", data)
  592. if(m):
  593. result = m.groups()
  594. end = str(result[0])
  595. while(1):
  596. isspace = re.search("( +)\*\s([^\n]*)", end)
  597. if(isspace):
  598. spacebar = isspace.groups()
  599. up = len(spacebar[0]) * 20
  600. end = re.sub("( +)\*\s([^\n]*)", "<li style='margin-left:" + str(up) + "px'>" + spacebar[1] + "</li>", end, 1)
  601. else:
  602. break
  603. end = re.sub("\n", '', end)
  604. data = re.sub("(\n(?:(?:( +)\*\s(?:[^\n]*))\n?)+)", '<ul style="margin-top: 10px; margin-bottom: 10px;" id="list">' + end + '</ul>', data, 1)
  605. else:
  606. break
  607. now_time = get_time()
  608. data = re.sub('\[date\]', now_time, data)
  609. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  610. time = time_data.groups()
  611. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  612. for age in age_data:
  613. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  614. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  615. e_data = old - will
  616. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  617. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  618. for dday in dday_data:
  619. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  620. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  621. e_data = old - will
  622. if(re.search('^-', str(e_data.days))):
  623. e_day = str(e_data.days)
  624. else:
  625. e_day = '+' + str(e_data.days)
  626. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  627. data = re.sub("-{4,11}", "<hr>", data)
  628. while(1):
  629. b = re.search("(<\/h[0-9]>|\n)( +)", data)
  630. if(b):
  631. result = b.groups()
  632. up = re.sub(' ', '<span id="in"></span>', result[1])
  633. if(re.search('<\/h[0-9]>', result[0])):
  634. data = re.sub("(?P<in>\/h[0-9]>)( +)", '\g<in>' + up, data, 1)
  635. else:
  636. data = re.sub("(?:\n)( +)", '<br>' + up, data, 1)
  637. else:
  638. break
  639. a = 1
  640. tou = "<hr style='margin-top: 30px;' id='footnote'><div><br>"
  641. namu = []
  642. pop_re = re.compile('(?:\[\*([^\s]*)(?:\s((?:(?!\[|\]).)*))?\]|(\[각주\]))')
  643. while(1):
  644. b = pop_re.search(data)
  645. if(b):
  646. results = b.groups()
  647. try:
  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. except:
  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. data = pop_re.sub("<br>" + tou, data, 1)
  699. tou = "<hr style='margin-top: 30px;' id='footnote'><div><br>"
  700. else:
  701. tou += '</div>'
  702. if(tou == "<hr style='margin-top: 30px;' id='footnote'><div><br></div>"):
  703. tou = ""
  704. else:
  705. tou = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', tou)
  706. break
  707. data = re.sub('#d#(?P<in>(?:(?!#\/d#).)*)#\/d#', '[\g<in>]', data)
  708. data = re.sub("\[각주\](?:(?:<br>| |\r|\n)+)?$", "", data)
  709. data = re.sub("(?:(?:<br>| |\r|\n)+)$", "", data)
  710. data += tou
  711. if(category):
  712. data += '<div style="margin-top: 30px;" id="cate">분류: ' + category + '</div>'
  713. data = re.sub("(?:\|\|\r\n)", "#table#<tablenobr>", data)
  714. while(1):
  715. y = re.search("(\|\|(?:(?:(?:(?:(?!\|\|).)*)(?:\n?))+))", data)
  716. if(y):
  717. a = y.groups()
  718. mid_data = re.sub("\|\|", "#table#", a[0])
  719. mid_data = re.sub("\r\n", "<br>", mid_data)
  720. data = re.sub("(\|\|((?:(?:(?:(?!\|\|).)*)(?:\n?))+))", mid_data, data, 1)
  721. else:
  722. break
  723. data = re.sub("#table#", "||", data)
  724. data = re.sub("<tablenobr>", "\r\n", data)
  725. while(1):
  726. m = re.search("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", data)
  727. if(m):
  728. results = m.groups()
  729. table = results[0]
  730. while(1):
  731. a = re.search("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  732. if(a):
  733. row = ''
  734. cel = ''
  735. celstyle = ''
  736. rowstyle = ''
  737. alltable = ''
  738. table_d = ''
  739. result = a.groups()
  740. if(result[1]):
  741. table_d = table_p(result[1], result[0])
  742. alltable = table_d[0]
  743. rowstyle = table_d[1]
  744. celstyle = table_d[2]
  745. row = table_d[3]
  746. cel = table_d[4]
  747. table_class = table_d[5]
  748. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table " + table_class + " " + alltable + "> \
  749. <tbody> \
  750. <tr " + rowstyle + "> \
  751. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  752. else:
  753. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  754. table = re.sub("^(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "<table> \
  755. <tbody> \
  756. <tr> \
  757. <td " + cel + ">", table, 1)
  758. else:
  759. break
  760. table = re.sub("\|\|$", "</td> \
  761. </tr> \
  762. </tbody> \
  763. </table>", table)
  764. while(1):
  765. b = re.search("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  766. if(b):
  767. row = ''
  768. cel = ''
  769. celstyle = ''
  770. rowstyle = ''
  771. table_d = ''
  772. result = b.groups()
  773. if(result[1]):
  774. table_d = table_p(result[1], result[0])
  775. rowstyle = table_d[1]
  776. celstyle = table_d[2]
  777. row = table_d[3]
  778. cel = table_d[4]
  779. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  780. </tr> \
  781. <tr " + rowstyle + "> \
  782. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  783. else:
  784. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  785. table = re.sub("\|\|\r\n(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  786. </tr> \
  787. <tr> \
  788. <td " + cel + ">", table, 1)
  789. else:
  790. break
  791. while(1):
  792. c = re.search("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", table)
  793. if(c):
  794. row = ''
  795. cel = ''
  796. celstyle = ''
  797. table_d = ''
  798. result = c.groups()
  799. if(result[1]):
  800. table_d = table_p(result[1], result[0])
  801. celstyle = table_d[2]
  802. row = table_d[3]
  803. cel = table_d[4]
  804. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  805. <td " + cel + " " + row + " " + celstyle + ">", table, 1)
  806. else:
  807. cel = 'colspan="' + str(round(len(result[0]) / 2)) + '"'
  808. table = re.sub("(\|\|(?:(?:\|\|)+)?)((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)?", "</td> \
  809. <td " + cel + ">", table, 1)
  810. else:
  811. break
  812. data = re.sub("(\|\|(?:(?:(?:.*)\n?)\|\|)+)", table, data, 1)
  813. else:
  814. break
  815. data = re.sub("\r\n(?P<in><h[0-6])", "\g<in>", data)
  816. data = re.sub("(\n<nobr>|<nobr>\n|<nobr>)", "", data)
  817. data = re.sub("#no#(?P<in>.)#\/no#", "\g<in>", data)
  818. data = re.sub("<space>", " ", data)
  819. data = re.sub('<\/blockquote>(?:(?:\r)?\n){2}<blockquote>', '</blockquote><blockquote>', data)
  820. data = re.sub('<\/blockquote>(?:(?:\r)?\n)<br><blockquote>', '</blockquote><blockquote>', data)
  821. data = re.sub('\n', '<br>', data)
  822. data = re.sub('<hr style="margin-top: -5px;"><br>', '<hr style="margin-top: -5px;">', data)
  823. data = re.sub('<isbr>', '\r\n', data)
  824. data = re.sub('^(?:<br>|\r|\n| )+', '', data)
  825. data = re.sub('^<div style="margin-top: 30px;" id="cate">', '<div id="cate">', data)
  826. if(num == 1):
  827. conn.commit()
  828. return(data)