Class: SuttyMigration::WordpressXml

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(site:, file:) ⇒ WordpressXml

Returns a new instance of WordpressXml.

Parameters:

  • :site (Jekyll::Site)

    Jekyll site

  • :file (String)

    File path



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

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/sutty_migration/wordpress_xml.rb', line 11

def file
  @file
end

#siteObject (readonly)

Returns the value of attribute site.



11
12
13
# File 'lib/sutty_migration/wordpress_xml.rb', line 11

def site
  @site
end

#xmlObject (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

#attachmentsHash

Attachments, indexed by ID

Returns:

  • (Hash)


128
129
130
131
132
# File 'lib/sutty_migration/wordpress_xml.rb', line 128

def attachments
  @attachments ||= items_find_by('post_type', 'attachment').map do |attachment|
    { attribute_value(attachment, 'post_id').to_i => Attachment.new(wordpress: self, item: attachment) }
  end.reduce(&:merge)
end

#attribute_value(element, attribute) ⇒ String

Get element’s attribute value

Parameters:

  • (Nokogiri::XML::Element)
  • (String)

Returns:

  • (String)


150
151
152
# File 'lib/sutty_migration/wordpress_xml.rb', line 150

def attribute_value(element, attribute)
  element.at_css(attribute).text
end

#authorsHash

Authors with attributes, indexed by author email.

Returns:

  • (Hash)


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
  @authors ||= xml.css('channel > author').map do |author|
    {
      attribute_value(author, 'author_email') => {
        id: attribute_value(author, 'author_id').to_i,
        display_name: attribute_value(author, 'author_display_name'),
        first_name: attribute_value(author, 'author_first_name'),
        last_name: attribute_value(author, 'author_last_name'),
        email: attribute_value(author, 'author_email')

      }
    }
  end.reduce(&:merge)
end

#categoriesHash

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

Returns:

  • (Hash)


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

#descriptionString

Description

Returns:

  • (String)


45
46
47
# File 'lib/sutty_migration/wordpress_xml.rb', line 45

def description
  @description ||= attribute_value(xml, 'channel > description')
end

#inspectObject



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

Parameters:

  • Attribute (String)

    name

  • Attribute (String)

    value

Returns:

  • (Nokogiri::NodeSet)


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

#languageString

Language

TODO: Migrate multilanguage sites.

Returns:

  • (String)


54
55
56
# File 'lib/sutty_migration/wordpress_xml.rb', line 54

def language
  @language ||= attribute_value(xml, 'channel > language')
end

#pagesHash

Pages, indexed by ID

Returns:

  • (Hash)


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

#postsHash

Posts, indexed by ID

Returns:

  • (Hash)


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

#tagsHash

Tags with attributes, indexed by slug

Returns:

  • (Hash)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sutty_migration/wordpress_xml.rb', line 95

def tags
  @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

#titleString

Site title

Returns:

  • (String)


38
39
40
# File 'lib/sutty_migration/wordpress_xml.rb', line 38

def title
  @title ||= attribute_value(xml, 'channel > title')
end

#urlString

Site URL

Returns:

  • (String)


31
32
33
# File 'lib/sutty_migration/wordpress_xml.rb', line 31

def url
  @url ||= attribute_value(xml, 'channel > link')
end