namumark.py 55 KB

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