Slight improvements to URL and email address parsing.
[sw-tools] / perl / SWInfo.pm
1 # -*-perl-*-
2 #
3 # $Id: SWInfo.pm,v 1.2 1999/08/18 17:10:07 mdw Exp $
4 #
5 # Read and output GNU Info files
6 #
7 # (c) 1999 EBI
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of sw-tools.
13 #
14 # sw-tools is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # sw-tools is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with sw-tools; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 #----- Revision history -----------------------------------------------------
29 #
30 # $Log: SWInfo.pm,v $
31 # Revision 1.2 1999/08/18 17:10:07 mdw
32 # Slight improvements to URL and email address parsing.
33 #
34 # Revision 1.1 1999/07/30 18:46:37 mdw
35 # New CGI script for browsing installed software and documentation.
36 #
37
38 #----- Package preamble -----------------------------------------------------
39
40 package SWInfo;
41
42 use IO;
43
44 use SWConfig;
45 use SWCGI qw(:DEFAULT :layout);
46 use SWMan;
47 use Info;
48
49 #----- Useful functions -----------------------------------------------------
50
51 # --- @subst(IREF, FILE, INFO)@ ---
52 #
53 # Given an Info reference and the name of the current Info file, returns an
54 # HTML anchor which represents the link.
55
56 sub subst($$$) {
57 my ($iref, $file, $i) = @_;
58 my $node;
59 my $dir;
60 my $tail = "";
61
62 # --- Dig out the node and file being referred to ---
63
64 if ($iref =~ /:$/) {
65 $tail = ":";
66 $iref = $`;
67 }
68 my $oref = $iref;
69 $iref =~ s/\s+/ /g;
70 if ($iref =~ /^.+: *(.+)$/) { $iref = $1; }
71 if ($iref =~ /(?:\(([^\)]*)\))?(.*)$/) {
72 $file = $1 || $file;
73 $node = $2 || "Top";
74 } else {
75 $node = $iref;
76 }
77
78 # --- Transform it into something that won't get mangled ---
79
80 $node =~ s/[+&=%]|[^ -~]/sprintf("%%%02x", ord($&))/eg;
81 $node =~ tr/ /+/;
82
83 ($dir = $i->{dir}) =~ s:$C{prefix}/info/?::;
84 $dir = "&dir=$dir" if $dir;
85
86 return "<a href=\"$ref?act=info&file=$file&node=$node$dir\">$oref</a>$tail";
87 }
88
89 #----- Actions --------------------------------------------------------------
90
91 sub info {
92 my $file = $Q{file} || "dir";
93 my $node = $Q{node} || "Top";
94 my $dir = $Q{dir} || "";
95 my $out;
96
97 # --- Read the node in ---
98
99 Info::setpath("$C{prefix}/info");
100
101 "$dir/$file" =~ m:\./: and
102 barf("bad filename `$dir/$file'");
103 my $i = (($dir && Info->new("$dir/$file")) ||
104 Info->new($file))
105 or barf("couldn't find info file `$file'");
106 my $n = $i->node($node) or
107 barf("info file `$file' doesn't contain node `$node'");
108
109 # --- Now translate the node into HTML, first line first ---
110
111 $n =~ s/\&/&amp;/;
112 $n =~ s/\</&lt;/;
113 $n =~ s/\>/&gt;/;
114 $n =~ s/\A( [^\n]* Next:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
115 $n =~ s/\A( [^\n]* Prev:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
116 $n =~ s/\A( [^\n]* Up:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
117
118 # --- Grind through picking up any notes ---
119
120 $out = "";
121
122 for (;;) {
123 if ($n =~ /(\*Note\s*)([^:]*: *(?:\([^\)]*\))?[^.,;:]*)([.,;:])/i) {
124 $out .= $` . $1 . subst($2, $file, $i) . $3;
125 $n = $';
126 } else {
127 last;
128 }
129 }
130
131 # --- If there's a menu then process that ---
132
133 if ($n =~ /\n\* *Menu:/s) {
134 $out .= $` . $&;
135 $n = $';
136 for (;;) {
137 if ($n =~ /(\n\* *)([^:]*: *(?:\([^\)]*\))?[^.,;:]*)([.,;:])/) {
138 $out .= $` . $1 . subst($2, $file, $i) . $3;
139 $n = $';
140 } else {
141 last;
142 }
143 }
144 }
145 $out .= $n;
146
147 $out =~ s!\b(https?|ftp|file|news):[^]&)\s]*[^]&).,\s\'\"]!<a href="$&">$&</a>!g;
148 $out =~ s!(?:\bmailto:)?([^\s()&;:{}.,\`\"][^\s()&;:{}\`\"]*\@[^\s()&;:{}\'\"]*[^\s()&;:{}.,\'\"])!<a href="mailto:$1">$&</a>!g;
149 $out =~ s!([-_.\w]+)\((\d+\w*)\)!SWMan::subst("$1($2)", $1, $2)!eg;
150
151 header("Info: ($file)$node");
152 print("<pre>\n$out</pre>\n");
153 footer();
154 }
155
156 #----- Actions provided -----------------------------------------------------
157
158 $main::ACT{"info"} = \&info;
159
160 #----- That's all, folks ----------------------------------------------------