Class: SuttyMigration::WordpressXml
- Inherits:
-
Object
- Object
- SuttyMigration::WordpressXml
- Defined in:
- lib/sutty_migration/wordpress_xml.rb,
lib/sutty_migration/wordpress_xml/post.rb,
lib/sutty_migration/wordpress_xml/attachment.rb
Overview
Understands the XML dump generated by Wordpress and creates Jekyll::Documents
Defined Under Namespace
Classes: Attachment, Post
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#attachments ⇒ Hash
Attachments, indexed by ID.
-
#attribute_value(element, attribute) ⇒ String
Get element’s attribute value.
-
#authors ⇒ Hash
Authors with attributes, indexed by author email.
-
#categories ⇒ Hash
Categories with attributes, indexed by slug (“nicename”).
-
#description ⇒ String
Description.
-
#initialize(site:, file:) ⇒ WordpressXml
constructor
A new instance of WordpressXml.
- #inspect ⇒ Object
-
#items_find_by(attribute, value) ⇒ Nokogiri::NodeSet
Find items by attribute and value.
-
#language ⇒ String
Language.
-
#pages ⇒ Hash
Pages, indexed by ID.
-
#posts ⇒ Hash
Posts, indexed by ID.
-
#tags ⇒ Hash
Tags with attributes, indexed by slug.
-
#title ⇒ String
Site title.
-
#url ⇒ String
Site URL.
Constructor Details
#initialize(site:, file:) ⇒ WordpressXml
Returns a new instance of WordpressXml.
15 16 17 18 19 20 21 22 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 15 def initialize(site:, file:) @site = site @file = file @xml = Nokogiri::XML File.read(file) # Make things easier by removing namespaces. xml.remove_namespaces! end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
11 12 13 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 11 def file @file end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
11 12 13 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 11 def site @site end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
11 12 13 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 11 def xml @xml end |
Instance Method Details
#attachments ⇒ Hash
Attachments, indexed by ID
128 129 130 131 132 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 128 def @attachments ||= items_find_by('post_type', 'attachment').map do || { attribute_value(, 'post_id').to_i => Attachment.new(wordpress: self, item: ) } end.reduce(&:merge) end |
#attribute_value(element, attribute) ⇒ String
Get element’s attribute value
150 151 152 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 150 def attribute_value(element, attribute) element.at_css(attribute).text end |
#authors ⇒ Hash
Authors with attributes, indexed by author email.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 61 def @authors ||= xml.css('channel > author').map do || { attribute_value(, 'author_email') => { id: attribute_value(, 'author_id').to_i, display_name: attribute_value(, 'author_display_name'), first_name: attribute_value(, 'author_first_name'), last_name: attribute_value(, 'author_last_name'), email: attribute_value(, 'author_email') } } end.reduce(&:merge) end |
#categories ⇒ Hash
Categories with attributes, indexed by slug (“nicename”)
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 79 def categories @categories ||= xml.css('channel > category').map do |category| { attribute_value(category, 'category_nicename') => { id: attribute_value(category, 'term_id').to_i, title: attribute_value(category, 'cat_name'), parent: attribute_value(category, 'category_parent'), slug: attribute_value(category, 'category_nicename') } } end.reduce(&:merge) end |
#description ⇒ String
Description
45 46 47 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 45 def description @description ||= attribute_value(xml, 'channel > description') end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 24 def inspect '#<SuttyMigration::WordpressXml>' end |
#items_find_by(attribute, value) ⇒ Nokogiri::NodeSet
Find items by attribute and value
139 140 141 142 143 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 139 def items_find_by(attribute, value) xml.css('channel > item').select do |item| attribute_value(item, attribute) == value end end |
#language ⇒ String
Language
TODO: Migrate multilanguage sites.
54 55 56 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 54 def language @language ||= attribute_value(xml, 'channel > language') end |
#pages ⇒ Hash
Pages, indexed by ID
119 120 121 122 123 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 119 def pages @pages ||= items_find_by('post_type', 'page').map do |page| { attribute_value(page, 'post_id').to_i => Post.new(wordpress: self, item: page) } end.reduce(&:merge) end |
#posts ⇒ Hash
Posts, indexed by ID
110 111 112 113 114 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 110 def posts @posts ||= items_find_by('post_type', 'post').map do |post| { attribute_value(post, 'post_id').to_i => Post.new(wordpress: self, item: post) } end.reduce(&:merge) end |
#tags ⇒ Hash
Tags with attributes, indexed by slug
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 95 def @tags ||= xml.css('channel > tag').map do |tag| { attribute_value(tag, 'tag_slug') => { id: attribute_value(tag, 'term_id').to_i, title: attribute_value(tag, 'tag_name'), slug: attribute_value(tag, 'tag_slug') } } end.reduce(&:merge) end |
#title ⇒ String
Site title
38 39 40 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 38 def title @title ||= attribute_value(xml, 'channel > title') end |
#url ⇒ String
Site URL
31 32 33 |
# File 'lib/sutty_migration/wordpress_xml.rb', line 31 def url @url ||= attribute_value(xml, 'channel > link') end |