1
2
3
4
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
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
|
require 'iconv'
require 'nokogiri'
class ChaosXml
include Enumerable
def initialize path
unless Node.root
Node.create!
end
@path = path
@years = {}
end
def import_xml
unless @updates = Node.find_by_unique_name('updates')
@updates = Node.create!( :slug => 'updates' )
@updates.move_to_child_of Node.root
end
self.each do |chaospage, chaos_id, lang|
node = find_or_create_node( chaospage, chaos_id )
html = convert_to_html( chaospage )
page = fill_draft_with_content(node.draft, html, lang)
end
end
def each
directories = Dir.glob("#{@path}/*/*.xml{,.de,.en}")
directories.each do |path|
next if path =~ /index\.xml/
chaospage = Nokogiri::XML( File.new(path).read )
lang = lang_from_path( path )
chaos_id = chaos_id_from_path( path )
yield chaospage, chaos_id, lang
end
end
def lang_from_path path
case path
when /\.de$/ then :de
when /\.en$/ then :en
else
:de
end
end
def chaos_id_from_path path
path.sub(@path, "").split(/\//).last.split(/\./)[0]
end
def find_or_create_node chaospage, chaos_id
date = chaospage.xpath("//date").first.content.to_date
unique_name = "updates/#{date.year}/#{chaos_id}"
year = date.year
unique_name_array = unique_name.split("/")
unless @years[year] || (@years[year] = Node.find_by_unique_name("updates/#{year}"))
@years[year] = Node.create :slug => year
@years[year].move_to_child_of @updates
end
unless node = Node.find_by_unique_name(unique_name)
node = Node.create :slug => chaos_id
node.move_to_child_of @years[year]
end
node
end
def fill_draft_with_content draft, chaospage, lang
I18n.locale = lang
options = {
:title => chaospage.xpath("//title")[0].content,
:abstract => chaospage.xpath("//abstract")[0].content,
:body => extract_body(chaospage)
}
puts options.inspect
#draft.update_attributes options
end
def extract_body chaospage
body = ""
element = chaospage.xpath("//abstract")[0].next_sibling
while element do
body << element.to_s
element = element.next_sibling
end
puts body
end
def convert_to_html chaospage
chaospage.xpath('//paragraph').each {|sub| sub.name = "p"}
chaospage.xpath('//quote').each {|sub| sub.name = "blockquote" }
chaospage.xpath('//subtitle').each {|sub| sub.name = "h3" }
chaospage.xpath('//strong').each {|sub| sub.name = "em" }
chaospage.xpath('//stronger').each {|sub| sub.name = "strong" }
chaospage.xpath('//chapter').each {|sub| sub.name = "h2" }
chaospage.xpath('//link').each do |sub|
sub.name = "a"
href = sub.[]("ref")
sub.remove_attribute("ref")
sub.[]=("href", href)
sub.remove_attribute("type")
end
chaospage.xpath('//list').each do |sub|
if !sub.css("row item").empty?
sub.name = "table"
sub.css("row").each {|x| x.name = "tr"}
sub.css("tr item").each {|x| x.name = "td"}
elsif !sub.css("item").empty?
sub.name = "ul"
sub.css("item").each {|x| x.name = "li"}
end
end
chaospage.xpath('//media').each do |sub|
sub.name = "img"
src = sub.[]("ref")
sub.remove_attribute("src")
sub.[]=("src", src)
unless sub.content
sub.[]=("alt", sub.content)
sub.xpath('//*').each {|x| x.remove}
end
end
chaospage.xpath('//name').each do |sub|
if sub.[]("email")
mail_href = "mailto:#{sub.[]('email')}"
sub.remove_attribute("email")
sub.[]=("href", mail_href)
end
sub.name = "a"
if href = sub.[]("ref")
sub.remove_attribute("ref")
sub.[]=("href", href)
end
end
chaospage
end
end
|