Sha256: 78e2e6a6b957f384bdd37b80f27ab17f221a347de2160fc88f3ad1c00a43b33d

Contents?: true

Size: 1.55 KB

Versions: 85

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Nanoc::Filters
  # @api private
  class XSL < Nanoc::Filter
    identifier :xsl

    requires 'nokogiri'

    always_outdated

    # 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

85 entries across 85 versions & 1 rubygems

Version Path
nanoc-4.8.19 lib/nanoc/filters/xsl.rb
nanoc-4.8.18 lib/nanoc/filters/xsl.rb
nanoc-4.8.17 lib/nanoc/filters/xsl.rb
nanoc-4.8.16 lib/nanoc/filters/xsl.rb
nanoc-4.8.15 lib/nanoc/filters/xsl.rb
nanoc-4.8.14 lib/nanoc/filters/xsl.rb
nanoc-4.8.13 lib/nanoc/filters/xsl.rb
nanoc-4.8.12 lib/nanoc/filters/xsl.rb
nanoc-4.8.11 lib/nanoc/filters/xsl.rb
nanoc-4.8.10 lib/nanoc/filters/xsl.rb
nanoc-4.8.9 lib/nanoc/filters/xsl.rb
nanoc-4.8.8 lib/nanoc/filters/xsl.rb
nanoc-4.8.7 lib/nanoc/filters/xsl.rb
nanoc-4.8.6 lib/nanoc/filters/xsl.rb
nanoc-4.8.5 lib/nanoc/filters/xsl.rb
nanoc-4.8.4 lib/nanoc/filters/xsl.rb
nanoc-4.8.3 lib/nanoc/filters/xsl.rb
nanoc-4.8.2 lib/nanoc/filters/xsl.rb
nanoc-4.8.1 lib/nanoc/filters/xsl.rb
nanoc-4.8.0 lib/nanoc/filters/xsl.rb