はてなダイアリーでソースコードを色付けする
https://dream.ie.ariake-nct.ac.jp/~bashi/d/?date=20051010#p01を参考にして、ソースコードをはてなダイアリー用に色付けするツールcode2hatena.rbを作りました。これを使うと、
./code2hatena.rb code2hatena.rb
以下の様になります。
#!/usr/local/bin/ruby $LOAD_PATH.unshift('.') require 'langscan' Highlight = { :fundef => '0000FF', :funcall => '0000FF', :classdef => '006600', :moduledef => '00CC00', :comment => '009900', :string => 'CC0000', :keyword => '0000FF', :const => '0099FF', :character => 'CC0000', :type => '0099FF', } def codelist(file) scanner = LangScan.choose(file) scanner = LangScan::Text unless scanner cl(File.open(file).readlines.join, scanner) end def cl(content, scanner = LangScan::Text) contents = [] scanner.scan(content) {|f| text = if Highlight.keys.include?(f.type) color = Highlight[f.type] "#{color}\">#{f.text}" else f.text end f.text = text contents.push(f) } contents.sort! {|a, b| a.byteno <=> b.byteno } ret = ">|\n" contents.each {|f| ret << f.text } ret << "|<\n" return ret end if __FILE__ == $0 ARGV.each do |file| puts codelist(file) end end # vim: tabstop=2 shiftwidth=2 expandtab
http://d.hatena.ne.jp/SumiTomohiko/20061212/1165938241のエントリーも、このツールで色付けしました。大分、見易くなったのではないかと思います。ただし、タグがたくさんできるので、記事の編集は大変になります。色の調節などは、おいおいやっていきます。
このスクリプトは、http://nekomimists.ddo.jp/~tom/tools/code2hatena.rbからダウンロードできます。また、実行にはlangscanが必要です。