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