Sha256: 21da8477ed1b2e7638e59e21cddef20fa8d6417c9660fbd1f6e061698a2ea2e8

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8

module Nanoc::Filters

  # @since 3.3.0
  class XSL < Nanoc::Filter

    requires 'nokogiri'

    # Runs the item content through an [XSLT](http://www.w3.org/TR/xslt)
    # stylesheet using  [Nokogiri](http://nokogiri.org/).
    #
    # This filter can only be run for layouts, because it will need both the
    # XML to convert (= the item content) as well as the XSLT stylesheet (=
    # the layout content).
    #
    # Additional parameters can be passed to the layout call. These parameters
    # will be turned into `xsl:param` elements.
    #
    # @example Invoking the filter as a layout
    #
    #     compile '/reports/*/' do
    #       layout 'xsl-report'
    #     end
    #
    #     layout 'xsl-report', :xsl, :awesome => 'definitely'
    #
    # @param [String] content Ignored. As the filter can be run only as a
    #   layout, the value of the `:content` parameter passed to the class at
    #   initialization is used as the content to transform.
    #
    # @param [Hash] params The parameters that will be stored in corresponding
    #   `xsl:param` elements.
    #
    # @return [String] The transformed content
    def run(content, params = {})
      Nanoc::Extra::JRubyNokogiriWarner.check_and_warn

      if assigns[:layout].nil?
        raise 'The XSL filter can only be run as a layout'
      end

      xml = ::Nokogiri::XML(assigns[:content])
      xsl = ::Nokogiri::XSLT(assigns[:layout].raw_content)

      xsl.apply_to(xml, ::Nokogiri::XSLT.quote_params(params))
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nanoc-3.7.3 lib/nanoc/filters/xsl.rb
nanoc-3.7.2 lib/nanoc/filters/xsl.rb
nanoc-3.7.1 lib/nanoc/filters/xsl.rb
nanoc-3.7.0 lib/nanoc/filters/xsl.rb
nanoc-3.6.11 lib/nanoc/filters/xsl.rb
nanoc-3.6.10 lib/nanoc/filters/xsl.rb