fork download
  1. from django.utils.html import conditional_escape
  2. from django.utils.safestring import mark_safe
  3.  
  4. @register.filter(needs_autoescape=True)
  5. def my_safe(text, autoescape=True):
  6. regs = [re.compile('>>[\d]+(?! \(OP\))'), re.compile('>>[\d]+ \(OP\)')]
  7. for reg in regs:
  8. q = 0
  9. for x in re.finditer(reg, text):
  10. q += 1
  11. t = q
  12. e = 0
  13. while q != 0:
  14. w = re.finditer(reg, text)
  15. k = 0
  16. for x in w:
  17. if e == t - q:
  18. if x.span()[0] == 0 and e == 0:
  19. if reg == re.compile('>>[\d]+(?! \(OP\))'):
  20. text = '<a class="link-reply" data-num="%s">' % text[2:x.span()[1]] +\
  21. text[:x.span()[1]] + '</a>' + conditional_escape(text[x.span()[1]:])
  22. else:
  23. text = '<a class="link-reply-thread" data-num="%s">' % text[2:x.span()[1] - 5] +\
  24. text[:x.span()[1]] + '</a>' + conditional_escape(text[x.span()[1]:])
  25. break
  26. else:
  27. if k == t - q:
  28. if reg == re.compile('>>[\d]+(?! \(OP\))'):
  29. text = conditional_escape(text[:x.span()[0]]) +\
  30. '<a class="link-reply" data-num="%s">' % text[x.span()[0] +2:x.span()[1]] +\
  31. text[x.span()[0]:x.span()[1]] + '</a>' + conditional_escape(text[x.span()[1]:])
  32. else:
  33. text = conditional_escape(text[:x.span()[0]]) +\
  34. '<a class="link-reply-thread" data-num="%s">' % text[x.span()[0] + 2:x.span()[1] - 5] +\
  35. text[x.span()[0]:x.span()[1]] + '</a>' + conditional_escape(text[x.span()[1]:])
  36. break
  37. k += 1
  38. e += 1
  39. q -= 1
  40. return mark_safe(text)
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty