trabajo-afectivo/lib/notification_factory/template.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

2016-04-13 23:40:37 +00:00
class NotificationFactory::Template
2016-11-11 10:17:53 +00:00
=begin
examples how to use
cleaned_template = NotificationFactory::Template.new(
'some template <b>#{ticket.title}</b> #{config.fqdn}',
true,
).to_s
2016-11-11 10:17:53 +00:00
=end
def initialize(template, escape)
2016-04-13 23:40:37 +00:00
@template = template
@escape = escape
2016-04-13 23:40:37 +00:00
end
def to_s
@template.gsub(/\#{\s*(.*?)\s*}/m) do
# some browsers start adding HTML tags
# fixes https://github.com/zammad/zammad/issues/385
input_template = $1.gsub(/\A<.+?>\s*|\s*<.+?>\z/, '') # rubocop:disable Lint/OutOfRangeRegexpRef
case input_template
when /\At\('(.+?)'\)\z/m
%(<%= t "#{sanitize_text($1)}", #{@escape} %>)
when /\At\((.+?)\)\z/m
%(<%= t d"#{sanitize_object_name($1)}", #{@escape} %>)
when /\Aconfig\.(.+?)\z/m
%(<%= c "#{sanitize_object_name($1)}", #{@escape} %>)
else
%(<%= d "#{sanitize_object_name(input_template)}", #{@escape} %>)
end
end
2016-04-13 23:40:37 +00:00
end
def sanitize_text(string)
string&.tr("\t\r\n", '')
&.gsub(/(?<!\\)(?=")/, '\\')
end
def sanitize_object_name(string)
string&.tr("\t\r\n\f \"'§;", '')
end
2016-04-13 23:40:37 +00:00
end