Class: RubyBBCode::Templates::HtmlTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bbcode/templates/html_template.rb

Overview

This class is designed to help us build up the HTML data. It starts out as a template such as…

@opening_part = '<a href="%url%">%between%'
@closing_part = '</a>'

and then slowly turns into…

@opening_part = '<a href="http://www.blah.com">cool beans'
@closing_part = '</a>'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ HtmlTemplate

Returns a new instance of HtmlTemplate.



11
12
13
14
15
16
# File 'lib/ruby-bbcode/templates/html_template.rb', line 11

def initialize(node)
  @node = node
  @tag_definition = node.definition # tag_definition
  @opening_part = node.definition[:html_open].dup
  @closing_part = node.definition[:html_close].dup
end

Instance Attribute Details

#closing_partObject

Returns the value of attribute closing_part.



9
10
11
# File 'lib/ruby-bbcode/templates/html_template.rb', line 9

def closing_part
  @closing_part
end

#opening_partObject

Returns the value of attribute opening_part.



9
10
11
# File 'lib/ruby-bbcode/templates/html_template.rb', line 9

def opening_part
  @opening_part
end

Class Method Details

.convert_text(node) ⇒ Object

Newlines are converted to html <br /> syntax before being returned.



19
20
21
22
23
# File 'lib/ruby-bbcode/templates/html_template.rb', line 19

def self.convert_text(node)
  return '' if node[:text].nil?
  # convert_newlines_to_br
  node[:text].gsub("\r\n", "\n").gsub("\n", "<br />\n")
end

Instance Method Details

#inlay_between_text!Object



25
26
27
# File 'lib/ruby-bbcode/templates/html_template.rb', line 25

def inlay_between_text!
  @opening_part.gsub!('%between%', @node[:between]) if between_text_as_param?
end

#inlay_closing_part!Object



38
39
40
# File 'lib/ruby-bbcode/templates/html_template.rb', line 38

def inlay_closing_part!
  @closing_part.gsub!('%between%', @node[:between]) if between_text_as_param?
end

#inlay_params!Object



29
30
31
32
33
34
35
36
# File 'lib/ruby-bbcode/templates/html_template.rb', line 29

def inlay_params!
  # Iterate over known tokens and fill in their values, if provided
  @tag_definition[:param_tokens].each do |token|
    param_value = @node[:params][token[:token]] || token[:default]
    param_value = CGI.escape(param_value) if token[:uri_escape]
    @opening_part.gsub!("%#{token[:token].to_s}%", "#{token[:prefix]}#{param_value}#{token[:postfix]}") unless param_value.nil?
  end
end

#remove_unused_tokens!Object



42
43
44
45
46
# File 'lib/ruby-bbcode/templates/html_template.rb', line 42

def remove_unused_tokens!
  @tag_definition[:param_tokens].each do |token|
    @opening_part.gsub!("%#{token[:token]}%", '')
  end
end