Class: Caboose::BlockTypeParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/block_type_parser.rb

Class Method Summary collapse

Class Method Details

.parse(str) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/caboose/block_type_parser.rb', line 164

def BlockTypeParser.parse(str)
  
  vars = {}
  pattern = /<%= \|(.*?)\| %>/
  new_lines = []
  lines = str.split("\n")
  lines.each do |line|        
    matches = line.to_enum(:scan, pattern).map{$&}
    if matches
      matches.each do |match|
        next if match.nil? || match.length == 0                        
        arr = match[5..-5].split('|')
                    
        name        = arr.count > 0 ? arr[0] : nil
        description = arr.count > 1 ? arr[1] : nil
        field_type  = arr.count > 2 ? arr[2] : nil
        default     = arr.count > 3 ? arr[3] : nil
        default = default && ((default.starts_with?('"') && default.ends_with?('"')) || (default.starts_with?("'") && default.ends_with?("'"))) ? default[1..-2] : nil 
        
        if vars[name.to_sym].nil?
          vars[name.to_sym] = StdClass.new({
            :name        => name        ,        
            :description => description ,
            :field_type  => field_type  ,
            :default     => default
          })            
        end
        line.gsub!(match, "<%= #{name} %>")          
      end
    end
    new_lines << line
  end
  
  str2 = "<%\n"
  vars.each do |k,var|
    str2 << "#{var.name} = block.child_value('#{var.name}')\n"                        
  end
  str2 << "\n"
  vars.each do |k,var|                
    str2 << "#{var.name} = \"#{var.default.gsub('"', '\"')}\" if #{var.name}.nil? || #{var.name}.length == 0\n" if var.default        
  end
  str2 << "%>\n"
  str2 << new_lines.join("\n")      
  return str2                  
end

.parse_html(str, tags, children = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/caboose/block_type_parser.rb', line 7

def BlockTypeParser.parse_html(str, tags, children = nil)

  doc = Nokogiri::HTML.fragment(str)            
  doc.children.first.set_attribute('id', "block_<%= block.id %>")

  count = 0
  rf_header = []
  rf_body = "#{doc.to_html}"      
  new_children = []
  tags.each do |tag|        
    case tag
      when 'heading'
        
        headings = doc.search('h1') + doc.search('h2') + doc.search('h3') + doc.search('h4') + doc.search('h5') + doc.search('h6')            
        headings.each_with_index do |h, i|
          
          info = children ? (children[count] ? children[count] : (children[count.to_s] ? children[count.to_s] : nil)) : nil
          description = info && info['description'] ? info['description'] : "#{(i+1).ordinalize} Heading"
          name        = info && info['name']        ? info['name']        : description.downcase.gsub(' ', '_')
          
          cv = StdClass.new
          cv.heading_text = h.text	                
         cv.size          = h.name.gsub('h','').to_i
          cv.align         = h.attributes['align'].to_s if h.attributes['align']
          cv.extra_classes = h.attributes['class'].to_s if h.attributes['class']
          if h['style']
            cv.align         = h.styles['text-align'] if h.styles['text-align']
           cv.color         = h.styles['color']      if h.styles['color']
           cv.margin_bottom = h.styles['margin-bottom'] if h.styles['margin-bottom']
           cv.margin_top    = h.styles['margin-top']    if h.styles['margin-top']
           cv.underline     = true if h.styles['text-decoration']
         end

          v = StdClass.new({              
            :name         => name,
            :description  => description,
            :field_type   => 'heading',
            :child_values => cv
          })
                        
          rf_body.gsub!(h.to_s, "<%= block.render('#{name}') %>")              
          new_children << v              
                          
          count = count + 1              
        end
        
      when 'link'
        
        links = doc.search('a')            
        links.each_with_index do |link, i|
          
          info = children ? (children[count] ? children[count] : (children[count.to_s] ? children[count.to_s] : nil)) : nil
          description = info && info['description'] ? info['description'] : "#{(i+1).ordinalize} Link"
          name        = info && info['name']        ? info['name']        : description.downcase.gsub(' ', '_')

          cv = StdClass.new
          cv.text          = link.text
          cv.align         = link.attributes['align'  ].to_s if link.attributes['align'  ]
          cv.target        = link.attributes['target' ].to_s if link.attributes['target' ]
          cv.url           = link.attributes['href'   ].to_s if link.attributes['href'   ]
         cv.extra_classes = link.attributes['class'  ].to_s if link.attributes['class'  ]
         if link['style']
            cv.align         = link.styles['text-align'   ] if link.styles['text-align'    ]
           cv.color         = link.styles['color'        ] if link.styles['color'         ]
           cv.margin        = link.styles['margin'       ] if link.styles['margin-bottom' ]
           cv.margin_top    = link.styles['margin-top'   ] if link.styles['margin-top'    ]
           cv.margin_bottom = link.styles['margin-bottom'] if link.styles['margin-bottom' ]	              
           cv.margin_left   = link.styles['margin-left'  ] if link.styles['margin-left'   ]
           cv.margin_right  = link.styles['margin-right' ] if link.styles['margin-right'  ]	              
         end

          v = StdClass.new({              
            :name         => name,
            :description  => description,
            :field_type   => 'button',
            :child_values => cv
          })
                        
          rf_body.gsub!(link.to_s, "<%= block.render('#{name}') %>")              
          new_children << v              
                          
          count = count + 1              
        end

      when 'richtext'
        
        paragraphs = doc.search('p')            
        paragraphs.each_with_index do |p, i|
          
          info = children ? (children[count] ? children[count] : (children[count.to_s] ? children[count.to_s] : nil)) : nil
          description = info && info['description'] ? info['description'] : "#{(i+1).ordinalize} Richtext"
          name        = info && info['name']        ? info['name']        : description.downcase.gsub(' ', '_')

          v = StdClass.new({              
            :name         => name,
            :description  => description,
            :field_type   => 'richtext',
            :default      => p.text                
          })
                        
          rf_body.gsub!(p.to_s, "<%= block.render('#{name}') %>")              
          new_children << v              
                          
          count = count + 1              
        end
                    
      when 'img'
        
        images = doc.search('img')            
        images.each_with_index do |img, i|
          
          info = children ? (children[count] ? children[count] : (children[count.to_s] ? children[count.to_s] : nil)) : nil
          description = info && info['description'] ? info['description'] : "#{(i+1).ordinalize} Image"
          name        = info && info['name']        ? info['name']        : description.downcase.gsub(' ', '_')
          
          cv = StdClass.new              
         cv.image_src	    = img.attributes['src'    ].to_s
         cv.alt_text	      = img.attributes['alt'    ].to_s if img.attributes['alt'    ]
         cv.width	        = img.attributes['width'  ].to_s if img.attributes['width'  ]
         cv.height	        = img.attributes['height' ].to_s if img.attributes['height' ]	            
         cv.width          = img.styles['width'         ] if img.styles['width'         ]
         cv.height         = img.styles['height'        ] if img.styles['height'        ]	            
         cv.align          = img.styles['float'         ] if img.styles['float'         ]
         cv.margin_bottom  = img.styles['margin-bottom' ] if img.styles['margin-bottom' ]
         cv.margin_left    = img.styles['margin-left'   ] if img.styles['margin-left'   ]
         cv.margin_right   = img.styles['margin-right'  ] if img.styles['margin-right'  ]
         cv.margin_top     = img.styles['margin-top'    ] if img.styles['margin-top'    ]
         
         Caboose.log(cv)

          v = StdClass.new({              
            :name         => name,
            :description  => description,
            :field_type   => 'image2',
            :child_values => cv
          })
                        
          rf_body.gsub!(img.to_s, "<%= block.render('#{name}') %>")              
          new_children << v              
                          
          count = count + 1              
        end
        
    end
  end

  render_function = ""
  render_function << "<%\n#{rf_header.join("\n")}\n%>\n" if rf_header.count > 0
  render_function << rf_body
  
  return {
    :original_html => str,
    :render_function => render_function,
    :children => new_children
  }                                    
end