Django Library Mycreole
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

__init__.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import creole
  2. from django.conf import settings
  3. from django.urls.base import reverse
  4. from .help import MYCREOLE_HELP
  5. import importlib
  6. import os
  7. from mycreole import parameter
  8. import shutil
  9. def get_full_path(rel_path):
  10. return os.path.join(parameter.get(parameter.MYCREOLE_ROOT), *rel_path.split('/'))
  11. def delete_attachment_target_path(attachment_target_path):
  12. shutil.rmtree(get_full_path(attachment_target_path), True)
  13. def mycreole_help_pagecontent():
  14. return creole.creole2html(MYCREOLE_HELP)
  15. def render_simple(text, macros=None):
  16. return creole.creole2html(text, macros=macros)
  17. def render(request, text, attachment_target_path, next_anchor='', macros=None):
  18. def get_attachment_name(text, state):
  19. end_idx = text.index(']]' if state == '[' else '}}')
  20. try:
  21. split_idx = text.index('|')
  22. except ValueError:
  23. split_idx = len(text)
  24. return text[:min(end_idx, split_idx)]
  25. # Call the additional filters before rendering
  26. try:
  27. ext_filters = settings.MYCREOLE_EXT_FILTERS
  28. except AttributeError:
  29. pass # No filters defined
  30. else:
  31. for function_string in ext_filters:
  32. m_name, f_name = function_string.rsplit('.', 1)
  33. try:
  34. mod = importlib.import_module(m_name)
  35. except ModuleNotFoundError:
  36. pass
  37. else:
  38. try:
  39. func = getattr(mod, f_name)
  40. except AttributeError:
  41. pass
  42. else:
  43. text = func(text)
  44. if not attachment_target_path.endswith('/'):
  45. attachment_target_path += '/'
  46. try:
  47. render_txt = ''
  48. while len(text) > 0:
  49. try:
  50. link_pos = text.index('[[attachment:')
  51. except ValueError:
  52. link_pos = len(text)
  53. try:
  54. embed_pos = text.index('{{attachment:')
  55. except ValueError:
  56. embed_pos = len(text)
  57. pos = min(link_pos, embed_pos)
  58. render_txt += text[:pos]
  59. text = text[pos + 13:]
  60. if link_pos != embed_pos:
  61. if link_pos < embed_pos:
  62. state = '['
  63. else:
  64. state = '{'
  65. attachment_name = get_attachment_name(text, state)
  66. text = text[len(attachment_name):]
  67. rel_path = attachment_target_path + attachment_name
  68. if os.path.isfile(get_full_path(rel_path)):
  69. render_txt += 2 * state + url_attachment(rel_path)
  70. else:
  71. render_txt += '[[%s|Upload]]' % url_upload(request, rel_path, next_anchor)
  72. text = text[text.index(']]' if state == '[' else '}}') + 2:]
  73. return creole.creole2html(render_txt, macros=macros)
  74. except:
  75. return creole.creole2html(text, macros=macros)
  76. def url_delete(request, rel_path, next_anchor=''):
  77. nxt = request.GET.get('next', request.get_full_path())
  78. return reverse('mycreole-delete', kwargs={'rel_path': rel_path}) + '?next=%s' % nxt + ('' if not next_anchor else '#%s' % next_anchor)
  79. def url_upload(request, rel_path, next_anchor=''):
  80. nxt = request.GET.get('next', request.get_full_path())
  81. return reverse('mycreole-upload', kwargs={'rel_path': rel_path}) + '?next=%s' % nxt + ('' if not next_anchor else '#%s' % next_anchor)
  82. def url_attachment(rel_path):
  83. return reverse('mycreole-attachment', kwargs={'rel_path': rel_path})
  84. def url_manage_uploads(request, rel_path):
  85. nxt = request.GET.get('next', request.get_full_path())
  86. return reverse('mycreole-manageuploads', kwargs={'rel_path': rel_path}) + '?next=%s' % nxt