Class: SuttyMigration::WordpressXml::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/sutty_migration/wordpress_xml/post.rb

Overview

Represents a WordPress post

Direct Known Subclasses

Attachment

Constant Summary collapse

SLASH =
'/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wordpress:, item:) ⇒ Post

Returns a new instance of Post.

Parameters:



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

#itemObject (readonly)

Returns the value of attribute item.



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

def item
  @item
end

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

Returns:

  • (String, nil)


180
181
182
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 180

def attribute_value(key)
  item.at_css(key)&.text
end

#authorHash

Author attributes.

Returns:

  • (Hash)


104
105
106
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 104

def author
  @author ||= wordpress.authors[attribute_value('creator')]
end

#categoriesHash

Categories with attributes.

Returns:

  • (Hash)


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

#contentString

Content as HTML, with site URL removed.

Returns:

  • (String)


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

#dateTime

Publication date.

WordPress can store this date in three different fields and sometimes they come empty or invalid.

Returns:



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

#descriptionString

Description

Returns:

  • (String)


58
59
60
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 58

def description
  @description ||= attribute_value('description')
end

#draft?Boolean

Publication status

Returns:

  • (Boolean)


173
174
175
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 173

def draft?
  @draft ||= attribute_value('status') == 'draft'
end

#idInteger

Post ID

Returns:

  • (Integer)


28
29
30
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 28

def id
  @id ||= attribute_value('post_id').to_i
end

#inspectObject



21
22
23
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 21

def inspect
  "#<#{self.class.name} title=\"#{title}\">"
end

#last_modified_atTime?

Modification date.

Returns:



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

#metaHash

Metadata. Plugins store useful information here. Duplicated keys are returned as an Array of values.

Returns:

  • (Hash)


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
  @meta ||= {}.tap do |meta|
    item.css('postmeta').each do |m|
      key = m.css('meta_key').text
      value = m.css('meta_value').text

      case meta[key]
      when nil then meta[key] = value
      when String then meta[key] = [meta[key], value]
      when Array then meta[key] << value
      end
    end
  end
end

#orderInteger

Order. Higher are sorted on top by jekyll-order.

Returns:

  • (Integer)


159
160
161
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 159

def order
  @order ||= attribute_value 'is_sticky'
end

#passwordString

Post password. Use with jekyll-crypto.

Returns:

  • (String)


111
112
113
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 111

def password
  @password ||= attribute_value 'post_password'
end

Permalink. Absolute URL to the post.

Returns:

  • (String)


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

Returns:

  • (Boolean)


166
167
168
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 166

def published?
  @published ||= attribute_value('status') == 'publish'
end

#slugString

Slug (“post name”)

Returns:

  • (String)


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

#tagsHash

Tags with attributes.

Returns:

  • (Hash)


118
119
120
121
122
123
124
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 118

def tags
  @tags ||= item.css('category').select do |c|
    c[:domain] == 'post_tag'
  end.map do |c|
    wordpress.tags[c[:nicename]] || { id: 0, title: c.text.strip, slug: c[:nicename] }
  end
end

#titleString

Title

Returns:

  • (String)


51
52
53
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 51

def title
  @title ||= attribute_value('title')
end