markdown.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 re.search(r'^http(s)?:\/\/', match[2], flags = re.I):
  30. return '<a id="out_link" href="' + match[2] + '">' + match[1] + '</a>'
  31. else:
  32. return '<a class="' + include_num + 'link_finder" href="/w/' + match[2] + '">' + match[1] + '</a>'
  33. link_r = r'\[((?:(?!\]\().)+)\]\(([^\]]+)\)'
  34. link_do = link_render()
  35. data = re.sub(link_r, link_do, data)
  36. return [data, plus_data, backlink]