Git based wiki inspired by Gollum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.9KB

  1. import re
  2. import os
  3. import hashlib
  4. def escape_repl(m):
  5. print "group 0"
  6. print m.group(0)
  7. print "group 1"
  8. print m.group(1)
  9. if m.group(1):
  10. return "```" + escape_html(m.group(1)) + "```"
  11. def unescape_repl(m):
  12. if m.group(1):
  13. return "```" + unescape_html(m.group(1)) + "```"
  14. def escape_html(s):
  15. s = s.replace("&", '&')
  16. s = s.replace("<", '&lt;')
  17. s = s.replace(">", '&gt;')
  18. s = s.replace('"', '&quot;')
  19. s = s.replace("'", '&#39;')
  20. return s
  21. def unescape_html(s):
  22. s = s.replace('&amp;', "&")
  23. s = s.replace('&lt;', "<")
  24. s = s.replace('&gt;', ">")
  25. s = s.replace('&quot;', '"')
  26. s = s.replace('&#39;', "'")
  27. return s
  28. def mkdir_safe(path):
  29. if path and not(os.path.exists(path)):
  30. os.makedirs(path)
  31. return path
  32. def extract_path(file_path):
  33. if not file_path:
  34. return None
  35. last_slash = file_path.rindex("/")
  36. if last_slash:
  37. return file_path[0, last_slash]
  38. def clean_path(path):
  39. if path:
  40. if path[0] != '/':
  41. path.insert(0, '/')
  42. return re.sub(r"//+", '/', path)
  43. def extract_name(file_path):
  44. if file_path[-1] == "/":
  45. return None
  46. return os.path.basename(file_path)
  47. def remove_ext(path):
  48. return os.path.splitext(path)[0]
  49. def clean_url(url):
  50. if not url:
  51. return url
  52. url = url.replace('%2F', '/')
  53. url = re.sub(r"^/+", "", url)
  54. return re.sub(r"//+", '/', url)
  55. def to_canonical(s):
  56. """
  57. Double space -> single dash
  58. Double dash -> single dash
  59. Remove all non alphanumeric and dash
  60. Limit to first 64 chars
  61. """
  62. s = s.encode('ascii', 'ignore')
  63. s = str(s)
  64. s = re.sub(r"\s\s*", "-", s)
  65. s = re.sub(r"\-\-+", "-", s)
  66. s = re.sub(r"[^a-zA-Z0-9\-]", "", s)
  67. s = s[:64]
  68. return s
  69. def gravatar_url(email):
  70. return "https://www.gravatar.com/avatar/" + hashlib.md5(email).hexdigest()