Class: JekyllImport::Importers::Blogger

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

Defined Under Namespace

Modules: BloggerAtomStreamListenerMethods Classes: BloggerAtomStreamListener

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.postprocess(options) ⇒ Object

Post-process after import.

replace-internal-link

a boolean if replace internal link

Returns nothing.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jekyll-import/importers/blogger.rb', line 62

def self.postprocess(options)
  # Replace internal link URL
  if options.fetch("replace-internal-link", false)
    original_url_base = options.fetch("original-url-base", nil)
    if original_url_base
      orig_url_pattern = Regexp.new(" href=([\"\'])(?:#{Regexp.escape(original_url_base)})?/([0-9]{4})/([0-9]{2})/([^\"\']+\.html)\\1")

      Dir.glob("_posts/*.*") do |filename|
        body = nil
        File.open(filename, "r") do |f|
          f.flock(File::LOCK_SH)
          body = f.read
        end

        body.gsub!(orig_url_pattern) do
          # for post_url
          quote = Regexp.last_match(1)
          post_file = Dir.glob("_posts/#{Regexp.last_match(2)}-#{Regexp.last_match(3)}-*-#{Regexp.last_match(4).to_s.tr("/", "-")}").first
          raise "Could not found: _posts/#{Regexp.last_match(2)}-#{Regexp.last_match(3)}-*-#{Regexp.last_match(4).to_s.tr("/", "-")}" if post_file.nil?
          " href=#{quote}{{ site.baseurl }}{% post_url #{File.basename(post_file, ".html")} %}#{quote}"
        end

        File.open(filename, "w") do |f|
          f.flock(File::LOCK_EX)
          f << body
        end
      end
    end
  end
end

.process(options) ⇒ Object

Process the import.

source

a local file String (or IO object for internal use purpose)..

no-blogger-info

a boolean if not leave blogger info (id and original URL).

replace-internal-link

a boolean if replace internal link

Returns nothing.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jekyll-import/importers/blogger.rb', line 39

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

  listener = BloggerAtomStreamListener.new

  listener.leave_blogger_info = !options.fetch("no-blogger-info", false),
  listener.comments = options.fetch("comments", false),

  File.open(source, "r") do |f|
    f.flock(File::LOCK_SH)
    REXML::Parsers::StreamParser.new(f, listener).parse
  end

  options["original-url-base"] = listener.original_url_base

  postprocess(options)
end

.require_depsObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-import/importers/blogger.rb', line 19

def self.require_deps
  JekyllImport.require_with_fallback(%w(
    rexml/document
    rexml/streamlistener
    rexml/parsers/streamparser
    uri
    time
    fileutils
    safe_yaml
    open-uri
  ))
end

.specify_options(c) ⇒ Object



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

def self.specify_options(c)
  c.option "source", "--source NAME", "The XML file (blog-MM-DD-YYYY.xml) path to import"
  c.option "no-blogger-info", "--no-blogger-info", "not to leave blogger-URL info (id and old URL) in the front matter (default: false)"
  c.option "replace-internal-link", "--replace-internal-link", "replace internal links using the post_url liquid tag. (default: false)"
  c.option "comments", "--comments", "import comments to _comments collection"
end

.validate(options) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/jekyll-import/importers/blogger.rb', line 11

def self.validate(options)
  if options["source"].nil?
    raise "Missing mandatory option: --source"
  elsif !File.exist?(options["source"])
    raise Errno::ENOENT, "File not found: #{options["source"]}"
  end
end