namumark.py 53 KB

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