Class: Inverter::Parser

Inherits:
Object
  • Object
show all
Includes:
RenderAnywhere
Defined in:
lib/inverter/parser.rb

Constant Summary collapse

NAME_START_DELIMITER =
"<!--//"
NAME_END_DELIMITER =
"//-->"
BLOCK_START_DELIMITER =
"<!--["
BLOCK_NAME_DELIMITER =
"]-->"
BLOCK_END_DELIMITER =
"<!--END-->"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name) ⇒ Parser

Returns a new instance of Parser.



14
15
16
# File 'lib/inverter/parser.rb', line 14

def initialize(template_name)
  @template_name = template_name
end

Class Method Details

.map_blocks_for(content) ⇒ Object

returns a hash with content blocks name and [start, end] positions to be replaced with updated content e.g: { “header”=>[32, 178], “body”=>[206, 352] }



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/inverter/parser.rb', line 83

def self.map_blocks_for(content)
  map   = {}
  delta = 0

  s = content.index(BLOCK_START_DELIMITER)
  e = content.index(BLOCK_END_DELIMITER)

  while s && e
    s += BLOCK_START_DELIMITER.size

    n   = content[s..e-1].index(BLOCK_NAME_DELIMITER)
    key = content[s..s+n-1].strip

    n += BLOCK_NAME_DELIMITER.size
    map[key] = [delta+s+n, delta+e-1]

    e += BLOCK_END_DELIMITER.size
    content = content[e..-1]
    delta  += e

    s = content.index(BLOCK_START_DELIMITER)
    e = content.index(BLOCK_END_DELIMITER)
  end

  return map
end

Instance Method Details

#parseObject Also known as: blocks

parses rendered template and returns hash with block names and content to be editable



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inverter/parser.rb', line 37

def parse
  result  = {}
  content = file_content

  s = content.index(BLOCK_START_DELIMITER)
  e = content.index(BLOCK_END_DELIMITER)

  while s && e
    s += BLOCK_START_DELIMITER.size
    block = content[s..e-1]

    key, value  = parse_block(block)
    result[key] = value

    e += BLOCK_END_DELIMITER.size
    content = content[e..-1]

    s = content.index(BLOCK_START_DELIMITER)
    e = content.index(BLOCK_END_DELIMITER)
  end

  return result
end

#parse_block(block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/inverter/parser.rb', line 62

def parse_block(block)
  n = block.index(BLOCK_NAME_DELIMITER)
  key = block[0..n-1].strip

  n += BLOCK_NAME_DELIMITER.size
  value = block[n..-1]

  begin
    html = render inline: value, layout: false
  rescue
    html = "Template block wasn't rendered cause of a template syntax error."
  end

  return key, html
end

#parse_nameObject Also known as: name

parse name from template



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/inverter/parser.rb', line 19

def parse_name
  name    = ''
  content = file_content

  s = content.index(NAME_START_DELIMITER)
  e = content.index(NAME_END_DELIMITER)

  unless s.nil?
    s += NAME_START_DELIMITER.size
    name = content[s..e-1].strip
  end

  return name
end