class SuttyMigration::WordpressXml::Post
Represents a WordPress post
Attributes
Public Class Methods
@param :wordpress [SuttyMigration::WordpressXml] @param :item [Nokogiri::XML::Element]
# File lib/sutty_migration/wordpress_xml/post.rb 14 def initialize(wordpress:, item:) 15 @wordpress = wordpress 16 @item = item 17 end
Public Instance Methods
Get a value from the attribute
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 166 def attribute_value(key) 167 item.at_css(key).text 168 end
Categories with attributes.
@return [Hash]
# File lib/sutty_migration/wordpress_xml/post.rb 115 def categories 116 @categories ||= item.css('category').select do |c| 117 c[:domain] == 'category' 118 end.map do |c| 119 wordpress.categories[c[:nicename]] 120 end 121 end
Content as HTML, with site URL removed.
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 81 def content 82 @content ||= WordpressFormatting::Wpautop.wpautop(attribute_value('encoded')).gsub( 83 / (href|src)="#{wordpress.url}/, ' \\1="' 84 ) 85 end
Publication date.
WordPress can store this date in three different fields and sometimes they come empty or invalid.
@return [Time]
# File lib/sutty_migration/wordpress_xml/post.rb 64 def date 65 @date ||= %w[pubDate post_date_gmt post_date].map do |date_attr| 66 ::Jekyll::Utils.parse_date attribute_value(date_attr) 67 rescue StandardError 68 end.compact.first 69 end
Description
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 47 def description 48 @description ||= attribute_value('description') 49 end
Publication status
@return [Boolean]
# File lib/sutty_migration/wordpress_xml/post.rb 159 def draft? 160 @draft ||= attribute_value('status') == 'draft' 161 end
Post
ID
@return [Integer]
# File lib/sutty_migration/wordpress_xml/post.rb 26 def id 27 @id ||= attribute_value('post_id').to_i 28 end
# File lib/sutty_migration/wordpress_xml/post.rb 19 def inspect 20 "#<SuttyMigration::WordpressXml::Post title=\"#{title}\">" 21 end
Modification date.
@return [Time]
# File lib/sutty_migration/wordpress_xml/post.rb 74 def last_modified_at 75 @last_modified_at ||= ::Jekyll::Utils.parse_date attribute_value('post_modified_gmt') 76 end
Metadata. Plugins store useful information here. Duplicated keys are returned as an Array of values.
@return [Hash]
# File lib/sutty_migration/wordpress_xml/post.rb 127 def meta 128 @meta ||= {}.tap do |meta| 129 item.css('postmeta').each do |m| 130 key = m.css('meta_key').text 131 value = m.css('meta_value').text 132 133 case meta[key] 134 when nil then meta[key] = value 135 when String then meta[key] = [meta[key], value] 136 when Array then meta[key] << value 137 end 138 end 139 end 140 end
Order. Higher are sorted on top by jekyll-order.
@return [Integer]
# File lib/sutty_migration/wordpress_xml/post.rb 145 def order 146 @order ||= attribute_value 'is_sticky' 147 end
Post
password. Use with jekyll-crypto.
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 97 def password 98 @password ||= attribute_value 'post_password' 99 end
Permalink. Absolute URL to the post.
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 33 def permalink 34 @permalink ||= attribute_value('link').sub(wordpress.url, '') 35 end
Publication status
@return [Boolean]
# File lib/sutty_migration/wordpress_xml/post.rb 152 def published? 153 @published ||= attribute_value('status') == 'publish' 154 end
Slug (“post name”)
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 54 def slug 55 @slug ||= attribute_value('post_name') 56 end
Title
@return [String]
# File lib/sutty_migration/wordpress_xml/post.rb 40 def title 41 @title ||= attribute_value('title') 42 end