Class: JekyllPicContainer::PicContainer

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll_pic_container.rb

Overview

Provices some markup around a picture tag. This tag will process the following arguments:

--alt Alt text will be placed on the image tag as well as being provided as a caption to the image
--attr_source A link to an original image that has been copied here. 
--attr_text The text to correctly credit the original source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_tag_name, markup, _tokens) ⇒ PicContainer

Returns a new instance of PicContainer.



15
16
17
18
19
# File 'lib/jekyll_pic_container.rb', line 15

def initialize(_tag_name, markup, _tokens)
  @markup = markup
  @tag_arguments = markup.split(/--/)[1..].freeze
  super
end

Instance Attribute Details

#markupObject (readonly)

Returns the value of attribute markup.



13
14
15
# File 'lib/jekyll_pic_container.rb', line 13

def markup
  @markup
end

#tag_argumentsObject (readonly)

Returns the value of attribute tag_arguments.



13
14
15
# File 'lib/jekyll_pic_container.rb', line 13

def tag_arguments
  @tag_arguments
end

Instance Method Details

#build_caption(alt) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll_pic_container.rb', line 50

def build_caption(alt)
  tags = ["<p>"]
  tags.push(alt)

  attr_source = get_argument("attr_source")
  unless attr_source.nil?
    tags.push(" ") # Add a space between the text and the tag
    tags.push("<a target='_blank' rel='noreferrer' href='#{attr_source}'>")
    attr_text = get_argument("attr_text")
    if attr_text.nil?
      tags.push("original source")
    else
      tags.push(attr_text)
    end
    tags.push("</a>")
  end

  tags.push("</p>")
  tags.join("")
end

#get_argument(arg_name) ⇒ Object

For a tag, the content is the markup of the tag, which means it has to be manually parsed. Picture tag 2 uses -- to designate options after the first required ones



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyll_pic_container.rb', line 38

def get_argument(arg_name)
  prefix = "#{arg_name} "
  raw_value = tag_arguments.filter { |text| text.start_with?(prefix) }.first
  return unless raw_value

  arg_value = raw_value.dup
  # Remove the arg prefix from the string
  arg_value.slice!(prefix)

  arg_value.strip
end

#render(context) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll_pic_container.rb', line 21

def render(context)
  tags = ['<div class="picture_container">']
  tags.push(<<~HTML.strip)
    {% picture #{markup} %}
  HTML

  alt = get_argument("alt")
  tags.push(build_caption(alt)) unless alt.nil?
  tags.push("</div>")

  content = tags.join(" ")
  Liquid::Template.parse(content).render(context)
end