Skip to content

Instantly share code, notes, and snippets.

@chezou
Created December 1, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chezou/fc55d8a142772b1fb42d732a79d6717f to your computer and use it in GitHub Desktop.
Save chezou/fc55d8a142772b1fb42d732a79d6717f to your computer and use it in GitHub Desktop.
hatena blog 2 markdown
#!/usr/bin/env ruby
# Forked from: http://hamasyou.com/blog/2014/02/13/movable-type-to-octopress/
require 'date'
require 'mustache'
require 'stringex'
require 'cgi'
require 'reverse_markdown'
export = File.open(ARGV[0])
target_dir = ARGV[1]
class Post < Mustache
self.template = <<-MARKDOWN
---
title: "{{title}}"
subtitle: ""
summary: ""
authors: ["author name"]
tags: []
categories: {{formatted_category}}
date: {{formatted_date}}
lastmod: {{formatted_date}}
featured: false
draft: false
image:
caption: ""
focal_point: ""
preview_only: false
projects: []
---
{{formatted_body}}
MARKDOWN
attr_accessor :author, :title, :category, :comments, :status, :body
def initialize
@body = []
end
def date=(date)
case date
when String
@date = DateTime.strptime(date, "%m/%d/%Y %H:%M:%S")
else
@date = date
end
end
def date
@date
end
def formatted_categories
if @categories.empty?
"[]"
else
@categories.to_s
end
end
def formatted_date
@date.rfc3339
end
def comments=(comments)
@comments = comments == "1" ? true : false
end
def formatted_body
ReverseMarkdown.convert(CGI.unescapeHTML(body.join).
#gsub(/<a href="(.+?)"(?: target=".+")?>*(.*?)<\/a>/i, '[\2](\1)').
gsub(/<a class="keyword" href="(?:.+?)">(.*?)<\/a>/i, '\1').
gsub("<!--more-->", "\n\n<!-- more -->\n\n"))
end
def file_name
"#{date.strftime("%Y-%m-%d") unless date.nil?}-#{title.to_url unless title.nil?}"
end
end
posts = [Post.new]
KEYWORDS = /(AUTHOR|TITLE|STATUS|COMMENTS|CATEGORY|DATE): (.*)/
def attr_and_value(line)
line =~ KEYWORDS
["#{$1.downcase}=", $2]
end
puts " # Reading from data '#{ARGV[0]}' ..."
puts " # Generating Markdown ..."
comment_section = false
export.each do |line|
if line.strip == "--------"
posts << Post.new
print '.'
comment_section = false
next
end
next if comment_section
case line.strip
when KEYWORDS
posts.last.send(*attr_and_value(line))
when /BODY/
next
when /BASENAME/
next
when /CONVERT BREAKS/
next
when /IMAGE/
next
when "-----"
next
when /COMMENT:/
comment_section = true
next
else
posts.last.body << line
end
end
puts
puts " # Writing files to #{target_dir} ..."
`mkdir -p #{target_dir}`
`cd #{target_dir} && rm -rf _posts && mkdir _posts`
ok, fails = 0, 0
posts.each do |post|
begin
dir_name = File.join(target_dir, "_posts", post.file_name)
Dir.mkdir(dir_name)
File.open(File.join(dir_name, "index.md"), "w+") { |f| f.puts post.render }
puts " -> #{post.file_name}"
ok += 1
rescue
puts " # Could not write file for '#{post.title}' and #{post.file_name}"
fails += 1
end
end
puts
puts " # All done! (#{ok}/#{fails})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment