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.



59
60
61
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
# File 'lib/jekyll-import/importers/blogger.rb', line 59

def self.postprocess(options)
  # Replace internal link URL
  return unless options.fetch("replace-internal-link", false)

  original_url_base = options.fetch("original-url-base", nil)
  return unless 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

.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.



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

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



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

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



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

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. (default: false)"
end

.validate(options) ⇒ Object

Raises:

  • (Errno::ENOENT)


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

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