Skip to content

Instantly share code, notes, and snippets.

@dnagir
Forked from baldowl/import.rb
Created February 8, 2012 04:41

Revisions

  1. dnagir revised this gist Feb 8, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions import.rb
    Original file line number Diff line number Diff line change
    @@ -119,13 +119,16 @@ def header
    [
    '---',
    %{layout: post},
    %{title: "#{title}"},
    %{title: "#{escaped title}"},
    %{date: #{creation_datetime}},
    %{comments: false},
    categories,
    '---'
    ].compact.join("\n")
    end

    def escaped(str)
    str.gsub('"') { '\"' }
    end

    def categories
    terms = @node.search('category[scheme="http://www.blogger.com/atom/ns#"]')
    @@ -174,4 +177,4 @@ def content

    @drafts.each do |id, post|
    write post, '_drafts'
    end
    end
  2. @baldowl baldowl revised this gist Jan 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion import.rb
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,7 @@ def creation_date
    end

    def creation_datetime
    @creation_datetime ||= Date.parse(@node.search('published').first.content)
    @creation_datetime ||= DateTime.parse(@node.search('published').first.content)
    end

    def permalink
  3. @juniorz juniorz revised this gist Jan 5, 2012. 1 changed file with 71 additions and 21 deletions.
    92 changes: 71 additions & 21 deletions import.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    require 'nokogiri'
    require 'fileutils'
    require 'date'
    require 'uri'

    # usage: ruby import.rb my-blog.xml
    # my-blog.xml is a file from Settings -> Basic -> Export in blogger.
    @@ -10,13 +11,18 @@
    doc = Nokogiri::XML(data)

    @posts = {}
    @drafts = {}

    def add(node)
    id = node.search('id').first.content
    type = node.search('category').first.attr('term').split('#').last
    case type
    when 'post'
    @posts[id] = Post.new(node)
    if published?(node)
    @posts[id] = Post.new(node)
    else
    @drafts[id] = Post.new(node)
    end
    when 'comment'
    reply_to = node.children.find {|c| c.name == 'in-reply-to' }
    post_id = reply_to.attr('ref')
    @@ -28,30 +34,38 @@ def add(node)
    end
    end

    def write(post)
    def published?(node)
    node.at_css('app|control app|draft', 'app' => 'http://purl.org/atom/app#').nil?
    end

    def write(post, path='_posts')
    puts "Post [#{post.title}] has #{post.comments.count} comments"

    puts "writing #{post.file_name}"
    File.open(File.join('_posts', post.file_name), 'w') do |file|
    File.open(File.join(path, post.file_name), 'w') do |file|
    file.write post.header
    file.write "\n\n"
    file.write "<h1>{{ page.title }}</h1>\n"
    #file.write "<h1>{{ page.title }}</h1>\n"
    file.write "<div class='post'>\n"
    file.write post.content
    file.write "</div>\n"
    file.write "<h2>Comments</h2>\n"
    file.write "<div class='comments'>\n"
    post.comments.each do |comment|
    file.write "<div class='comment'>\n"
    file.write "<div class='author'>"
    file.write comment.author
    file.write "</div>\n"
    file.write "<div class='content'>\n"
    file.write comment.content
    file.write "</div>\n"

    unless post.comments.empty?
    file.write "<h2>Comments</h2>\n"
    file.write "<div class='comments'>\n"
    post.comments.each do |comment|
    file.write "<div class='comment'>\n"
    file.write "<div class='author'>"
    file.write comment.author
    file.write "</div>\n"
    file.write "<div class='content'>\n"
    file.write comment.content
    file.write "</div>\n"
    file.write "</div>\n"
    end
    file.write "</div>\n"
    end
    file.write "</div>\n"

    end
    end

    @@ -67,23 +81,37 @@ def add_comment(comment)
    end

    def title
    @node.search('title').first.content
    @title ||= @node.at_css('title').content
    end

    def content
    @node.search('content').first.content
    @content ||= @node.at_css('content').content
    end

    def creation_date
    creation_datetime.strftime("%Y-%m-%d")
    @creation_date ||= creation_datetime.strftime("%Y-%m-%d")
    end

    def creation_datetime
    Date.parse(@node.search('published').first.content)
    @creation_datetime ||= Date.parse(@node.search('published').first.content)
    end

    def permalink
    return @permalink unless @permalink.nil?

    link_node = @node.at_css('link[rel=alternate]')
    @permalink = link_node && link_node.attr('href')
    end

    def param_name
    if permalink.nil?
    title.split(/[^a-zA-Z0-9]+/).join('-').downcase
    else
    File.basename(URI(permalink).path, '.*')
    end
    end

    def file_name
    param_name = title.split(/[^a-zA-Z0-9]+/).join('-').downcase
    %{#{creation_date}-#{param_name}.html}
    end

    @@ -94,8 +122,19 @@ def header
    %{title: "#{title}"},
    %{date: #{creation_datetime}},
    %{comments: false},
    categories,
    '---'
    ].join("\n")
    ].compact.join("\n")
    end

    def categories
    terms = @node.search('category[scheme="http://www.blogger.com/atom/ns#"]')
    unless Array(terms).empty?
    [
    'categories:',
    terms.map{ |t| t.attr('term') && " - #{t.attr('term')}" }.compact.join("\n"),
    ].join("\n")
    end
    end
    end

    @@ -119,9 +158,20 @@ def content
    add entry
    end

    puts "** Writing PUBLISHED posts"
    FileUtils.rm_rf('_posts')
    Dir.mkdir("_posts") unless File.directory?("_posts")

    @posts.each do |id, post|
    write post
    end

    puts "\n"
    puts "** Writing DRAFT posts"

    FileUtils.rm_rf('_drafts')
    Dir.mkdir("_drafts") unless File.directory?("_drafts")

    @drafts.each do |id, post|
    write post, '_drafts'
    end
  4. @juniorz juniorz revised this gist Jan 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion import.rb
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,7 @@ def header
    [
    '---',
    %{layout: post},
    %{title: #{title}},
    %{title: "#{title}"},
    %{date: #{creation_datetime}},
    %{comments: false},
    '---'
  5. @ngauthier ngauthier revised this gist Dec 26, 2011. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions import.rb
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ def initialize(node)
    end

    def add_comment(comment)
    @comments << comment
    @comments.unshift comment
    end

    def title
    @@ -83,7 +83,8 @@ def creation_datetime
    end

    def file_name
    %{#{creation_date}-#{title.split(/\s+/).join('-').delete('\/')}.html}
    param_name = title.split(/[^a-zA-Z0-9]+/).join('-').downcase
    %{#{creation_date}-#{param_name}.html}
    end

    def header
    @@ -123,4 +124,4 @@ def content

    @posts.each do |id, post|
    write post
    end
    end
  6. @ngauthier ngauthier created this gist Dec 21, 2011.
    126 changes: 126 additions & 0 deletions import.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    require 'rubygems'
    require 'nokogiri'
    require 'fileutils'
    require 'date'

    # usage: ruby import.rb my-blog.xml
    # my-blog.xml is a file from Settings -> Basic -> Export in blogger.

    data = File.read ARGV[0]
    doc = Nokogiri::XML(data)

    @posts = {}

    def add(node)
    id = node.search('id').first.content
    type = node.search('category').first.attr('term').split('#').last
    case type
    when 'post'
    @posts[id] = Post.new(node)
    when 'comment'
    reply_to = node.children.find {|c| c.name == 'in-reply-to' }
    post_id = reply_to.attr('ref')
    #post_id = node.search('thr').first.attr('ref')
    @posts[post_id].add_comment(Comment.new(node))
    when 'template', 'settings'
    else
    raise 'dunno '+type
    end
    end

    def write(post)
    puts "Post [#{post.title}] has #{post.comments.count} comments"

    puts "writing #{post.file_name}"
    File.open(File.join('_posts', post.file_name), 'w') do |file|
    file.write post.header
    file.write "\n\n"
    file.write "<h1>{{ page.title }}</h1>\n"
    file.write "<div class='post'>\n"
    file.write post.content
    file.write "</div>\n"
    file.write "<h2>Comments</h2>\n"
    file.write "<div class='comments'>\n"
    post.comments.each do |comment|
    file.write "<div class='comment'>\n"
    file.write "<div class='author'>"
    file.write comment.author
    file.write "</div>\n"
    file.write "<div class='content'>\n"
    file.write comment.content
    file.write "</div>\n"
    file.write "</div>\n"
    end
    file.write "</div>\n"
    end
    end

    class Post
    attr_reader :comments
    def initialize(node)
    @node = node
    @comments = []
    end

    def add_comment(comment)
    @comments << comment
    end

    def title
    @node.search('title').first.content
    end

    def content
    @node.search('content').first.content
    end

    def creation_date
    creation_datetime.strftime("%Y-%m-%d")
    end

    def creation_datetime
    Date.parse(@node.search('published').first.content)
    end

    def file_name
    %{#{creation_date}-#{title.split(/\s+/).join('-').delete('\/')}.html}
    end

    def header
    [
    '---',
    %{layout: post},
    %{title: #{title}},
    %{date: #{creation_datetime}},
    %{comments: false},
    '---'
    ].join("\n")
    end
    end

    class Comment
    def initialize(node)
    @node = node
    end

    def author
    @node.search('author name').first.content
    end

    def content
    @node.search('content').first.content
    end
    end

    entries = {}

    doc.search('entry').each do |entry|
    add entry
    end

    FileUtils.rm_rf('_posts')
    Dir.mkdir("_posts") unless File.directory?("_posts")

    @posts.each do |id, post|
    write post
    end