Sha256: 554016b7968483455fecb4255675c01a410f3cf6f4c51dbb9480d7325c76bb3a

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

# encoding: utf-8
require 'nokogiri'

module Nanoc::Filters

  # @since 3.3.0
  class XSL < Nanoc::Filter

    # 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 The XML 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={})
      if assigns[:layout].nil?
        raise "The XSL filter can only be run as a layout"
      end

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

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

  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
nanoc-3.5.0b1 lib/nanoc/filters/xsl.rb
nanoc-3.4.3 lib/nanoc/filters/xsl.rb
nanoc-3.4.2 lib/nanoc/filters/xsl.rb
nanoc-3.4.1 lib/nanoc/filters/xsl.rb
nanoc-3.4.0 lib/nanoc/filters/xsl.rb
nanoc-3.3.7 lib/nanoc/filters/xsl.rb
nanoc-3.3.6 lib/nanoc/filters/xsl.rb
nanoc-3.3.5 lib/nanoc/filters/xsl.rb
nanoc-3.3.4 lib/nanoc/filters/xsl.rb
nanoc-3.3.3 lib/nanoc/filters/xsl.rb
nanoc-3.3.2 lib/nanoc/filters/xsl.rb
nanoc-3.3.1 lib/nanoc/filters/xsl.rb
nanoc-3.3.0 lib/nanoc/filters/xsl.rb