Browse code

Makes img tag more flexible, adds support for relative paths. Includes pull request #145, fixes #161

Frederic Hemberger authored on 26/09/2011 at 09:00:35
Showing 1 changed files
... ...
@@ -1,46 +1,47 @@
1 1
 # Title: Simple Image tag for Jekyll
2
-# Author: Brandon Mathis http://brandonmathis.com
3
-# Description: Easily output images with optional class names and title/alt attributes
2
+# Authors: Brandon Mathis http://brandonmathis.com
3
+#          Felix Schäfer, Frederic Hemberger
4
+# Description: Easily output images with optional class names, width, height, title and alt attributes
4 5
 #
5
-# Syntax {% image [class name(s)] url [title text] %}
6
+# Syntax {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
6 7
 #
7
-# Example:
8
-# {% ima left half http://site.com/images/ninja.png Ninja Attack! %}
8
+# Examples:
9
+# {% img /images/ninja.png Ninja Attack! %}
10
+# {% img left half http://site.com/images/ninja.png Ninja Attack! %}
11
+# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
9 12
 #
10 13
 # Output:
11
-# <image class='left' src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
14
+# <img src="/images/ninja.png">
15
+# <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
16
+# <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
12 17
 #
13 18
 
14 19
 module Jekyll
15 20
 
16 21
   class ImageTag < Liquid::Tag
17 22
     @img = nil
18
-    @title = nil
19
-    @class = ''
20
-    @width = ''
21
-    @height = ''
22 23
 
23 24
     def initialize(tag_name, markup, tokens)
24
-      if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+\s+\d+)?(\s+.+)?/i
25
-        @class = $1 || ''
26
-        @img = $2 + $3
27
-        if $5
28
-          @title = $5.strip
29
-        end
30
-        if $4 =~ /\s*(\d+)\s+(\d+)/
31
-          @width = $1
32
-          @height = $2
25
+      attributes = ['class', 'src', 'width', 'height', 'title']
26
+
27
+      if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i
28
+        @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img }
29
+        if @img['title'] =~ /(?:"|')([^"']+)?(?:"|')\s+(?:"|')([^"']+)?(?:"|')/
30
+          @img['title']  = $1
31
+          @img['alt']    = $2
32
+        else
33
+          @img['alt']    = @img['title'].gsub!(/"/, '')
33 34
         end
35
+        @img['class'].gsub!(/"/, '')
34 36
       end
35 37
       super
36 38
     end
37 39
 
38 40
     def render(context)
39
-      output = super
40 41
       if @img
41
-        "<img class='#{@class}' src='#{@img}' width='#{@width}' height='#{@height}' alt='#{@title}' title='#{@title}'>"
42
+        "<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
42 43
       else
43
-        "Error processing input, expected syntax: {% img [class name(s)] /url/to/image [width height] [title text] %}"
44
+        "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
44 45
       end
45 46
     end
46 47
   end