Class: JekyllPicContainer::PicContainer
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- JekyllPicContainer::PicContainer
- 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
-
#markup ⇒ Object
readonly
Returns the value of attribute markup.
-
#tag_arguments ⇒ Object
readonly
Returns the value of attribute tag_arguments.
Instance Method Summary collapse
- #build_caption(alt) ⇒ Object
-
#get_argument(arg_name) ⇒ Object
For a tag, the content is the markup of the tag, which means it has to be manually parsed.
-
#initialize(_tag_name, markup, _tokens) ⇒ PicContainer
constructor
A new instance of PicContainer.
- #render(context) ⇒ Object
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
#markup ⇒ Object (readonly)
Returns the value of attribute markup.
13 14 15 |
# File 'lib/jekyll_pic_container.rb', line 13 def markup @markup end |
#tag_arguments ⇒ Object (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) = ["<p>"] .push(alt) attr_source = get_argument("attr_source") unless attr_source.nil? .push(" ") # Add a space between the text and the tag .push("<a target='_blank' rel='noreferrer' href='#{attr_source}'>") attr_text = get_argument("attr_text") if attr_text.nil? .push("original source") else .push(attr_text) end .push("</a>") end .push("</p>") .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) = ['<div class="picture_container">'] .push(<<~HTML.strip) {% picture #{markup} %} HTML alt = get_argument("alt") .push(build_caption(alt)) unless alt.nil? .push("</div>") content = .join(" ") Liquid::Template.parse(content).render(context) end |