class JekyllImport::Importers::Blogger
Public Class Methods
postprocess(options)
click to toggle source
Post-process after import.
- replace-internal-link
-
a boolean if replace internal link
Returns nothing.
# 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)
click to toggle source
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.
# 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_deps()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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