namumark.py 54 KB

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