Skip to content

Commit d3cbe06

Browse files
committed
Now processing liquid variables in tag. Fixes #1
1 parent 456236b commit d3cbe06

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
### 1.1.0 (2015-01-07)
4+
- New: Liquid can be processed in tag.
5+
6+
### 1.0.0 (2015-01-07)
7+
- Initial release

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ Then add the gem to your Jekyll configuration.
3232
```
3333

3434
Examples:
35+
3536
```
3637
{% img /images/ninja.png "Ninja Attack!" %}
3738
{% img left half http://site.com/images/ninja.png title:"Hidden Ninja" %}
38-
{% img http://site.com/images/ninja.png 150px 150px %}
39+
{% img {{ site.cdn }}/images/ninja.png 150px 150px %}
3940
```
4041

4142
Output:
43+
4244
```
4345
<img src="/images/ninja.png" alt="Ninja Attack!" >
4446
<img class="left half" src="http://site.com/images/ninja.png" alt="Hidden Ninja" title="Hidden Ninja" >
45-
<img src="http://site.com/images/ninja.png" width="150px" height="150px" alt="Ninja in the shadows" title="Hidden Ninja">
47+
<img src="http://some.cdn.io/images/ninja.png" width="150px" height="150px" alt="Ninja in the shadows" title="Hidden Ninja">
4648
```
4749

48-
4950
## Contributing
5051

5152
1. Fork it ( https://github.com/[my-github-username]/image-tag/fork )

lib/octopress-image-tag.rb

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,52 @@ class Tag < Liquid::Tag
1212
@img = nil
1313

1414
def initialize(tag_name, markup, tokens)
15-
if markup =~ /title:['|"](.+?)['|"]/
16-
@title = $1.strip
15+
@markup = markup
16+
super
17+
end
18+
19+
def render(context)
20+
begin
21+
attributes = image(context).collect do |k,v|
22+
"#{k}=\"#{v}\"" if v
23+
end.join(" ")
24+
25+
"<img #{attributes}>"
26+
rescue
27+
raise "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
1728
end
29+
end
1830

19-
markup = markup.gsub(/title:['|"].+?['|"]/, '').strip
31+
def image(context)
32+
@markup = process_liquid(context)
2033

21-
if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d\S+))?(?:\s+(?<height>\d\S+))?(?<alt>\s+.+)?/i
34+
title = /title:['|"](.+?)['|"]/
35+
@title = @markup.scan(title).flatten.compact.last
36+
@markup.gsub!(title, '')
37+
38+
if @markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d\S+))?(?:\s+(?<height>\d\S+))?(?<alt>\s+.+)?/i
2239
attributes = ['class', 'src', 'width', 'height', 'alt']
23-
@img = attributes.reduce({}) { |img, attr| img[attr] ||= $~[attr].strip if $~[attr]; img }
24-
text = @img['alt']
40+
image = attributes.reduce({}) { |img, attr| img[attr] ||= $~[attr].strip if $~[attr]; img }
41+
text = image['alt']
2542

2643
# Allow parsing "title" "alt"
2744
if text =~ /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/
28-
@img['title'] = title
29-
@img['alt'] = alt
45+
image['title'] = title
46+
image['alt'] = alt
3047
else
3148
# Set alt text and title from text
32-
@img['alt'].gsub!(/"/, '') if @img['alt']
49+
image['alt'].gsub!(/"/, '') if image['alt']
3350
end
3451
end
3552

36-
@img['title'] ||= @title
37-
@img['alt'] ||= @title
38-
super
53+
image['title'] ||= @title
54+
image['alt'] ||= @title
55+
56+
image
3957
end
4058

41-
def render(context)
42-
if @img
43-
"<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
44-
else
45-
"Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
46-
end
59+
def process_liquid(context)
60+
Liquid::Template.parse(@markup).render!(context.environments.first)
4761
end
4862
end
4963
end

lib/octopress-image-tag/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Octopress
22
module Tags
33
module ImageTag
4-
VERSION = "1.0.0"
4+
VERSION = "1.1.0"
55
end
66
end
77
end

test/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
gems:
22
- octopress-image-tag
3+
images_dir: /images

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
---
33

4-
{% img /images/ninja.png "Ninja Attack!" %}
4+
{% img {{ site.images_dir }}/ninja.png "Ninja Attack!" %}
55
{% img left half http://site.com/images/ninja.png "Ninja in the shadows" %}
66
{% img http://site.com/images/ninja.png 150px 150px "Ninja in the shadows" title:"Hidden Ninja" %}
77

0 commit comments

Comments
 (0)