Class: Caboose::RichTextBlockParser

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

Class Method Summary collapse

Class Method Details

.parse(block, html, domain) ⇒ Object



5
6
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
# File 'app/models/caboose/rich_text_block_parser.rb', line 5

def self.parse(block, html, domain)
  
  random_string = (0...10).map { ('a'..'z').to_a[rand(26)] }.join
  html = html.strip
  page = Nokogiri::HTML("<div class='#{random_string}'>#{html}</div>")
  if page.css('div').children.count == 1
    block.value = html
    block.save
    return block
  end
  
  nodes = []
  page.css("div.#{random_string}").children.each do |node|
    #Caboose.log(node.to_s)
    case node.name.downcase
      when 'h1'    then nodes << node           
      when 'h2'    then nodes << node
      when 'h3'    then nodes << node
      when 'h4'    then nodes << node
      when 'h5'    then nodes << node
      when 'h6'    then nodes << node
      when 'p'     then
        str = node.inner_html.strip
        nodes << node if str.length > 0 && str != "&#160;" && str != "&nbsp;"
      when 'ul'    then nodes << node
      when 'div'   then nodes << node
      when 'a'     then nodes << node
      when 'img'   then nodes << node
      when 'table' then nodes << node
    end                    
  end
  
  # Increment the sort order of any blocks after the current one                
  sort_order = block.sort_order + nodes.count                
  block.parent.children.where('sort_order > ?', block.sort_order).reorder(:sort_order).all.each do |b2|
    b2.sort_order = sort_order
    b2.save
    sort_order = sort_order + 1                  
  end
  
  # Now add all the new blocks
  sort_order = block.sort_order
  new_first_block_id = false
  paragraphs = []
  nodes.each do |node|
    b = Block.create(   
      :page_id       => block.page_id,              
      :parent_id     => block.parent_id,
      :sort_order    => sort_order
    )
    if sort_order == block.sort_order
      new_first_block_id = b.id
    end
    
    case node.name.downcase
      when 'h1'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 1)                        
        b.child('text').update_attribute(:value, node.inner_html)            
      when 'h2'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 2)                        
        b.child('text').update_attribute(:value, node.inner_html)
      when 'h3'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 3)                        
        b.child('text').update_attribute(:value, node.inner_html)
      when 'h4'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 4)                        
        b.child('text').update_attribute(:value, node.inner_html)
      when 'h5'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 5)                        
        b.child('text').update_attribute(:value, node.inner_html)
      when 'h6'
        b.block_type_id = BlockType.where(:name => 'heading').first.id
        b.save            
        b.create_children            
        b.child('size').update_attribute(:value, 6)                        
        b.child('text').update_attribute(:value, node.inner_html)
      when 'p'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = node.to_s                        
        b.save
        paragraphs << b
      when 'ul'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = node.to_s                        
        b.save
      when 'div'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = node.to_s                        
        b.save
      when 'a'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = "<p>#{node.to_s}</p>"                        
        b.save
      when 'img'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = node.to_s                        
        b.save
      when 'table'
        b.block_type_id = BlockType.where(:name => 'richtext').first.id
        b.value = node.to_s                        
        b.save            
    end                    
    sort_order = sort_order + 1
  end
  
  paragraphs.each do |p|
    self.parse_paragraph(p, domain)
  end
  
  # Now delete the original block      
  block.destroy
  block = Block.find(new_first_block_id)
  return block

end

.parse_paragraph(block, domain) ⇒ Object



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
163
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
# File 'app/models/caboose/rich_text_block_parser.rb', line 136

def self.parse_paragraph(block, domain)
        
  page = Nokogiri::HTML(block.value)
  return if page.css('img').count == 0
  
  image_count = page.css('img').count
  sort_order = block.sort_order
  
  # Increment the sort order of any blocks after and including the current one      
  i = 0
  block.parent.children.where('sort_order >= ?', block.sort_order).reorder(:sort_order).all.each do |b2|
    b2.sort_order = sort_order + image_count + i
    b2.save
    i = i + 1                  
  end
  
  # Create blocks for each image
  i = 0
  image_block_type_id = BlockType.where("name = ? and parent_id is null", 'image').first.id
  page.css('img').each do |node|
    b = Block.create(          
      :page_id       => block.page_id,              
      :parent_id     => block.parent_id,
      :block_type_id => image_block_type_id,
      :sort_order    => sort_order + i
    )
    b.create_children
    
    # Set the alignment
    if node['align'] && node['align'].length > 0
      align = node['align']
      align[0] = align[0].capitalize
      b.child('align').update_attribute(:value, align)
    end
    
    # Download the image
    src = node['src']
    src = "http://#{domain}#{src}" if src.starts_with?('/')        
    img = b.child('image_src')
    img.image = URI.parse(src)
    img.save
    
    #b.child('image_style'   ).update_attribute(:value, )
    #b.child('link'          ).update_attribute(:value, )
    #b.child('width'         ).update_attribute(:value, )
    #b.child('height'        ).update_attribute(:value, )
    #b.child('margin_top'    ).update_attribute(:value, )
    #b.child('margin_bottom' ).update_attribute(:value, )
    #b.child('margin_right'  ).update_attribute(:value, )
    #b.child('margin_left'   ).update_attribute(:value, )
                 
    i = i + 1        
  end
  
  # Now remove any images from the paragraph
  page.search('.//img').remove
  block.value = page.to_s
  block.save
  
end