# Bloxsom Plugin:TextRef
# Author: Yasuhiro Matsumoto
# Version: 0.1

package textref;
use strict;
my $files_path = "http://mattn.kaoriya.net/files";

sub textref_func {
	my $file = shift;
	$file =~ s/\.\.//g;
	$file =~ s/\/\/+/\//g;
	$file =~ s/^\///g;
	$file =~ m/^(.*?)(\.[^.\/]*)?$/;
	my $html;
	my ($tag, $ext) = ($1, $2);
	if (open(FILE, "$files_path/$file")) {
		my $text = join '', <FILE>;
		$text =~ s/&/&amp;/g;
		$text =~ s/"/&quot;/g;
		$text =~ s/</&lt;/g;
		$text =~ s/>/&gt;/g;
		#$text =~ s/\n/<br>/g;
		close(FILE);
		$html =<<EOF;
<a href="javascript:void(0);" onclick="on_textref_toggle('$tag');">[+] $file<\/a><br>
<div id="$tag" style="display: none; border: black solid 2px; color: black; background-color: #ffffcc;">
<pre style="margin-top: 0px; margin-bottom: 0px;">
$text
</pre>
</div>
EOF
	} else {
$html =<<EOF;
<a href="javascript:void(0);">[-] $file<\/a><br>
EOF
	}
	return $html;
}

sub head {
	my($pkg, $path, $head_ref) = @_;
	if ($blosxom::flavour eq "html" or $blosxom::flavour eq "htm") {
		$$head_ref .= <<EOF;
<script type="text/javascript">
<!--
function on_textref_toggle(id){
	var textref = document.getElementById(id);
	textref.style.display = textref.style.display != 'none' ? 'none' : 'block';
}
//-->
</script>
EOF
	}
	1;
}

sub start {1}

sub story {
	my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
	unless ($blosxom::flavour eq "rss") {
		$$body_ref =~ s@<!-- textref\s*(\S*)?\s*-->@textref_func($1)@ige;
	} else {
		$$body_ref =~ s@<!-- textref\s*(\S*)?\s*-->@...@ig;
	}
	1;
}

1;
