Class: SuttyMigration::WordpressXml::Post
- Inherits:
-
Object
- Object
- SuttyMigration::WordpressXml::Post
- Defined in:
- lib/sutty_migration/wordpress_xml/post.rb
Overview
Represents a WordPress post
Direct Known Subclasses
Constant Summary collapse
- SLASH =
'/'
Instance Attribute Summary collapse
-
#item ⇒ Object
readonly
Returns the value of attribute item.
-
#wordpress ⇒ Object
readonly
Returns the value of attribute wordpress.
Instance Method Summary collapse
-
#attribute_value(key) ⇒ String?
Get a value from the attribute.
-
#author ⇒ Hash
Author attributes.
-
#categories ⇒ Hash
Categories with attributes.
-
#content ⇒ String
Content as HTML, with site URL removed.
-
#date ⇒ Time
Publication date.
-
#description ⇒ String
Description.
-
#draft? ⇒ Boolean
Publication status.
-
#id ⇒ Integer
Post ID.
-
#initialize(wordpress:, item:) ⇒ Post
constructor
A new instance of Post.
- #inspect ⇒ Object
-
#last_modified_at ⇒ Time?
Modification date.
-
#meta ⇒ Hash
Metadata.
-
#order ⇒ Integer
Order.
-
#password ⇒ String
Post password.
-
#permalink ⇒ String
Permalink.
-
#published? ⇒ Boolean
Publication status.
-
#slug ⇒ String
Slug (“post name”).
-
#tags ⇒ Hash
Tags with attributes.
-
#title ⇒ String
Title.
Constructor Details
#initialize(wordpress:, item:) ⇒ Post
Returns a new instance of Post.
16 17 18 19 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 16 def initialize(wordpress:, item:) @wordpress = wordpress @item = item end |
Instance Attribute Details
#item ⇒ Object (readonly)
Returns the value of attribute item.
10 11 12 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 10 def item @item end |
#wordpress ⇒ Object (readonly)
Returns the value of attribute wordpress.
10 11 12 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 10 def wordpress @wordpress end |
Instance Method Details
#attribute_value(key) ⇒ String?
Get a value from the attribute
180 181 182 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 180 def attribute_value(key) item.at_css(key)&.text end |
#author ⇒ Hash
Author attributes.
104 105 106 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 104 def @author ||= wordpress.[attribute_value('creator')] end |
#categories ⇒ Hash
Categories with attributes.
129 130 131 132 133 134 135 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 129 def categories @categories ||= item.css('category').select do |c| c[:domain] == 'category' end.map do |c| wordpress.categories[c[:nicename]] || { id: 0, title: c.text.strip, slug: c[:nicename], parent: nil } end end |
#content ⇒ String
Content as HTML, with site URL removed.
95 96 97 98 99 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 95 def content @content ||= WordpressFormatting::Wpautop.wpautop(attribute_value('encoded')).gsub( / (href|src)="#{wordpress.url}/, ' \\1="' ) end |
#date ⇒ Time
Publication date.
WordPress can store this date in three different fields and sometimes they come empty or invalid.
75 76 77 78 79 80 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 75 def date @date ||= %w[pubDate post_date_gmt post_date].map do |date_attr| ::Jekyll::Utils.parse_date attribute_value(date_attr) rescue StandardError end.compact.first end |
#description ⇒ String
Description
58 59 60 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 58 def description @description ||= attribute_value('description') end |
#draft? ⇒ Boolean
Publication status
173 174 175 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 173 def draft? @draft ||= attribute_value('status') == 'draft' end |
#id ⇒ Integer
Post ID
28 29 30 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 28 def id @id ||= attribute_value('post_id').to_i end |
#inspect ⇒ Object
21 22 23 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 21 def inspect "#<#{self.class.name} title=\"#{title}\">" end |
#last_modified_at ⇒ Time?
Modification date.
85 86 87 88 89 90 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 85 def last_modified_at @last_modified_at ||= if (date = attribute_value('post_modified_gmt')) ::Jekyll::Utils.parse_date(date) end end |
#meta ⇒ Hash
Metadata. Plugins store useful information here. Duplicated keys are returned as an Array of values.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 141 def @meta ||= {}.tap do || item.css('postmeta').each do |m| key = m.css('meta_key').text value = m.css('meta_value').text case [key] when nil then [key] = value when String then [key] = [[key], value] when Array then [key] << value end end end end |
#order ⇒ Integer
Order. Higher are sorted on top by jekyll-order.
159 160 161 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 159 def order @order ||= attribute_value 'is_sticky' end |
#password ⇒ String
Post password. Use with jekyll-crypto.
111 112 113 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 111 def password @password ||= attribute_value 'post_password' end |
#permalink ⇒ String
Permalink. Absolute URL to the post.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 35 def permalink @permalink ||= begin p = attribute_value('link').sub(wordpress.url, '') if !p.end_with?(SLASH) && File.extname(p).empty? p << SLASH end p.squeeze(SLASH) end end |
#published? ⇒ Boolean
Publication status
166 167 168 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 166 def published? @published ||= attribute_value('status') == 'publish' end |
#slug ⇒ String
Slug (“post name”)
65 66 67 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 65 def slug @slug ||= attribute_value('post_name').tr(SLASH, '-').squeeze('-').sub(/\A-+/, '').sub(/-+\z/, '') end |
#tags ⇒ Hash
Tags with attributes.
118 119 120 121 122 123 124 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 118 def @tags ||= item.css('category').select do |c| c[:domain] == 'post_tag' end.map do |c| wordpress.[c[:nicename]] || { id: 0, title: c.text.strip, slug: c[:nicename] } end end |
#title ⇒ String
Title
51 52 53 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 51 def title @title ||= attribute_value('title') end |