-
Notifications
You must be signed in to change notification settings - Fork 115
/
image.rb
73 lines (60 loc) · 1.68 KB
/
image.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Jekyll
module Tags
class ImageTagError < StandardError
def initialize(msg)
super
end
end
class ImageTag < Liquid::Tag
def initialize(tag_name, markup, tokens)
@markup = markup.strip
@markup = split_string_with_quotes(@markup)
# the source of the image
@img = @markup[0].gsub("'", "").gsub('"', '')
if(not @img.include? "/")
@img = "/assets/images/#@img"
end
# contains every attribute (i.e. alt or class)
@attributes = ''
# gets the alt attribute
@alt = @markup.select {|sub| sub.include? "alt:"}[0]
# gets the class attribute
@class = @markup.select {|sub| sub.include? "class:"}[0]
if @alt != nil
@alt[":"] = "="
@alt.gsub("'", '"')
@attributes += "#@alt "
end
if @class != nil
@class[":"] = "="
@class.gsub("'", '"')
@attributes += "#@class "
end
end
def render(context)
"<img src=\"#@img\" #@attributes >"
end
end
end
end
# splits strings at spaces but not if the spaces are inside quotes
def split_string_with_quotes(str)
words = []
current_word = ''
in_quotes = false
str.each_char do |char|
if char == ' ' && !in_quotes
words << current_word unless current_word.empty?
current_word = ''
else
current_word += char
if char == '"' || char == "'"
in_quotes = !in_quotes
end
end
end
words << current_word unless current_word.empty?
words
end
Liquid::Template.register_tag('image', Jekyll::Tags::ImageTag)
Liquid::Template.register_tag('img', Jekyll::Tags::ImageTag)