Class: JekyllImport::Importers::Blogger
- Inherits:
-
JekyllImport::Importer
- Object
- JekyllImport::Importer
- JekyllImport::Importers::Blogger
- Defined in:
- lib/jekyll-import/importers/blogger.rb
Defined Under Namespace
Modules: BloggerAtomStreamListenerMethods Classes: BloggerAtomStreamListener
Class Method Summary collapse
-
.postprocess(options) ⇒ Object
Post-process after import.
-
.process(options) ⇒ Object
Process the import.
- .require_deps ⇒ Object
- .specify_options(c) ⇒ Object
- .validate(options) ⇒ Object
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() # Replace internal link URL return unless .fetch("replace-internal-link", false) original_url_base = .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() source = .fetch("source") listener = BloggerAtomStreamListener.new listener.leave_blogger_info = !.fetch("no-blogger-info", false) listener.comments = .fetch("comments", false) File.open(source, "r") do |f| f.flock(File::LOCK_SH) REXML::Parsers::StreamParser.new(f, listener).parse end ["original-url-base"] = listener.original_url_base postprocess() end |
.require_deps ⇒ Object
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.(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
13 14 15 16 |
# File 'lib/jekyll-import/importers/blogger.rb', line 13 def self.validate() raise "Missing mandatory option: --source" if ["source"].nil? raise Errno::ENOENT, "File not found: #{["source"]}" unless File.exist?(["source"]) end |