markdown.py 2.4 KB

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