Re: スーパー pre 記法で実行可能な JavaScript を - IT戦記ã¨ããã¨ã³ããªã¼ã®ã¹ã¼ãã¼pre executableè¨æ³*1ãText::Hatena 0.20ã使ã£ã¦å®è£ ãã¦ã¿ã¾ãããã¤ãã§ã«ã¹ã¼ãã¼preè¨æ³ã§è²ãä»ãããã«ãã¦ã¿ããã©*2失æã
- 以ä¸ã®ã¨ã³ããªã¼ãåèã«ããã¦ãããã¾ããããããã¨ããããã¾ãã
- 使ç¨ä¾
ä½ã£ã¦ã¯ã¿ããã®ã®ãã£ã±ãå®éã«ã¯ã¦ãªã§å®è£ ããããã¨ã¯ãªããããªäºæãèªåã§ä½¿ãåã«ã¯ã¡ãã£ã¨ã¯ä¾¿å©ãããããªãã
package MyParser; use strict; use warnings; use base qw/Text::Hatena/; __PACKAGE__->syntax(q{ block : h5 | h4 | blockquote | dl | list | exec_pre | super_pre | pre | table | cdata | p exec_pre : /\n>\|\w+\|/o exec_line(s) "\n|executable|<" ..."\n" exec_line : ...!"\n|executable|<\n" "\n" /[^\n]*/o }); sub exec_pre { my $class = shift; my $items = shift->{items}; my $texts = $class->expand($items->[1]); my $exec_button = qq/<input type="submit" value="å®è¡" onclick="try{confirm('ãã®ã³ã¼ãã¯å±éºããããã¾ãããä¿¡é ¼ã§ãããµã¤ãã§ããå ´åæå¤ã¯å®è¡ããªãã§ãã ããã')?eval(this.previousSibling.value):void(0);}catch(e){alert(e)}">\n/; return qq(<pre class="executable">\n$texts</pre>\n<textarea style="display: none;">\n(function(){\nalert($texts)})()\n</textarea>$exec_button); } sub exec_line { my $class = shift; my $text = shift->{items}->[2]; return "$text\n"; } sub super_pre { my $class = shift; my $items = shift->{items}; my $texts = _format_plain($class->expand($items->[1])); my ($filetype) = $items->[0] =~ /\n>\|(.*?)\|/; if ($filetype) { eval { require Text::VimColor }; warn $@ if $@; $texts = Text::VimColor->new( string => $texts, filetype => $filetype, )->html; } return qq{<pre class="syntax-highlight">\n$texts</pre>\n}; } sub _format_plain { my $s = shift; $s =~ s/\&/\&/g; $s =~ s/</\</g; $s =~ s/>/\>/g; $s =~ s/"/\"/g; $s =~ s/\'/\'/g; $s =~ s/\\/\\/g; return $s; } 1;