加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 嵌套两个具有可选参数的自定义Liquid标签

发布时间:2020-12-17 02:31:14 所属栏目:百科 来源:网络整理
导读:如果一个类有多个可选的标记作为参数传入,是否可以嵌套用 ruby编写的自定义Liquid标签?在没有提供相关示例的情况下,我很难描述这个问题.如果这个问题看起来太具体一个用例,请原谅. 给出以下ruby代码,源自Octopress(一个jekyll fork),它创建一个自定义Liquid
如果一个类有多个可选的标记作为参数传入,是否可以嵌套用 ruby编写的自定义Liquid标签?在没有提供相关示例的情况下,我很难描述这个问题.如果这个问题看起来太具体一个用例,请原谅.

给出以下ruby代码,源自Octopress(一个jekyll fork),它创建一个自定义Liquid标签来解析标签.

# Title: Simple Image tag for Jekyll
# Authors: Brandon Mathis http://brandonmathis.com
#          Felix Sch?fer,Frederic Hemberger
# Description: Easily output images with optional class names,width,height,title and alt attributes
#
# Syntax {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
#
# Examples:
# {% img /images/ninja.png Ninja Attack! %}
# {% img left half http://site.com/images/ninja.png Ninja Attack! %}
# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
#
# Output:
# <img src="/images/ninja.png">
# <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
# <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
#

module Jekyll

class ImageTag < Liquid::Tag
  @img = nil

  def initialize(tag_name,markup,tokens)
    attributes = ['class','src','width','height','title']

    if markup =~ /(?<class>S.*s+)?(?<src>(?:https?://|/|S+/)S+)(?:s+(?<width>d+))?(?:s+(?<height>d+))?(?<title>s+.+)?/i
      @img = attributes.reduce({}) { |img,attr| img[attr] = $~[attr].strip if $~[attr]; img }
      if /(?:"|')(?<title>[^"']+)?(?:"|')s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title']
        @img['title']  = title
        @img['alt']    = alt
      else
        @img['alt']    = @img['title'].gsub!(/"/,'&#34;') if @img['title']
      end
      @img['class'].gsub!(/"/,'') if @img['class']
    end
    super
  end

  def render(context)
    if @img
      "<img #{@img.collect {|k,v| "#{k}="#{v}"" if v}.join(" ")}>"
    else
      "Error processing input,expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}"
    end
  end
end
end
Liquid::Template.register_tag('img',Jekyll::ImageTag)

创建另一个自定义标记的最佳方法是为[< img>]元素展示相同的功能,但嵌套在[< figure>]元素中,并且可能将图像alt描述或附加标记显示为[ < figcaption>]元素可能包含自己的链接?或者甚至可能是元素的一系列类名,表明它是否应该居中.

换句话说,我可能希望输出类似于:

<figure class=center>
  <img src="/contra.jpg" alt="One of the greatest nintendo games of all time">
  <figcaption>Up Up Down Down Left Right Left Right B A B A <a href="http://www.youtube.com/contramoves/">Watch on Youtube</a></figcaption>
</figure>

我错误地认为可以嵌套自定义Liquid标签吗?我确信我可以再次重写现有代码并稍微修改它以处理[< figcaption>]的附加属性,但这似乎相当多余并且违反DRY原则.就目前的情况而言,考虑到现有的类本身采用可选的令牌,我对如何考虑可能的附加令牌感到困惑.

解决方法

我需要做的是创建一个Liquid Block,而不是Liquid Tag.该解决方案允许人们理论上在图中嵌套其他液体标签甚至其他液体块,这正是人们对[< figure>]标签所期望的.

由于Markdown目前不支持HTML5,因此这种基于Liquid的解决方案是一个很好的折衷方案.

# Example:
 #
 # {% fig This is my caption! http://site.com/link.html Link Caption %}
 #   {% img center http://site.com/images/mylinks.png A collection of my favorite links %}
 # {% endfig %}
 #
 # Output:
 #
 # <figure class='center'>
 #    <img class="center" src="http://site.com/images/mylinks.png" title="A collection of my favorite links" >
 #    <figcaption>This is my caption!<a href='http://site.com/link.html'>Link Caption </a></figcaption>
 #</figure>
 #
 #

 module Jekyll

   class FigureTag < Liquid::Block
     include TemplateWrapper
     CaptionUrl = /(S[Ss]*)s+(https?://S+)s+(.+)/i
     Caption = /(S[Ss]*)/
     def initialize(tag_name,tokens)
       @title = nil
       @caption = nil
       if markup =~ CaptionUrl
         @caption = "ntt<figcaption>#{$1}<a href='#{$2}'>#{$3}</a></figcaption>nt"
       elsif markup =~ Caption
         @caption = "ntt<figcaption>#{$1}</figcaption>nt"
       end
       super
     end

     def render(context)
       output = super
       fig = super.join
       source = "t<figure class='center'>ntt"
       markdown = RDiscount.new(fig.lstrip).to_html[/<p>(.+)</p>/i]
       source += $1
       source += @caption if @caption
       source += "</figure>"
       source = safe_wrap(source)
       source
     end
   end
 end

 Liquid::Template.register_tag('fig',Jekyll::FigureTag)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读