Class: Octopress::Tags::ImageCaptionTag::Block

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/octopress-image-caption-tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Block

Returns a new instance of Block.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/octopress-image-caption-tag.rb', line 73

def initialize(tag_name, markup, tokens)
  if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+%?\s+\d+%?)?(\s+.+)?/i
    @class = $1 || ''
    @img = $2 + $3
    if $5
      @title = $5.strip
    end
    if $4 =~ /\s*(\d+%?)\s+(\d+%?)/
      @width = $1
      @height = $2
    elsif @class.rstrip == "right" or @class.rstrip == "left"
      @width = "33%"
    else
      @width = "100%"
    end
    if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @title
      @title  = title
      @alt    = alt
    else
      @alt    = @title.gsub!(/"/, '&#34;') if @title
    end
  end
  super
end

Instance Method Details

#render(context) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/octopress-image-caption-tag.rb', line 98

def render(context)
  @caption = super
  site = context.registers[:site]
  converter = site.getConverterImpl(Jekyll::Converters::Markdown)
  if @img && @width[-1] == "%" # Relative width, so width goes on outer span
    "<figure class='#{('caption-wrapper ' + @class).rstrip}' style='width:#{@width};'>" +
      "<a class='image-popup' href='#{@img}'>" +
      "<img class='caption' src='#{@img}' width='100%' height='100%' title='#{@title}' alt='#{@alt}'>" +
      "</a>" +
      "<figurecaption class='caption-text'>#{@caption}</figurecaption>" +
      "</figure>"
  elsif @img # Absolute width, so width goes on the img tag and text span gets sytle-width:@width-15;
    "<figure class='#{('caption-wrapper ' + @class).rstrip}'>" +
      "<a class='image-popup' href='#{@img}'>" +
      "<img class='caption' src='#{@img}' width='#{@width}px' height='#{@height}px' title='#{@title}' alt='#{@alt}'>" +
      "</a>" +
      "<figurecaption class='caption-text' style='width:#{@width.to_i - 10}px;'>#{converter.convert(@caption)}</figurecaption>" +
      "</figure>"
  else
    "Error processing input, expected syntax: {% imgcaption [class name(s)] /url/to/image [width height] [title [alt]] %} Caption Text {% endimgcaption %}"
  end
end