namumark.py 53 KB

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