【发布时间】:2014-01-26 16:53:45
【问题描述】:
我目前有以下 Jekyll 标题标签代码:
# A Liquid tag for Jekyll sites that allows easy creation of captioned
# images like they are in WordPress.
#
# Author: Martin Thoma (info@martin-thoma.de)
# Source: https://github.com/MartinThoma/jekyll-caption-tag
# Version: 1.2
#
# Example usage:
# {% caption align="aligncenter" width="500" alt="WER calculation" text="WER calculation" url="/images/2013/11/WER-calculation.png" %}
#
# Plugin replaces the template above with:
# <div style="width: 510px" class="wp-caption aligncenter">
# <a href="/images/2013/11/WER-calculation.png">
# <img src="/images/2013/11/WER-calculation.png" alt="WER calculation" width="500" height="494" class="size-full">
# </a>
# <p class="wp-caption-text">WER calculation</p>
# </div>
require 'csv'
#require 'dimensions'
module Jekyll
class CaptionTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
@tokens = tokens
end
def parse_attrs(input)
options = { col_sep: '=', row_sep: ' ', quote_char: '"' }
csv = CSV.new input, options
csv.each_with_object({}) do |row, attrs|
attr, value = row
value ||= true
attrs[attr] = value
end
end
def render(context)
@hash = parse_attrs(@text)
if @hash.has_key?('text') && @hash.has_key?('caption')
puts "[Warning]["+context.environments.first["page"]["url"]+"] One caption Liquid tag has both, 'text' and 'caption' attribute. Using 'caption' is better."
end
if @hash.has_key?('title') && @hash.has_key?('caption')
puts "[Warning]["+context.environments.first["page"]["url"]+"] One caption Liquid tag has both, 'title' and 'caption' attribute. Using 'caption' is better."
end
if @hash.has_key?('text') && !@hash.has_key?('caption')
@hash['caption'] = @hash['text']
end
if @hash.has_key?('title') && !@hash.has_key?('caption')
@hash['caption'] = @hash['title']
end
@divWidth = (@hash['width'].to_i+10).to_s
#puts context.inspect
#Dimensions.dimensions(@hash['url'])
"<div style=\"width: #{@divWidth}px\" class=\"wp-caption #{@hash['align']}\">" +
"<a href=\"#{@hash['url']}\">" +
"<img src=\"#{@hash['url']}\" alt=\"#{@hash['text']}\" width=\"#{@hash['width']}\" height=\"#{@hash['height']}\" class=\"#{@hash['class']}\"/>" +
"</a>" +
"<p class=\"wp-caption-text\">#{@hash['caption']}</p>" +
"</div>"
end
end
end
Liquid::Template.register_tag('caption', Jekyll::CaptionTag)
我想检查图像尺寸。为此,我必须访问图像。为此,我需要当前渲染对象的路径。请注意,它并不总是像basepath + /posts 那样简单,可能会有basepath/author/moose/index.html 或类似情况。
那么:如何在 Jekyll Tag 中访问当前渲染文档的路径?
我想到了网站生成之前的路径。所以结果中不应该有basepath/_site。
【问题讨论】: