Created
November 10, 2013 13:14
-
-
Save rwboyer/7398113 to your computer and use it in GitHub Desktop.
image_tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module IMGtag | |
| class ImageTag < Liquid::Tag | |
| @img = nil | |
| def initialize(tag_name, markup, tokens) | |
| attributes = ['class', 'src', 'width', 'height', 'title'] | |
| if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i | |
| @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img } | |
| if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title'] | |
| @img['title'] = title | |
| @img['alt'] = alt | |
| else | |
| @img['alt'] = @img['title'].gsub!(/"/, '"') if @img['title'] | |
| end | |
| @img['class'].gsub!(/"/, '') if @img['class'] | |
| end | |
| super | |
| end | |
| def render(context) | |
| #page_url = context.environments.first["page"]["url"] | |
| if @img | |
| tag = "<div class=\"box #{@img['class']}\">" + | |
| "<a href=\"#{@img['src']}\" class=\"lightview\">\n" + | |
| #{}"<a href=\"#{@img['src']}\" rel=\"lightbox\">\n" + | |
| #"<a href=\"#{@img['src']}\" class=\"lightview\">\n" + | |
| #"<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>\n" + | |
| "<img title=\"#{@img['title']}\" src=\"#{@img['src']}\">\n" + | |
| "</a>" | |
| if @img['title'] | |
| tag = tag + "<span class=\"caption fade-caption\">" + | |
| "<p>#{@img['title']}</p>" + | |
| "</span>" | |
| end | |
| tag = tag + "</div>" | |
| tag | |
| else | |
| "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 | |
| Liquid::Template.register_tag('img', IMGtag::ImageTag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment