namumark.py 52 KB

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