start.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. from . import tool
  2. import datetime
  3. import html
  4. import re
  5. def table_parser(data, cel_data, start_data, num = 0):
  6. table_class = 'class="'
  7. all_table = 'style="'
  8. cel_style = 'style="'
  9. row_style = 'style="'
  10. row = ''
  11. cel = ''
  12. table_width = re.search("<table ?width=((?:(?!>).)*)>", data)
  13. if table_width:
  14. all_table += 'width: ' + table_width.groups()[0] + 'px;'
  15. table_height = re.search("<table ?height=((?:(?!>).)*)>", data)
  16. if table_height:
  17. all_table += 'height: ' + table_height.groups()[0] + 'px;'
  18. table_align = re.search("<table ?align=((?:(?!>).)*)>", data)
  19. if table_align:
  20. if table_align.groups()[0] == 'right':
  21. all_table += 'float: right;'
  22. elif table_align.groups()[0] == 'center':
  23. all_table += 'margin: auto;'
  24. table_text_align = re.search("<table ?textalign=((?:(?!>).)*)>", data)
  25. if table_text_align:
  26. num = 1
  27. if table_text_align.groups()[0] == 'right':
  28. all_table += 'text-align: right;'
  29. elif table_text_align.groups()[0] == 'center':
  30. all_table += 'text-align: center;'
  31. row_t_a = re.search("<row ?textalign=((?:(?!>).)*)>", data)
  32. if row_t_a:
  33. if row_t_a.groups()[0] == 'right':
  34. row_style += 'text-align: right;'
  35. elif row_t_a.groups()[0] == 'center':
  36. row_style += 'text-align: center;'
  37. else:
  38. row_style += 'text-align: left;'
  39. table_cel = re.search("<-((?:(?!>).)*)>", data)
  40. if table_cel:
  41. cel = 'colspan="' + table_cel.groups()[0] + '"'
  42. else:
  43. cel = 'colspan="' + str(round(len(start_data) / 2)) + '"'
  44. table_row = re.search("<\|((?:(?!>).)*)>", data)
  45. if table_row:
  46. row = 'rowspan="' + table_row.groups()[0] + '"'
  47. row_bgcolor = re.search("<rowbgcolor=(#(?:[0-9a-f-A-F]{3}){1,2}|\w+)>", data)
  48. if row_bgcolor:
  49. row_style += 'background: ' + row_bgcolor.groups()[0] + ';'
  50. table_border = re.search("<table ?bordercolor=(#(?:[0-9a-f-A-F]{3}){1,2}|\w+)>", data)
  51. if table_border:
  52. all_table += 'border: ' + table_border.groups()[0] + ' 2px solid;'
  53. table_bgcolor = re.search("<table ?bgcolor=(#(?:[0-9a-f-A-F]{3}){1,2}|\w+)>", data)
  54. if table_bgcolor:
  55. all_table += 'background: ' + table_bgcolor.groups()[0] + ';'
  56. bgcolor = re.search("<(?:bgcolor=)?(#(?:[0-9a-f-A-F]{3}){1,2}|\w+)>", data)
  57. if bgcolor:
  58. cel_style += 'background: ' + bgcolor.groups()[0] + ';'
  59. cel_width = re.search("<width=((?:(?!>).)*)>", data)
  60. if cel_width:
  61. cel_style += 'width: ' + cel_width.groups()[0] + 'px;'
  62. cel_height = re.search("<height=((?:(?!>).)*)>", data)
  63. if cel_height:
  64. cel_style += 'height: ' + cel_height.groups()[0] + 'px;'
  65. text_right = re.search("<\)>", data)
  66. text_center = re.search("<:>", data)
  67. text_left = re.search("<\(>", data)
  68. if text_right:
  69. cel_style += 'text-align: right;'
  70. elif text_center:
  71. cel_style += 'text-align: center;'
  72. elif text_left:
  73. cel_style += 'text-align: left;'
  74. elif num == 0:
  75. if re.search('^ (.*) $', cel_data):
  76. cel_style += 'text-align: center;'
  77. elif re.search('^ (.*)$', cel_data):
  78. cel_style += 'text-align: right;'
  79. elif re.search('^(.*) $', cel_data):
  80. cel_style += 'text-align: left;'
  81. text_class = re.search("<table ?class=((?:(?!>).)+)>", data)
  82. if text_class:
  83. table_class += text_class.groups()[0]
  84. all_table += '"'
  85. cel_style += '"'
  86. row_style += '"'
  87. table_class += '"'
  88. return [all_table, row_style, cel_style, row, cel, table_class, num]
  89. def start(conn, data, title):
  90. # DB 지정
  91. curs = conn.cursor()
  92. # 맨 앞과 끝에 개행 문자 추가
  93. data = '\r\n' + data + '\r\n'
  94. # XSS 이스케이프
  95. data = html.escape(data)
  96. data = re.sub('&lt;(?P<in>(table|row)? ?(text|bg|border|width|height|class)?(color|align)?(=(((?!&gt;).)+))|\(|:|\)|(-|\|)[0-9]+|(#(?:[0-9a-f-A-F]{3}){1,2})|(\w+))&gt;', '<\g<in>>', data)
  97. data = re.sub('&#x27;&#x27;&#x27;(?P<in>((?!&#x27;&#x27;&#x27;).)+)&#x27;&#x27;&#x27;', '\'\'\'\g<in>\'\'\'', data)
  98. data = re.sub('&#x27;&#x27;(?P<in>((?!&#x27;&#x27;).)+)&#x27;&#x27;', '\'\'\g<in>\'\'', data)
  99. # 추가 데이터 지정
  100. plus_data = ''
  101. while 1:
  102. include = re.search('\[include\(((?:(?!\)\]).)+)\)\]', data)
  103. if include:
  104. include = include.groups()[0]
  105. include_data = re.search('^((?:(?!,).)+)', include)
  106. if include_data:
  107. include_data = include_data.groups()[0]
  108. else:
  109. include_data = 'Test'
  110. include = re.sub('^((?:(?!,).)+)', '', include)
  111. curs.execute("select data from data where title = ?", [include_data])
  112. include_data = curs.fetchall()
  113. if include_data:
  114. include_parser = include_data[0][0]
  115. while 1:
  116. include_plus = re.search(', ?((?:(?!=).)+)=((?:(?!,).)+)', include)
  117. if include_plus:
  118. include_plus = include_plus.groups()
  119. include_parser = re.sub('@' + include_plus[0] + '@', include_plus[1], include_parser)
  120. include = re.sub(', ?((?:(?!=).)+)=((?:(?!,).)+)', '', include, 1)
  121. else:
  122. break
  123. include_parser = re.sub('\[\[분류:(((?!\]\]|#include).)+)\]\]', '', include_parser)
  124. data = re.sub('\[include\(((?:(?!\)\]).)+)\)\]', '\r\n<span id="include"></span>' + include_parser + '<span id="include"></span>\r\n', data, 1)
  125. else:
  126. data = re.sub('\[include\(((?:(?!\)\]).)+)\)\]', '[[' + include + ']]', data, 1)
  127. else:
  128. break
  129. # 수식 처리
  130. first = 0
  131. while 1:
  132. math = re.search('<math>((?:(?!<\/math>).)+)<\/math>', data)
  133. if math:
  134. if first == 0:
  135. plus_data += '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css" integrity="sha384-TEMocfGvRuD1rIAacqrknm5BQZ7W7uWitoih+jMNFXQIbNl16bO8OZmylH/Vi/Ei" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.js" integrity="sha384-jmxIlussZWB7qCuB+PgKG1uLjjxbVVIayPJwi6cG6Zb4YKq0JIw+OMnkkEC7kYCq" crossorigin="anonymous"></script>'
  136. math = math.groups()[0]
  137. first += 1
  138. data = re.sub('<math>((?:(?!<\/math>).)+)<\/math>', '<span id="math_' + str(first) + '"></span>', data, 1)
  139. plus_data += '<script>katex.render("' + math.replace('\\', '\\\\') +'", document.getElementById("math_' + str(first) + '"));</script>'
  140. else:
  141. break
  142. # 텍스트 꾸미기 문법
  143. data = re.sub('\'\'\'(?P<in>(?:(?!\'\'\').)+)\'\'\'', '<b>\g<in></b>', data)
  144. data = re.sub('\'\'(?P<in>(?:(?!\'\').)+)\'\'', '<i>\g<in></i>', data)
  145. data = re.sub('~~(?P<in>(?:(?!~~).)+)~~', '<s>\g<in></s>', data)
  146. data = re.sub('--(?P<in>(?:(?!~~).)+)--', '<s>\g<in></s>', data)
  147. data = re.sub('__(?P<in>(?:(?!__).)+)__', '<u>\g<in></u>', data)
  148. data = re.sub('^^(?P<in>(?:(?!^^).)+)^^', '<sup>\g<in></sup>', data)
  149. data = re.sub(',,(?P<in>(?:(?!,,).)+),,', '<sub>\g<in></sub>', data)
  150. # 넘겨주기 변환
  151. data = re.sub('\r\n#(?:redirect|넘겨주기) (?P<in>(?:(?!\r\n).)+)\r\n', '<meta http-equiv="refresh" content="0; url=/w/\g<in>?froms=' + tool.url_pas(title) + '">', data)
  152. # 각주 처리
  153. footnote_number = 0
  154. footnote_all = '\r\n<hr><ul id="footnote_data">'
  155. while 1:
  156. footnote = re.search('(?:\[\*((?:(?! ).)*) ((?:(?!\]).)+)\]|(\[각주\]))', data)
  157. if footnote:
  158. footnote_data = footnote.groups()
  159. if footnote_data[2]:
  160. footnote_all += '</ul>'
  161. data = re.sub('(?:\[\*((?:(?! ).)*) ((?:(?!\]).)+)\]|(\[각주\]))', footnote_all, data, 1)
  162. footnote_all = '\r\n<hr><ul id="footnote_data">'
  163. else:
  164. footnote = footnote_data[1]
  165. footnote_name = footnote_data[0]
  166. footnote_number += 1
  167. if not footnote_name:
  168. footnote_name = str(footnote_number)
  169. footnote_all += '<li><a href="#rfn-' + str(footnote_number) + '" id="fn-' + str(footnote_number) + '">(' + footnote_name + ')</a> ' + footnote + '</li>'
  170. data = re.sub('(?:\[\*((?:(?! ).)*) ((?:(?!\]).)+)\]|(\[각주\]))', '<sup><a href="#fn-' + str(footnote_number) + '" id="rfn-' + str(footnote_number) + '">(' + footnote_name + ')</a></sup>', data, 1)
  171. else:
  172. break
  173. footnote_all += '</ul>'
  174. if footnote_all == '\r\n<hr><ul id="footnote_data"></ul>':
  175. footnote_all = ''
  176. data = re.sub('\r\n$', footnote_all, data)
  177. # [목차(없음)] 처리
  178. if not re.search('\[목차\(없음\)\]\r\n', data):
  179. if not re.search('\[목차\]', data):
  180. data = re.sub('\r\n(?P<in>={1,6}) ?(?P<out>(?:(?!=).)+) ?={1,6}\r\n', '\r\n[목차]\r\n\g<in> \g<out> \g<in>\r\n', data, 1)
  181. else:
  182. data = re.sub('\[목차\(없음\)\]\r\n', '', data)
  183. # 문단 문법
  184. toc_full = 0
  185. toc_top_stack = 6
  186. toc_stack = [0, 0, 0, 0, 0, 0]
  187. edit_number = 0
  188. toc_data = '<div id="toc"><span style="font-size: 18px;">목차</span>\r\n\r\n'
  189. while 1:
  190. toc = re.search('\r\n(={1,6}) ?((?:(?!=).)+) ?={1,6}\r\n', data)
  191. if toc:
  192. toc = toc.groups()
  193. toc_number = len(toc[0])
  194. edit_number += 1
  195. # 더 크면 그 전 스택은 초기화
  196. if toc_full > toc_number:
  197. for i in range(toc_number, 6):
  198. toc_stack[i] = 0
  199. if toc_top_stack > toc_number:
  200. toc_top_stack = toc_number
  201. toc_full = toc_number
  202. toc_stack[toc_number - 1] += 1
  203. toc_number = str(toc_number)
  204. all_stack = ''
  205. # 스택 합치기
  206. for i in range(0, 6):
  207. all_stack += str(toc_stack[i]) + '.'
  208. all_stack = re.sub('0.', '', all_stack)
  209. data = re.sub('\r\n(={1,6}) ?((?:(?!=).)+) ?={1,6}\r\n', '\r\n<h' + toc_number + '><a href="">' + all_stack + '</a> ' + toc[1] + ' <span style="font-size: 12px"><a href="/edit/' + tool.url_pas(title) + '?section=' + str(edit_number) + '">(편집)</a></span></h' + toc_number + '><hr id="under_bar" style="margin-top: -5px; margin-bottom: 10px;">\r\n', data, 1)
  210. toc_data += '<span style="margin-left: ' + str((toc_full - toc_top_stack) * 10) + 'px;"><a href="">' + all_stack + '</a> ' + toc[1] + '</span>\r\n'
  211. else:
  212. break
  213. toc_data += '</div>'
  214. data = re.sub('\[목차\]', toc_data, data)
  215. while 1:
  216. hr = re.search('\r\n-{4,9}\r\n', data)
  217. if hr:
  218. data = re.sub('\r\n-{4,9}\r\n', '<hr>', data, 1)
  219. else:
  220. break
  221. data += '\r\n'
  222. # 일부 매크로 처리
  223. data = tool.savemark(data)
  224. data = re.sub("\[br\]", '\r\n', data)
  225. data = re.sub("\[anchor\((?P<in>(?:(?!\)\]).)+)\)\]", '<span id="\g<in>"></span>', data)
  226. data = re.sub("\[nicovideo\((?P<in>(?:(?!,|\)\]).)+)(?:(?:(?!\)\]).)*)\)\]", "[[http://embed.nicovideo.jp/watch/\g<in>|\g<in>]]", data)
  227. data = re.sub('\[ruby\((?P<in>(?:(?!,).)+)\, ?(?P<out>(?:(?!\)\]).)+)\)\]', '<ruby>\g<in><rp>(</rp><rt>\g<out></rt><rp>)</rp></ruby>', data)
  228. now_time = tool.get_time()
  229. data = re.sub('\[date\]', now_time, data)
  230. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  231. time = time_data.groups()
  232. age_data = re.findall('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  233. for age in age_data:
  234. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  235. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  236. e_data = old - will
  237. data = re.sub('\[age\(([0-9]{4})-([0-9]{2})-([0-9]{2})\)\]', str(int(int(e_data.days) / 365)), data, 1)
  238. dday_data = re.findall('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', data)
  239. for dday in dday_data:
  240. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  241. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  242. e_data = old - will
  243. if re.search('^-', str(e_data.days)):
  244. e_day = str(e_data.days)
  245. else:
  246. e_day = '+' + str(e_data.days)
  247. data = re.sub('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', e_day, data, 1)
  248. # 유튜브, 카카오 티비 처리
  249. while 1:
  250. video = re.search('\[(youtube|kakaotv)\(((?:(?!\)\]).)+)\)\]', data)
  251. if video:
  252. video = video.groups()
  253. width = re.search(', ?width=((?:(?!,).)+)', video[1])
  254. if width:
  255. video_width = width.groups()[0]
  256. else:
  257. video_width = '560'
  258. height = re.search(', ?height=((?:(?!,).)+)', video[1])
  259. if height:
  260. video_height = height.groups()[0]
  261. else:
  262. video_height = '315'
  263. code = re.search('^(((?!,).)+)', video[1])
  264. if code:
  265. video_code = code.groups()[0]
  266. else:
  267. if video[0] == 'youtube':
  268. video_code = 'BQ5PcIUcdUE'
  269. else:
  270. video_code = '66861302'
  271. if video[0] == 'youtube':
  272. video_code = re.sub('^https:\/\/www\.youtube\.com\/watch\?v=', '', video_code)
  273. video_code = re.sub('^https:\/\/youtu\.be\/', '', video_code)
  274. video_src = 'https://www.youtube.com/embed/' + video_code
  275. else:
  276. video_code = re.sub('^https:\/\/tv\.kakao\.com\/channel\/9262\/cliplink\/', '', video_code)
  277. video_code = re.sub('^http:\/\/tv\.kakao\.com\/v\/', '', video_code)
  278. video_src = 'https://tv.kakao.com/embed/player/cliplink/' + video_code +'?service=kakao_tv'
  279. data = re.sub('\[(youtube|kakaotv)\(((?:(?!\)\]).)+)\)\]', '<iframe width="' + video_width + '" height="' + video_height + '" src="' + video_src + '" allowfullscreen></iframe>', data, 1)
  280. else:
  281. break
  282. # 인용문 구현
  283. while 1:
  284. block = re.search('(\r\n(?:> ?(?:(?:(?!\r\n).)+)\r\n)+)', data)
  285. if block:
  286. block = block.groups()[0]
  287. block = re.sub('^\r\n> ?', '', block)
  288. block = re.sub('\r\n> ?', '\r\n', block)
  289. block = re.sub('\r\n$', '', block)
  290. data = re.sub('(\r\n(?:> ?(?:(?:(?!\r\n).)+)\r\n)+)', '<blockquote>' + block + '</blockquote>\r\n', data, 1)
  291. else:
  292. break
  293. # 리스트 구현
  294. while 1:
  295. li = re.search('(\r\n(?:(?: *)\* ?(?:(?:(?!\r\n).)+)\r\n)+)', data)
  296. if li:
  297. li = li.groups()[0]
  298. while 1:
  299. sub_li = re.search('\r\n(?:( *)\* ?((?:(?!\r\n).)+))', li)
  300. if sub_li:
  301. sub_li = sub_li.groups()
  302. # 앞의 공백 만큼 margin 먹임
  303. if len(sub_li[0]) == 0:
  304. margin = 20
  305. else:
  306. margin = len(sub_li[0]) * 20
  307. li = re.sub('\r\n(?:( *)\* ?((?:(?!\r\n).)+))', '<li style="margin-left: ' + str(margin) + 'px;">' + sub_li[1] + '</li>', li, 1)
  308. else:
  309. break
  310. data = re.sub('(\r\n(?:(?: *)\* ?(?:(?:(?!\r\n).)+)\r\n)+)', '\r\n\r\n<ul>' + li + '</ul>\r\n', data, 1)
  311. else:
  312. break
  313. # 들여쓰기 구현
  314. while 1:
  315. indent = re.search('\r\n( +)', data)
  316. if indent:
  317. indent = len(indent.groups()[0])
  318. # 앞에 공백 만큼 margin 먹임
  319. margin = '<span style="margin-left: 20px;"></span>' * indent
  320. data = re.sub('\r\n( +)', '\r\n' + margin, data, 1)
  321. else:
  322. break
  323. # 표 처리
  324. while 1:
  325. table = re.search('((?:(?:(?:(?:\|\|)+(?:(?:(?!\|\|).(?:\r\n)*)+))+)\|\|(?:\r\n)?)+)', data)
  326. if table:
  327. table = table.groups()[0]
  328. # return [all_table, row_style, cel_style, row, cel, table_class, num]
  329. while 1:
  330. all_table = re.search('^((?:\|\|)+)((?:<(?:(?:(?!>).)+)>)*)((?:(?!\|\||<\/td>).)+)', table)
  331. if all_table:
  332. all_table = all_table.groups()
  333. return_table = table_parser(all_table[1], all_table[2], all_table[0])
  334. number = return_table[6]
  335. table = re.sub('^\|\|((?:<(?:(?:(?!>).)+)>)*)', '<table ' + return_table[5] + ' ' + return_table[0] + '><tbody><tr ' + return_table[1] + '><td ' + return_table[2] + ' ' + return_table[3] + ' ' + return_table[4] + '>', table, 1)
  336. else:
  337. break
  338. table = re.sub('\|\|\r\n$', '</td></tr></tbody></table>', table)
  339. while 1:
  340. row_table = re.search('\|\|\r\n((?:\|\|)+)((?:<(?:(?:(?!>).)+)>)*)((?:(?!\|\||<\/td>).)+)', table)
  341. if row_table:
  342. row_table = row_table.groups()
  343. return_table = table_parser(row_table[1], row_table[2], row_table[0], number)
  344. table = re.sub('\|\|\r\n((?:\|\|)+)((?:<(?:(?:(?!>).)+)>)*)', '</td></tr><tr ' + return_table[1] + '><td ' + return_table[2] + ' ' + return_table[3] + ' ' + return_table[4] + '>', table, 1)
  345. else:
  346. break
  347. while 1:
  348. cel_table = re.search('((?:\|\|)+)((?:<(?:(?:(?!>).)+)>)*)((?:(?!\|\||<\/td>).)+)', table)
  349. if cel_table:
  350. cel_table = cel_table.groups()
  351. return_table = table_parser(cel_table[1], cel_table[2], cel_table[0], number)
  352. table = re.sub('((?:\|\|)+)((?:<(?:(?:(?!>).)+)>)*)', '</td><td ' + return_table[2] + ' ' + return_table[3] + ' ' + return_table[4] + '>', table, 1)
  353. else:
  354. break
  355. data = re.sub('((?:(?:(?:(?:\|\|)+(?:(?:(?!\|\|).(?:\r\n)*)+))+)\|\|(?:\r\n)?)+)', table, data, 1)
  356. else:
  357. break
  358. # 링크 관련 문법 구현
  359. category = '\r\n<div id="cate">분류: '
  360. while 1:
  361. link = re.search('\[\[((?:(?!\[\[|\]\]).)+)\]\]', data)
  362. if link:
  363. link = link.groups()[0]
  364. link_split = re.search('((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  365. if link_split:
  366. link_split = link_split.groups()
  367. main_link = link_split[0]
  368. see_link = link_split[1]
  369. else:
  370. main_link = link
  371. see_link = link
  372. if re.search('^(파일|외부):', main_link):
  373. width = re.search('width=((?:(?!,).)+)', see_link)
  374. if width:
  375. file_width = width.groups()[0]
  376. else:
  377. file_width = 'auto'
  378. height = re.search('height=((?:(?!,).)+)', see_link)
  379. if height:
  380. file_height = height.groups()[0]
  381. else:
  382. file_height = 'auto'
  383. align = re.search('align=((?:(?!,).)+)', see_link)
  384. if align:
  385. file_align = align.groups()[0]
  386. if file_align == 'center':
  387. file_align = 'display: block; text-align: center;'
  388. else:
  389. file_align = 'float: ' + file_align + ';'
  390. else:
  391. file_align = ''
  392. if re.search('^외부:', main_link):
  393. file_src = re.sub('^외부:', '', main_link)
  394. file_alt = main_link
  395. else:
  396. file_data = re.search('^파일:((?:(?!\.).)+)\.(.+)$', main_link)
  397. if file_data:
  398. file_data = file_data.groups()
  399. file_name = file_data[0]
  400. file_end = file_data[1]
  401. else:
  402. file_name = 'TEST'
  403. file_end = 'jpg'
  404. file_src = '/image/' + tool.sha224(file_name) + '.' + file_end
  405. file_alt = '파일:' + file_name + '.' + file_end
  406. data = re.sub('\[\[((?:(?!\[\[|\]\]).)+)\]\]', '<span style="' + file_align + '"><img width="' + file_width + '" height="' + file_height + '" alt="' + file_alt + '" src="' + file_src + '"></span>', data, 1)
  407. elif re.search('^분류:', main_link):
  408. see_link = re.sub('#include', '', see_link)
  409. main_link = re.sub('#include', '', main_link)
  410. if re.search('#blur', main_link):
  411. see_link = '스포일러'
  412. link_id = 'id="inside"'
  413. main_link = re.sub('#blur', '', main_link)
  414. else:
  415. link_id = ''
  416. category += '<a ' + link_id + ' href="' + tool.url_pas(main_link) + '">' + re.sub('^분류:', '', see_link) + '</a> / '
  417. data = re.sub('\[\[((?:(?!\[\[|\]\]).)+)\]\]', '', data, 1)
  418. elif re.search('^wiki:', main_link):
  419. data = re.sub('\[\[((?:(?!\[\[|\]\]).)+)\]\]', '<a id="inside" href="/' + tool.url_pas(re.sub('^wiki:', '', main_link)) + '">' + see_link + '</a>', data, 1)
  420. elif re.search('^http(s)?:\/\/', main_link):
  421. data = re.sub('\[\[((?:(?!\[\[|\]\]).)+)\]\]', '<a class="out_link" rel="nofollow" href="' + main_link + '">' + see_link + '</a>', data, 1)
  422. else:
  423. if re.search('^:', main_link):
  424. main_link = re.sub('^:', '', main_link)
  425. curs.execute("select title from data where title = ?", [main_link])
  426. if not curs.fetchall():
  427. link_class = 'class="not_thing"'
  428. else:
  429. link_class = ''
  430. data = re.sub('\[\[((?:(?!\[\[|\]\]).)+)\]\]', '<a ' + link_class + ' href="/w/' + tool.url_pas(main_link) + '">' + see_link + '</a>', data, 1)
  431. else:
  432. break
  433. category += '</div>'
  434. category = re.sub(' / <\/div>$', '</div>', category)
  435. if category == '\r\n<div id="cate">분류: </div>':
  436. category = ''
  437. data += category
  438. # 마지막 처리
  439. data = re.sub('(?P<in><hr id="under_bar" style="margin-top: -5px; margin-bottom: 10px;">)(\r\n)+', '\g<in>', data)
  440. data = re.sub('<\/ul>\r\n\r\n', '</ul>\r\n', data)
  441. data = re.sub('^(\r\n)+', '', data)
  442. data = re.sub('(\r\n)+$', '', data)
  443. data = re.sub('(\r\n)?<span id="include"><\/span>(\r\n)?(<span style="margin-left: 20px;"><\/span>)?', '', data)
  444. data = re.sub('\r\n', '<br>', data)
  445. return [data, plus_data]