namumark.py 53 KB

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