namumark.py 54 KB

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