]> Sergey Matveev's repositories - swg.git/commitdiff
Ability to use images in links
authorSergey Matveev <stargrave@stargrave.org>
Sat, 31 May 2025 11:14:50 +0000 (14:14 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 31 May 2025 11:14:50 +0000 (14:14 +0300)
FORMAT
swg

diff --git a/FORMAT b/FORMAT
index e789d9f25ee4d6caa6bd2a0fab94010c569175a2..28954aa3292c2572fb3983882f310a8d9703913e 100644 (file)
--- a/FORMAT
+++ b/FORMAT
@@ -8,8 +8,9 @@ role only (currently) during HTML generation.
 
 * => URL[ optional text]
   Creates <a href="URL">{optional text|URL}</a> link in HTML.
-* img URL[ optional text]
+* img URL[ optional text][ => URL-A]
   Creates <img src="URL" [alt="optional text"] /> in HTML.
+  If "=> URL" is specified, then <a href="URL-A"><img ... /></a>.
 * |...
   Just inserts raw ... line to HTML output as-is.
 * #...
@@ -17,5 +18,5 @@ role only (currently) during HTML generation.
 * do-backs
   Forcefully creates a table with all backlinks to the page.
 * <<[indent][page]
-  Include contents of the specified page. Brackets around
-  [page] are for making a link to page. indent is optional.
+  Include contents of the specified page. indent is optional.
+  Brackets around [page] are for making a link to page.
diff --git a/swg b/swg
index 9d84468220b6cb7fefa52dd258c8430d15260c8e..b42e7bc20f9f67fa477798b7c2edf85579db8e5c 100755 (executable)
--- a/swg
+++ b/swg
@@ -268,34 +268,42 @@ END_HTML
         if (/${CR}$/) {
             chop;
             my $head = q{};
-            my @cols;
-            if (/^(\s*)(.*)$/) {
+            my $cmd = $_;
+            if (/^(\s+)(.*)$/) {
                 $head = $1;
-                @cols = split /\s+/, $2;
-            } else {
-                @cols = split /\s+/;
+                $cmd = $2;
             }
-            if ($cols[0] eq q{=>}) {
-                my $t = ($#cols > 1) ? (join q{ }, @cols[2..$#cols]) : $cols[1];
+            if ($cmd =~ /^=> (\S+)\s?(.*)$/) {
+                my $t = ($2 eq q{}) ? $1 : $2;
                 $t = htmlescape $t;
                 $t =~ s/"/\&guot;/g;
-                $_ = "$head=&gt; <a href=\"$cols[1]\">$t</a>";
-            } elsif ($cols[0] eq q{img}) {
-                if ($#cols > 1) {
-                    my $t = htmlescape join q{ }, @cols[2..$#cols];
+                $_ = "$head=&gt; <a href=\"$1\">$t</a>";
+            } elsif ($cmd =~ /^img (\S+)\s?(.*) => (\S+)$/) {
+                if ($2 eq q{}) {
+                    $_ = "$head<a href=\"$3\"><img src=\"$1\" /></a>";
+                } else {
+                    my $t = $2;
+                    $t = htmlescape $t;
                     $t =~ s/"/\&guot;/g;
-                    $_ = "$head<img src=\"$cols[1]\" alt=\"$t\" />";
+                    $_ = "$head<a href=\"$3\"><img src=\"$1\" alt=\"$t\" /></a>";
+                }
+            } elsif ($cmd =~ /^img (\S+)\s?(.*)$/) {
+                if ($2 eq q{}) {
+                    $_ = "$head<img src=\"$1\" />";
                 } else {
-                    $_ = "$head<img src=\"$cols[1]\" />";
+                    my $t = $2;
+                    $t = htmlescape $t;
+                    $t =~ s/"/\&guot;/g;
+                    $_ = "$head<img src=\"$1\" alt=\"$t\" />";
                 }
-            } elsif ($cols[0] eq q{do-backs}) {
+            } elsif ($cmd eq q{do-backs}) {
                 $doBacks = 1;
                 return;
-            } elsif (/^\s*#/) {
+            } elsif ($cmd =~ /^#/) {
                 return;
-            } elsif (/^\s*[|]/) {
-                $_ = $head . substr $_, 1 + (index $_, q{|});
-            } elsif (/^\s*<<(.*)\[([^[]+)\]/) {
+            } elsif ($cmd =~ /^[|]/) {
+                $_ = $head . substr $cmd, 1 + (index $cmd, q{|});
+            } elsif ($cmd =~ /^<<(.*)\[([^[]+)\]$/) {
                 my $indentOrig = $indent;
                 $indent .= "${head}$1";
                 open my $fh, q{<:encoding(UTF-8)}, $2 or croak "$!";
@@ -304,7 +312,7 @@ END_HTML
                 $indent = $indentOrig;
                 return;
             } else {
-                croak "unknown $cols[0] command: $page\n";
+                croak "unknown \"$cmd\" command: $page\n";
             }
         } else {
             $_ = htmlescape $_;