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
  @opening_part = node.definition[:html_open] + add_whitespace(:opening_whitespace)
  @closing_part = node.definition[:html_close] + add_whitespace(:closing_whitespace)
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_newlines(text) ⇒ Object



58
59
60
# File 'lib/ruby-bbcode/templates/html_template.rb', line 58

def self.convert_newlines(text)
  text.gsub("\r\n", "\n").gsub("\n", "<br />\n")
end

.convert_text(node, parent_node) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-bbcode/templates/html_template.rb', line 19

def self.convert_text(node, parent_node)
  return '' if node[:text].nil?

  text = node[:text]
  whitespace = ''

  if !parent_node.nil? && parent_node.definition[:block_tag]
    # Strip EOL whitespace, so it does not get converted
    text.scan(/(\s+)$/) do |result|
      whitespace = result[0]
      text = text[0..-result[0].length - 1]
    end
  end
  convert_newlines(text) + whitespace
end

Instance Method Details

#inlay_between_text!Object



35
36
37
# File 'lib/ruby-bbcode/templates/html_template.rb', line 35

def inlay_between_text!
  @opening_part.gsub!('%between%', format_between) if between_text_as_param?
end

#inlay_closing_part!Object



48
49
50
# File 'lib/ruby-bbcode/templates/html_template.rb', line 48

def inlay_closing_part!
  @closing_part.gsub!('%between%', format_between) if between_text_as_param?
end

#inlay_params!Object



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

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]}%", "#{token[:prefix]}#{param_value}#{token[:postfix]}") unless param_value.nil?
  end
end

#remove_unused_tokens!Object



52
53
54
55
56
# File 'lib/ruby-bbcode/templates/html_template.rb', line 52

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