renderer.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. class NotificationFactory::Renderer
  2. =begin
  3. examples how to use
  4. message_subject = NotificationFactory::Renderer.new(
  5. {
  6. ticket: Ticket.first,
  7. },
  8. 'de-de',
  9. 'some template <b><%= d "ticket.title", false %></b> <%= c "fqdn", false %>',
  10. false
  11. ).render
  12. message_body = NotificationFactory::Renderer.new(
  13. {
  14. ticket: Ticket.first,
  15. },
  16. 'de-de',
  17. 'some template <b><%= d "ticket.title", true %></b> <%= c "fqdn", true %>',
  18. ).render
  19. =end
  20. def initialize(objects, locale, template, escape = true)
  21. @objects = objects
  22. @locale = locale || 'en-us'
  23. @template = NotificationFactory::Template.new(template)
  24. @escape = escape
  25. end
  26. def render
  27. ERB.new(@template.to_s).result(binding)
  28. end
  29. # d - data of object
  30. # d('user.firstname', htmlEscape)
  31. def d(key, escape = nil)
  32. # do validaton, ignore some methodes
  33. return "\#{#{key} / not allowed}" if !data_key_valid?(key)
  34. value = nil
  35. object_methods = key.split('.')
  36. object_name = object_methods.shift
  37. # if no object is given, just return
  38. return "\#{no such object}" if object_name.empty?
  39. object_refs = @objects[object_name] || @objects[object_name.to_sym]
  40. # if object is not in avalable objects, just return
  41. return "\#{#{object_name} / no such object}" if !object_refs
  42. # if content of method is a complex datatype, just return
  43. if object_methods.empty? && object_refs.class != String && object_refs.class != Float && object_refs.class != Fixnum
  44. return "\#{#{key} / no such method}"
  45. end
  46. object_methods_s = ''
  47. object_methods.each { |method_raw|
  48. method = method_raw.strip
  49. if object_methods_s != ''
  50. object_methods_s += '.'
  51. end
  52. object_methods_s += method
  53. if object_methods_s == ''
  54. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  55. break
  56. end
  57. # if method exists
  58. if !object_refs.respond_to?(method.to_sym)
  59. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  60. break
  61. end
  62. object_refs = object_refs.send(method.to_sym)
  63. }
  64. placeholder = if !value
  65. object_refs
  66. else
  67. value
  68. end
  69. escaping(placeholder, escape)
  70. end
  71. # c - config
  72. # c('fqdn', htmlEscape)
  73. def c(key, escape = nil)
  74. config = Setting.get(key)
  75. escaping(config, escape)
  76. end
  77. # t - translation
  78. # t('yes', htmlEscape)
  79. def t(key, escape = nil)
  80. translation = Translation.translate(@locale, key)
  81. escaping(translation, escape)
  82. end
  83. # a_html - article body in html
  84. # a_html(article)
  85. def a_html(article)
  86. content_type = d "#{article}.content_type", false
  87. if content_type =~ /html/
  88. return d "#{article}.body", false
  89. end
  90. d("#{article}.body", false).text2html
  91. end
  92. # a_text - article body in text
  93. # a_text(article)
  94. def a_text(article)
  95. content_type = d "#{article}.content_type", false
  96. body = d "#{article}.body", false
  97. if content_type =~ /html/
  98. body = body.html2text
  99. end
  100. (body.strip + "\n").gsub(/^(.*?)$/, '> \\1')
  101. end
  102. # h - htmlEscape
  103. # h('fqdn', htmlEscape)
  104. def h(key)
  105. return key if !key
  106. CGI.escapeHTML(key.to_s)
  107. end
  108. private
  109. def escaping(key, escape)
  110. return key if escape == false
  111. return key if escape.nil? && !@escape
  112. h key
  113. end
  114. def data_key_valid?(key)
  115. return false if key =~ /`|\.(|\s*)(save|destroy|delete|remove|drop|update|create|new|all|where|find)/i
  116. true
  117. end
  118. end