Class: Splitter_atom

Inherits:
Object
  • Object
show all
Includes:
Splitter
Defined in:
lib/abelard/load.rb

Instance Method Summary collapse

Methods included from Splitter

#item, #write_doc_clean, #write_item

Constructor Details

#initialize(document, destination) ⇒ Splitter_atom

Returns a new instance of Splitter_atom.



132
133
134
135
# File 'lib/abelard/load.rb', line 132

def initialize(document, destination)
  @doc = document
  @dest = destination
end

Instance Method Details

#save(node) ⇒ Object



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
# File 'lib/abelard/load.rb', line 163

def save(node)
	id = node.children.find { |n| n.name == "id" }
	id = id && id.content

	path = XmlUtil::self_link(node)

	case node.name
	when "entry"
 category = XmlUtil::child_attribute(node, "category", "term")

 if @feed_type
entry_type = @feed_type 
 else
entry_type = category.split('#').last if category
 end
 
 case entry_type
 when "post"
postnumber = path.split('/').last
filename = "#{@dest}/post-#{postnumber}.xml"
write_item(node, filename)
 when "comment"
pathsplit = path.split('/')
postnumber = pathsplit[-4]
commentnumber = pathsplit[-1]
filename = "#{@dest}/comment-#{postnumber}-#{commentnumber}.xml"
write_item(node,filename)
 end
	end
end

#split_itemsObject



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
# File 'lib/abelard/load.rb', line 137

def split_items
  feed = @doc.root

  feedself = XmlUtil::self_link(feed)

  @feed_type = nil # unknown
  @feed_type = "post" if (feedself =~ %r{/posts/default$})
  @feed_type = "comment" if (feedself =~ %r{/comments/default$})
  
  @parent = LibXML::XML::Document.new()
  root = LibXML::XML::Node.new(feed.name)
  @parent.root = root
  feed.namespaces.definitions.each {|ns| LibXML::XML::Namespace.new(root, ns.prefix, ns.href)}
  feed.attributes.each { |a| root.attributes[a.name] = a.value }

  feed.children.select(&:element?).each do |node|
    if (node.name == "entry")
      save(node)
    else
      root << @parent.import(node)
    end
  end

  write_doc_clean(@parent, "#{@dest}/feed.xml")
end