root/misc/runpugs/perl/pugs_example.pl

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
2use strict;
3use JSON;
4use CGI qw(:standard);
5use LWP::UserAgent;
6
7my $cgi = new CGI;
8my $json = new JSON;
9
10my $ok = 1;
11my $reason = '';
12
13# get 'source' parameter...
14my $source = param('source');
15# for testing...
16##$source = 'games/tic_tac_toe4.pl';
17
18if(!$source) {
19    $ok = 0;
20    $reason = "The 'source' parameter is missing";
21} else {
22    #filter source from '..'
23    $source =~ s/\.\.//g;
24}
25
26unless($source =~ /\.pl$/) {
27    $ok = 0;
28    $reason = "The 'source' parameter should end in .pl";
29}
30
31my $content = undef;
32if($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...
46my $obj = {
47    'ok'        => $ok,
48    'reason'    => $reason,
49    'source'    => $source,
50    'content'   => $content
51};
52my $js  = objToJson($obj);
53
54
55# write back response
56print header( -TYPE => 'text/plain'),  $js;
57
58# load file from Url
59sub 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}
Note: See TracBrowser for help on using the browser.