namumark.py 53 KB

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