Makes img tag more flexible, adds support for relative paths. Includes pull request #145, fixes #161
This commit is contained in:
parent
8c5ce51b3f
commit
50eaf98c24
@ -1,46 +1,47 @@
|
|||||||
# Title: Simple Image tag for Jekyll
|
# Title: Simple Image tag for Jekyll
|
||||||
# Author: Brandon Mathis http://brandonmathis.com
|
# Authors: Brandon Mathis http://brandonmathis.com
|
||||||
# Description: Easily output images with optional class names and title/alt attributes
|
# Felix Schäfer, Frederic Hemberger
|
||||||
|
# Description: Easily output images with optional class names, width, height, title and alt attributes
|
||||||
#
|
#
|
||||||
# Syntax {% image [class name(s)] url [title text] %}
|
# Syntax {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
|
||||||
#
|
#
|
||||||
# Example:
|
# Examples:
|
||||||
# {% ima left half http://site.com/images/ninja.png Ninja Attack! %}
|
# {% img /images/ninja.png Ninja Attack! %}
|
||||||
|
# {% img left half http://site.com/images/ninja.png Ninja Attack! %}
|
||||||
|
# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
|
||||||
#
|
#
|
||||||
# Output:
|
# Output:
|
||||||
# <image class='left' src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
|
# <img src="/images/ninja.png">
|
||||||
|
# <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
|
||||||
|
# <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
|
||||||
#
|
#
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
|
|
||||||
class ImageTag < Liquid::Tag
|
class ImageTag < Liquid::Tag
|
||||||
@img = nil
|
@img = nil
|
||||||
@title = nil
|
|
||||||
@class = ''
|
|
||||||
@width = ''
|
|
||||||
@height = ''
|
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+\s+\d+)?(\s+.+)?/i
|
attributes = ['class', 'src', 'width', 'height', 'title']
|
||||||
@class = $1 || ''
|
|
||||||
@img = $2 + $3
|
if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i
|
||||||
if $5
|
@img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img }
|
||||||
@title = $5.strip
|
if @img['title'] =~ /(?:"|')([^"']+)?(?:"|')\s+(?:"|')([^"']+)?(?:"|')/
|
||||||
end
|
@img['title'] = $1
|
||||||
if $4 =~ /\s*(\d+)\s+(\d+)/
|
@img['alt'] = $2
|
||||||
@width = $1
|
else
|
||||||
@height = $2
|
@img['alt'] = @img['title'].gsub!(/"/, '')
|
||||||
end
|
end
|
||||||
|
@img['class'].gsub!(/"/, '')
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
output = super
|
|
||||||
if @img
|
if @img
|
||||||
"<img class='#{@class}' src='#{@img}' width='#{@width}' height='#{@height}' alt='#{@title}' title='#{@title}'>"
|
"<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
|
||||||
else
|
else
|
||||||
"Error processing input, expected syntax: {% img [class name(s)] /url/to/image [width height] [title text] %}"
|
"Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user