diff options
| author | hukl <contact@smyck.org> | 2009-03-31 22:56:23 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-31 22:56:23 +0200 |
| commit | 54d31710a4976163d74e4040cd6c2ad8c055c43d (patch) | |
| tree | bf20a11c40920ae53bb6260da999bfab24eaa02d /lib/chaos_xml.rb | |
| parent | d9c2a33aed53679c2fbd4f2e69660a16fe35fbb3 (diff) | |
renamed and refactored even further
Diffstat (limited to 'lib/chaos_xml.rb')
| -rw-r--r-- | lib/chaos_xml.rb | 294 |
1 files changed, 0 insertions, 294 deletions
diff --git a/lib/chaos_xml.rb b/lib/chaos_xml.rb deleted file mode 100644 index 94dbe10a..00000000 --- a/lib/chaos_xml.rb +++ /dev/null | |||
| @@ -1,294 +0,0 @@ | |||
| 1 | require 'iconv' | ||
| 2 | require 'nokogiri' | ||
| 3 | require 'lib/chaos_calendar/ical_occurrences' | ||
| 4 | |||
| 5 | |||
| 6 | class ChaosXml | ||
| 7 | include Enumerable | ||
| 8 | |||
| 9 | def initialize path | ||
| 10 | unless Node.root | ||
| 11 | Node.create! | ||
| 12 | end | ||
| 13 | |||
| 14 | @path = path | ||
| 15 | @years = {} | ||
| 16 | end | ||
| 17 | |||
| 18 | def import_updates | ||
| 19 | unless @updates = Node.find_by_unique_name('updates') | ||
| 20 | @updates = Node.create!( :slug => 'updates' ) | ||
| 21 | @updates.move_to_child_of Node.root | ||
| 22 | end | ||
| 23 | |||
| 24 | self.each do |chaospage, chaos_id, lang| | ||
| 25 | node = find_or_create_node( chaospage, chaos_id ) | ||
| 26 | html = convert_to_html( chaospage ) | ||
| 27 | page = fill_draft_with_content(node.draft, html, lang) | ||
| 28 | |||
| 29 | add_tags_to_page page, chaospage, "update" | ||
| 30 | add_event_to_node node, chaospage if page.tag_list.include?("event") | ||
| 31 | page.save | ||
| 32 | puts node.unique_name | ||
| 33 | end | ||
| 34 | |||
| 35 | Node.all.each {|node| node.publish_draft!} | ||
| 36 | end | ||
| 37 | |||
| 38 | def each | ||
| 39 | directories = Dir.glob("#{@path}/*/*.xml{,.de,.en}") | ||
| 40 | |||
| 41 | directories.each do |path| | ||
| 42 | next if path =~ /index\.xml/ | ||
| 43 | chaospage = Nokogiri::XML( File.new(path).read ) | ||
| 44 | lang = lang_from_path( path ) | ||
| 45 | chaos_id = chaos_id_from_path( path ) | ||
| 46 | |||
| 47 | yield chaospage, chaos_id, lang | ||
| 48 | end | ||
| 49 | end | ||
| 50 | |||
| 51 | def lang_from_path path | ||
| 52 | case path | ||
| 53 | when /\.de$/ then :de | ||
| 54 | when /\.en$/ then :en | ||
| 55 | else | ||
| 56 | :de | ||
| 57 | end | ||
| 58 | end | ||
| 59 | |||
| 60 | def chaos_id_from_path path | ||
| 61 | path.sub(@path, "").split(/\//).last.split(/\./)[0] | ||
| 62 | end | ||
| 63 | |||
| 64 | def find_or_create_node chaospage, chaos_id | ||
| 65 | |||
| 66 | date = chaospage.xpath("//date").first.content.to_date | ||
| 67 | unique_name = "updates/#{date.year}/#{chaos_id}" | ||
| 68 | year = date.year | ||
| 69 | |||
| 70 | unique_name_array = unique_name.split("/") | ||
| 71 | |||
| 72 | unless @years[year] || (@years[year] = Node.find_by_unique_name("updates/#{year}")) | ||
| 73 | @years[year] = Node.create :slug => year | ||
| 74 | @years[year].move_to_child_of @updates | ||
| 75 | end | ||
| 76 | |||
| 77 | unless node = Node.find_by_unique_name(unique_name) | ||
| 78 | node = Node.create :slug => chaos_id | ||
| 79 | node.move_to_child_of @years[year] | ||
| 80 | end | ||
| 81 | |||
| 82 | node | ||
| 83 | end | ||
| 84 | |||
| 85 | def fill_draft_with_content draft, html, lang | ||
| 86 | I18n.locale = lang | ||
| 87 | |||
| 88 | options = { | ||
| 89 | :title => html.xpath("//title")[0].content, | ||
| 90 | :abstract => html.xpath("//abstract")[0].content, | ||
| 91 | :body => extract_body(html) | ||
| 92 | } | ||
| 93 | |||
| 94 | draft.update_attributes options | ||
| 95 | draft | ||
| 96 | end | ||
| 97 | |||
| 98 | def extract_body html, excluded_tags=[] | ||
| 99 | default_excluded_tags = [ | ||
| 100 | "DTSTART", | ||
| 101 | "DTEND", | ||
| 102 | "DURATION", | ||
| 103 | "LOCATION", | ||
| 104 | "GEO", | ||
| 105 | "SUMMARY", | ||
| 106 | "URL" | ||
| 107 | ] | ||
| 108 | |||
| 109 | excluded_tags = (default_excluded_tags + excluded_tags).uniq | ||
| 110 | |||
| 111 | body = "" | ||
| 112 | element = html.xpath("//abstract")[0].next_sibling | ||
| 113 | |||
| 114 | while element do | ||
| 115 | body << element.to_s unless excluded_tags.include? element.name | ||
| 116 | element = element.next_sibling | ||
| 117 | end | ||
| 118 | |||
| 119 | body | ||
| 120 | end | ||
| 121 | |||
| 122 | def add_tags_to_page page, chaospage, *custom_tags | ||
| 123 | tag_list = custom_tags | ||
| 124 | |||
| 125 | chaospage.xpath("//flags").each do |node| | ||
| 126 | node.each do |k,v| | ||
| 127 | case k | ||
| 128 | when "calendar" | ||
| 129 | tag_list << "event" | ||
| 130 | when "pm" | ||
| 131 | tag_list << "pressemitteilung" | ||
| 132 | end | ||
| 133 | end | ||
| 134 | end | ||
| 135 | |||
| 136 | # Getting rid of duplicate flags | ||
| 137 | tag_list.uniq! | ||
| 138 | |||
| 139 | page.tag_list = tag_list.join(",") | ||
| 140 | page.save | ||
| 141 | end | ||
| 142 | |||
| 143 | def add_event_to_node node, chaospage | ||
| 144 | rrule = get_rrule(chaospage) | ||
| 145 | |||
| 146 | event_options = { | ||
| 147 | :start_time => get_start_time(chaospage), | ||
| 148 | :end_time => get_end_time(chaospage), | ||
| 149 | :allday => is_allday?(chaospage), | ||
| 150 | :rrule => rrule, | ||
| 151 | :custom_rrule => is_custom_rrule?(rrule), | ||
| 152 | :location => get_location(chaospage), | ||
| 153 | :url => get_url(chaospage), | ||
| 154 | :latitude => get_latitude(chaospage), | ||
| 155 | :longitude => get_logitude(chaospage) | ||
| 156 | } | ||
| 157 | |||
| 158 | unless tmp_event = node.event | ||
| 159 | tmp_event = Event.create! event_options.merge({:node_id => node.id}) | ||
| 160 | else | ||
| 161 | tmp_event.update_attributes event_options | ||
| 162 | end | ||
| 163 | end | ||
| 164 | |||
| 165 | def get_start_time chaospage | ||
| 166 | chaospage.at("//ical:DTSTART").content || raise("DTSTART not found") | ||
| 167 | end | ||
| 168 | |||
| 169 | def get_end_time chaospage | ||
| 170 | dtstart = chaospage.at("//ical:DTSTART") | ||
| 171 | dtend = chaospage.at("//ical:DTEND") | ||
| 172 | duration = chaospage.at("//ical:DURATION") | ||
| 173 | |||
| 174 | if dtend | ||
| 175 | return dtend.content | ||
| 176 | elsif duration | ||
| 177 | parsed_duration = Ical_occurrences.duration_to_fixnum(duration.content) | ||
| 178 | return (dtstart.content.to_time + parsed_duration) | ||
| 179 | else | ||
| 180 | raise("Neiter DTEND nor DURATION found") | ||
| 181 | end | ||
| 182 | end | ||
| 183 | |||
| 184 | def is_allday? chaospage | ||
| 185 | !chaospage.at("//ical:DTSTART").[]("VALUE").nil? | ||
| 186 | end | ||
| 187 | |||
| 188 | def get_rrule chaospage | ||
| 189 | if rrule = chaospage.at("//ical:RRULE") | ||
| 190 | rrtxt = '' | ||
| 191 | rrule.children.each do |subrule| | ||
| 192 | rule_name = subrule.name | ||
| 193 | rule_content = subrule.content.sub(/\W/,'') | ||
| 194 | |||
| 195 | next if rule_content.blank? | ||
| 196 | |||
| 197 | rrtxt += "#{rule_name}=#{rule_content};" | ||
| 198 | end | ||
| 199 | rrtxt.chomp!(';') | ||
| 200 | rrtxt | ||
| 201 | else | ||
| 202 | nil | ||
| 203 | end | ||
| 204 | end | ||
| 205 | |||
| 206 | def is_custom_rrule? rrule | ||
| 207 | default_rules = [ | ||
| 208 | "FREQ=WEEKLY;INTERVAL=1", | ||
| 209 | "FREQ=MONTHLY;INTERVAL=1", | ||
| 210 | "FREQ=YEARLY;INTERVAL=1" | ||
| 211 | ] | ||
| 212 | |||
| 213 | rrule && !default_rules.include?(rrule) ? true : false | ||
| 214 | end | ||
| 215 | |||
| 216 | def get_location chaospage | ||
| 217 | location = chaospage.at("//ical:LOCATION") | ||
| 218 | location ? location.content : nil | ||
| 219 | end | ||
| 220 | |||
| 221 | def get_url chaospage | ||
| 222 | location = chaospage.at("//ical:LOCATION") | ||
| 223 | location.[]("ALTREP") if location | ||
| 224 | end | ||
| 225 | |||
| 226 | def get_latitude chaospage | ||
| 227 | geo = chaospage.at("//ical:GEO") | ||
| 228 | geo.text.split(";")[0] if geo | ||
| 229 | end | ||
| 230 | |||
| 231 | def get_logitude chaospage | ||
| 232 | geo = chaospage.at("//ical:GEO") | ||
| 233 | geo.text.split(";")[1] if geo | ||
| 234 | end | ||
| 235 | |||
| 236 | def convert_to_html chaospage | ||
| 237 | |||
| 238 | chaospage.xpath('//paragraph').each {|sub| sub.name = "p"} | ||
| 239 | chaospage.xpath('//quote').each {|sub| sub.name = "blockquote" } | ||
| 240 | chaospage.xpath('//subtitle').each {|sub| sub.name = "h3" } | ||
| 241 | chaospage.xpath('//strong').each {|sub| sub.name = "em" } | ||
| 242 | chaospage.xpath('//stronger').each {|sub| sub.name = "strong" } | ||
| 243 | chaospage.xpath('//chapter').each {|sub| sub.name = "h2" } | ||
| 244 | |||
| 245 | chaospage.xpath('//link').each do |sub| | ||
| 246 | sub.name = "a" | ||
| 247 | href = sub.[]("ref") | ||
| 248 | sub.remove_attribute("ref") | ||
| 249 | sub.[]=("href", href) | ||
| 250 | sub.remove_attribute("type") | ||
| 251 | end | ||
| 252 | |||
| 253 | chaospage.xpath('//list').each do |sub| | ||
| 254 | if !sub.css("row item").empty? | ||
| 255 | sub.name = "table" | ||
| 256 | |||
| 257 | sub.css("row").each {|x| x.name = "tr"} | ||
| 258 | sub.css("tr item").each {|x| x.name = "td"} | ||
| 259 | elsif !sub.css("item").empty? | ||
| 260 | sub.name = "ul" | ||
| 261 | |||
| 262 | sub.css("item").each {|x| x.name = "li"} | ||
| 263 | end | ||
| 264 | end | ||
| 265 | |||
| 266 | chaospage.xpath('//media').each do |sub| | ||
| 267 | sub.name = "img" | ||
| 268 | src = sub.[]("ref") | ||
| 269 | sub.remove_attribute("src") | ||
| 270 | sub.[]=("src", src) | ||
| 271 | unless sub.content | ||
| 272 | sub.[]=("alt", sub.content) | ||
| 273 | sub.xpath('//*').each {|x| x.remove} | ||
| 274 | end | ||
| 275 | end | ||
| 276 | |||
| 277 | chaospage.xpath('//name').each do |sub| | ||
| 278 | if sub.[]("email") | ||
| 279 | mail_href = "mailto:#{sub.[]('email')}" | ||
| 280 | sub.remove_attribute("email") | ||
| 281 | sub.[]=("href", mail_href) | ||
| 282 | end | ||
| 283 | sub.name = "a" | ||
| 284 | |||
| 285 | if href = sub.[]("ref") | ||
| 286 | sub.remove_attribute("ref") | ||
| 287 | sub.[]=("href", href) | ||
| 288 | end | ||
| 289 | end | ||
| 290 | |||
| 291 | chaospage | ||
| 292 | |||
| 293 | end | ||
| 294 | end \ No newline at end of file | ||
