viewsource.pl (Plain text version)

#!/usr/bin/perl
use PerlHP;

<%
	use strict;

	our ($plain);

	my $file=$ENV{PATH_INFO};
	$file=~s!^/+!!;
	die "No haxoring!" if($file=~m!(?:\.\.|//)!);

	open FILE,$file or die "No such file";
	my $source=join "",<FILE>;
	close FILE;

	if($plain)
	{
		header("Content-Type: text/plain");
		print $source;
		exit 0;
	}

	$file=escape_html($file);
	$source=escape_html($source);

	$source=~s{(.*?)(?:(&lt;([\%\?])(?:perl|=|))(\s.*?)?(\3&gt;)|$)}{
		my ($html,$start,$code,$end)=($1,$2,$4,$5);

		$html=~s{(&lt;/?)(\w+)(.*?)(&gt;)}{
			my ($start,$tagname,$attrs,$end)=($1,$2,$3,$4);

			$attrs=~s{(\w+\s*)=(\s*(?:(&quot;|').*?\3|\S+))}{<span class="tagattr">$1</span>=<span class="tagvalue">$2</span>}sg;
			"<span class=\"tag\">$start<span class=\"tagname\">$tagname</span>$attrs$end</span>"
		}sge;

		$code=~s{(?:((&quot;|').*?(?<!\\)\2)|((?<!&)#[^\n]*)|([\$\@\%]\w+))}
		{
			my ($string,$comment,$var)=($1,$3,$4);

			if(length $string) { "<span class=\"codestring\">$string</span>" }
			elsif(length $comment) { "<span class=\"codecomment\">$comment</span>" }
			else { "<span class=\"codevar\">$var</span>" }
		}sge;

		"<span class=\"html\">$html</span><span class=\"code\">$start$code$end</span>"
	}sgei;
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><% print $file %></title>

<style type="text/css">
h1 { background:#eef; font-family: sans-serif; font-size: 1.5em; font-weight: bold; border-bottom:2px solid #ccf }
h1 small { font-size: 0.5em; font-weight: normal; margin-left: 1em }
a { color: #f83; }
a:visited { color: #d72; }
a:hover { color: #fff; background: #f83; text-decoration: none; }
pre { border:1px dashed #99f; padding:1em; }
.tag { }
.tagname { color: #22f }
.tagattr { color: #007 }
.tagvalue { color: #77f }
.code { background: #fff6d7 }
.codecomment { color: #876 }
.codestring { color: #a60 }
.codevar { color: #a20 }
</style>

</head><body>

<h1><% print $file %> <small>(<a href="<% print $file %>?plain=1">Plain text version</a>)</small></h1>

<pre><% print $source %></pre>

</body>
</html>