namumark.py 53 KB

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