Class: JekyllImport::Importers::S9Y

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



23
24
25
26
27
28
29
30
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
# File 'lib/jekyll-import/importers/s9y.rb', line 23

def self.process(options)
  source = options.fetch("source")

  FileUtils.mkdir_p("_posts")

  text = ""
  URI.parse(source).open { |line| text = line.read }
  rss = ::RSS::Parser.parse(text)

  rss.items.each do |item|
    post_url = item.link.match(".*(/archives/.*)")[1]
    categories = item.categories.collect(&:content)
    content = item.content_encoded.strip
    date = item.date
    slug = item.link.match('.*/archives/[0-9]+-(.*)\.html')[1]
    name = format("%02d-%02d-%02d-%s.markdown", date.year, date.month, date.day, slug)

    data = {
      "layout"     => "post",
      "title"      => item.title,
      "categories" => categories,
      "permalink"  => post_url,
      "s9y_link"   => item.link,
      "date"       => item.date,
    }.delete_if { |_k, v| v.nil? || v == "" }.to_yaml

    # Write out the data and content to file
    File.open("_posts/#{name}", "w") do |f|
      f.puts data
      f.puts "---"
      f.puts content
    end
  end
end

.require_depsObject



14
15
16
17
18
19
20
21
# File 'lib/jekyll-import/importers/s9y.rb', line 14

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

.specify_options(c) ⇒ Object



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

def self.specify_options(c)
  c.option "source", "--source SOURCE", "The URL of the S9Y RSS feed"
end

.validate(options) ⇒ Object



10
11
12
# File 'lib/jekyll-import/importers/s9y.rb', line 10

def self.validate(options)
  abort "Missing mandatory option --source, e.g. --source \"http://blog.example.com/rss.php?version=2.0&all=1\"" if options["source"].nil?
end