namumark.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. from . import tool
  2. import datetime
  3. import html
  4. import re
  5. def link_fix(main_link):
  6. global end_data
  7. if re.search('^:', main_link):
  8. main_link = re.sub('^:', '', main_link)
  9. main_link = re.sub('^사용자:', 'user:', main_link)
  10. main_link = re.sub('^파일:', 'file:', main_link)
  11. main_link = re.sub('^분류:', 'category:', main_link)
  12. other_link = re.search('[^\\\\]?(#[^#]+)$', main_link)
  13. if other_link:
  14. other_link = other_link.groups()[0]
  15. main_link = re.sub('(#[^#]+)$', '', main_link)
  16. else:
  17. other_link = ''
  18. main_link = re.sub('\\\\#', '%23', main_link)
  19. find_data = re.findall('<span id="(nowiki_[0-9]+)">', main_link)
  20. for i in find_data:
  21. main_link = main_link.replace('<span id="' + i + '"></span>', end_data[i])
  22. find_data = re.findall('<span id="(nowiki_[0-9]+)">', other_link)
  23. for i in find_data:
  24. other_link = other_link.replace('<span id="' + i + '"></span>', end_data[i])
  25. return [main_link, other_link]
  26. def table_parser(data, cel_data, cel_num, start_data, num = 0, cel_color = {}):
  27. table_class = 'class="'
  28. div_style = 'style="'
  29. all_table = 'style="'
  30. cel_style = 'style="'
  31. row_style = 'style="'
  32. row = ''
  33. cel = 'colspan="' + str(round(len(start_data) / 2)) + '"'
  34. if not cel_num in cel_color:
  35. cel_color[cel_num] = ''
  36. cel_style += cel_color[cel_num]
  37. if num == 0:
  38. if re.search('^ ', cel_data) and re.search(' $', cel_data):
  39. cel_style += 'text-align: center;'
  40. elif re.search('^ ', cel_data):
  41. cel_style += 'text-align: right;'
  42. elif re.search(' $', cel_data):
  43. cel_style += 'text-align: left;'
  44. table_state = re.findall('&lt;((?:(?!&gt;).)+)&gt;', data)
  45. for in_state in table_state:
  46. if re.search("^table ?width=([^=]+)$", in_state):
  47. table_data = re.sub('^table ?width=', '', in_state)
  48. div_style += 'width: ' + ((table_data + 'px') if re.search('^[0-9]+$', table_data) else table_data) + ';'
  49. all_table += 'width: 100%;'
  50. elif re.search("^table ?height=([^=]+)$", in_state):
  51. table_data = re.sub('^table ?height=', '', in_state)
  52. div_style += 'height: ' + ((table_data + 'px') if re.search('^[0-9]+$', table_data) else table_data) + ';'
  53. elif re.search("^table ?align=([^=]+)$", in_state):
  54. table_data = re.sub('^table ?align=', '', in_state)
  55. if table_data == 'right':
  56. div_style += 'float: right;'
  57. elif table_data == 'center':
  58. all_table += 'margin: auto;'
  59. elif re.search("^table ?textalign=([^=]+)$", in_state):
  60. num = 1
  61. table_data = re.sub('^table ?textalign=', '', in_state)
  62. if table_data == 'right':
  63. all_table += 'text-align: right;'
  64. elif table_data == 'center':
  65. all_table += 'text-align: center;'
  66. elif re.search("^row ?textalign=([^=]+)$", in_state):
  67. table_data = re.sub('^row ?textalign=', '', in_state)
  68. if table_data == 'right':
  69. row_style += 'text-align: right;'
  70. elif table_data == 'center':
  71. row_style += 'text-align: center;'
  72. else:
  73. row_style += 'text-align: left;'
  74. elif re.search('^-([0-9]+)$', in_state):
  75. cel = 'colspan="' + re.sub('^-', '', in_state) + '"'
  76. elif re.search("^(\^|v)?\|([^|]+)$", in_state):
  77. if re.search('^\^', in_state):
  78. cel_style += 'vertical-align: top;'
  79. elif re.search('^v', in_state):
  80. cel_style += 'vertical-align: bottom;'
  81. row = 'rowspan="' + re.sub('^(\^|v)?\|', '', in_state) + '"'
  82. elif re.search("^row ?bgcolor=([^=]+)$", in_state):
  83. table_data = re.sub('^row ?bgcolor=', '', in_state)
  84. row_style += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  85. elif re.search("^row ?color=([^=]+)$", in_state):
  86. table_data = re.sub('^row ?color=', '', in_state)
  87. row_style += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  88. elif re.search("^table ?bordercolor=([^=]+)$", in_state):
  89. table_data = re.sub('^table ?bordercolor=', '', in_state)
  90. all_table += 'border: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ' 2px solid;'
  91. elif re.search("^table ?bgcolor=([^=]+)$", in_state):
  92. table_data = re.sub('^table ?bgcolor=', '', in_state)
  93. all_table += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  94. elif re.search("^table ?color=([^=]+)$", in_state):
  95. table_data = re.sub('^table ?color=', '', in_state)
  96. all_table += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  97. elif re.search("^col ?bgcolor=([^=]+)$", in_state):
  98. table_data = re.sub('^col ?bgcolor=', '', in_state)
  99. table_data = (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data)
  100. cel_color[cel_num] += 'background: ' + table_data + ';'
  101. cel_style += 'background: ' + table_data + ';'
  102. elif re.search("^col ?color=([^=]+)$", in_state):
  103. table_data = re.sub('^col ?color=', '', in_state)
  104. table_data = (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data)
  105. cel_color[cel_num] += 'color: ' + table_data + ';'
  106. cel_style += 'color: ' + table_data + ';'
  107. elif re.search("^(bgcolor=([^=]+)|#(?:[0-9a-f-A-F]{3}){1,2}|\w+)$", in_state):
  108. table_data = re.sub('^bgcolor=', '', in_state)
  109. cel_style += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  110. elif re.search("^color=([^=]+)$", in_state):
  111. table_data = re.sub('^color=', '', in_state)
  112. cel_style += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(',', table_data) else table_data) + ';'
  113. elif re.search("^width=([^=]+)$", in_state):
  114. table_data = re.sub('^width=', '', in_state)
  115. cel_style += 'width: ' + ((table_data + 'px') if re.search('^[0-9]+$', table_data) else table_data) + ';'
  116. elif re.search("^height=([^=]+)$", in_state):
  117. table_data = re.sub('^height=', '', in_state)
  118. cel_style += 'height: ' + ((table_data + 'px') if re.search('^[0-9]+$', table_data) else table_data) + ';'
  119. elif re.search('^\(|:|\)$', in_state):
  120. if in_state == '(':
  121. cel_style += 'text-align: right;'
  122. elif in_state == ':':
  123. cel_style += 'text-align: center;'
  124. else:
  125. cel_style += 'text-align: left;'
  126. elif re.search("^table ?class=([^=]+)$", in_state):
  127. table_class += re.sub("^table ?class=", '', in_state)
  128. div_style += '"'
  129. all_table += '"'
  130. cel_style += '"'
  131. row_style += '"'
  132. table_class += '"'
  133. return [all_table, row_style, cel_style, row, cel, table_class, num, div_style, cel_color]
  134. def table_start(data):
  135. while 1:
  136. cel_num = 0
  137. table_num = 0
  138. table_end = ''
  139. cel_color = {}
  140. table = re.search('\n((?:(?:(?:(?:\|\||\|[^|]+\|)+(?:(?:(?!\|\|).\n*)*))+)\|\|(?:\n)?)+)', data)
  141. if table:
  142. table = '\n' + table.groups()[0]
  143. table_cel = re.findall('(\n(?:(?:\|\|)+)|\|\|\n(?:(?:\|\|)+)|(?:(?:\|\|)+))((?:(?:(?!\n|\|\|).)+\n*)+)', table)
  144. for i in table_cel:
  145. cel_plus = re.search('^((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)', i[1])
  146. cel_plus = cel_plus.groups()[0] if cel_plus else ''
  147. cel_data = re.sub('^((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)', '', i[1])
  148. if re.search('^\n', i[0]):
  149. cel_num = 1
  150. cel_plus = table_parser(
  151. cel_plus,
  152. cel_data,
  153. cel_num,
  154. re.sub('^\n', '', i[0]),
  155. table_num,
  156. cel_color
  157. )
  158. cel_color = cel_plus[8]
  159. table_num = cel_plus[6]
  160. table_end += '' + \
  161. '<div class="table_safe" ' + cel_plus[7] + '>' + \
  162. '<table ' + cel_plus[5] + ' ' + cel_plus[0] + '>' + \
  163. '<tr ' + cel_plus[1] + '>' + \
  164. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  165. cel_data
  166. elif re.search('\n', i[0]):
  167. cel_num = 1
  168. cel_plus = table_parser(
  169. cel_plus,
  170. cel_data,
  171. cel_num,
  172. re.sub('^\|\|\n', '', i[0]),
  173. table_num,
  174. cel_color
  175. )
  176. cel_color = cel_plus[8]
  177. table_end += '' + \
  178. '</td>' + \
  179. '</tr>' + \
  180. '<tr ' + cel_plus[1] + '>' + \
  181. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  182. cel_data
  183. else:
  184. cel_num += 1
  185. cel_plus = table_parser(
  186. cel_plus,
  187. cel_data,
  188. cel_num,
  189. re.sub('^\|\|\n', '', i[0]),
  190. table_num,
  191. cel_color
  192. )
  193. cel_color = cel_plus[8]
  194. table_end += '' + \
  195. '</td>' + \
  196. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  197. cel_data
  198. table_end += '</td></tr></table></div>'
  199. data = re.sub('\n((?:(?:(?:(?:\|\||\|[^|]+\|)+(?:(?:(?!\|\|).\n*)*))+)\|\|(?:\n)?)+)', table_end + '\n', data, 1)
  200. else:
  201. break
  202. return data
  203. def middle_parser(data, include_num):
  204. global end_data
  205. global plus_data
  206. global nowiki_num
  207. middle_stack = 0
  208. middle_list = []
  209. middle_num = 0
  210. html_num = 0
  211. syntax_num = 0
  212. folding_num = 0
  213. middle_re = re.compile('(?:{{{((?:(?:(?! |{{{|}}}|&lt;).)*) ?)|(}}}))')
  214. middle_all_data = middle_re.findall(data)
  215. for middle_data in middle_all_data:
  216. if not middle_data[1]:
  217. if middle_stack > 0:
  218. middle_stack += 1
  219. data = re.sub('(?:{{{((?:(?! |{{{|}}}|&lt;).)*)(?P<in> ?)|(}}}))', '&#123;&#123;&#123;' + middle_data[0] + '\g<in>', data, 1)
  220. else:
  221. if re.search('^(#|@|\+|\-)', middle_data[0]) and not re.search('^(#|@|\+|\-){2}|(#|@|\+|\-)\\\\', middle_data[0]):
  222. if re.search('^(#(?:[0-9a-f-A-F]{3}){1,2})', middle_data[0]):
  223. middle_search = re.search('^(#(?:[0-9a-f-A-F]{3}){1,2})', middle_data[0])
  224. middle_list += ['span']
  225. data = middle_re.sub('<span style="color: ' + middle_search.groups()[0] + ';">', data, 1)
  226. elif re.search('^(?:#(\w+))', middle_data[0]):
  227. middle_search = re.search('^(?:#(\w+))', middle_data[0])
  228. middle_list += ['span']
  229. data = middle_re.sub('<span style="color: ' + middle_search.groups()[0] + ';">', data, 1)
  230. elif re.search('^(?:@((?:[0-9a-f-A-F]{3}){1,2}))', middle_data[0]):
  231. middle_search = re.search('^(?:@((?:[0-9a-f-A-F]{3}){1,2}))', middle_data[0])
  232. middle_list += ['span']
  233. data = middle_re.sub('<span style="background: #' + middle_search.groups()[0] + ';">', data, 1)
  234. elif re.search('^(?:@(\w+))', middle_data[0]):
  235. middle_search = re.search('^(?:@(\w+))', middle_data[0])
  236. middle_list += ['span']
  237. data = middle_re.sub('<span style="background: ' + middle_search.groups()[0] + ';">', data, 1)
  238. elif re.search('^(\+|-)([1-5])', middle_data[0]):
  239. middle_search = re.search('^(\+|-)([1-5])', middle_data[0])
  240. middle_search = middle_search.groups()
  241. if middle_search[0] == '+':
  242. font_size = str(int(middle_search[1]) * 20 + 100)
  243. else:
  244. font_size = str(100 - int(middle_search[1]) * 10)
  245. middle_list += ['span']
  246. data = middle_re.sub('<span style="font-size: ' + font_size + '%;">', data, 1)
  247. elif re.search('^#!wiki', middle_data[0]):
  248. middle_data_2 = re.search('{{{#!wiki(?: style=(?:&quot;|&#x27;)((?:(?!&quot;|&#x27;).)*)(?:&quot;|&#x27;))?(?: *)\n?', data)
  249. if middle_data_2:
  250. middle_data_2 = middle_data_2.groups()
  251. else:
  252. middle_data_2 = ['']
  253. middle_list += ['div_1']
  254. data = re.sub(
  255. '{{{#!wiki(?: style=(?:&quot;|&#x27;)((?:(?!&quot;|&#x27;).)*)(?:&quot;|&#x27;))?(?: *)\n?',
  256. '<div id="wiki_div" style="' + str(middle_data_2[0] if middle_data_2[0] else '') + '">',
  257. data,
  258. 1
  259. )
  260. elif re.search('^#!syntax', middle_data[0]):
  261. middle_data_2 = re.search('{{{#!syntax ((?:(?!\n).)+)\n?', data)
  262. if middle_data_2:
  263. middle_data_2 = middle_data_2.groups()
  264. else:
  265. middle_data_2 = ['python']
  266. if syntax_num == 0:
  267. plus_data += 'hljs.initHighlightingOnLoad();\n'
  268. syntax_num = 1
  269. middle_list += ['pre']
  270. data = re.sub(
  271. '{{{#!syntax ?((?:(?!\n).)*)\n?',
  272. '<pre id="syntax"><code class="' + middle_data_2[0] + '">',
  273. data,
  274. 1
  275. )
  276. elif re.search('^#!folding', middle_data[0]):
  277. middle_list += ['2div']
  278. folding_data = re.search('{{{#!folding ?((?:(?!\n).)*)\n?', data)
  279. if folding_data:
  280. folding_data = folding_data.groups()
  281. else:
  282. folding_data = ['Test']
  283. data = re.sub(
  284. '{{{#!folding ?((?:(?!\n).)*)\n?', '' + \
  285. '<div>' + \
  286. str(folding_data[0]) + ' ' + \
  287. '<div style="display: inline-block;">' + \
  288. '<a href="javascript:void(0);" onclick="do_open_folding(\'' + include_num + 'folding_' + str(folding_num) + '\', this);">' + \
  289. '[+]' + \
  290. '</a>' + \
  291. '</div_2>' + \
  292. '<div id="' + include_num + 'folding_' + str(folding_num) + '" style="display: none;">' + \
  293. '<div id="wiki_div" style="">',
  294. data,
  295. 1
  296. )
  297. folding_num += 1
  298. elif re.search('^#!html', middle_data[0]):
  299. middle_list += ['span']
  300. html_num += 1
  301. data = middle_re.sub('<span id="' + include_num + 'render_contect_' + str(html_num) + '">', data, 1)
  302. else:
  303. middle_list += ['span']
  304. data = middle_re.sub('<span>', data, 1)
  305. else:
  306. middle_list += ['code']
  307. middle_stack += 1
  308. data = middle_re.sub('<code>' + middle_data[0].replace('\\', '\\\\'), data, 1)
  309. middle_num += 1
  310. else:
  311. if middle_list == []:
  312. data = middle_re.sub('&#125;&#125;&#125;', data, 1)
  313. else:
  314. if middle_stack > 0:
  315. middle_stack -= 1
  316. if middle_stack > 0:
  317. data = middle_re.sub('&#125;&#125;&#125;', data, 1)
  318. else:
  319. if middle_num > 0:
  320. middle_num -= 1
  321. if middle_list[middle_num] == '2div':
  322. data = middle_re.sub('</div_1></div_2></div_2>', data, 1)
  323. elif middle_list[middle_num] == 'pre':
  324. data = middle_re.sub('</code></pre>', data, 1)
  325. else:
  326. data = middle_re.sub('</' + middle_list[middle_num] + '>', data, 1)
  327. del(middle_list[middle_num])
  328. while 1:
  329. if middle_stack == 0:
  330. break
  331. else:
  332. if middle_list == []:
  333. data += '&#125;&#125;&#125;'
  334. else:
  335. if middle_stack > 0:
  336. middle_stack -= 1
  337. if middle_stack > 0:
  338. data += '&#125;&#125;&#125;'
  339. else:
  340. if middle_num > 0:
  341. middle_num -= 1
  342. if middle_list[middle_num] == '2div':
  343. data += '</div_1></div_2></div_2>'
  344. elif middle_list[middle_num] == 'pre':
  345. data += '</code></pre>'
  346. else:
  347. data += '</' + middle_list[middle_num] + '>'
  348. del(middle_list[middle_num])
  349. while 1:
  350. nowiki_data = re.search('<code>((?:(?:(?!<\/code>).)*\n*)*)<\/code>', data)
  351. if nowiki_data:
  352. nowiki_data = nowiki_data.groups()
  353. nowiki_num += 1
  354. end_data['nowiki_' + str(nowiki_num)] = nowiki_data[0]
  355. plus_data += 'document.getElementById("nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_data[0].replace('\\', '\\\\').replace('"', '\\"') + '";\n'
  356. data = re.sub(
  357. '<code>((?:(?:(?!<\/code>).)*\n*)*)<\/code>',
  358. '<span id="nowiki_' + str(nowiki_num) + '"></span>',
  359. data,
  360. 1
  361. )
  362. else:
  363. break
  364. while 1:
  365. syntax_data = re.search('<code class="((?:(?!"|>|<).)+)">((?:\n*(?:(?:(?!<\/code>|<span id="nowiki_).)+)\n*)+)<\/code>', data)
  366. if syntax_data:
  367. syntax_data = syntax_data.groups()
  368. nowiki_num += 1
  369. end_data['nowiki_' + str(nowiki_num)] = syntax_data[1]
  370. plus_data += 'document.getElementById("nowiki_' + str(nowiki_num) + '").innerHTML = "' + syntax_data[1].replace('\\', '\\\\').replace('"', '\\"') + '";\n'
  371. data = re.sub(
  372. '<code class="((?:(?!"|>|<).)+)">((?:\n*(?:(?:(?!<\/code>|<span id="syntax_).)+)\n*)+)<\/code>',
  373. '<code class="' + syntax_data[0] + '"><span id="nowiki_' + str(nowiki_num) + '"></span></code>',
  374. data,
  375. 1
  376. )
  377. else:
  378. break
  379. return data
  380. def namumark(conn, data, title, main_num, include_num):
  381. curs = conn.cursor()
  382. global plus_data
  383. global end_data
  384. global nowiki_num
  385. nowiki_num = 0
  386. data = '\n' + data + '\n'
  387. include_num = include_num + '_' if include_num else ''
  388. plus_data = 'get_link_state("' + include_num + '");\nget_file_state("' + include_num + '");\n'
  389. backlink = []
  390. end_data = {}
  391. data = re.sub('<math>(?P<in>(?:(?!<\/math>).)+)<\/math>', '[math(\g<in>)]', data)
  392. data = html.escape(data)
  393. data = re.sub('\r\n', '\n', data)
  394. first = 0
  395. math_re = re.compile('\[math\(((?:(?!\)\]).)+)\)\]', re.I)
  396. while 1:
  397. math = math_re.search(data)
  398. if math:
  399. math = math.groups()[0]
  400. first += 1
  401. data = math_re.sub('<span id="math_' + str(first) + '"></span>', data, 1)
  402. plus_data += '''
  403. try {
  404. katex.render(
  405. "''' + html.unescape(math).replace('\\', '\\\\').replace('"', '\\"') + '''",
  406. document.getElementById("math_''' + str(first) + '''")
  407. );
  408. } catch {
  409. document.getElementById("math_''' + str(first) + '''").innerHTML = '<span style="color: red;">''' + math.replace('\\', '\\\\') + '''</span>';
  410. }\n
  411. '''
  412. else:
  413. break
  414. data = re.sub('\\\\{', '<break_middle>', data)
  415. data = middle_parser(data, include_num)
  416. data = re.sub('<break_middle>', '\\{', data)
  417. num = 0
  418. while 1:
  419. one_nowiki = re.search('(?:\\\\)(.)', data)
  420. if one_nowiki:
  421. one_nowiki = one_nowiki.groups()
  422. nowiki_num += 1
  423. end_data['nowiki_' + str(nowiki_num)] = one_nowiki[0]
  424. plus_data += 'document.getElementById("nowiki_' + str(nowiki_num) + '").innerHTML = "' + one_nowiki[0].replace('\\', '\\\\').replace('"', '\\"') + '";\n'
  425. data = re.sub('(?:\\\\)(.)', '<span id="nowiki_' + str(nowiki_num) + '"></span>', data, 1)
  426. else:
  427. break
  428. include_re = re.compile('\[include\(((?:(?!\)\]).)+)\)\]', re.I)
  429. i = 0
  430. while 1:
  431. i += 1
  432. include = include_re.search(data)
  433. if include:
  434. include = include.groups()[0]
  435. include_data = re.search('^((?:(?!,).)+)', include)
  436. if include_data:
  437. include_data = include_data.groups()[0]
  438. else:
  439. include_data = 'Test'
  440. include_link = include_data
  441. backlink += [[title, include_link, 'include']]
  442. data = include_re.sub('' + \
  443. '<a id="include_link" class="include_' + str(i) + '" href="/w/' + tool.url_pas(include_link) + '">[' + include_link + ']</a>' + \
  444. '<div id="include_' + str(i) + '"></div>' + \
  445. '', data, 1)
  446. include_plus_data = []
  447. while 1:
  448. include_plus = re.search(', ?((?:(?!=).)+)=((?:(?!,).)+)', include)
  449. if include_plus:
  450. include_plus = include_plus.groups()
  451. include_data_set = include_plus[1]
  452. find_data = re.findall('<span id="(nowiki_[0-9]+)">', include_data_set)
  453. for j in find_data:
  454. include_data_set = include_data_set.replace('<span id="' + j + '"></span>', end_data[j])
  455. include_plus_data += [[include_plus[0], include_data_set]]
  456. include = re.sub(', ?((?:(?!=).)+)=((?:(?!,).)+)', '', include, 1)
  457. else:
  458. break
  459. plus_data += 'load_include("' + include_link + '", "include_' + str(i) + '", ' + str(include_plus_data) + ');\n'
  460. else:
  461. break
  462. data = re.sub('\r\n', '\n', data)
  463. data = re.sub('&amp;', '&', data)
  464. data = re.sub('\n( +)\|\|', '\n||', data)
  465. data = re.sub('\|\|( +)\n', '||\n', data)
  466. data = re.sub('\n##(((?!\n).)+)', '', data)
  467. data = re.sub('<div id="wiki_div" style="">\n', '<div id="wiki_div" style="">', data)
  468. while 1:
  469. wiki_table_data = re.search('<div id="wiki_div" ((?:(?!>).)+)>((?:(?!<div id="wiki_div"|<\/div_1>).\n*)+)<\/div_1>', data)
  470. if wiki_table_data:
  471. wiki_table_data = wiki_table_data.groups()
  472. if re.search('\|\|', wiki_table_data[1]):
  473. end_parser = re.sub('\n$', '', re.sub('^\n', '', table_start('\n' + wiki_table_data[1] + '\n')))
  474. else:
  475. end_parser = wiki_table_data[1]
  476. data = re.sub('<div id="wiki_div" ((?:(?!>).)+)>((?:(?!<div id="wiki_div"|<\/div_1>).\n*)+)<\/div_1>', '<div ' + wiki_table_data[0] + '>' + end_parser + '</div_2>', data, 1)
  477. else:
  478. break
  479. data = re.sub('<\/div_2>', '</div>', data)
  480. data = re.sub('<\/td>', '</td_1>', data)
  481. data += '\n'
  482. data = data.replace('\\', '&#92;')
  483. redirect_re = re.compile('\n#(?:redirect|넘겨주기) ((?:(?!\n).)+)\n', re.I)
  484. redirect = redirect_re.search(data)
  485. if redirect:
  486. redirect = redirect.groups()[0]
  487. return_link = link_fix(redirect)
  488. main_link = return_link[0]
  489. other_link = return_link[1]
  490. backlink += [[title, main_link + other_link, 'redirect']]
  491. data = redirect_re.sub(
  492. '\n' + \
  493. '<ul>' + \
  494. '<li>' + \
  495. '<a href="' + tool.url_pas(main_link) + other_link + '">' + main_link + other_link + '</a>' + \
  496. '</li>' + \
  497. '</ul>' + \
  498. '\n',
  499. data,
  500. 1
  501. )
  502. no_toc_re = re.compile('\[(?:목차|toc)\((?:no)\)\]\n', re.I)
  503. toc_re = re.compile('\[(?:목차|toc)\]', re.I)
  504. if not no_toc_re.search(data):
  505. if not toc_re.search(data):
  506. data = re.sub('\n(?P<in>={1,6}) ?(?P<out>(?:(?!=).)+) ?={1,6}\n', '\n[toc]\n\g<in> \g<out> \g<in>\n', data, 1)
  507. else:
  508. data = no_toc_re.sub('', data)
  509. data = '<div class="all_in_data" id="in_data_0">' + data
  510. toc_full = 0
  511. toc_top_stack = 6
  512. toc_stack = [0, 0, 0, 0, 0, 0]
  513. edit_number = 0
  514. toc_data = '<div id="toc"><span id="toc_title">TOC</span>\n\n'
  515. while 1:
  516. toc = re.search('\n(={1,6}) ?((?:(?!\n).)+) ?(?:={1,6})\n', data)
  517. if toc:
  518. toc = toc.groups()
  519. toc_number = len(toc[0])
  520. edit_number += 1
  521. if toc_full > toc_number:
  522. for i in range(toc_number, 6):
  523. toc_stack[i] = 0
  524. if toc_top_stack > toc_number:
  525. toc_top_stack = toc_number
  526. toc_full = toc_number
  527. toc_stack[toc_number - 1] += 1
  528. toc_number = str(toc_number)
  529. all_stack = ''
  530. for i in range(0, 6):
  531. all_stack += str(toc_stack[i]) + '.'
  532. while 1:
  533. if re.search('[^0-9]0\.', all_stack):
  534. all_stack = re.sub('[^0-9]0\.', '.', all_stack)
  535. else:
  536. break
  537. all_stack = re.sub('^0\.', '', all_stack)
  538. all_stack = re.sub('\.$', '', all_stack)
  539. new_toc_data = re.sub('=*$', '', toc[1])
  540. new_toc_data = re.sub(' +$', '', new_toc_data)
  541. if re.search('^# ?(?P<in>[^#]+) ?#$', new_toc_data):
  542. fol_head = '+'
  543. new_toc_data = re.sub('^# ?(?P<in>[^#]+) ?#$', '\g<in>', new_toc_data)
  544. else:
  545. fol_head = '-'
  546. data = re.sub(
  547. '\n(={1,6}) ?((?:(?!\n).)+) ?\n',
  548. '\n' + \
  549. '</div>'
  550. '<h' + toc_number + ' id="s-' + all_stack + '">' + \
  551. '<a href="#toc">' + all_stack + '.</a> ' + new_toc_data + ' ' + \
  552. '<span style="font-size: 12px">' + \
  553. '<a href="/edit/' + tool.url_pas(title) + '?section=' + str(edit_number) + '">(Edit)</a>' + \
  554. ' ' + \
  555. '<a href="javascript:void(0);" onclick="do_open_folding(\'in_data_' + all_stack + '\', this);">' + \
  556. '[' + fol_head + ']' + \
  557. '</a>' + \
  558. '</span>' + \
  559. '</h' + toc_number + '>' + \
  560. '<div class="all_in_data"' + (' style="display: none;"' if fol_head == '+' else '') + ' id="in_data_' + all_stack + '">' + \
  561. '\n',
  562. data,
  563. 1
  564. )
  565. toc_main_data = new_toc_data
  566. toc_main_data = re.sub('\[\*((?:(?! |\]).)*)(?: ((?:(?!(\[\*(?:(?:(?!\]).)+)\]|\])).)+))?\]', '', toc_main_data)
  567. toc_main_data = re.sub('<span id="math_[0-9]"><\/span>', '(Math)', toc_main_data)
  568. toc_data += '' + \
  569. '<span style="margin-left: ' + str((toc_full - toc_top_stack) * 10) + 'px;">' + \
  570. '<a href="#s-' + all_stack + '">' + all_stack + '.</a> ' + toc_main_data + \
  571. '</span>' + \
  572. '\n' + \
  573. ''
  574. else:
  575. break
  576. toc_data += '</div>'
  577. data = toc_re.sub(toc_data, data)
  578. data = tool.savemark(data)
  579. data = re.sub("\[anchor\((?P<in>(?:(?!\)\]).)+)\)\]", '<span id="\g<in>"></span>', data, flags = re.I)
  580. ruby_all = re.findall("\[ruby\(((?:(?:(?!\)\]).)+))\)\]", data, flags = re.I)
  581. for i in ruby_all:
  582. ruby_code = re.search('^([^,]+)', i)
  583. if ruby_code:
  584. ruby_code = ruby_code.groups()[0]
  585. else:
  586. ruby_code = 'Test'
  587. ruby_top = re.search('ruby=([^,]+)', i, flags = re.I)
  588. if ruby_top:
  589. ruby_top = ruby_top.groups()[0]
  590. else:
  591. ruby_top = 'Test'
  592. ruby_color = re.search('color=([^,]+)', i, flags = re.I)
  593. if ruby_color:
  594. ruby_color = 'color: ' + ruby_color.groups()[0] + ';'
  595. else:
  596. ruby_color = ''
  597. ruby_data = '' + \
  598. '<ruby>' + \
  599. ruby_code \
  600. + '<rp>(</rp>' + \
  601. '<rt style="' + ruby_color + '">' + ruby_top + '</rt>' + \
  602. '<rp>)</rp>' + \
  603. '</ruby>' + \
  604. ''
  605. data = re.sub("\[ruby\(((?:(?:(?!\)\]).)+))\)\]", ruby_data, data, 1, flags = re.I)
  606. curs.execute(tool.db_change('select data from other where name = "count_all_title"'))
  607. all_title = curs.fetchall()
  608. data = re.sub('\[pagecount\]', all_title[0][0], data, flags = re.I)
  609. now_time = tool.get_time()
  610. data = re.sub('\[date\]', now_time, data, flags = re.I)
  611. time_data = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  612. time = time_data.groups()
  613. age_re = re.compile('\[age\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', re.I)
  614. while 1:
  615. age_data = age_re.search(data)
  616. if age_data:
  617. age = age_data.groups()[0]
  618. try:
  619. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  620. will = datetime.datetime.strptime(age, '%Y-%m-%d')
  621. e_data = old - will
  622. data = age_re.sub(str(int(e_data.days / 365)), data, 1)
  623. except:
  624. data = age_re.sub('age-error', data, 1)
  625. else:
  626. break
  627. dday_re = re.compile('\[dday\(([0-9]{4}-[0-9]{2}-[0-9]{2})\)\]', re.I)
  628. while 1:
  629. dday_data = dday_re.search(data)
  630. if dday_data:
  631. dday = dday_data.groups()[0]
  632. try:
  633. old = datetime.datetime.strptime(time[0], '%Y-%m-%d')
  634. will = datetime.datetime.strptime(dday, '%Y-%m-%d')
  635. e_data = old - will
  636. if re.search('^-', str(e_data.days)):
  637. e_day = str(e_data.days)
  638. else:
  639. e_day = '+' + str(e_data.days)
  640. data = dday_re.sub(e_day, data, 1)
  641. except:
  642. data = dday_re.sub('dday-error', data, 1)
  643. else:
  644. break
  645. video_re = re.compile('\[(youtube|kakaotv|nicovideo)\(((?:(?!\)\]).)+)\)\]', re.I)
  646. youtube_re = re.compile('youtube', re.I)
  647. kakaotv_re = re.compile('kakaotv', re.I)
  648. while 1:
  649. video = video_re.search(data)
  650. if video:
  651. video = video.groups()
  652. width = re.search(', ?width=((?:(?!,).)+)', video[1])
  653. if width:
  654. video_width = width.groups()[0]
  655. if re.search('^[0-9]+$', video_width):
  656. video_width += 'px'
  657. else:
  658. video_width = '560px'
  659. height = re.search(', ?height=((?:(?!,).)+)', video[1])
  660. if height:
  661. video_height = height.groups()[0]
  662. if re.search('^[0-9]+$', video_height):
  663. video_height += 'px'
  664. else:
  665. video_height = '315px'
  666. code = re.search('^((?:(?!,).)+)', video[1])
  667. if code:
  668. video_code = code.groups()[0]
  669. else:
  670. video_code = ''
  671. video_start = ''
  672. if youtube_re.search(video[0]):
  673. start = re.search(', ?(start=(?:(?!,).)+)', video[1])
  674. if start:
  675. video_start = '?' + start.groups()[0]
  676. video_code = re.sub('^https:\/\/www\.youtube\.com\/watch\?v=', '', video_code)
  677. video_code = re.sub('^https:\/\/youtu\.be\/', '', video_code)
  678. video_src = 'https://www.youtube.com/embed/' + video_code
  679. elif kakaotv_re.search(video[0]):
  680. video_code = re.sub('^https:\/\/tv\.kakao\.com\/channel\/9262\/cliplink\/', '', video_code)
  681. video_code = re.sub('^http:\/\/tv\.kakao\.com\/v\/', '', video_code)
  682. video_src = 'https://tv.kakao.com/embed/player/cliplink/' + video_code +'?service=kakao_tv'
  683. else:
  684. video_src = 'https://embed.nicovideo.jp/watch/' + video_code
  685. data = video_re.sub(
  686. '<iframe style="width: ' + video_width + '; height: ' + video_height + ';" src="' + video_src + video_start + '" allowfullscreen></iframe>',
  687. data,
  688. 1
  689. )
  690. else:
  691. break
  692. while 1:
  693. block = re.search('(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', data)
  694. if block:
  695. block = block.groups()[0]
  696. block = re.sub('^\n&gt; ?', '', block)
  697. block = re.sub('\n&gt; ?', '\n', block)
  698. block = re.sub('\n$', '', block)
  699. data = re.sub('(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', '\n<blockquote>' + block + '</blockquote>\n', data, 1)
  700. else:
  701. break
  702. while 1:
  703. hr = re.search('\n-{4,9}\n', data)
  704. if hr:
  705. data = re.sub('\n-{4,9}\n', '\n<hr>\n', data, 1)
  706. else:
  707. break
  708. data = re.sub('(?P<in>\n +\* ?(?:(?:(?!\|\|).)+))\|\|', '\g<in>\n ||', data)
  709. data = re.sub('(?P<in><div id="folding_(?:[0-9]+)" style="display: none;"><div style="">|<blockquote>)(?P<out> )?\* ', '\g<in>\n\g<out>* ', data)
  710. while 1:
  711. li = re.search('(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', data)
  712. if li:
  713. li = li.groups()[0]
  714. while 1:
  715. sub_li = re.search('\n(?:( *)\* ?((?:(?!\n).)+))', li)
  716. if sub_li:
  717. sub_li = sub_li.groups()
  718. if len(sub_li[0]) == 0:
  719. margin = 20
  720. else:
  721. margin = len(sub_li[0]) * 20
  722. li = re.sub('\n(?:( *)\* ?((?:(?!\n).)+))', '<li style="margin-left: ' + str(margin) + 'px;">' + sub_li[1] + '</li>', li, 1)
  723. else:
  724. break
  725. data = re.sub('(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', '\n\n<ul>' + li + '</ul>\n', data, 1)
  726. else:
  727. break
  728. data = re.sub('<\/ul>\n \|\|', '</ul>||', data)
  729. data = re.sub('\|\|</blockquote>', '</blockquote>||', data)
  730. while 1:
  731. indent = re.search('\n( +)', data)
  732. if indent:
  733. indent = len(indent.groups()[0])
  734. margin = '<span style="margin-left: 20px;"></span>' * indent
  735. data = re.sub('\n( +)', '\n' + margin, data, 1)
  736. else:
  737. break
  738. data = table_start(data)
  739. category = ''
  740. category_re = re.compile('^(?:category|분류):', re.I)
  741. while 1:
  742. link = re.search('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', data)
  743. if link:
  744. link = link.groups()[0]
  745. link_split = re.search('((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  746. if link_split:
  747. link_split = link_split.groups()
  748. main_link = link_split[0]
  749. see_link = link_split[1]
  750. else:
  751. main_link = link
  752. see_link = link
  753. if re.search('^((?:file|파일)|(?:out|외부)):', main_link):
  754. file_style = ''
  755. file_width = re.search('width=((?:(?!&).)+)', see_link)
  756. if file_width:
  757. file_width = file_width.groups()[0]
  758. if re.search('px$', file_width):
  759. file_style += 'width: ' + file_width + ';'
  760. else:
  761. file_style += 'width: ' + file_width + 'px;'
  762. file_height = re.search('height=((?:(?!&).)+)', see_link)
  763. if file_height:
  764. file_height = file_height.groups()[0]
  765. if re.search('px$', file_height):
  766. file_style += 'height: ' + file_height + ';'
  767. else:
  768. file_style += 'height: ' + file_height + 'px;'
  769. file_align = re.search('align=((?:(?!&).)+)', see_link)
  770. if file_align:
  771. file_align = file_align.groups()[0]
  772. if file_align == 'center':
  773. file_align = 'display: block; text-align: center;'
  774. else:
  775. file_align = 'float: ' + file_align + ';'
  776. else:
  777. file_align = ''
  778. file_color = re.search('bgcolor=((?:(?!&).)+)', see_link)
  779. if file_color:
  780. file_color = 'background: ' + file_color.groups()[0] + '; display: inline-block;'
  781. else:
  782. file_color = ''
  783. if re.search('^(?:out|외부):', main_link):
  784. file_src = re.sub('^(?:out|외부):', '', main_link)
  785. file_alt = main_link
  786. exist = 'Yes'
  787. else:
  788. file_data = re.search('^(?:file|파일):((?:(?!\.).)+)\.(.+)$', main_link)
  789. if file_data:
  790. file_data = file_data.groups()
  791. file_name = file_data[0]
  792. file_end = file_data[1]
  793. if main_link != title:
  794. backlink += [[title, main_link, 'file']]
  795. else:
  796. file_name = 'TEST'
  797. file_end = 'jpg'
  798. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  799. file_alt = 'file:' + file_name + '.' + file_end
  800. exist = None
  801. if exist:
  802. data = re.sub(
  803. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  804. '<span style="' + file_align + '">' + \
  805. '<span style="' + file_color + '">' + \
  806. '<img style="' + file_style + '" alt="' + file_alt + '" src="' + file_src + '">' + \
  807. '</span>' + \
  808. '</span>',
  809. data,
  810. 1
  811. )
  812. else:
  813. data = re.sub(
  814. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  815. '<span style="' + file_align + '">' + \
  816. '<span style="' + file_color + '">' + \
  817. '<img class="' + include_num + 'file_finder_1" style="' + file_style + '" alt="' + file_alt + '" src="' + file_src + '">' + \
  818. '<a class="' + include_num + 'file_finder_2" id="not_thing" href="/upload?name=' + tool.url_pas(file_name) + '">' + file_alt + '</a>' + \
  819. '</span>' + \
  820. '</span>',
  821. data,
  822. 1
  823. )
  824. elif category_re.search(main_link):
  825. if category == '':
  826. category += '<div id="cate_all"><hr><div id="cate">Category : '
  827. main_link = category_re.sub('category:', main_link)
  828. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  829. if re.search('#blur', main_link):
  830. see_link = 'Hidden'
  831. link_id = 'id="inside"'
  832. main_link = re.sub('#blur', '', main_link)
  833. backlink += [[title, main_link, 'cat']]
  834. category += '<a class="' + include_num + 'link_finder" href="/w/' + tool.url_pas(main_link) + '">' + category_re.sub('', see_link) + '</a> | '
  835. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '', data, 1)
  836. elif re.search('^wiki:', main_link):
  837. data = re.sub(
  838. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  839. '<a id="inside" href="/' + tool.url_pas(re.sub('^wiki:', '', main_link)) + '">' + see_link + '</a>',
  840. data,
  841. 1
  842. )
  843. elif re.search('^inter:((?:(?!:).)+):', main_link):
  844. inter_data = re.search('^inter:((?:(?!:).)+):((?:(?!\]\]|\|).)+)', main_link)
  845. inter_data = inter_data.groups()
  846. curs.execute(tool.db_change('select link, icon from inter where title = ?'), [inter_data[0]])
  847. inter = curs.fetchall()
  848. if inter:
  849. if inter[0][1] != '':
  850. inter_view = inter[0][1]
  851. else:
  852. inter_view = inter_data[0] + ':'
  853. if see_link != main_link:
  854. data = re.sub(
  855. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  856. '<a id="inside" href="' + inter[0][0] + inter_data[1] + '">' + inter_view + see_link + '</a>',
  857. data,
  858. 1
  859. )
  860. else:
  861. data = re.sub(
  862. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  863. '<a id="inside" href="' + inter[0][0] + inter_data[1] + '">' + inter_view + inter_data[1] + '</a>',
  864. data,
  865. 1
  866. )
  867. else:
  868. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', 'Not exist', data, 1)
  869. elif re.search('^(\/(?:.+))$', main_link):
  870. under_title = re.search('^(\/(?:.+))$', main_link)
  871. under_title = under_title.groups()[0]
  872. if see_link != main_link:
  873. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '[[' + title + under_title + '|' + see_link + ']]', data, 1)
  874. else:
  875. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '[[' + title + under_title + ']]', data, 1)
  876. elif re.search('^http(s)?:\/\/', main_link):
  877. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '<a id="out_link" rel="nofollow" href="' + main_link + '">' + see_link + '</a>', data, 1)
  878. else:
  879. return_link = link_fix(main_link)
  880. main_link = html.unescape(return_link[0])
  881. other_link = return_link[1]
  882. if re.search('^\/', main_link):
  883. main_link = re.sub('^\/', title + '/', main_link)
  884. elif re.search('\.\.\/\/', main_link):
  885. main_link = re.sub('\.\.\/\/', '/', main_link)
  886. elif re.search('^\.\.\/', main_link):
  887. main_link = re.sub('^\.\.\/', re.sub('(?P<in>.+)\/.*$', '\g<in>', title), main_link)
  888. if not re.search('^\|', main_link):
  889. if main_link != title:
  890. if main_link != '':
  891. backlink += [[title, main_link, '']]
  892. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  893. if not curs.fetchall():
  894. backlink += [[title, main_link, 'no']]
  895. data = re.sub(
  896. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  897. '<a class="' + include_num + 'link_finder" ' + \
  898. 'title="' + main_link + other_link + '" ' + \
  899. 'href="/w/' + tool.url_pas(main_link) + other_link + '"' + \
  900. '>' + see_link + '</a>',
  901. data,
  902. 1
  903. )
  904. else:
  905. data = re.sub(
  906. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  907. '<a title="' + other_link + '" href="' + other_link + '">' + see_link + '</a>',
  908. data,
  909. 1
  910. )
  911. else:
  912. if re.search('^#', other_link):
  913. data = re.sub(
  914. '\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]',
  915. '<a title="' + other_link + '" href="' + other_link + '">' + other_link + '</a>',
  916. data,
  917. 1
  918. )
  919. else:
  920. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '<b>' + see_link + '</b>', data, 1)
  921. else:
  922. data = re.sub('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]', '&#91;&#91;' + link + '&#93;&#93;', data, 1)
  923. else:
  924. break
  925. br_re = re.compile('\[br\]', re.I)
  926. data = br_re.sub('<br>', data)
  927. footnote_number = 0
  928. footnote_all = []
  929. footnote_dict = {}
  930. footnote_re = {}
  931. footdata_all = '<hr><ul id="footnote_data">'
  932. re_footnote = re.compile('(?:\[\*((?:(?! |\]).)*)(?: ((?:(?!(?:\[\*|\])).)+))?\]|(\[(?:각주|footnote)\]))')
  933. while 1:
  934. footnote = re_footnote.search(data)
  935. if footnote:
  936. footnote_data = footnote.groups()
  937. if footnote_data[2]:
  938. footnote_all.sort()
  939. for footdata in footnote_all:
  940. if footdata[2] == 0:
  941. footdata_in = ''
  942. else:
  943. footdata_in = footdata[2]
  944. footdata_all += '' + \
  945. '<li>' + \
  946. '<a href="#' + include_num + 'rfn-' + str(footdata[0]) + '" ' + \
  947. 'id="' + include_num + 'cfn-' + str(footdata[0]) + '" ' + \
  948. 'onclick="do_open_foot(\'' + include_num + 'fn-' + str(footdata[0]) + '\', 1);">' + \
  949. '(' + footdata[1] + ')' + \
  950. '</a> <span id="' + include_num + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  951. '</li>' + \
  952. ''
  953. data = re_footnote.sub(footdata_all + '</ul>', data, 1)
  954. footnote_all = []
  955. footdata_all = '<hr><ul id="footnote_data">'
  956. else:
  957. footnote = footnote_data[1]
  958. footnote_name = footnote_data[0]
  959. if footnote_name and not footnote:
  960. if footnote_name in footnote_dict:
  961. footnote_re[footnote_name] += 1
  962. foot_plus_num = str(footnote_re[footnote_name])
  963. footshort = footnote_dict[footnote_name] + '.' + foot_plus_num
  964. footnote_all += [[float(footshort), footshort, 0]]
  965. data = re_footnote.sub('' + \
  966. '<sup>' + \
  967. '<a href="#' + include_num + 'fn-' + footshort + '" ' + \
  968. 'id="' + include_num + 'rfn-' + footshort + '" ' + \
  969. 'onclick="do_open_foot(\'' + include_num + 'fn-' + footshort + '\');">' + \
  970. '(' + footnote_name + ')' + \
  971. '</a>' + \
  972. '</sup>' + \
  973. '', data, 1)
  974. else:
  975. data = re_footnote.sub('<sup><a href="javascript:void(0);">(' + footnote_name + ')</a></sup>', data, 1)
  976. else:
  977. footnote_number += 1
  978. if not footnote_name:
  979. footnote_name = str(footnote_number)
  980. footnote_dict.update({ footnote_name : str(footnote_number) })
  981. if not footnote_name in footnote_re:
  982. footnote_re.update({ footnote_name : 0 })
  983. else:
  984. footnote_re[footnote_name] += 1
  985. footnote_all += [[footnote_number, footnote_name, footnote]]
  986. data = re_footnote.sub('' + \
  987. '<sup>' + \
  988. '<a href="#' + include_num + 'fn-' + str(footnote_number) + '" ' + \
  989. 'id="' + include_num + 'rfn-' + str(footnote_number) + '" ' + \
  990. 'onclick="do_open_foot(\'' + include_num + 'fn-' + str(footnote_number) + '\');">' + \
  991. '(' + footnote_name + ')' + \
  992. '</a>' + \
  993. '</sup>' + \
  994. '', data, 1)
  995. else:
  996. break
  997. data = re.sub('\n+$', '', data)
  998. footnote_all.sort()
  999. for footdata in footnote_all:
  1000. if footdata[2] == 0:
  1001. footdata_in = ''
  1002. else:
  1003. footdata_in = str(footdata[2])
  1004. footdata_all += '' + \
  1005. '<li>' + \
  1006. '<a href="#' + include_num + 'rfn-' + str(footdata[0]) + '" ' + \
  1007. 'id="' + include_num + 'cfn-' + str(footdata[0]) + '" ' + \
  1008. 'onclick="do_open_foot(\'' + include_num + 'fn-' + str(footdata[0]) + '\', 1);">' + \
  1009. '(' + str(footdata[1]) + ')' + \
  1010. '</a> <span id="' + include_num + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1011. '</li>' + \
  1012. ''
  1013. footdata_all += '</ul>'
  1014. footdata_all = '</div>' + footdata_all
  1015. if footdata_all == '</div><hr><ul id="footnote_data"></ul>':
  1016. footdata_all = '</div>'
  1017. data = re.sub('\n$', footdata_all, data + '\n', 1)
  1018. data = re.sub('&#x27;&#x27;&#x27;(?P<in>((?!&#x27;&#x27;&#x27;).)+)&#x27;&#x27;&#x27;', '<b>\g<in></b>', data)
  1019. data = re.sub('&#x27;&#x27;(?P<in>((?!&#x27;&#x27;).)+)&#x27;&#x27;', '<i>\g<in></i>', data)
  1020. data = re.sub('~~(?P<in>(?:(?!~~).)+)~~', '<s>\g<in></s>', data)
  1021. data = re.sub('--(?P<in>(?:(?!--).)+)--', '<s>\g<in></s>', data)
  1022. data = re.sub('__(?P<in>(?:(?!__).)+)__', '<u>\g<in></u>', data)
  1023. data = re.sub('\^\^(?P<in>(?:(?!\^\^).)+)\^\^', '<sup>\g<in></sup>', data)
  1024. data = re.sub(',,(?P<in>(?:(?!,,).)+),,', '<sub>\g<in></sub>', data)
  1025. if category != '':
  1026. category = re.sub(' \| $', '', category) + '</div></div>'
  1027. data += category
  1028. data = re.sub('<\/td_1>', '</td>', data)
  1029. data = re.sub('<\/ul>\n?', '</ul>', data)
  1030. data = re.sub('<\/pre>\n?', '</pre>', data)
  1031. data = re.sub('(?P<in><div class="all_in_data"(?:(?:(?!id=).)+)? id="in_data_([^"]+)">)(\n)+', '\g<in>', data)
  1032. data = re.sub('\n\n<ul>', '\n<ul>', data)
  1033. data = re.sub('<\/ul>\n\n', '</ul>', data)
  1034. data = re.sub('^(\n)+', '', data)
  1035. data = re.sub('(\n)+<hr><ul id="footnote_data">', '<hr><ul id="footnote_data">', data)
  1036. data = re.sub('(?P<in><td(((?!>).)*)>)\n', '\g<in>', data)
  1037. data = re.sub('(\n)?<hr>(\n)?', '<hr>', data)
  1038. data = re.sub('<\/ul>\n\n<ul>', '</ul>\n<ul>', data)
  1039. data = re.sub('<\/ul>\n<ul>', '</ul><ul>', data)
  1040. data = re.sub('\n<\/ul>', '</ul>', data)
  1041. data = re.sub('\n', '<br>', data)
  1042. plus_data = 'render_html("' + include_num + 'render_contect");\n' + plus_data
  1043. return [data, plus_data, backlink]