2
0

toc_pas.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. from urllib import parse
  3. def url_pas(data):
  4. return(parse.quote(data).replace('/','%2F'))
  5. def toc_pas(data, title, num, toc_y):
  6. if(not re.search('\[목차\]', data)):
  7. if(not re.search('\[목차\(없음\)\]', data)):
  8. data = re.sub("(?P<in>(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "[목차]\n\g<in>", data, 1)
  9. else:
  10. data = re.sub("\[목차\(없음\)\]", "", data)
  11. data = re.sub("(\n)(?P<in>\r\n(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n)", "\g<in>", data)
  12. i = [0, 0, 0, 0, 0, 0, 0]
  13. last = 0
  14. toc_c = -1
  15. toc_d = -1
  16. span = ''
  17. rtoc = '<div id="toc"><span id="toc-name">목차</span><br><br>'
  18. while(1):
  19. i[0] += 1
  20. m = re.search('(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\r\n', data)
  21. if(m):
  22. result = m.groups()
  23. wiki = len(result[0])
  24. if(last < wiki):
  25. last = wiki
  26. else:
  27. last = wiki
  28. for a in range(wiki + 1, 7):
  29. i[a] = 0
  30. i[wiki] += 1
  31. toc = str(i[1]) + '.' + str(i[2]) + '.' + str(i[3]) + '.' + str(i[4]) + '.' + str(i[5]) + '.' + str(i[6]) + '.'
  32. toc = re.sub("(?P<in>[0-9]0(?:[0]*)?)\.", '\g<in>#.', toc)
  33. toc = re.sub("0\.", '', toc)
  34. toc = re.sub("#\.", '.', toc)
  35. toc = re.sub("\.$", '', toc)
  36. if(toc_c == -1):
  37. margin = 'style="margin-top: 30px;"'
  38. toc_c = toc.count('.')
  39. else:
  40. toc_d = toc.count('.')
  41. if(toc_c == toc_d):
  42. margin = 'style="margin-top: 30px;"'
  43. else:
  44. if(toc_d < toc_c):
  45. margin = 'style="margin-top: 30px;"'
  46. else:
  47. margin = 'style="margin-top: 15px;"'
  48. toc_c = toc_d
  49. t = toc.count('.')
  50. span = '<span style="margin-left: 5px;"></span>' * t
  51. rtoc += span + '<a href="#s-' + toc + '">' + toc + '</a>. ' + result[1] + '<br>'
  52. c = re.sub(" $", "", result[1])
  53. d = c
  54. c = re.sub("\[\[(([^|]*)\|)?(?P<in>[^\]]*)\]\]", "\g<in>", c)
  55. edit_d = ''
  56. if(toc_y == 1):
  57. edit_d = ' <span style="font-size:11px;">[<a href="/edit/' + url_pas(title) + '/section/' + str(i[0]) + '">편집</a>]</span>'
  58. data = re.sub('(={1,6})\s?([^=]*)\s?(?:={1,6})(?:\s+)?\n', '<tablenobr><h' + str(wiki) + ' id="' + c + '" ' + margin + '><a href="#toc" id="s-' + toc + '">' + toc + '.<span style="margin-left: 5px;"></span></a> ' + d + edit_d + '</h' + str(wiki) + '><hr style="margin-top: -5px;">\n', data, 1)
  59. else:
  60. rtoc += '</div>'
  61. break
  62. data = re.sub("\[목차\]", rtoc, data)
  63. return(data)