Class: Nanoc::Filters::XSL

Inherits:
Nanoc::Filter show all
Defined in:
lib/nanoc/filters/xsl.rb

Overview

Since:

Constant Summary

Constant Summary

Constants inherited from Nanoc::Filter

Nanoc::Filter::TMP_BINARY_ITEMS_DIR

Instance Attribute Summary

Attributes inherited from Nanoc::Filter

#assigns

Instance Method Summary (collapse)

Methods inherited from Nanoc::Filter

#depend_on, #filename, from_binary?, #initialize, #output_filename, requires, setup, #setup_and_run, to_binary?, type

Methods included from PluginRegistry::PluginMethods

#all, #identifier, #identifiers, #named, #register

Methods inherited from Context

#get_binding, #initialize

Constructor Details

This class inherits a constructor from Nanoc::Filter

Instance Method Details

- (String) run(_content, params = {})

Runs the item content through an XSLT stylesheet using Nokogiri.

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.

Examples:

Invoking the filter as a layout


compile '/reports/*/' do
  layout 'xsl-report'
end

layout 'xsl-report', :xsl, :awesome => 'definitely'

Parameters:

  • _content (String)

    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.

  • params (Hash) (defaults to: {})

    The parameters that will be stored in corresponding xsl:param elements.

Returns:

  • (String)

    The transformed content

Since:

  • 3.3.0



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nanoc/filters/xsl.rb', line 34

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