mid_pas.py 6.3 KB

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