namumark.py 56 KB

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