template.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if $1 =~ /^'(.+?)'$/
  21. %(<%= t "#{strip_content($1)}", #{@escape} %>)
  22. else
  23. %(<%= t d"#{strip_variable(strip_content($1))}", #{@escape} %>)
  24. end
  25. end.gsub(/\#\{\s*config\.(.+?)\s*\}/m) do
  26. %(<%= c "#{strip_variable($1)}", #{@escape} %>)
  27. end.gsub(/\#\{(.*?)\}/m) do
  28. %(<%= d "#{strip_variable($1)}", #{@escape} %>)
  29. end
  30. end
  31. def strip_content(string)
  32. string&.gsub(/\t|\r|\n/, '')
  33. &.gsub(/"/, '\"')
  34. end
  35. def strip_variable(string)
  36. string&.gsub(/\t|\r|\n|"|'|§|;/, '')
  37. &.gsub(/\s*/, '')
  38. &.gsub(/<.+?>/, '')
  39. end
  40. end