Module: Wp2tumblr::Wordpress

Included in:
Wp2tumblr
Defined in:
lib/wp2tumblr/wordpress.rb

Class Method Summary collapse

Class Method Details

.get_post_meta(post, type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wp2tumblr/wordpress.rb', line 62

def self.(post, type)
  @meta = []
    case type
    when :category
      post.css("category").each do |item| 
        if item.attr("domain") === "category"
          @meta.push(item.text) unless @meta.include?(item.text)
        end
      end
    when :tag
      post.css("category").each do |item|
        if item.attr("domain") === "post_tag"
          @meta.push(item.text) unless @meta.include?(item.text)
        end 
      end
    when :comment 
      post.xpath("wp:comment").to_enum.with_index(0) do |comment, i|
        @meta.push({
          author: comment.at_xpath("wp:comment_author").text, 
          author_email: comment.at_xpath("wp:comment_author_email").text, 
          content: comment.at_xpath("wp:comment_content").text,
          approved: comment.at_xpath("wp:comment_approved").text === "1" ? true : false
        })
      end
    end
  @meta
end

.parse_all(file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wp2tumblr/wordpress.rb', line 46

def self.parse_all(file)
  items = get_file_contents(file)
  @posts = []
  items.to_enum.with_index(0) do |item, i|
    @posts[i] = {
      title: item.at_xpath("title").text, 
      content: item.at_xpath("content:encoded").text, 
      created_at: item.at_xpath("pubDate").text, 
      categories: (item, :category), 
      tags: (item, :tag),
      comments: (item, :comment)
    }
  end
  @posts
end

.parse_categories(file) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/wp2tumblr/wordpress.rb', line 28

def self.parse_categories(file)
  items = get_file_contents(file)
  @categories = []
  items.to_enum.with_index(0) do |item, i| 
    @categories = (item, :category)
  end
  @categories
end

.parse_images(post_content) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wp2tumblr/wordpress.rb', line 90

def self.parse_images(post_content)
  html = post_content
  html.css("img").each do |image|
    begin
      encoded_image = Base64.encode64(open(image['src']) {|io| io.read})
    rescue 
      puts "Error processing image: #{$!}, #{image['src']}"
      next
    end
    file_extension = image['src'][/\.[^.]*$/].split('.')[1]
    image['src'] = "data:image/#{file_extension};base64,#{encoded_image}" if encoded_image
  end
  html
end

.parse_posts(file) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/wp2tumblr/wordpress.rb', line 19

def self.parse_posts(file)
  items = get_file_contents(file)
  @posts = []
  items.to_enum.with_index(0) do |item, i|
    @posts[i] = {title: item.at_xpath("title").text, content: parse_images(item.at_xpath("content:encoded")), created_at: item.at_xpath("pubDate").text}
  end
  @posts
end

.parse_tags(file) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/wp2tumblr/wordpress.rb', line 37

def self.parse_tags(file)
  items = get_file_contents(file)
  @tags = []
  items.to_enum.with_index(0) do |item, i| 
    @tags = (item, :tag)
  end
  @tags
end

.parse_xml(file, type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wp2tumblr/wordpress.rb', line 6

def self.parse_xml(file, type)
  case type
  when :posts
    self.parse_posts(file)
  when :categories
    self.parse_categories(file)
  when :tags
    self.parse_tags(file)
  when :all
    self.parse_all(file)
  end
end