]> matita.cs.unibo.it Git - helm.git/blob - helm/www/matita/cgi-bin/hl.cgi
f2a146ccc98355f503cb632f72d25ab60d5ee592
[helm.git] / helm / www / matita / cgi-bin / hl.cgi
1 #!/usr/bin/perl -w
2 use strict;
3
4 # Copyright (C) 2006, HELM Team.
5 #
6 # This file is part of HELM, an Hypertextual, Electronic
7 # Library of Mathematics, developed at the Computer Science
8 # Department, University of Bologna, Italy.
9 #
10 # HELM is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # HELM is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with HELM; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 # MA  02111-1307, USA.
24 #
25 # For details, see the HELM World-Wide-Web page,
26 # http://helm.cs.unibo.it/
27
28 use CGI;
29 use Syntax::Highlight::Universal;
30
31 # Configuration
32
33 my $documentroot = '/projects/matita/public_html/';
34 my $protohrc = $documentroot . 'helm-proto.hrc';
35 my $scriptsdir = $documentroot . 'library/';
36
37 my %formatmap = (
38   '.ma' => 'grafite',
39   '.c' => 'c',  # debug
40 );
41
42 # Code
43   
44 my $query = CGI->new;                      # used globally by some 'sub' below
45 print $query->header(-type=>'text/html', -charset=>'utf-8');
46
47 sub die_invalid_file() {
48   print $query->start_html;
49   print 'Invalid script.';
50   print $query->end_html;
51   exit 0;
52 }
53
54 sub lookup_script($) {
55   my ($f) = @_;
56   die_invalid_file() unless $f =~ /^(.*)(\.[^.]+)$/;
57   my $base = $1;
58   my $extension = $2;
59   die_invalid_file() unless $base =~ /[-a-zA-Z_]+(\/[-a-zA-Z_])*/;
60   die_invalid_file() unless exists $formatmap{$extension};
61   my $path = $scriptsdir . $f;
62   die_invalid_file() unless -f $path;
63   return ($path, $formatmap{$extension});
64 }
65
66 my $fname = $query->param('f');
67
68 my $highlighter = Syntax::Highlight::Universal->new;
69 $highlighter->addConfig($protohrc);
70
71 my ($script, $format) = lookup_script($fname);
72 open SCRIPT, "< $script" or die "Can't open Matita script \"$script\"\n";
73 my @lines = <SCRIPT>;
74 my $text = join '', @lines;
75 close SCRIPT;
76
77 print $query->start_html(-style=>{'src'=>"/$format-format.css"},
78                          -encoding=>'utf-8');
79 my $result = $highlighter->highlight($format, $text);
80 print "<pre>\n";
81 print $result;
82 print "</pre>\n";
83 print $query->end_html;
84