2
0

mark.py 42 KB

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