Sha256: 964e83d3d1b4ae6d1e16432f4b3f2c14c942696681a61af06fc1bc59239a4525

Contents?: true

Size: 1.65 KB

Versions: 14

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module JekyllImport
  module Importers
    class GoogleReader < Importer
      def self.validate(options)
        abort "Missing mandatory option --source." if options["source"].nil?
      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-uri
          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")

        URI.parse(source).open 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

14 entries across 14 versions & 1 rubygems

Version Path
jekyll-import-0.24.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.23.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.22.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.21.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.20.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.19.2 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.19.1 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.19.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.18.1 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.18.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.17.1 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.17.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.16.0 lib/jekyll-import/importers/google_reader.rb
jekyll-import-0.15.0 lib/jekyll-import/importers/google_reader.rb