image.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2004, 2005, 2006, 2007 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('image.pl', 'Image Extension');
  18. our ($q, @MyRules, $FullUrlPattern, $FreeLinkPattern, $FreeInterLinkPattern, %IndexHash, $ScriptName, $UsePathInfo, $Monolithic);
  19. our ($ImageUrlPath);
  20. $ImageUrlPath = '/images'; # URL where the images are to be found
  21. push(@MyRules, \&ImageSupportRule);
  22. # [[image/class:page name|alt text|target]]
  23. sub ImageSupportRule {
  24. my $result = undef;
  25. if (m!\G\[\[image((/[-a-z]+)*)( external)?:\s*([^]|]+?)\s*(\|[^]|]+?)?\s*(\|[^]|]*?)?\s*(\|[^]|]*?)?\s*(\|[^]|]*?)?\s*\]\](\{([^}]+)\})?!cg) {
  26. my $oldpos = pos;
  27. my $class = 'image' . $1;
  28. my $external = $3;
  29. my $name = $4;
  30. # Don't generate an alt text if none was specified, since the rule
  31. # forces you to pick an alt text if you're going to provide a
  32. # link target.
  33. my $alt = UnquoteHtml($5 ? substr($5, 1) : '');
  34. $alt = NormalToFree($name)
  35. if not $alt and not $external and $name !~ /^$FullUrlPattern$/;
  36. my $link = $6 ? substr($6, 1) : '';
  37. my $caption = $7 ? substr($7, 1) : '';
  38. my $reference = $8 ? substr($8, 1) : '';
  39. my $comments = $10;
  40. my $id = FreeToNormal($name);
  41. $class =~ s!/! !g;
  42. my $linkclass = $class;
  43. my $found = 1;
  44. # link to the image if no link was given
  45. $link = $name unless $link;
  46. if ($link =~ /^($FullUrlPattern|$FreeInterLinkPattern)$/
  47. or $link =~ /^$FreeLinkPattern$/ and not $external) {
  48. ($link, $linkclass) = ImageGetExternalUrl($link, $linkclass);
  49. } else {
  50. $link = $ImageUrlPath . '/' . ImageUrlEncode($link);
  51. }
  52. my $src = $name;
  53. if ($src =~ /^($FullUrlPattern|$FreeInterLinkPattern)$/) {
  54. ($src) = ImageGetExternalUrl($src);
  55. } elsif ($src =~ /^$FreeLinkPattern$/ and not $external) {
  56. $found = $IndexHash{FreeToNormal($src)};
  57. $src = ImageGetInternalUrl($src) if $found;
  58. } else {
  59. $src = $ImageUrlPath . '/' . ImageUrlEncode($name);
  60. }
  61. if ($found) {
  62. $result = $q->img({-src=>$src, -alt=>$alt, -title=>$alt, -class=>'upload', -loading=>'lazy'});
  63. $result = $q->a({-href=>$link, -class=>$linkclass}, $result);
  64. if ($comments) {
  65. for (split '\n', $comments) {
  66. my $valRegex = qr/(([0-9.]+[a-z]*%?)\s+)/;
  67. if ($_ =~ /^\s*(([a-zA-Z ]+)\/)?$valRegex$valRegex$valRegex$valRegex(.*)$/) { # can't use {4} here? :(
  68. my $commentClass = $2 ? "imagecomment $2" : 'imagecomment';
  69. $result .= $q->div({-class=>$commentClass, -style=>"position: absolute; top: $6; left: $4; width: $8; height: $10"}, $11);
  70. }
  71. }
  72. $result = CloseHtmlEnvironments() . $q->div({-class=>"imageholder", -style=>"position: relative"}, $result);
  73. }
  74. } else {
  75. $result = GetDownloadLink($src, 1, undef, $alt);
  76. }
  77. if ($caption) {
  78. if ($reference) {
  79. my $refclass = $class;
  80. ($reference, $refclass) = ImageGetExternalUrl($reference, $refclass);
  81. $caption = $q->a({-href=>$reference, -class=>$refclass}, $caption);
  82. }
  83. $result .= $q->br() . $q->span({-class=>'caption'}, $caption);
  84. $result = CloseHtmlEnvironments() . $q->div({-class=>$class}, $result);
  85. }
  86. pos = $oldpos;
  87. }
  88. return $result;
  89. }
  90. sub ImageUrlEncode {
  91. # url encode everything except for slashes
  92. return join('/', map { UrlEncode($_) } split(/\//, shift));
  93. }
  94. sub ImageGetExternalUrl {
  95. my ($link, $class) = @_;
  96. if ($link =~ /^$FullUrlPattern$/) {
  97. $link = UnquoteHtml($link);
  98. $class .= ' outside';
  99. } elsif ($link =~ /^$FreeInterLinkPattern$/) {
  100. my ($site, $page) = split(/:/, $link, 2);
  101. $link = GetInterSiteUrl($site, $page, 1); # quote!
  102. $class .= ' inter ' . $site;
  103. } else {
  104. $link = FreeToNormal($link);
  105. if (substr($link, 0, 1) eq '/') {
  106. # do nothing -- relative URL on the same server
  107. } elsif ($UsePathInfo and !$Monolithic) {
  108. $link = $ScriptName . '/' . $link;
  109. } elsif ($Monolithic) {
  110. # if used together with all.pl
  111. $link = '#' . $link;
  112. } else {
  113. $link = $ScriptName . '?' . $link;
  114. }
  115. }
  116. return ($link, $class);
  117. }
  118. # split off to support overriding from Static Extension
  119. sub ImageGetInternalUrl {
  120. my $id = FreeToNormal(shift);
  121. if ($UsePathInfo) {
  122. return $ScriptName . "/download/" . UrlEncode($id);
  123. }
  124. return $ScriptName . "?action=download;id=" . UrlEncode($id);
  125. }