namumark.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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 = main_link.replace('\\#', '%23')
  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_1 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 += ['div_dd']
  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_1 style="">\n',
  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] == 'div_dd':
  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) + '")) {\n' + \
  411. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(nowiki_data[0]) + '";\n' + \
  412. '}\n' + \
  413. ''
  414. data = re.sub(
  415. r'<code>((?:(?:(?!<\/code>).)*\n*)*)<\/code>',
  416. '<span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span>',
  417. data,
  418. 1
  419. )
  420. else:
  421. break
  422. while 1:
  423. syntax_data = re.search(
  424. r'<code class="([^"\'>]+)">((?:(?:(?:(?!<\/code>|<span ).)*)\n*)+)<\/code>',
  425. data
  426. )
  427. if syntax_data:
  428. syntax_data = syntax_data.groups()
  429. nowiki_num += 1
  430. end_data[include_name + 'nowiki_' + str(nowiki_num)] = syntax_data[1]
  431. plus_data += '' + \
  432. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) {\n' + \
  433. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(syntax_data[1]) + '";\n' + \
  434. '}\n' + \
  435. ''
  436. data = re.sub(
  437. r'<code class="([^"\'>]+)">((?:(?:(?:(?!<\/code>|<span ).)*)\n*)+)<\/code>',
  438. '<code class="' + syntax_data[0] + '"><span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span></code>',
  439. data,
  440. 1
  441. )
  442. else:
  443. break
  444. return data
  445. def namumark(conn, data, title, include_num):
  446. curs = conn.cursor()
  447. global plus_data
  448. global end_data
  449. global nowiki_num
  450. global include_name
  451. nowiki_num = 0
  452. data = '\n' + data + '\n'
  453. include_name = include_num + '_' if include_num else ''
  454. plus_data = ''
  455. backlink = []
  456. end_data = {}
  457. data = re.sub(r'@([^=@]+)=(?P<in>[^=@]+)@', '\g<in>', data)
  458. data = re.sub(r'<math>(?P<in>(?:(?!<\/math>).)+)<\/math>', '[math(\g<in>)]', data)
  459. data = html.escape(data)
  460. data = data.replace('\r\n', '\n')
  461. math_re = re.compile(r'\[math\(((?:(?!\)\]).)+)\)\]', re.I)
  462. while 1:
  463. math = math_re.search(data)
  464. if math:
  465. math = math.group(1)
  466. math = math.replace('{', '<math_mid_1>')
  467. math = math.replace('}', '<math_mid_2>')
  468. math = math.replace('\\', '<math_slash>')
  469. data = math_re.sub('<math>' + math + '</math>', data, 1)
  470. else:
  471. break
  472. data = data.replace('\\{', '<break_middle>')
  473. data = middle_parser(data)
  474. data = data.replace('<break_middle>', '\\{')
  475. first = 0
  476. math_re = re.compile(r'<math>((?:(?!<\/math>).)+)<\/math>', re.I)
  477. while 1:
  478. math = math_re.search(data)
  479. if math:
  480. math = math.group(1)
  481. math = math.replace('<math_mid_1>', '{')
  482. math = math.replace('<math_mid_2>', '}')
  483. math = math.replace('<math_slash>', '\\')
  484. first += 1
  485. data = math_re.sub('<span id="math_' + str(first) + '"></span>', data, 1)
  486. plus_data += '' + \
  487. 'try {\n' + \
  488. 'katex.render("' + nowiki_js(html.unescape(math)) + '", document.getElementById(\"' + include_name + 'math_' + str(first) + '\"));\n' + \
  489. '} catch {\n' + \
  490. 'document.getElementById(\"' + include_name + 'math_' + str(first) + '\").innerHTML = "<span style=\'color: red;\'>' + nowiki_js(math) + '</span>";\n' + \
  491. '}\n' + \
  492. ''
  493. else:
  494. break
  495. num = 0
  496. while 1:
  497. one_nowiki = re.search(r'(?:\\)(.)', data)
  498. if one_nowiki:
  499. one_nowiki = one_nowiki.groups()
  500. nowiki_num += 1
  501. end_data[include_name + 'nowiki_' + str(nowiki_num)] = one_nowiki[0]
  502. plus_data += '' + \
  503. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) {\n' + \
  504. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(one_nowiki[0]) + '";\n' + \
  505. '}\n' + \
  506. ''
  507. data = re.sub(r'(?:\\)(.)', '<span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span>', data, 1)
  508. else:
  509. break
  510. include_re = re.compile(r'\[include\(((?:(?!\)\]).)+)\)\]', re.I)
  511. i = 0
  512. while 1:
  513. i += 1
  514. include = include_re.search(data)
  515. if include:
  516. include = include.group(1)
  517. include_data = re.search(r'^((?:(?!,).)+)', include)
  518. if include_data:
  519. include_data = include_data.group(1)
  520. else:
  521. include_data = 'Test'
  522. include_link = include_data
  523. backlink += [[title, include_link, 'include']]
  524. data = include_re.sub('' + \
  525. '<a id="' + include_name + 'include_link" class="include_' + str(i) + '" href="/w/' + tool.url_pas(include_link) + '">(' + include_link + ')</a>' + \
  526. '<div id="' + include_name + 'include_' + str(i) + '"></div>' + \
  527. '', data, 1)
  528. include_plus_data = []
  529. while 1:
  530. include_plus = re.search(r', ?((?:(?!=).)+)=((?:(?!,).)+)', include)
  531. if include_plus:
  532. include_plus = include_plus.groups()
  533. include_data_set = include_plus[1]
  534. find_data = re.findall(r'<span id="(nowiki_[0-9]+)">', include_data_set)
  535. for j in find_data:
  536. include_data_set = include_data_set.replace('<span id="' + j + '"></span>', end_data[j])
  537. include_plus_data += [[include_plus[0], include_data_set]]
  538. include = re.sub(r', ?((?:(?!=).)+)=((?:(?!,).)+)', '', include, 1)
  539. else:
  540. break
  541. plus_data += 'load_include("' + include_link + '", "' + include_name + 'include_' + str(i) + '", ' + str(include_plus_data) + ');\n'
  542. else:
  543. break
  544. data = data.replace('&amp;', '&')
  545. data = re.sub(r'\n##[^\n]+', '', data)
  546. div_1_re = re.compile(r'<div_1 ([^>]+)>((?:(?!<div_1|<\/div_1>).|\n*)+)<\/div_1>')
  547. while 1:
  548. wiki_table_data = div_1_re.search(data)
  549. if wiki_table_data:
  550. wiki_table_data = wiki_table_data.groups()
  551. if re.search(r'\|\|', wiki_table_data[1]):
  552. end_parser = table_start('\n' + wiki_table_data[1] + '\n')
  553. end_parser = re.sub(r'^\n', '', end_parser)
  554. end_parser = re.sub(r'\n$', '', end_parser)
  555. else:
  556. end_parser = wiki_table_data[1]
  557. data = div_1_re.sub('<div id="wiki_div" ' + wiki_table_data[0] + '>' + end_parser + '</div_2>', data, 1)
  558. else:
  559. break
  560. data = data.replace('</div_2>', '</div>')
  561. data = data.replace('</td>', '</td_1>')
  562. data += '\n'
  563. data = data.replace('\\', '&#92;')
  564. redirect_re = re.compile(r'\n#(?:redirect|넘겨주기) ([^\n]+)', re.I)
  565. redirect = redirect_re.search(data)
  566. if redirect:
  567. redirect = redirect.group(1)
  568. return_link = link_fix(redirect)
  569. main_link = html.unescape(return_link[0])
  570. other_link = return_link[1]
  571. backlink += [[title, main_link, 'redirect']]
  572. plus_data += '' + \
  573. 'var get_link = window.location.search.match(/(?:\?|&)from=([^&]+)/);\n' + \
  574. 'if(!get_link) {\n' + \
  575. 'window.location.href = "/w/' + tool.url_pas(main_link) + other_link + '?from=' + tool.url_pas(title) + '";\n' + \
  576. '}\n' + \
  577. ''
  578. data = redirect_re.sub('', data, 1)
  579. no_toc_re = re.compile(r'\[(?:목차|toc)\((?:no)\)\]\n', re.I)
  580. toc_re = re.compile(r'\[(?:목차|toc)\]', re.I)
  581. if not no_toc_re.search(data):
  582. if not toc_re.search(data):
  583. data = re.sub(r'(?P<in>\n(={1,6})(#)? ?((?:(?!(?: #=| =)).)+) ?#?(?:=+)\n)', '\n[toc]\g<in>', data, 1)
  584. else:
  585. data = no_toc_re.sub('', data)
  586. data = '<div class="all_in_data" id="in_data_0">' + data
  587. toc_stack = [0, 0, 0, 0, 0, 0]
  588. toc_num = 0
  589. toc_data = '' + \
  590. '<div id="toc">' + \
  591. '<span id="toc_title">TOC</span>' + \
  592. '<br>' + \
  593. '<br>' + \
  594. ''
  595. edit_num = 0
  596. toc_head_re = re.compile(r'\n(={1,6})(#)? ?((?:(?!(?: #=| =)).)+) ?#?(?:=+)\n')
  597. while 1:
  598. toc = toc_head_re.search(data)
  599. if toc:
  600. toc = toc.groups()
  601. edit_num += 1
  602. toc_len_num = len(toc[0])
  603. toc_len_str = str(toc_len_num)
  604. toc_len_num -= 1
  605. toc_stack[toc_len_num] += 1
  606. for i in range(toc_len_num + 1, 6):
  607. toc_stack[i] = 0
  608. edit_num_str = str(edit_num)
  609. toc_level_str = '.'.join([str(i) for i in toc_stack if i != 0])
  610. toc_fol = '+' if toc[1] else '-'
  611. data = toc_head_re.sub(
  612. '\n' + \
  613. '</div>'
  614. '<h' + toc_len_str + ' id="s-' + toc_level_str + '">' + \
  615. '<a href="#toc">' + toc_level_str + '.</a> ' + toc[2] + ' ' + \
  616. '<span style="font-size: 12px">' + \
  617. '<a href="/edit/' + tool.url_pas(title) + '?section=' + edit_num_str + '">(Edit)</a>' + \
  618. ' ' + \
  619. '<a href="javascript:void(0);" onclick="do_open_folding(\'in_data_' + edit_num_str + '\', this);">' + \
  620. '(' + toc_fol + ')' + \
  621. '</a>' + \
  622. '</span>' + \
  623. '</h' + toc_len_str + '>' + \
  624. '<div class="all_in_data"' + (' style="display: none;"' if toc_fol == '+' else '') + ' id="in_data_' + edit_num_str + '">' + \
  625. '\n',
  626. data,
  627. 1
  628. )
  629. toc_main_data = toc[2]
  630. toc_main_data = re.sub(r'\[\*((?:(?! |\]).)*)(?: ((?:(?!(\[\*(?:(?:(?!\]).)+)\]|\])).)+))?\]', '', toc_main_data)
  631. toc_main_data = re.sub(r'<span id="math_[0-9]"><\/span>', '(Math)', toc_main_data)
  632. toc_data += '' + \
  633. '<span style="margin-left:' + str(len(re.findall(r'\.', toc_level_str)) * 10) + 'px;">' + \
  634. '<a href="#s-' + toc_level_str + '">' + toc_level_str + '.</a> ' + toc_main_data + \
  635. '</span>' + \
  636. '<br>' + \
  637. ''
  638. else:
  639. break
  640. toc_data += '</div>'
  641. data = toc_re.sub(toc_data, data)
  642. macro_re = re.compile(r'\[([^[(]+)\(((?:(?!\[|\)]).)+)\)\]')
  643. macro_data = macro_re.findall(data)
  644. for i in macro_data:
  645. macro_name = i[0].lower()
  646. if macro_name == 'youtube' or macro_name == 'kakaotv' or macro_name == 'nicovideo':
  647. width = re.search(r', ?width=((?:(?!,).)+)', i[1])
  648. if width:
  649. video_width = width.group(1)
  650. if re.search(r'^[0-9]+$', video_width):
  651. video_width += 'px'
  652. else:
  653. video_width = '560px'
  654. height = re.search(r', ?height=((?:(?!,).)+)', i[1])
  655. if height:
  656. video_height = height.group(1)
  657. if re.search(r'^[0-9]+$', video_height):
  658. video_height += 'px'
  659. else:
  660. video_height = '315px'
  661. code = re.search(r'^((?:(?!,).)+)', i[1])
  662. if code:
  663. video_code = code.group(1)
  664. else:
  665. video_code = ''
  666. video_start = ''
  667. if macro_name == 'youtube':
  668. start = re.search(r', ?(start=(?:(?!,).)+)', i[1])
  669. if start:
  670. video_start = '?' + start.group(1)
  671. video_code = re.sub(r'^https:\/\/www\.youtube\.com\/watch\?v=', '', video_code)
  672. video_code = re.sub(r'^https:\/\/youtu\.be\/', '', video_code)
  673. video_src = 'https://www.youtube.com/embed/' + video_code
  674. elif macro_name == 'kakaotv':
  675. video_code = re.sub(r'^https:\/\/tv\.kakao\.com\/channel\/9262\/cliplink\/', '', video_code)
  676. video_code = re.sub(r'^http:\/\/tv\.kakao\.com\/v\/', '', video_code)
  677. video_src = 'https://tv.kakao.com/embed/player/cliplink/' + video_code +'?service=kakao_tv'
  678. else:
  679. video_src = 'https://embed.nicovideo.jp/watch/' + video_code
  680. data = macro_re.sub(
  681. '<iframe style="width: ' + video_width + '; height: ' + video_height + ';" src="' + video_src + video_start + '" frameborder="0" allowfullscreen></iframe>',
  682. data,
  683. 1
  684. )
  685. elif macro_name == 'anchor':
  686. data = macro_re.sub('<span id="' + i[1] + '"></span>', data, 1)
  687. elif macro_name == 'ruby':
  688. ruby_code = re.search(r'^([^,]+)', i[1])
  689. if ruby_code:
  690. ruby_code = ruby_code.group(1)
  691. else:
  692. ruby_code = 'Test'
  693. ruby_top = re.search(r'ruby=([^,]+)', i[1], flags = re.I)
  694. if ruby_top:
  695. ruby_top = ruby_top.group(1)
  696. else:
  697. ruby_top = 'Test'
  698. ruby_color = re.search(r'color=([^,]+)', i[1], flags = re.I)
  699. if ruby_color:
  700. ruby_color = 'color: ' + ruby_color.group(1) + ';'
  701. else:
  702. ruby_color = ''
  703. ruby_data = '' + \
  704. '<ruby>' + \
  705. ruby_code \
  706. + '<rp>(</rp>' + \
  707. '<rt style="' + ruby_color + '">' + ruby_top + '</rt>' + \
  708. '<rp>)</rp>' + \
  709. '</ruby>' + \
  710. ''
  711. data = macro_re.sub(ruby_data, data, 1)
  712. elif macro_name == 'age' or macro_name == 'dday':
  713. try:
  714. old = datetime.datetime.strptime(time, '%Y-%m-%d')
  715. will = datetime.datetime.strptime(i[1], '%Y-%m-%d')
  716. e_data = old - will
  717. if macro_name == 'age':
  718. data = macro_re.sub(str(int(e_data.days / 365)), data, 1)
  719. else:
  720. data = macro_re.sub((str(e_data.days) if re.search(r'^-', str(e_data.days)) else ('+' + str(e_data.days))), data, 1)
  721. except:
  722. data = macro_re.sub('age-dday-error', data, 1)
  723. else:
  724. data = macro_re.sub('<macro_start>' + i[0] + '<macro_middle>' + i[1] + '<macro_end>', data, 1)
  725. data = data.replace('<macro_start>', '[')
  726. data = data.replace('<macro_middle>', '(')
  727. data = data.replace('<macro_end>', ')]')
  728. blockquote_re = re.compile(r'(\n(?:&gt; ?(?:[^\n]+)\n)+)')
  729. while 1:
  730. blockquote = blockquote_re.search(data)
  731. if blockquote:
  732. blockquote = re.sub(r'^\n&gt; ?', '', blockquote.group(1))
  733. blockquote = re.sub(r'\n&gt; ?', '\n', blockquote)
  734. blockquote = re.sub(r'\n$', '', blockquote)
  735. data = blockquote_re.sub('\n<blockquote>' + blockquote + '</blockquote>\n', data, 1)
  736. else:
  737. break
  738. hr_re = re.compile(r'\n-{4,9}\n')
  739. while 1:
  740. if hr_re.search(data):
  741. data = hr_re.sub('\n<hr>\n', data, 1)
  742. else:
  743. break
  744. data = re.sub(r'(?P<in>\n +\* ?(?:(?:(?!\|\|).)+))\|\|', '\g<in>\n ||', data)
  745. 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)
  746. li_re = re.compile(r'(\n(?:(?: +)\* (?:[^\n]+)\n)+)')
  747. while 1:
  748. li_data = li_re.search(data)
  749. if li_data:
  750. li_data = li_data.group(1)
  751. li_end_data = ''
  752. sub_li = re.findall(r'( +)\* ([^\n]+)\n', li_data)
  753. for i in sub_li:
  754. li_end_data += '<li style="margin-left: ' + str(20 if len(i[0]) == 0 else (len(i[0]) * 20)) + 'px;">' + i[1] + '</li>'
  755. data = li_re.sub('\n<ul>' + li_end_data + '</ul>\n', data, 1)
  756. else:
  757. break
  758. data = re.sub(r'<\/ul>\n \|\|', '</ul>||', data)
  759. data = re.sub(r'\|\|</blockquote>', '</blockquote>||', data)
  760. while 1:
  761. indent = re.search(r'\n( +)', data)
  762. if indent:
  763. data = re.sub(r'\n( +)', '\n' + ('<span style="margin-left: 20px;"></span>' * len(indent.group(1))), data, 1)
  764. else:
  765. break
  766. data = table_start(data)
  767. category = ''
  768. link_re = re.compile('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]')
  769. category_re = re.compile(r'^(?:category|분류):', re.I)
  770. e_link_id = 0
  771. while 1:
  772. link = link_re.search(data)
  773. if link:
  774. link = link.group(1)
  775. str_e_link_id = str(e_link_id)
  776. e_link_id += 1
  777. link_split = re.search(r'((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  778. if link_split:
  779. link_split = link_split.groups()
  780. main_link = link_split[0]
  781. see_link = link_split[1]
  782. inter_same = 0
  783. else:
  784. main_link = link
  785. see_link = link
  786. inter_same = 1
  787. if re.search(r'^((?:file|파일)|(?:out|외부)):', main_link):
  788. file_style = ''
  789. file_width = re.search(r'width=((?:(?!&).)+)', see_link)
  790. if file_width:
  791. file_width = file_width.group(1)
  792. if re.search(r'px$', file_width):
  793. file_style += 'width: ' + file_width + ';'
  794. else:
  795. file_style += 'width: ' + file_width + 'px;'
  796. file_height = re.search(r'height=((?:(?!&).)+)', see_link)
  797. if file_height:
  798. file_height = file_height.group(1)
  799. if re.search(r'px$', file_height):
  800. file_style += 'height: ' + file_height + ';'
  801. else:
  802. file_style += 'height: ' + file_height + 'px;'
  803. file_align = re.search(r'align=((?:(?!&).)+)', see_link)
  804. if file_align:
  805. file_align = file_align.group(1)
  806. if file_align == 'center':
  807. file_align = 'display: block; text-align: center;'
  808. else:
  809. file_align = 'float: ' + file_align + ';'
  810. else:
  811. file_align = ''
  812. file_color = re.search(r'bgcolor=((?:(?!&).)+)', see_link)
  813. if file_color:
  814. file_color = 'background: ' + file_color.group(1) + '; display: inline-block;'
  815. else:
  816. file_color = ''
  817. file_alt = re.search(r'alt=((?:(?!&).)+)', see_link)
  818. if file_alt:
  819. file_alt = file_alt.group(1)
  820. else:
  821. file_alt = ''
  822. if re.search(r'^(?:out|외부):', main_link):
  823. file_src = re.sub(r'^(?:out|외부):', '', main_link)
  824. file_alt = main_link if file_alt == '' else file_alt
  825. exist = 'Yes'
  826. else:
  827. file_data = re.search(r'^(?:file|파일):((?:(?!\.).)+)\.(.+)$', main_link)
  828. if file_data:
  829. file_data = file_data.groups()
  830. file_name = file_data[0]
  831. file_end = file_data[1]
  832. if main_link != title:
  833. backlink += [[title, main_link, 'file']]
  834. else:
  835. file_name = 'TEST'
  836. file_end = 'jpg'
  837. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  838. file_alt = ('file:' + file_name + '.' + file_end) if file_alt == '' else file_alt
  839. exist = None
  840. data = link_re.sub(
  841. '<span style="' + file_align + '">' + \
  842. '<span style="' + file_color + '" ' + \
  843. 'class="' + include_name + 'file_finder" ' + \
  844. 'under_style="' + file_style + '" ' + \
  845. 'under_alt="' + file_alt + '" ' + \
  846. 'under_src="' + file_src + '" ' + \
  847. 'under_href="' + ("out_link" if exist else '/upload?name=' + tool.url_pas(file_name)) + '">' + \
  848. '</span>' + \
  849. '</span>',
  850. data,
  851. 1
  852. )
  853. elif category_re.search(main_link):
  854. if category == '':
  855. category += '<div id="cate_all"><div id="cate">Category : '
  856. main_link = category_re.sub('category:', main_link)
  857. link_id = ''
  858. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  859. if re.search(r'#blur', main_link):
  860. link_id = ' hidden_link'
  861. main_link = main_link.replace('#blur', '')
  862. see_link = see_link.replace('#blur', '')
  863. backlink += [[title, main_link, 'cat']]
  864. category += '' + \
  865. '<a class="' + include_name + 'link_finder' + link_id + '" ' + \
  866. 'href="/w/' + tool.url_pas(main_link) + '">' + \
  867. category_re.sub('', see_link) + \
  868. '</a> | ' + \
  869. ''
  870. data = link_re.sub('', data, 1)
  871. elif re.search(r'^inter:((?:(?!:).)+):', main_link):
  872. inter_data = re.search(r'^inter:((?:(?!:).)+):((?:(?!\]\]).)+)', main_link)
  873. inter_data = inter_data.groups()
  874. curs.execute(tool.db_change('select link, icon from inter where title = ?'), [inter_data[0]])
  875. inter = curs.fetchall()
  876. if inter:
  877. return_link = link_fix(inter_data[1], 1)
  878. main_link = html.unescape(return_link[0])
  879. other_link = return_link[1]
  880. inter_view = inter[0][1] if inter[0][1] != '' else (inter_data[0] + ':')
  881. data = link_re.sub('' + \
  882. '<a id="inside" ' + \
  883. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  884. 'href="">' + inter_view + see_link + '</a>' + \
  885. '',
  886. data,
  887. 1
  888. )
  889. plus_data += "" + \
  890. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  891. (inter[0][0] + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  892. "\n" + \
  893. ""
  894. if inter_same == 1:
  895. plus_data += "" + \
  896. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  897. (inter_view + main_link + other_link).replace('\'', '\\\'') + "';" + \
  898. "\n" + \
  899. ""
  900. else:
  901. data = link_re.sub('', data, 1)
  902. elif re.search(r'^(\/(?:.+))$', main_link):
  903. under_title = re.search(r'^(\/(?:.+))$', main_link)
  904. under_title = under_title.group(1)
  905. if see_link != main_link:
  906. data = link_re.sub('[[' + title + under_title + '|' + see_link + ']]', data, 1)
  907. else:
  908. data = link_re.sub('[[' + title + under_title + ']]', data, 1)
  909. elif re.search(r'^http(s)?:\/\/', main_link):
  910. data = link_re.sub('' + \
  911. '<a id="out_link" ' + \
  912. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  913. 'rel="nofollow" ' + \
  914. 'href="">' + see_link + '</a>' + \
  915. '',
  916. data,
  917. 1
  918. )
  919. plus_data += "" + \
  920. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  921. main_link.replace('\'', '\\\'') + "';" + \
  922. "\n" + \
  923. ""
  924. if inter_same == 1:
  925. plus_data += "" + \
  926. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  927. main_link.replace('\'', '\\\'') + "';" + \
  928. "\n" + \
  929. ""
  930. else:
  931. return_link = link_fix(main_link)
  932. main_link = html.unescape(return_link[0])
  933. other_link = return_link[1]
  934. if re.search(r'^\/', main_link):
  935. main_link = re.sub(r'^\/', title + '/', main_link)
  936. elif re.search(r'\.\.\/\/', main_link):
  937. main_link = re.sub(r'\.\.\/\/', '/', main_link)
  938. elif re.search(r'^\.\.\/', main_link):
  939. main_link = re.sub(r'^\.\.\/', re.sub(r'(?P<in>.+)\/.*$', '\g<in>', title), main_link)
  940. if main_link != title and main_link != '':
  941. backlink += [[title, main_link, '']]
  942. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  943. if not curs.fetchall():
  944. backlink += [[title, main_link, 'no']]
  945. data = link_re.sub('' + \
  946. '<a class="' + include_name + 'link_finder" ' + \
  947. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  948. 'title="" ' + \
  949. 'href="">' + see_link + '</a>' + \
  950. '',
  951. data,
  952. 1
  953. )
  954. plus_data += "" + \
  955. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  956. ('/w/' + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  957. "\n" + \
  958. ""
  959. plus_data += "" + \
  960. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].title = '" + \
  961. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  962. "\n" + \
  963. ""
  964. if inter_same == 1:
  965. plus_data += "" + \
  966. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  967. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  968. "\n" + \
  969. ""
  970. else:
  971. if re.search(r'^#', other_link):
  972. data = link_re.sub(
  973. '<a title="' + other_link + '" href="' + other_link + '">' + (other_link if see_link == other_link else see_link) + '</a>',
  974. data,
  975. 1
  976. )
  977. else:
  978. data = link_re.sub('<b>' + see_link + '</b>', data, 1)
  979. else:
  980. break
  981. if re.search(r'\[pagecount\]', data, flags = re.I):
  982. plus_data += 'page_count();\n'
  983. data = re.sub(r'\[pagecount\]', '<span class="all_page_count"></span>', data, flags = re.I)
  984. data = re.sub(r'\[date\]', tool.get_time().split()[0], data, flags = re.I)
  985. data = re.sub(r'\[clearfix\]', '<div style="clear:both"></div>', data, flags = re.I)
  986. data = re.sub(r'\[br\]', '<br>', data, flags = re.I)
  987. # 각주 기능
  988. footnote_number = 0
  989. footnote_all = []
  990. footnote_dict = {}
  991. footnote_re = {}
  992. footdata_all = '<ul id="footnote_data">'
  993. re_footnote = re.compile(r'(?:\[\*((?:(?! |\]).)*)(?: ((?:(?!(?:\[\*|\])).)+))?\]|(\[(?:각주|footnote)\]))')
  994. while 1:
  995. footnote = re_footnote.search(data)
  996. if footnote:
  997. footnote_data = footnote.groups()
  998. if footnote_data[2]:
  999. footnote_all.sort()
  1000. for footdata in footnote_all:
  1001. if footdata[2] == 0:
  1002. footdata_in = ''
  1003. else:
  1004. footdata_in = footdata[2]
  1005. footdata_all += '' + \
  1006. '<li>' + \
  1007. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1008. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1009. '(' + footdata[1] + ')' + \
  1010. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1011. '</li>' + \
  1012. ''
  1013. data = re_footnote.sub(footdata_all + '</ul>', data, 1)
  1014. footnote_all = []
  1015. footdata_all = '<ul id="footnote_data">'
  1016. else:
  1017. footnote = footnote_data[1]
  1018. footnote_name = footnote_data[0]
  1019. if footnote_name and not footnote:
  1020. if footnote_name in footnote_dict:
  1021. footnote_re[footnote_name] += 1
  1022. foot_plus_num = str(footnote_re[footnote_name])
  1023. footshort = footnote_dict[footnote_name] + '.' + foot_plus_num
  1024. footnote_all += [[float(footshort), footshort, 0]]
  1025. data = re_footnote.sub('' + \
  1026. '<sup>' + \
  1027. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + footshort + '\', 0);" ' + \
  1028. 'id="' + include_name + 'rfn-' + footshort + '">' + \
  1029. '(' + footnote_name + ')' + \
  1030. '</a>' + \
  1031. '</sup><span id="' + include_name + 'dfn-' + footshort + '"></span>' + \
  1032. '', data, 1)
  1033. else:
  1034. data = re_footnote.sub('<sup><a href="javascript:void(0);">(' + footnote_name + ')</a></sup>', data, 1)
  1035. else:
  1036. footnote_number += 1
  1037. if not footnote_name:
  1038. footnote_name = str(footnote_number)
  1039. footnote_dict.update({ footnote_name : str(footnote_number) })
  1040. if not footnote_name in footnote_re:
  1041. footnote_re.update({ footnote_name : 0 })
  1042. else:
  1043. footnote_re[footnote_name] += 1
  1044. footnote_all += [[footnote_number, footnote_name, footnote]]
  1045. data = re_footnote.sub('' + \
  1046. '<sup>' + \
  1047. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footnote_number) + '\', 0);" ' + \
  1048. 'id="' + include_name + 'rfn-' + str(footnote_number) + '">' + \
  1049. '(' + footnote_name + ')' + \
  1050. '</a>' + \
  1051. '</sup><span id="' + include_name + 'dfn-' + str(footnote_number) + '"></span>' + \
  1052. '', data, 1)
  1053. else:
  1054. break
  1055. data = re.sub(r'\n+$', '', data)
  1056. footnote_all.sort()
  1057. for footdata in footnote_all:
  1058. if footdata[2] == 0:
  1059. footdata_in = ''
  1060. else:
  1061. footdata_in = str(footdata[2])
  1062. footdata_all += '' + \
  1063. '<li>' + \
  1064. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1065. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1066. '(' + str(footdata[1]) + ')' + \
  1067. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1068. '</li>' + \
  1069. ''
  1070. footdata_all += '</ul>'
  1071. footdata_all = '' if footdata_all == '<ul id="footnote_data"></ul>' else footdata_all
  1072. footdata_all = '</div>' + footdata_all
  1073. data += footdata_all
  1074. # 기본 꾸미기 문법
  1075. data = re.sub(r'&#x27;&#x27;&#x27;(?P<in>((?!&#x27;&#x27;&#x27;).)+)&#x27;&#x27;&#x27;', '<b>\g<in></b>', data)
  1076. data = re.sub(r'&#x27;&#x27;(?P<in>((?!&#x27;&#x27;).)+)&#x27;&#x27;', '<i>\g<in></i>', data)
  1077. data = re.sub(r'~~(?P<in>(?:(?!~~).)+)~~', '<s>\g<in></s>', data)
  1078. data = re.sub(r'--(?P<in>(?:(?!--).)+)--', '<s>\g<in></s>', data)
  1079. data = re.sub(r'__(?P<in>(?:(?!__).)+)__', '<u>\g<in></u>', data)
  1080. data = re.sub(r'\^\^(?P<in>(?:(?!\^\^).)+)\^\^', '<sup>\g<in></sup>', data)
  1081. data = re.sub(r',,(?P<in>(?:(?!,,).)+),,', '<sub>\g<in></sub>', data)
  1082. # 최종 처리
  1083. data += (re.sub(r' \| $', '', category) + '</div></div>') if category != '' else ''
  1084. data = data.replace('<no_table>', '||')
  1085. data = data.replace('</td_1>', '</td>')
  1086. data = re.sub(r'<\/ul>\n?', '</ul>', data)
  1087. data = re.sub(r'<\/pre>\n?', '</pre>', data)
  1088. data = re.sub(r'(?P<in><div class="all_in_data"(?:(?:(?!id=).)+)? id="in_data_([^"]+)">)(\n)+', '\g<in>', data)
  1089. data = data.replace('\n\n<ul>', '\n<ul>')
  1090. data = data.replace('</ul>\n\n', '</ul>')
  1091. data = re.sub(r'^(\n)+', '', data)
  1092. data = re.sub(r'(\n)+<ul id="footnote_data">', '<ul id="footnote_data">', data)
  1093. data = re.sub(r'(?P<in><td(((?!>).)*)>)\n', '\g<in>', data)
  1094. data = re.sub(r'(\n)?<hr>(\n)?', '<hr>', data)
  1095. data = data.replace('</ul>\n\n<ul>', '</ul>\n<ul>')
  1096. data = data.replace('</ul>\n<ul>', '</ul><ul>')
  1097. data = data.replace('\n</ul>', '</ul>')
  1098. data = data.replace('\n', '<br>')
  1099. # 파일, 링크, HTML 작동 JS
  1100. plus_data += '' + \
  1101. 'get_link_state("' + include_name + '");\n' + \
  1102. 'get_file_state("' + include_name + '");\n' + \
  1103. ''
  1104. plus_data = 'render_html("' + include_name + 'render_contect");\n' + plus_data
  1105. return [data, plus_data, backlink]