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