namumark.py 52 KB

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