2008/07/26

Perlのコードをハイライトしてみる

Blogger Syntax Highlighterウィジェットをちょっと弄ってみました。

まぁ、やったことはウィジェット (JavaScript) の最後の方にあるRubyを処理する部分をコピーして、次のページを参考に内容を書き換えてRubyを処理する部分の下に追加しただけなんですけど。




正規表現がよくわかんないので、たぶんPerlの変数を上手く引っ掛けられないと思いますが、今後直そうと思います。

テストで表示させるのに "Hello, world!" ではつまらないしPerlの変数も出てこないので、オープンストリートマップで国土数値情報インポート作業用の資料を作る際に、ファイルをソートするために使った短いスクリプトが残っていたので、それにしました。

preタグに class="perl" を指定して、果たして上手くいくのか...

#!/usr/bin/perl

#####
#
# データ調査用スクリプト UTF8ファイルのSORT
#
# KSJ2 Railway Data 2007 - EB02 (Railway Line)
# 国土数値情報(鉄道データ)平成19年 国土交通省 - EB02 (鉄道路線)
#
# Files
#   Input : N02-07_EB02-r1.osm
#   Output : N02-07_EB02-r1-sort.osm
#    
#####


use strict;
use warnings;
use encoding "utf8";
use Encode;
use open IO => "utf8";


sub main() {

 open(IN, "<N02-07_EB02-r1.osm");
 open(OUT, ">N02-07_EB02-r1-sort.osm");

 my $i = 0;
 my @file = <IN>;
 my @array = sort @file;

 foreach my $line(@array) {
   print OUT $line;
   $i++;
 }

 close IN;
 close OUT;

 printf "Output %d records\n", $i;   

}




# run this script.

main();





うーん、やっと出た。

予想通り最初は正規表現の所がおかしくて、JavaScriptの実行自体がエラーになり、全くのプレーンテキストしか表示されませんでした。その後、キーワードや関数の名前を登録している部分、それに対応するCSSなども修正。これからも少しづつ直す所が出てきそうな気がします。

でも、とりあえず今の時点で動いているものを貼っておきます。

dp.sh.Brushes.Perl=function()
{var keywords='break continue do elsif else for foreach goto if last next return sub undef unless '+'until while';var builtins='abs accept alarm atan2 bind binmode bless caller chdir chmod chomp chop chown '+'chr chroot close closedir connect continue cos crypt dbmclose dbmopen defined '+'delete die do dump each eof eval exec exists exit exp fcntl fileno flock fork '+'format formline getc getlogin getpeername getpgrp getppid getpriority getpwnam '+'getgrnam gethostbyname getnetbyname getprotobyname getpwuid getgrgid '+'getservbyname gethostbyaddr getnetbyaddr getprotobynumber getservbyport '+'getpwent getgrent gethostent getnetent getprotoent getservent setpwent '+'setgrent sethostent setnetent setprotoent setservent endpwent endgrent '+'endhostent endnetent endprotoent endservent getsockname getsockopt glob gmtime '+'goto grep hex import index int ioctl join keys kill last lc lcfirst length '+'link listen local localtime log lstat m map mkdir msgctl msgget msgsnd msgrcv '+'my next no oct open opendir ord our pack package pipe pop pos print printf '+'prototype push q qq qr qx qw quotemeta rand read readdir readline readlink '+'readpipe recv redo ref rename require reset return reverse rewinddir rindex '+'rmdir s scalar seek seekdir select semctl semget semop send setpgrp '+'setpriority setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep '+'socket socketpair sort splice split sprintf sqrt srand stat study substr '+'symlink syscall sysopen sysread sysseek system syswrite tell telldir tie tied '+'time times tr truncate uc ucfirst umask undef unlink unpack untie unshift use '+'utime values vec wait waitpid wantarray warn write y'
this.regexList=[{regex:dp.sh.RegexLib.SingleLinePerlComments,css:'comment'},{regex:dp.sh.RegexLib.DoubleQuotedString,css:'string'},{regex:dp.sh.RegexLib.SingleQuotedString,css:'string'},{regex:new RegExp(':[a-z][A-Za-z0-9_]*','g'),css:'symbol'},{regex:new RegExp('(\\$|@|%|\\*)\\w+','g'),css:'variable'},{regex:new RegExp(this.GetKeywords(keywords),'gm'),css:'keyword'},{regex:new RegExp(this.GetKeywords(builtins),'gm'),css:'builtin'}];this.CssClass='dp-pl';this.Style='.dp-pl .symbol { color: #a70; }'+'.dp-pl .variable { color: #a70; font-weight: bold; }'+'.dp-pl .builtin { color: #069; font-weight: bold; }';}
dp.sh.Brushes.Perl.prototype=new dp.sh.Highlighter();dp.sh.Brushes.Perl.Aliases=['perl'];


桁番号表示のオプションは TeraPad みたいなものを勝手に想像していたので、ちょっとガッカリ。コードを見るにはメニューにある + expand source のリンクをクリックして下さい。

私はこれをウィジェット (JavaScript) の最後の方にある ['ruby','rails','ror']; で終わっている行の次に追加しています。

元のsyntaxhighlighterのページには"Code License: GNU Lesser General Public License"と書いてあるので、この部分もGLGPLということかな?  よくわからんけど...

2008年7月28日 追記

Perl対応部分を組み込んだウィジェットの追加ボタンを作成してみました。

おかげで、[設定] → [フォーマット] → 改行の変換を「いいえ」にして、自分でbrタグを入力して改行させなければいけなくなりました。「はい」だとウィジェットの中に在る改行までbrタグに変換されてしまうので...
Atom