Sha256: 96d6b14a3609b99af0f3e7e7296309bfe1b1d34647f301230fb522990bf4b8a2

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# Usage:
#   (Local file)
#   ruby -r 'jekyll/jekyll-import/rss' -e "JekyllImport::GoogleReader.process(:source => './somefile/on/your/computer.xml')"

require 'rss'
require 'open-uri'
require 'fileutils'
require 'safe_yaml'

require 'rexml/document'
require 'date'

module JekyllImport
  module GoogleReader
    def self.validate(options)
      if !options[:source]
        abort "Missing mandatory option --source."
      end
    end

    # Process the import.
    #
    # source - a URL or a local file String.
    #
    # Returns nothing.
    def self.process(options)
      validate(options)

      source = options[:source]

      open(source) do |content|
        feed = RSS::Parser.parse(content)

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

        feed.items.each do |item|
          title = item.title.content.to_s
          formatted_date = Date.parse(item.published.to_s)
          post_name = title.split(%r{ |!|/|:|&|-|$|,}).map do |i|
            i.downcase if i != ''
          end.compact.join('-')
          name = "#{formatted_date}-#{post_name}" 

          header = {
            'layout' => 'post',
            'title' => title
          }

          FileUtils.mkdir_p("_posts")

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-import-0.1.0.beta3 lib/jekyll/jekyll-import/google_reader.rb