markdown.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from . import tool
  2. import datetime
  3. import html
  4. import re
  5. def markdown(conn, data, title, include_num):
  6. backlink = []
  7. include_num = include_num + '_' if include_num else ''
  8. plus_data = '' + \
  9. 'get_link_state("' + include_num + '");\n' + \
  10. 'get_file_state("' + include_num + '");\n' + \
  11. ''
  12. data = html.escape(data)
  13. data = data.replace('\r\n', '\n')
  14. data = '\n' + data
  15. class head_render:
  16. def __init__(self):
  17. pass
  18. def __call__(self, match):
  19. head_len = str(len(match[1]))
  20. head_data = match[2]
  21. return '<h' + head_len + '>' + head_data + '</h' + head_len + '>'
  22. head_r = r'\n(#{1,6}) ?([^\n]+)'
  23. head_do = head_render()
  24. data = re.sub(head_r, head_do, data)
  25. class link_render:
  26. def __init__(self):
  27. pass
  28. def __call__(self, match):
  29. if match[1] == '!':
  30. if re.search(r'^http(s)?:\/\/', match[3], flags = re.I):
  31. return '<img alt="' + match[2] + '" src="' + match[3] + '">'
  32. else:
  33. file_name = re.search(r'^([^.]+)\.([^.]+)$', match[3])
  34. if file_name:
  35. file_end = file_name.group(2)
  36. file_name = file_name.group(1)
  37. else:
  38. file_name = 'Test'
  39. file_end = 'jpg'
  40. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  41. file_alt = 'file:' + file_name + '.' + file_end
  42. return '' + \
  43. '<img class="' + include_num + 'file_finder_1" alt="' + match[2] + '" src="' + file_src + '">' + \
  44. '<a class="' + include_num + 'file_finder_2" id="not_thing" href="/upload?name=' + tool.url_pas(file_name) + '">' + file_alt + '</a>' + \
  45. ''
  46. else:
  47. if re.search(r'^http(s)?:\/\/', match[3], flags = re.I):
  48. return '<a id="out_link" href="' + match[3] + '">' + match[2] + '</a>'
  49. else:
  50. return '<a class="' + include_num + 'link_finder" href="/w/' + match[3] + '">' + match[2] + '</a>'
  51. link_r = r'(!)?\[((?:(?!\]\().)+)\]\(([^\]]+)\)'
  52. link_do = link_render()
  53. data = re.sub(link_r, link_do, data)
  54. data = re.sub(r'\*\*((?:(?!\*\*).)+)\*\*', '<b>\1</b>', data)
  55. data = re.sub(r'__((?:(?!__).)+)__', '<i>\1</i>', data)
  56. return [data, plus_data, backlink]