Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.