More improvements to URL recognizer.
[sw-tools] / perl / SWInfo.pm
CommitLineData
961ce1c2 1# -*-perl-*-
2#
fae2108b 3# $Id: SWInfo.pm,v 1.3 1999/08/19 12:11:10 mdw Exp $
961ce1c2 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 $
fae2108b 31# Revision 1.3 1999/08/19 12:11:10 mdw
32# More improvements to URL recognizer.
33#
44b3c589 34# Revision 1.2 1999/08/18 17:10:07 mdw
35# Slight improvements to URL and email address parsing.
36#
961ce1c2 37# Revision 1.1 1999/07/30 18:46:37 mdw
38# New CGI script for browsing installed software and documentation.
39#
40
41#----- Package preamble -----------------------------------------------------
42
43package SWInfo;
44
45use IO;
46
47use SWConfig;
48use SWCGI qw(:DEFAULT :layout);
49use SWMan;
50use Info;
51
52#----- Useful functions -----------------------------------------------------
53
54# --- @subst(IREF, FILE, INFO)@ ---
55#
56# Given an Info reference and the name of the current Info file, returns an
57# HTML anchor which represents the link.
58
59sub subst($$$) {
60 my ($iref, $file, $i) = @_;
61 my $node;
62 my $dir;
63 my $tail = "";
64
65 # --- Dig out the node and file being referred to ---
66
67 if ($iref =~ /:$/) {
68 $tail = ":";
69 $iref = $`;
70 }
71 my $oref = $iref;
72 $iref =~ s/\s+/ /g;
73 if ($iref =~ /^.+: *(.+)$/) { $iref = $1; }
74 if ($iref =~ /(?:\(([^\)]*)\))?(.*)$/) {
75 $file = $1 || $file;
76 $node = $2 || "Top";
77 } else {
78 $node = $iref;
79 }
80
81 # --- Transform it into something that won't get mangled ---
82
83 $node =~ s/[+&=%]|[^ -~]/sprintf("%%%02x", ord($&))/eg;
84 $node =~ tr/ /+/;
85
86 ($dir = $i->{dir}) =~ s:$C{prefix}/info/?::;
87 $dir = "&dir=$dir" if $dir;
88
89 return "<a href=\"$ref?act=info&file=$file&node=$node$dir\">$oref</a>$tail";
90}
91
92#----- Actions --------------------------------------------------------------
93
94sub info {
95 my $file = $Q{file} || "dir";
96 my $node = $Q{node} || "Top";
97 my $dir = $Q{dir} || "";
98 my $out;
99
100 # --- Read the node in ---
101
102 Info::setpath("$C{prefix}/info");
103
104 "$dir/$file" =~ m:\./: and
105 barf("bad filename `$dir/$file'");
106 my $i = (($dir && Info->new("$dir/$file")) ||
107 Info->new($file))
108 or barf("couldn't find info file `$file'");
109 my $n = $i->node($node) or
110 barf("info file `$file' doesn't contain node `$node'");
111
112 # --- Now translate the node into HTML, first line first ---
113
114 $n =~ s/\&/&amp;/;
fae2108b 115 $n =~ s/\</&lt;</;
116 $n =~ s/\>/>&gt;/;
961ce1c2 117 $n =~ s/\A( [^\n]* Next:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
118 $n =~ s/\A( [^\n]* Prev:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
119 $n =~ s/\A( [^\n]* Up:\ *) ([^,\n]*) / $1 . subst($2, $file, $i) /eix;
120
121 # --- Grind through picking up any notes ---
122
123 $out = "";
124
125 for (;;) {
126 if ($n =~ /(\*Note\s*)([^:]*: *(?:\([^\)]*\))?[^.,;:]*)([.,;:])/i) {
127 $out .= $` . $1 . subst($2, $file, $i) . $3;
128 $n = $';
129 } else {
130 last;
131 }
132 }
133
134 # --- If there's a menu then process that ---
135
136 if ($n =~ /\n\* *Menu:/s) {
137 $out .= $` . $&;
138 $n = $';
139 for (;;) {
140 if ($n =~ /(\n\* *)([^:]*: *(?:\([^\)]*\))?[^.,;:]*)([.,;:])/) {
141 $out .= $` . $1 . subst($2, $file, $i) . $3;
142 $n = $';
143 } else {
144 last;
145 }
146 }
147 }
148 $out .= $n;
149
fae2108b 150 # --- Spot URLs (except `mailto') ---
151
152 $out =~ s! \b (http s? | ftp | file | news) :
153 [^]<>)\s]* [^]<>).,\s\']
154 !urlsubst($&, $&)!egx;
155
156 # --- Spot email addresses (including `mailto' URLs) ---
157
158 $out =~ s! (?:\bmailto:)?
159 ([^\s()<>&;:{}.,\`\'\"] [^\s()<>&;:{}\`\'\"]*
160 \@
161 [^\s()<>&;:{}\'\"]* [^\s()<>&;:{}.,\'\"])
162 !<a href="mailto:$1">$&</a>!gx;
163
164 # --- Spot manpage references ---
165
166 $out =~ s! ([-_.\w]+) \( (\d+\w*) \)
167 !SWMan::subst("$1($2)", $1, $2)!egx;
168
169 # --- Fix up the HTML ---
170
171 $out =~ s/\&lt;\</&lt;/g;
172 $out =~ s/\>\&gt;/&gt;/g;
961ce1c2 173
174 header("Info: ($file)$node");
175 print("<pre>\n$out</pre>\n");
176 footer();
177}
178
179#----- Actions provided -----------------------------------------------------
180
181$main::ACT{"info"} = \&info;
182
183#----- That's all, folks ----------------------------------------------------