Django Library Mycreole
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

__init__.py 3.8KB

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