Sha256: 139ecf846a2b120b29bc9e03d0f857fbf506facda566d2dc59926985191e5b7c

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

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

module JekyllImport
  module Importers
    class GoogleReader < Importer
      def self.validate(options)
        if options['source'].nil?
          abort "Missing mandatory option --source."
        end
      end

      def self.specify_options(c)
        c.option 'source', '--source', 'Source XML file of Google Reader export'
      end

      def self.require_deps
        JekyllImport.require_with_fallback(%w[
          rubygems
          rss
          fileutils
          safe_yaml
          open-url
          rexml/document
          date
        ])
      end

      # Process the import.
      #
      # source - a URL or a local file String.
      #
      # Returns nothing.
      def self.process(options)
        source = options.fetch('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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jekyll-import-0.2.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.1.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.1.0.rc1 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.1.0.beta4 lib/jekyll-import/importers/google_reader.rb