2
0

mid_pas.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import re
  2. from urllib import parse
  3. def mid_pas(data, fol_num, include, in_c, toc_y):
  4. syntax = 0
  5. folding_test = 0
  6. com = re.compile("{{{((?!(?:(?:\+|-)(?:[^ ]+))|(?:#|@)(?:(?:[0-9a-f-A-F]{3}){1,2}|(?:\w+))|(?:#!(?:html|wiki|noin|folding|syntax)))(?:(?!{|}).)+)}}}", re.DOTALL)
  7. while 1:
  8. m = com.search(data)
  9. if m:
  10. com4 = re.compile('#base64#((?:(?!#\/base64#).)+)#\/base64#', re.DOTALL)
  11. test = com4.search(m.groups()[0])
  12. if test:
  13. test2 = com4.sub('#mid2#' + parse.unquote(test.groups()[0]).replace('_', '_').replace('‐', '-') + '#/mid2#', m.groups()[0])
  14. else:
  15. test2 = m.groups()[0]
  16. data = com.sub('#base64#' + parse.quote(test2).replace('_', '_').replace('-', '‐').replace('%3C', '<').replace('%3E', '>') + '#/base64#', data, 1)
  17. else:
  18. break
  19. data = re.sub("{#base64#%7B%7B", "{{{#base64#", data)
  20. p = re.compile('{{{((?!#mid#)(?:(?:(?:\+|-)(?:[^ ]+))|(?:#|@)(?:(?:[0-9a-f-A-F]{3}){1,2}|(?:\w+))|(?:#!(?:html|wiki|noin|folding|syntax)))(?:(?!{{{|}}}).)+)}}}', re.DOTALL)
  21. while 1:
  22. m = p.search(data)
  23. if m:
  24. data = p.sub('#mid#' + m.groups()[0] + '#/mid#', data, 1)
  25. else:
  26. break
  27. com = re.compile("{{{((?:(?!{{{|}}}).)*)}}}", re.DOTALL)
  28. while 1:
  29. m = com.search(data)
  30. if m:
  31. com4 = re.compile('#base64#((?:(?!#\/base64#).)+)#\/base64#', re.DOTALL)
  32. test = com4.search(m.groups()[0])
  33. if test:
  34. test2 = com4.sub('#mid2#' + parse.unquote(test.groups()[0]).replace('_', '_').replace('‐', '-') + '#/mid2#', m.groups()[0])
  35. else:
  36. test2 = m.groups()[0]
  37. data = com.sub('#base64#' + parse.quote(test2).replace('_', '_').replace('-', '‐').replace('%3C', '<').replace('%3E', '>') + '#/base64#', data, 1)
  38. else:
  39. break
  40. data = re.sub("{#base64#%7B%7B", "{{{#base64#", data)
  41. com3 = re.compile('#mid#((?:(?!#\/mid#).)+)#\/mid#', re.DOTALL)
  42. m = com3.search(data)
  43. while 1:
  44. m = com3.search(data)
  45. if m:
  46. data = com3.sub('{{{' + m.groups()[0] + '}}}', data, 1)
  47. else:
  48. break
  49. while 1:
  50. is_it = com.search(data)
  51. if is_it:
  52. it_d = is_it.groups()[0]
  53. big_a = re.compile("^\+([1-5]) (.*)$", re.DOTALL)
  54. big = big_a.search(it_d)
  55. small_a = re.compile("^\-([1-5]) (.*)$", re.DOTALL)
  56. small = small_a.search(it_d)
  57. color_b = re.compile("^(#(?:[0-9a-f-A-F]{3}){1,2}) (.*)$", re.DOTALL)
  58. color_2 = color_b.search(it_d)
  59. color_c = re.compile("^#(\w+) (.*)$", re.DOTALL)
  60. color_3 = color_c.search(it_d)
  61. back_a = re.compile("^@((?:[0-9a-f-A-F]{3}){1,2}) (.*)$", re.DOTALL)
  62. back = back_a.search(it_d)
  63. back_c = re.compile("^@(\w+) (.*)$", re.DOTALL)
  64. back_3 = back_c.search(it_d)
  65. include_out_a = re.compile("^#!noin ?(.*)$", re.DOTALL)
  66. include_out = include_out_a.search(it_d)
  67. div_a = re.compile("^#!wiki style=(?:"|')((?:(?!"|').)*)(?:"|')\r\n(.*)$", re.DOTALL)
  68. div = div_a.search(it_d)
  69. html_a = re.compile("^#!html ?(.*)$", re.DOTALL)
  70. html_d = html_a.search(it_d)
  71. fol_a = re.compile("^#!folding ((?:(?!\n).)*)\n(.*)$", re.DOTALL)
  72. fol = fol_a.search(it_d)
  73. syn_a = re.compile("^#!syntax ((?:(?!\n).)*)\n(.*)$", re.DOTALL)
  74. syn = syn_a.search(it_d)
  75. if big:
  76. big_d = big.groups()
  77. data = com.sub('<span style="font-size: ' + str(int(big_d[0]) * 20 + 100) + '%;">' + big_d[1] + '</span>', data, 1)
  78. elif small:
  79. sm_d = small.groups()
  80. data = com.sub('<span style="font-size: ' + str(100 - int(sm_d[0]) * 10) + '%;">' + sm_d[1] + '</span>', data, 1)
  81. elif color_2:
  82. c_d_2 = color_2.groups()
  83. data = com.sub('<span style="color: ' + c_d_2[0] + '">' + c_d_2[1] + '</span>', data, 1)
  84. elif color_3:
  85. c_d_3 = color_3.groups()
  86. data = com.sub('<span style="color: ' + c_d_3[0] + '">' + c_d_3[1] + '</span>', data, 1)
  87. elif back:
  88. back_d_1 = back.groups()
  89. data = com.sub('<span style="background: #' + back_d_1[0] + '">' + back_d_1[1] + '</span>', data, 1)
  90. elif back_3:
  91. back_d_3 = back_3.groups()
  92. data = com.sub('<span style="background: ' + back_d_3[0] + '">' + back_d_3[1] + '</span>', data, 1)
  93. elif div:
  94. div_d = div.groups()
  95. data = com.sub('<div style="' + div_d[0] + '">' + div_d[1] + '</div>', data, 1)
  96. elif html_d:
  97. data = com.sub(html_d.groups()[0], data, 1)
  98. elif fol:
  99. fol_d = fol.groups()
  100. if toc_y != 0:
  101. data = com.sub("<div>" + fol_d[0] + " <div id='folding_" + str(fol_num + 1) + "' style='display: inline-block;'>" + "[<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + "); folding(" + str(fol_num) + ");'>펼치기</a>]</div>" + "<div id='folding_" + str(fol_num + 2) + "' style='display: none;'>[" + "<a href='javascript:void(0);' onclick='folding(" + str(fol_num + 1) + "); folding(" + str(fol_num + 2) + ");" + " folding(" + str(fol_num) + ");'>접기</a>]</div><div id='folding_" + str(fol_num) + "' style='display: none;'>" + "<br>" + fol_d[1] + "</div></div>", data, 1)
  102. fol_num += 3
  103. else:
  104. data = com.sub("<div>" + fol_d[0] + "<br><br>" + fol_d[1] + "</div>", data, 1)
  105. elif syn:
  106. if syntax == 0:
  107. data += '<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script><script>hljs.initHighlightingOnLoad();</script>'
  108. syntax = 1
  109. syn_d = syn.groups()
  110. tax_d = syn_d[1].replace(' ', '<space>')
  111. tax_d = tax_d.replace('\r\n', '<isbr>')
  112. data = com.sub('<pre id="syntax"><code class="' + syn_d[0] + '"><code>' + tax_d + '</code></code></pre>', data, 1)
  113. elif include_out:
  114. if (include or in_c) == 1:
  115. data = com.sub("", data, 1)
  116. else:
  117. data = com.sub(include_out.groups()[0], data, 1)
  118. else:
  119. data = com.sub(it_d, data, 1)
  120. else:
  121. break
  122. return [data, fol_num]