template.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. end
  16. def strip_html
  17. # some browsers start adding HTML tags
  18. # fixes https://github.com/zammad/zammad/issues/385
  19. @template.gsub(/\#\{\s*t\((.+?)\)\s*\}/m) do
  20. content = $1
  21. if content =~ /^'(.+?)'$/
  22. %(<%= t "#{strip_content($1)}", #{@escape} %>)
  23. else
  24. %(<%= t d"#{strip_variable(content)}", #{@escape} %>)
  25. end
  26. end.gsub(/\#\{\s*config\.(.+?)\s*\}/m) do
  27. %(<%= c "#{strip_variable($1)}", #{@escape} %>)
  28. end.gsub(/\#\{(.*?)\}/m) do
  29. %(<%= d "#{strip_variable($1)}", #{@escape} %>)
  30. end
  31. end
  32. def strip_content(string)
  33. string&.gsub(/\t|\r|\n/, '')
  34. &.gsub(/"/, '\"')
  35. end
  36. def strip_variable(string)
  37. string&.gsub(/\t|\r|\n|"|'|§|;/, '')
  38. &.gsub(/\s*/, '')
  39. &.gsub(/<.+?>/, '')
  40. end
  41. end