title: Nesting two custom Liquid tags that have optional parameters
body: <p>Is it possible to nest custom Liquid tags written in ruby if one class has multiple optional tokens passed in as parameters? This question is rather hard for me to describe without providing the relevant example. Please excuse me if this question appears to be too specific a use case.</p>
  
  <p>Given the following ruby code, sourced from Octopress (a jekyll fork), which creates a custom Liquid tag to parse  tags.</p>
  
  <pre><code># 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:
  # &lt;img src="/images/ninja.png"&gt;
  # &lt;img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!"&gt;
  # &lt;img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture"&gt;
  #
  
  module Jekyll
  
  class ImageTag &lt; Liquid::Tag
    @img = nil
  
    def initialize(tag_name, markup, tokens)
      attributes = ['class', 'src', 'width', 'height', 'title']
  
      if markup =~ /(?&lt;class&gt;\S.*\s+)?(?&lt;src&gt;(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?&lt;width&gt;\d+))?(?:\s+(?&lt;height&gt;\d+))?(?&lt;title&gt;\s+.+)?/i
        @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img }
        if /(?:"|')(?&lt;title&gt;[^"']+)?(?:"|')\s+(?:"|')(?&lt;alt&gt;[^"']+)?(?:"|')/ =~ @img['title']
          @img['title']  = title
          @img['alt']    = alt
        else
          @img['alt']    = @img['title'].gsub!(/"/, '&amp;#34;') if @img['title']
        end
        @img['class'].gsub!(/"/, '') if @img['class']
      end
      super
    end
  
    def render(context)
      if @img
        "&lt;img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}&gt;"
      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)
  </code></pre>
  
  <p>What is the best way to create another custom tag for exhibiting the same functionality for the [<code>&lt;img&gt;</code>] element, but nested within a [<code>&lt;figure&gt;</code>] element and perhaps displaying the image alt description or an additional token as a [<code>&lt;figcaption&gt;</code>] element which could potentially include its own link? Or possibly even a series of class names for the element that say whether or not it should be centered or not.</p>
  
  <p>In other words, I might want the output to be something like:</p>
  
  <pre><code>&lt;figure class=center&gt;
    &lt;img src="/contra.jpg" alt="One of the greatest nintendo games of all time"&gt;
    &lt;figcaption&gt;Up Up Down Down Left Right Left Right B A B A &lt;a href="http://www.youtube.com/contramoves/"&gt;Watch on Youtube&lt;/a&gt;&lt;/figcaption&gt;
  &lt;/figure&gt;
  </code></pre>
  
  <p>Am I wrong to assume that it is possible to nest custom Liquid tags? I'm sure I could rewrite the existing code a second time and modify it slightly to handle the additional attribute for [<code>&lt;figcaption&gt;</code>], but this seems rather redundant and against DRY principles. And as it currently stands, I'm rather confused as to how I might account for a possible, additional token given that the existing class takes <strong>optional</strong> tokens itself.</p>