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|
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 != " " && str != " "
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
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
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
block.destroy
block = Block.find(new_first_block_id)
return block
end
|