namumark.py 55 KB

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