]> matita.cs.unibo.it Git - helm.git/blob - helm/www/matita/cgi-bin/hl.cgi
proof of concept implementation of scripts highlighting, grafite.hrc is still bugged
[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 my $hlformat = "grafite";
37
38 # Code
39   
40 my $query = CGI->new;                      # used globally by some 'sub' below
41 print $query->header, $query->start_html;
42
43 sub die_invalid_file() {
44   print 'Invalid script.';
45   print $query->end_html;
46   exit 0;
47 }
48
49 sub lookup_script($) {
50   my ($f) = @_;
51   die_invalid_file() unless $f =~ /^(.*)(\.ma)$/;
52   my $base = $1;
53   die_invalid_file() unless $base =~ /[-a-zA-Z_]+(\/[-a-zA-Z_])*/;
54   my $path = $scriptsdir . $f;
55   die_invalid_file() unless -f $path;
56   return $path;
57 }
58
59 my $fname = $query->param('f');
60
61 my $highlighter = Syntax::Highlight::Universal->new;
62 $highlighter->addConfig($protohrc);
63
64 my $script = lookup_script($fname);
65 open SCRIPT, "< $script" or die "Can't open Matita script \"$script\"\n";
66 my @lines = <SCRIPT>;
67 my $text = join "", @lines;
68 close SCRIPT;
69
70 my $result = $highlighter->highlight($hlformat, $text);
71 print $result;
72
73 print $query->end_html;
74