Skip to content

Instantly share code, notes, and snippets.

@mdaisuke
Forked from tomykaira/markdown-to-hiki.rb
Created December 18, 2013 08:06
Show Gist options
  • Save mdaisuke/8018849 to your computer and use it in GitHub Desktop.
Save mdaisuke/8018849 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
ARGV.each do |fn|
lines = File.readlines(fn)
hiki = lines.map { |l|
l.sub(/^(#+)\s/){ '!'*($1.length) + ' ' }.
sub(/^(-+)\s/){ '*'*($1.length) + ' ' }.
gsub(/\!\[([^\]]+)\]\(([^\)]+)\)/){ $2[0..3] == 'http' ? $2 : ('http://goos-lokka.heroku.com' + $2) }. # picture
gsub(/\[([^\]]+)\]\(([^\)]+)\)/, '[[\1|\2]]'). # url
gsub(/\*\*([^*]*)\*\*/, "'''\\1'''").
sub(/^ /, ' ').
gsub(/`([^`]+)`/, '\'\'\1\'\'').
gsub('<span style="color:green">', 'Green: ').
gsub('<span style="color:red">', 'Red: ').
gsub(/<span style="color:.*">/, '').
gsub('</span>', '')
}
# コードブロック中の空行を修正
hiki.each_with_index { |l, idx|
if l == "\n" && hiki[idx-1].match(/^\s/) && hiki.size > idx+1 && hiki[idx+1].match(/^\s/)
hiki[idx] = " \n"
end
}
hiki_fn = fn.sub(/\.md$/, '.hiki')
open(hiki_fn, "w") { |f| f.write(hiki.join) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment