namumark.py 55 KB

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