|
Revision 18101, 1.5 kB
(checked in by azawawi, 14 months ago)
|
|
[runpugs] added ':show' command to show 100< lines syntax highlighter examples
[runpugs] pugs_example.pl shows only things under svn.pugs.code/pugs/examples
[p6_syntax_hilite] project p6_syntax_highlighter is now moved here
|
-
Property svn:executable set to
*
|
| Line | |
|---|
| 1 | #!/usr/bin/perl |
|---|
| 2 | use strict; |
|---|
| 3 | use JSON; |
|---|
| 4 | use CGI qw(:standard); |
|---|
| 5 | use LWP::UserAgent; |
|---|
| 6 | |
|---|
| 7 | my $cgi = new CGI; |
|---|
| 8 | my $json = new JSON; |
|---|
| 9 | |
|---|
| 10 | my $ok = 1; |
|---|
| 11 | my $reason = ''; |
|---|
| 12 | |
|---|
| 13 | # get 'source' parameter... |
|---|
| 14 | my $source = param('source'); |
|---|
| 15 | # for testing... |
|---|
| 16 | ##$source = 'games/tic_tac_toe4.pl'; |
|---|
| 17 | |
|---|
| 18 | if(!$source) { |
|---|
| 19 | $ok = 0; |
|---|
| 20 | $reason = "The 'source' parameter is missing"; |
|---|
| 21 | } else { |
|---|
| 22 | #filter source from '..' |
|---|
| 23 | $source =~ s/\.\.//g; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | unless($source =~ /\.pl$/) { |
|---|
| 27 | $ok = 0; |
|---|
| 28 | $reason = "The 'source' parameter should end in .pl"; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | my $content = undef; |
|---|
| 32 | if($ok) { |
|---|
| 33 | # load source file |
|---|
| 34 | my $url = "http://svn.pugscode.org/pugs/examples/" . $source; |
|---|
| 35 | my ($errtext,$text) = &loadFileFromUrl($url); |
|---|
| 36 | |
|---|
| 37 | if($errtext) { |
|---|
| 38 | $ok = 0; |
|---|
| 39 | $reason = $errtext; |
|---|
| 40 | } else { |
|---|
| 41 | $content = $text; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # convert to json... |
|---|
| 46 | my $obj = { |
|---|
| 47 | 'ok' => $ok, |
|---|
| 48 | 'reason' => $reason, |
|---|
| 49 | 'source' => $source, |
|---|
| 50 | 'content' => $content |
|---|
| 51 | }; |
|---|
| 52 | my $js = objToJson($obj); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | # write back response |
|---|
| 56 | print header( -TYPE => 'text/plain'), $js; |
|---|
| 57 | |
|---|
| 58 | # load file from Url |
|---|
| 59 | sub loadFileFromUrl($) |
|---|
| 60 | { |
|---|
| 61 | my $url = shift; |
|---|
| 62 | my $content = undef; |
|---|
| 63 | my $errtext = undef; |
|---|
| 64 | # $.get(url) |
|---|
| 65 | my $ua = LWP::UserAgent->new; |
|---|
| 66 | $ua->agent("runpugs/0.1"); |
|---|
| 67 | my $req = HTTP::Request->new(GET => $url); |
|---|
| 68 | my $res = $ua->request($req); |
|---|
| 69 | if ($res->is_success) { |
|---|
| 70 | $content = $res->content; |
|---|
| 71 | } else { |
|---|
| 72 | $errtext = $res->status_line; |
|---|
| 73 | } |
|---|
| 74 | # return error text and content |
|---|
| 75 | return ($errtext,$content); |
|---|
| 76 | } |
|---|