class BuntoImport::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/bunto-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 = $1 post_file = Dir.glob("_posts/#{$2}-#{$3}-*-#{$4.to_s.tr('/', '-')}").first raise "Could not found: _posts/#{$2}-#{$3}-*-#{$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)
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/bunto-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_deps()
click to toggle source
# File lib/bunto-import/importers/blogger.rb, line 19 def self.require_deps BuntoImport.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/bunto-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)
click to toggle source
# File lib/bunto-import/importers/blogger.rb, line 11 def self.validate(options) if options['source'].nil? raise 'Missing mandatory option: --source' elsif not File.exist?(options['source']) raise Errno::ENOENT, "File not found: #{options['source']}" end end