More improvements to URL recognizer.
[sw-tools] / perl / SWMan.pm
index b27fca8..e799148 100644 (file)
@@ -1,6 +1,6 @@
 # -*-perl-*-
 #
-# $Id: SWMan.pm,v 1.2 1999/08/18 17:10:07 mdw Exp $
+# $Id: SWMan.pm,v 1.3 1999/08/19 12:11:10 mdw Exp $
 #
 # Display and other fiddling of manual pages
 #
@@ -28,6 +28,9 @@
 #----- Revision history -----------------------------------------------------
 #
 # $Log: SWMan.pm,v $
+# Revision 1.3  1999/08/19 12:11:10  mdw
+# More improvements to URL recognizer.
+#
 # Revision 1.2  1999/08/18 17:10:07  mdw
 # Slight improvements to URL and email address parsing.
 #
@@ -48,7 +51,7 @@ use SWConfig;
 use SWCGI qw(:DEFAULT :layout);
 
 @ISA = qw(Exporter);
-@EXPORT_OK = qw(subst check);
+@EXPORT_OK = qw(subst urlsubst check);
 
 #----- Useful functions -----------------------------------------------------
 
@@ -107,6 +110,19 @@ sub subst($$$) {
   return "$s";
 }
 
+# --- @urlsubst(URL, STRING)@ ---
+#
+# Substitutes in a URL reference.  The important bit is that embedded `&'
+# characters are un-entitied from `&'.  This doesn't seem to upset
+# Netscape or Lynx as much as I'd expect (or, in fact, at all), but it's
+# slightly untidy.
+
+sub urlsubst($$) {
+  my ($url, $name) = @_;
+  $url =~ s/\&amp\;/&/;
+  return "<a href=\"$url\">$name</a>";
+}
+
 # --- @sections()@ ---
 #
 # Return a list of manual sections.
@@ -266,8 +282,8 @@ sub man {
        # --- Translate the character if it's magical ---
 
        $ch eq "&" and $ch = "&amp;";
-       $ch eq "<" and $ch = "&lt;";
-       $ch eq ">" and $ch = "&gt;";
+       $ch eq "<" and $ch = "&lt;<";
+       $ch eq ">" and $ch = ">&gt;";
        $l .= $ch;
       }
       $state and $l .= "</$state>";
@@ -291,18 +307,23 @@ sub man {
       # --- And email and hypertext references too ---
 
       $l =~ s! ((?:\<[bi]\>)*)         # Leading highlighting
-              ((?:https?|ftp|file|news) # A protocol name
+              ( \b (?: http s? | ftp | file | news ) # A protocol name
                :                       # The important and obvious bit
-               [^]&)\s]+               # Most characters are allowed
-               [^]&).,\s\'\"])         # Don't end on punctuation
+               [^]<>)\s<>\'\"]+        # Most characters are allowed
+               [^]<>).,\s<>\'\"])      # Don't end on punctuation
               ((?:\</[bi]\>)*)         # Closing tags, optional
-            !$1<a href="$2">$&</a>$3!gx;
+            !urlsubst($2, $&)!egx;
 
-      $l =~ s! ((?:\<[bi]\>)*(?:\bmailto:)?)
-               ( [^\s()&;:{}<>,.\`\"] [^\s()&;:{}<>\`\"]* \@
-                 [^\s()&;:{}<>\'\"]* [^\s()&;:{}<>.,\'\"])
+      $l =~ s! ( (?:\<[bi]\>)* (?:\bmailto:)? )
+               ( [^\s()<>;:{}&<>,.\`\'\"] [^\s()<>;:{}&<>\`\'\"]* \@
+                 [^\s()<>;:{&}<>\'\"]* [^\s()<>;:{}&<>.,\'\"])
                ((?:\</[bi]\>)*)
-             !$1<a href="mailto:$2">$&</a>$3!gx;
+             !<a href="mailto:$2">$&</a>!gx;
+
+      # --- Fix up the HTML ---
+
+      $l =~ s/\&lt\;\</&lt;/g;
+      $l =~ s/\>\&gt\;/&gt;/g;
 
       # --- Done! ---