2
0

namumark.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. if re.search(r'\[pagecount\]', data, flags = re.I):
  731. plus_data += 'page_count();\n'
  732. data = re.sub(r'\[pagecount\]', '<span class="all_page_count"></span>', data, flags = re.I)
  733. data = re.sub(r'\[date\]', now_time, data, flags = re.I)
  734. while 1:
  735. block = re.search(r'(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', data)
  736. if block:
  737. block = block.group(1)
  738. block = re.sub(r'^\n&gt; ?', '', block)
  739. block = re.sub(r'\n&gt; ?', '\n', block)
  740. block = re.sub(r'\n$', '', block)
  741. data = re.sub(r'(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', '\n<blockquote>' + block + '</blockquote>\n', data, 1)
  742. else:
  743. break
  744. while 1:
  745. hr = re.search(r'\n-{4,9}\n', data)
  746. if hr:
  747. data = re.sub(r'\n-{4,9}\n', '\n<hr>\n', data, 1)
  748. else:
  749. break
  750. data = re.sub(r'(?P<in>\n +\* ?(?:(?:(?!\|\|).)+))\|\|', '\g<in>\n ||', data)
  751. 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)
  752. while 1:
  753. li = re.search(r'(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', data)
  754. if li:
  755. li = li.group(1)
  756. while 1:
  757. sub_li = re.search(r'\n(?:( *)\* ?((?:(?!\n).)+))', li)
  758. if sub_li:
  759. sub_li = sub_li.groups()
  760. if len(sub_li[0]) == 0:
  761. margin = 20
  762. else:
  763. margin = len(sub_li[0]) * 20
  764. li = re.sub(r'\n(?:( *)\* ?((?:(?!\n).)+))', '<li style="margin-left: ' + str(margin) + 'px;">' + sub_li[1] + '</li>', li, 1)
  765. else:
  766. break
  767. data = re.sub(r'(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', '\n\n<ul>' + li + '</ul>\n', data, 1)
  768. else:
  769. break
  770. data = re.sub(r'<\/ul>\n \|\|', '</ul>||', data)
  771. data = re.sub(r'\|\|</blockquote>', '</blockquote>||', data)
  772. while 1:
  773. indent = re.search(r'\n( +)', data)
  774. if indent:
  775. indent = len(indent.group(1))
  776. margin = '<span style="margin-left: 20px;"></span>' * indent
  777. data = re.sub(r'\n( +)', '\n' + margin, data, 1)
  778. else:
  779. break
  780. data = table_start(data)
  781. category = ''
  782. link_re = re.compile('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]')
  783. category_re = re.compile(r'^(?:category|분류):', re.I)
  784. e_link_id = 0
  785. while 1:
  786. link = link_re.search(data)
  787. if link:
  788. link = link.group(1)
  789. str_e_link_id = str(e_link_id)
  790. e_link_id += 1
  791. link_split = re.search(r'((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  792. if link_split:
  793. link_split = link_split.groups()
  794. main_link = link_split[0]
  795. see_link = link_split[1]
  796. inter_same = 0
  797. else:
  798. main_link = link
  799. see_link = link
  800. inter_same = 1
  801. if re.search(r'^((?:file|파일)|(?:out|외부)):', main_link):
  802. file_style = ''
  803. file_width = re.search(r'width=((?:(?!&).)+)', see_link)
  804. if file_width:
  805. file_width = file_width.group(1)
  806. if re.search(r'px$', file_width):
  807. file_style += 'width: ' + file_width + ';'
  808. else:
  809. file_style += 'width: ' + file_width + 'px;'
  810. file_height = re.search(r'height=((?:(?!&).)+)', see_link)
  811. if file_height:
  812. file_height = file_height.group(1)
  813. if re.search(r'px$', file_height):
  814. file_style += 'height: ' + file_height + ';'
  815. else:
  816. file_style += 'height: ' + file_height + 'px;'
  817. file_align = re.search(r'align=((?:(?!&).)+)', see_link)
  818. if file_align:
  819. file_align = file_align.group(1)
  820. if file_align == 'center':
  821. file_align = 'display: block; text-align: center;'
  822. else:
  823. file_align = 'float: ' + file_align + ';'
  824. else:
  825. file_align = ''
  826. file_color = re.search(r'bgcolor=((?:(?!&).)+)', see_link)
  827. if file_color:
  828. file_color = 'background: ' + file_color.group(1) + '; display: inline-block;'
  829. else:
  830. file_color = ''
  831. if re.search(r'^(?:out|외부):', main_link):
  832. file_src = re.sub(r'^(?:out|외부):', '', main_link)
  833. file_alt = main_link
  834. exist = 'Yes'
  835. else:
  836. file_data = re.search(r'^(?:file|파일):((?:(?!\.).)+)\.(.+)$', main_link)
  837. if file_data:
  838. file_data = file_data.groups()
  839. file_name = file_data[0]
  840. file_end = file_data[1]
  841. if main_link != title:
  842. backlink += [[title, main_link, 'file']]
  843. else:
  844. file_name = 'TEST'
  845. file_end = 'jpg'
  846. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  847. file_alt = 'file:' + file_name + '.' + file_end
  848. exist = None
  849. data = link_re.sub(
  850. '<span style="' + file_align + '">' + \
  851. '<span style="' + file_color + '" ' + \
  852. 'class="' + include_name + 'file_finder" ' + \
  853. 'under_style="' + file_style + '" ' + \
  854. 'under_alt="' + file_alt + '" ' + \
  855. 'under_src="' + file_src + '" ' + \
  856. 'under_href="' + ("out_link" if exist else '/upload?name=' + tool.url_pas(file_name)) + '">' + \
  857. '</span>' + \
  858. '</span>',
  859. data,
  860. 1
  861. )
  862. elif category_re.search(main_link):
  863. if category == '':
  864. category += '<div id="cate_all"><hr><div id="cate">Category : '
  865. main_link = category_re.sub('category:', main_link)
  866. link_id = ''
  867. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  868. if re.search(r'#blur', main_link):
  869. link_id = ' hidden_link'
  870. main_link = main_link.replace('#blur', '')
  871. see_link = see_link.replace('#blur', '')
  872. backlink += [[title, main_link, 'cat']]
  873. category += '' + \
  874. '<a class="' + include_name + 'link_finder' + link_id + '" ' + \
  875. 'href="/w/' + tool.url_pas(main_link) + '">' + \
  876. category_re.sub('', see_link) + \
  877. '</a> | ' + \
  878. ''
  879. data = link_re.sub('', data, 1)
  880. elif re.search(r'^inter:((?:(?!:).)+):', main_link):
  881. inter_data = re.search(r'^inter:((?:(?!:).)+):((?:(?!\]\]).)+)', main_link)
  882. inter_data = inter_data.groups()
  883. curs.execute(tool.db_change('select link, icon from inter where title = ?'), [inter_data[0]])
  884. inter = curs.fetchall()
  885. if inter:
  886. return_link = link_fix(inter_data[1], 1)
  887. main_link = html.unescape(return_link[0])
  888. other_link = return_link[1]
  889. inter_view = inter[0][1] if inter[0][1] != '' else (inter_data[0] + ':')
  890. data = link_re.sub('' + \
  891. '<a id="inside" ' + \
  892. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  893. 'href="">' + inter_view + see_link + '</a>' + \
  894. '',
  895. data,
  896. 1
  897. )
  898. plus_data += "" + \
  899. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  900. (inter[0][0] + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  901. "\n" + \
  902. ""
  903. if inter_same == 1:
  904. plus_data += "" + \
  905. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  906. (inter_view + main_link + other_link).replace('\'', '\\\'') + "';" + \
  907. "\n" + \
  908. ""
  909. else:
  910. data = link_re.sub('', data, 1)
  911. elif re.search(r'^(\/(?:.+))$', main_link):
  912. under_title = re.search(r'^(\/(?:.+))$', main_link)
  913. under_title = under_title.group(1)
  914. if see_link != main_link:
  915. data = link_re.sub('[[' + title + under_title + '|' + see_link + ']]', data, 1)
  916. else:
  917. data = link_re.sub('[[' + title + under_title + ']]', data, 1)
  918. elif re.search(r'^http(s)?:\/\/', main_link):
  919. data = link_re.sub('' + \
  920. '<a id="out_link" ' + \
  921. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  922. 'rel="nofollow" ' + \
  923. 'href="">' + see_link + '</a>' + \
  924. '',
  925. data,
  926. 1
  927. )
  928. plus_data += "" + \
  929. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  930. main_link.replace('\'', '\\\'') + "';" + \
  931. "\n" + \
  932. ""
  933. if inter_same == 1:
  934. plus_data += "" + \
  935. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  936. main_link.replace('\'', '\\\'') + "';" + \
  937. "\n" + \
  938. ""
  939. else:
  940. return_link = link_fix(main_link)
  941. main_link = html.unescape(return_link[0])
  942. other_link = return_link[1]
  943. if re.search(r'^\/', main_link):
  944. main_link = re.sub(r'^\/', title + '/', main_link)
  945. elif re.search(r'\.\.\/\/', main_link):
  946. main_link = re.sub(r'\.\.\/\/', '/', main_link)
  947. elif re.search(r'^\.\.\/', main_link):
  948. main_link = re.sub(r'^\.\.\/', re.sub(r'(?P<in>.+)\/.*$', '\g<in>', title), main_link)
  949. if main_link != title or main_link != '':
  950. backlink += [[title, main_link, '']]
  951. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  952. if not curs.fetchall():
  953. backlink += [[title, main_link, 'no']]
  954. data = link_re.sub('' + \
  955. '<a class="' + include_name + 'link_finder" ' + \
  956. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  957. 'title="" ' + \
  958. 'href="">' + see_link + '</a>' + \
  959. '',
  960. data,
  961. 1
  962. )
  963. plus_data += "" + \
  964. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  965. ('/w/' + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  966. "\n" + \
  967. ""
  968. plus_data += "" + \
  969. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].title = '" + \
  970. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  971. "\n" + \
  972. ""
  973. if inter_same == 1:
  974. plus_data += "" + \
  975. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  976. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  977. "\n" + \
  978. ""
  979. else:
  980. if re.search(r'^#', other_link):
  981. data = link_re.sub(
  982. '<a title="' + other_link + '" href="' + other_link + '">' + other_link + '</a>',
  983. data,
  984. 1
  985. )
  986. else:
  987. data = link_re.sub('<b>' + see_link + '</b>', data, 1)
  988. else:
  989. break
  990. br_re = re.compile(r'\[br\]', re.I)
  991. data = br_re.sub('<br>', data)
  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]