mark.py 43 KB

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