template.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. class NotificationFactory::Template
  2. =begin
  3. examples how to use
  4. cleaned_template = NotificationFactory::Template.new(
  5. 'some template <b>#{ticket.title}</b> #{config.fqdn}',
  6. true,
  7. ).to_s
  8. =end
  9. def initialize(template, escape)
  10. @template = template
  11. @escape = escape
  12. end
  13. def to_s
  14. strip_html
  15. @template
  16. end
  17. def strip_html
  18. # some browsers start adding HTML tags
  19. # fixes https://github.com/zammad/zammad/issues/385
  20. @template.gsub!(/\#\{\s*t\((.+?)\)\s*\}/m) do
  21. content = $1
  22. if content =~ /^'(.+?)'$/
  23. "<%= t \"#{strip_content($1)}\", #{@escape} %>"
  24. else
  25. "<%= t d\"#{strip_variable(content)}\", #{@escape} %>"
  26. end
  27. end
  28. @template.gsub!(/\#\{\s*config\.(.+?)\s*\}/m) do
  29. "<%= c \"#{strip_variable($1)}\", #{@escape} %>"
  30. end
  31. @template.gsub!(/\#\{(.*?)\}/m) do
  32. "<%= d \"#{strip_variable($1)}\", #{@escape} %>"
  33. end
  34. end
  35. def strip_content(string)
  36. return string if !string
  37. string.gsub!(/\t|\r|\n/, '')
  38. string.gsub!(/"/, '\"')
  39. string
  40. end
  41. def strip_variable(string)
  42. return string if !string
  43. string.gsub!(/\t|\r|\n|"|'|§|;/, '')
  44. string.gsub!(/\s*/, '')
  45. string.gsub!(/<.+?>/, '')
  46. string
  47. end
  48. end