]> Sergey Matveev's repositories - zk.git/commitdiff
Much more simple URL/img format
authorSergey Matveev <stargrave@stargrave.org>
Wed, 7 May 2025 18:45:59 +0000 (21:45 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 8 May 2025 07:01:06 +0000 (10:01 +0300)
zk

diff --git a/zk b/zk
index 20a1fb3cbe25c3e58de431e692e4ab0aa5feb335..4a7f63c27a59b872238366649ada39b7cfa96b95 100755 (executable)
--- a/zk
+++ b/zk
@@ -65,11 +65,14 @@ quickly open results in the editor.
 
 =item *
 
-C<[img|src|alt]> can be used to create link to images in HTML output.
+C<E<gt> URL[ optional text]E<lt>CRE<gt>> on a separate line will add a
+link to the specified URL (with optional text). Pay attention that line
+contains carriage-return character at the end.
 
 =item *
 
-C<[raw|url|text]> can be used to create arbitrary link in HTML output.
+C<I URL[ alt]E<lt>CRE<gt>> on a separate line will add an image with
+optional alternative text similarly as link is specified.
 
 =back
 
@@ -186,19 +189,13 @@ sub startBody {
 </head><body>";
 }
 
-
-my %schemes = (
-    "finger" => 1,
-    "ftp" => 1,
-    "gemini" => 1,
-    "gopher" => 1,
-    "http" => 1,
-    "https" => 1,
-    "irc" => 1,
-    "ircs" => 1,
-    "news" => 1,
-    "telnet" => 1,
-);
+sub htmlescape {
+    $_ = shift;
+    s/&/\&amp;/g;
+    s/</\&lt;/g;
+    s/>/\&gt;/g;
+    return $_;
+}
 
 use File::Spec;
 
@@ -222,39 +219,33 @@ sub genHTML {
     print $out "<pre>\n";
     open(my $fh, "<", $page) or die "$!";
     while (<$fh>) {
-        my @ws;
         chop;
-        /( *)$/;
-        my $tail = $1;
-        foreach my $w (split / /) {
-            my ($scheme, $authority, $path, $query, $fragment) = $w =~
-                m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
-            if (defined $scheme and exists $schemes{$scheme}) {
-                my $e = $w;
-                $e =~ s/&/\&amp;/g;
-                $w = "<a href=\"$w\">$e</a>";
-            } else {
-                if ($w =~ /^\[img\|(.*)\|(.+)\]$/) {
-                    $w = "<img src=\"$1\" alt=\"$2\" />";
-                } elsif ($w =~ /^\[raw\|(.*)\|(.+)\]$/) {
-                    $w = "<a href=\"$1\">$2</a>";
-                } elsif ($w =~ /\[.+\]/) {
-                    $w =~ s/&/\&amp;/g;
-                    $w =~ s/</\&lt;/g;
-                    $w =~ s/>/\&gt;/g;
-                    while (my ($i, $l) = each @lnks) {
-                        $w =~ s/\[$l\]/<a href="$rels[$i].html">[$l]<\/a>/g;
-                    }
+        if (/\r$/) {
+            chop;
+            my @cols = split /\s+/;
+            if ($cols[0] eq ">") {
+                my $t = ($#cols > 1) ? (join " ", @cols[2..$#cols]) : $cols[1];
+                $t = htmlescape $t;
+                $_ = "<a href=\"$cols[1]\">$t</a>";
+            } elsif ($cols[0] eq "I") {
+                if ($#cols > 1) {
+                    my $t = htmlescape join " ", @cols[2..$#cols];
+                    $_ = "<img src=\"$cols[1]\" alt=\"$t\" />";
                 } else {
-                    $w =~ s/&/\&amp;/g;
-                    $w =~ s/</\&lt;/g;
-                    $w =~ s/>/\&gt;/g;
+                    $_ = "<img src=\"$cols[1]\" />";
+                }
+            } else {
+                print STDERR "unknown $cols[0]\n";
+            }
+        } else {
+            $_ = htmlescape $_;
+            if (/\[.+\]/) {
+                while (my ($i, $l) = each @lnks) {
+                    s/\[$l\]/<a href="$rels[$i].html">[$l]<\/a>/g;
                 }
             }
-            push @ws, $w;
         }
-        my $w = join " ", @ws;
-        print $out "${w}${tail}\n";
+        print $out "$_\n";
     }
     close $fh;
     print $out "</pre>\n";