[Orgmode] org batch job using emacsclient?
を見ていたら、emacsclientの --evalオプションを使うといろいろ遊べそう
だなと、いうことでシンタックスハイライトのウェブアプリを書いてみた。
Text::VimColorみたいなことを Emacsでもできないかと思ってたのも
ありますね。
事前準備
Emacsを立ち上げて server-startを実行しておきます。
htmlize.elがある場所に load-pathを通し, htmlizeをrequireしておきます.
ソース
use strict; use warnings; use Cwd; use Plack::Request; use File::Spec; use File::Temp qw(tempfile); sub res_404 { return [404, [ 'Content-Type' => 'text/plain'], [shift]]; } my $current_dir = getcwd(); my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $path = $req->path_info; my $file = $path eq '/' ? File::Spec->catfile($current_dir, __FILE__) : $current_dir . $path; my ($fh, $tmpfile) = tempfile(); my $s_expression = qq{(htmlize-file "$file" "$tmpfile")}; my @cmd = ("emacsclient", "--no-wait", "--eval", $s_expression); my $status = system @cmd; return res_404("emacsclient failed\n") if $status != 0; return [200, ['Content-Type' => 'text/html'], $fh]; };
実行
% plackup app.psgi % firefox http://localost:5000/