class SuttyMigration::WordpressXml

Understands the XML dump generated by Wordpress and creates Jekyll::Documents

Attributes

file[R]
site[R]
xml[R]

Public Class Methods

new(site:, file:) click to toggle source

@param :site [Jekyll::Site] Jekyll site @param :file [String] File path

   # File lib/sutty_migration/wordpress_xml.rb
15 def initialize(site:, file:)
16   @site = site
17   @file = file
18   @xml  = Nokogiri::XML File.read(file)
19 
20   # Make things easier by removing namespaces.
21   xml.remove_namespaces!
22 end

Public Instance Methods

attachments() click to toggle source

Attachments, indexed by ID

@return [Hash]

    # File lib/sutty_migration/wordpress_xml.rb
128 def attachments
129   @attachments ||= items_find_by('post_type', 'attachment').map do |attachment|
130     { attribute_value(attachment, 'post_id').to_i => Attachment.new(wordpress: self, item: attachment) }
131   end.reduce(&:merge)
132 end
attribute_value(element, attribute) click to toggle source

Get element's attribute value

@param [Nokogiri::XML::Element] @param [String] @return [String]

    # File lib/sutty_migration/wordpress_xml.rb
150 def attribute_value(element, attribute)
151   element.at_css(attribute).text
152 end
authors() click to toggle source

Authors with attributes, indexed by author email.

@return [Hash]

   # File lib/sutty_migration/wordpress_xml.rb
61 def authors
62   @authors ||= xml.css('channel > author').map do |author|
63     {
64       attribute_value(author, 'author_email') => {
65         id: attribute_value(author, 'author_id').to_i,
66         display_name: attribute_value(author, 'author_display_name'),
67         first_name: attribute_value(author, 'author_first_name'),
68         last_name: attribute_value(author, 'author_last_name'),
69         email: attribute_value(author, 'author_email')
70 
71       }
72     }
73   end.reduce(&:merge)
74 end
categories() click to toggle source

Categories with attributes, indexed by slug (“nicename”)

@return [Hash]

   # File lib/sutty_migration/wordpress_xml.rb
79 def categories
80   @categories ||= xml.css('channel > category').map do |category|
81     {
82       attribute_value(category, 'category_nicename') => {
83         id: attribute_value(category, 'term_id').to_i,
84         title: attribute_value(category, 'cat_name'),
85         parent: attribute_value(category, 'category_parent'),
86         slug: attribute_value(category, 'category_nicename')
87       }
88     }
89   end.reduce(&:merge)
90 end
description() click to toggle source

Description

@return [String]

   # File lib/sutty_migration/wordpress_xml.rb
45 def description
46   @description ||= attribute_value(xml, 'channel > description')
47 end
inspect() click to toggle source
   # File lib/sutty_migration/wordpress_xml.rb
24 def inspect
25   '#<SuttyMigration::WordpressXml>'
26 end
items_find_by(attribute, value) click to toggle source

Find items by attribute and value

@param [String] Attribute name @param [String] Attribute value @return [Nokogiri::NodeSet]

    # File lib/sutty_migration/wordpress_xml.rb
139 def items_find_by(attribute, value)
140   xml.css('channel > item').select do |item|
141     attribute_value(item, attribute) == value
142   end
143 end
language() click to toggle source

Language

TODO: Migrate multilanguage sites.

@return [String]

   # File lib/sutty_migration/wordpress_xml.rb
54 def language
55   @language ||= attribute_value(xml, 'channel > language')
56 end
pages() click to toggle source

Pages, indexed by ID

@return [Hash]

    # File lib/sutty_migration/wordpress_xml.rb
119 def pages
120   @pages ||= items_find_by('post_type', 'page').map do |page|
121     { attribute_value(page, 'post_id').to_i => Post.new(wordpress: self, item: page) }
122   end.reduce(&:merge)
123 end
posts() click to toggle source

Posts, indexed by ID

@return [Hash]

    # File lib/sutty_migration/wordpress_xml.rb
110 def posts
111   @posts ||= items_find_by('post_type', 'post').map do |post|
112     { attribute_value(post, 'post_id').to_i => Post.new(wordpress: self, item: post) }
113   end.reduce(&:merge)
114 end
tags() click to toggle source

Tags with attributes, indexed by slug

@return [Hash]

    # File lib/sutty_migration/wordpress_xml.rb
 95 def tags
 96   @tags ||= xml.css('channel > tag').map do |tag|
 97     {
 98       attribute_value(tag, 'tag_slug') => {
 99         id: attribute_value(tag, 'term_id').to_i,
100         title: attribute_value(tag, 'tag_name'),
101         slug: attribute_value(tag, 'tag_slug')
102       }
103     }
104   end.reduce(&:merge)
105 end
title() click to toggle source

Site title

@return [String]

   # File lib/sutty_migration/wordpress_xml.rb
38 def title
39   @title ||= attribute_value(xml, 'channel > title')
40 end
url() click to toggle source

Site URL

@return [String]

   # File lib/sutty_migration/wordpress_xml.rb
31 def url
32   @url ||= attribute_value(xml, 'channel > link')
33 end