image.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('image.pl', 'Image Extension');
  21. our ($q, @MyRules, $FullUrlPattern, $FreeLinkPattern, $FreeInterLinkPattern, %IndexHash, $ScriptName, $UsePathInfo, $Monolithic);
  22. our ($ImageUrlPath);
  23. $ImageUrlPath = '/images'; # URL where the images are to be found
  24. push(@MyRules, \&ImageSupportRule);
  25. # [[image/class:page name|alt text|target]]
  26. sub ImageSupportRule {
  27. my $result = undef;
  28. if (m!\G\[\[image((/[a-z]+)*)( external)?:\s*([^]|]+?)\s*(\|[^]|]+?)?\s*(\|[^]|]*?)?\s*(\|[^]|]*?)?\s*(\|[^]|]*?)?\s*\]\](\{([^}]+)\})?!cg) {
  29. my $oldpos = pos;
  30. my $class = 'image' . $1;
  31. my $external = $3;
  32. my $name = $4;
  33. # Don't generate an alt text if none was specified, since the rule
  34. # forces you to pick an alt text if you're going to provide a
  35. # link target.
  36. my $alt = UnquoteHtml($5 ? substr($5, 1) : '');
  37. $alt = NormalToFree($name)
  38. if not $alt and not $external and $name !~ /^$FullUrlPattern$/;
  39. my $link = $6 ? substr($6, 1) : '';
  40. my $caption = $7 ? substr($7, 1) : '';
  41. my $reference = $8 ? substr($8, 1) : '';
  42. my $comments = $10;
  43. my $id = FreeToNormal($name);
  44. $class =~ s!/! !g;
  45. my $linkclass = $class;
  46. my $found = 1;
  47. # link to the image if no link was given
  48. $link = $name unless $link;
  49. if ($link =~ /^($FullUrlPattern|$FreeInterLinkPattern)$/
  50. or $link =~ /^$FreeLinkPattern$/ and not $external) {
  51. ($link, $linkclass) = ImageGetExternalUrl($link, $linkclass);
  52. } else {
  53. $link = $ImageUrlPath . '/' . ImageUrlEncode($link);
  54. }
  55. my $src = $name;
  56. if ($src =~ /^($FullUrlPattern|$FreeInterLinkPattern)$/) {
  57. ($src) = ImageGetExternalUrl($src);
  58. } elsif ($src =~ /^$FreeLinkPattern$/ and not $external) {
  59. $found = $IndexHash{FreeToNormal($src)};
  60. $src = ImageGetInternalUrl($src) if $found;
  61. } else {
  62. $src = $ImageUrlPath . '/' . ImageUrlEncode($name);
  63. }
  64. if ($found) {
  65. $result = $q->img({-src=>$src, -alt=>$alt, -title=>$alt, -class=>'upload'});
  66. $result = $q->a({-href=>$link, -class=>$linkclass}, $result);
  67. if ($comments) {
  68. for (split '\n', $comments) {
  69. my $valRegex = qr/(([0-9.]+[a-z]*%?)\s+)/;
  70. if ($_ =~ /^\s*(([a-zA-Z ]+)\/)?$valRegex$valRegex$valRegex$valRegex(.*)$/) { # can't use {4} here? :(
  71. my $commentClass = $2 ? "imagecomment $2" : 'imagecomment';
  72. $result .= $q->div({-class=>$commentClass, -style=>"position: absolute; top: $6; left: $4; width: $8; height: $10"}, $11);
  73. }
  74. }
  75. $result = CloseHtmlEnvironments() . $q->div({-class=>"imageholder", -style=>"position: relative"}, $result);
  76. }
  77. } else {
  78. $result = GetDownloadLink($src, 1, undef, $alt);
  79. }
  80. if ($caption) {
  81. if ($reference) {
  82. my $refclass = $class;
  83. ($reference, $refclass) = ImageGetExternalUrl($reference, $refclass);
  84. $caption = $q->a({-href=>$reference, -class=>$refclass}, $caption);
  85. }
  86. $result .= $q->br() . $q->span({-class=>'caption'}, $caption);
  87. $result = CloseHtmlEnvironments() . $q->div({-class=>$class}, $result);
  88. }
  89. pos = $oldpos;
  90. }
  91. return $result;
  92. }
  93. sub ImageUrlEncode {
  94. # url encode everything except for slashes
  95. return join('/', map { UrlEncode($_) } split(/\//, shift));
  96. }
  97. sub ImageGetExternalUrl {
  98. my ($link, $class) = @_;
  99. if ($link =~ /^$FullUrlPattern$/) {
  100. $link = UnquoteHtml($link);
  101. $class .= ' outside';
  102. } elsif ($link =~ /^$FreeInterLinkPattern$/) {
  103. my ($site, $page) = split(/:/, $link, 2);
  104. $link = GetInterSiteUrl($site, $page, 1); # quote!
  105. $class .= ' inter ' . $site;
  106. } else {
  107. $link = FreeToNormal($link);
  108. if (substr($link, 0, 1) eq '/') {
  109. # do nothing -- relative URL on the same server
  110. } elsif ($UsePathInfo and !$Monolithic) {
  111. $link = $ScriptName . '/' . $link;
  112. } elsif ($Monolithic) {
  113. # if used together with all.pl
  114. $link = '#' . $link;
  115. } else {
  116. $link = $ScriptName . '?' . $link;
  117. }
  118. }
  119. return ($link, $class);
  120. }
  121. # split off to support overriding from Static Extension
  122. sub ImageGetInternalUrl {
  123. my $id = FreeToNormal(shift);
  124. if ($UsePathInfo) {
  125. return $ScriptName . "/download/" . UrlEncode($id);
  126. }
  127. return $ScriptName . "?action=download;id=" . UrlEncode($id);
  128. }