Skip to content

Instantly share code, notes, and snippets.

@tomykaira
Created March 2, 2012 08:50
Show Gist options
  • Save tomykaira/1956916 to your computer and use it in GitHub Desktop.
Save tomykaira/1956916 to your computer and use it in GitHub Desktop.
Convert markdown to hiki / markdown テキストから hiki 形式に変換するスクリプト(適当)
# -*- 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
@sowawa
Copy link

sowawa commented Sep 11, 2012

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment