Class: JekyllImport::Importers::RSS

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/rss.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object

Process the import.

source - a URL or a local file String.

Returns nothing.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-import/importers/rss.rb', line 31

def self.process(options)
  source = options.fetch("source")
  frontmatter = options.fetch("frontmatter", [])
  body = options.fetch("body", ["description"])

  content = ""
  open(source) { |s| content = s.read }
  rss = ::RSS::Parser.parse(content, false)

  raise "There doesn't appear to be any RSS items at the source (#{source}) provided." unless rss

  rss.items.each do |item|
    formatted_date = item.date.strftime("%Y-%m-%d")
    post_name = Jekyll::Utils.slugify(item.title, :mode => "latin")
    name = "#{formatted_date}-#{post_name}"

    header = {
      "layout" => "post",
      "title"  => item.title,
    }

    header["tag"] = options["tag"] unless options["tag"].nil? || options["tag"].empty?

    frontmatter.each do |value|
      header[value] = item.send(value)
    end

    output = +""

    body.each do |row|
      output << item.send(row).to_s
    end

    output.strip!
    output = item.content_encoded if output.empty?

    FileUtils.mkdir_p("_posts")

    File.open("_posts/#{name}.html", "w") do |f|
      f.puts header.to_yaml
      f.puts "---\n\n"
      f.puts output
    end
  end
end

.require_depsObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll-import/importers/rss.rb', line 15

def self.require_deps
  JekyllImport.require_with_fallback(%w(
    rss
    rss/1.0
    rss/2.0
    open-uri
    fileutils
    safe_yaml
  ))
end

.specify_options(c) ⇒ Object



6
7
8
9
# File 'lib/jekyll-import/importers/rss.rb', line 6

def self.specify_options(c)
  c.option "source", "--source NAME", "The RSS file or URL to import"
  c.option "tag", "--tag NAME", "Add a tag to posts"
end

.validate(options) ⇒ Object



11
12
13
# File 'lib/jekyll-import/importers/rss.rb', line 11

def self.validate(options)
  abort "Missing mandatory option --source." if options["source"].nil?
end