Class: Nanoc::Filters::RelativizePaths
- Inherits:
-
Nanoc::Filter
- Object
- Context
- Nanoc::Filter
- Nanoc::Filters::RelativizePaths
- Includes:
- Helpers::LinkTo
- Defined in:
- lib/nanoc/filters/relativize_paths.rb
Constant Summary
- SELECTORS =
[ '*/@href', '*/@src', 'object/@data', 'param[@name="movie"]/@content', 'comment()' ]
Constants inherited from Nanoc::Filter
Nanoc::Filter::TMP_BINARY_ITEMS_DIR
Instance Attribute Summary
Attributes inherited from Nanoc::Filter
Instance Method Summary (collapse)
-
- (String) run(content, params = {})
Relativizes all paths in the given content, which can be HTML, XHTML, XML or CSS.
Methods included from Helpers::LinkTo
#link_to, #link_to_unless_current, #relative_path_to
Methods included from Helpers::HTMLEscape
Methods included from Helpers::Capturing
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
Constructor Details
This class inherits a constructor from Nanoc::Filter
Instance Method Details
- (String) run(content, params = {})
Relativizes all paths in the given content, which can be HTML, XHTML, XML
or CSS. This filter is quite useful if a site needs to be hosted in a
subdirectory instead of a subdomain. In HTML, all href
and src
attributes will be relativized. In CSS, all url()
references will be
relativized.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nanoc/filters/relativize_paths.rb', line 31 def run(content, params = {}) Nanoc::Extra::JRubyNokogiriWarner.check_and_warn # Set assigns so helper function can be used @item_rep = assigns[:item_rep] if @item_rep.nil? # Filter case params[:type] when :css # FIXME parse CSS the proper way using csspool or something content.gsub(/url\((['"]?)(\/(?:[^\/].*?)?)\1\)/) do 'url(' + $1 + relative_path_to($2) + $1 + ')' end when :html, :xml, :xhtml selectors = params.fetch(:select) { SELECTORS } namespaces = params[:namespaces] || {} require 'nokogiri' case params[:type] when :html klass = ::Nokogiri::HTML when :xml klass = ::Nokogiri::XML when :xhtml klass = ::Nokogiri::XML # FIXME cleanup because it is ugly # this cleans the XHTML namespace to process fragments and full # documents in the same way. At least, Nokogiri adds this namespace # if detects the `html` element. content = content.sub(%r{(<html[^>]+)xmlns="http://www.w3.org/1999/xhtml"}, '\1') end nokogiri_process(content, selectors, namespaces, klass, params[:type]) else raise RuntimeError.new( 'The relativize_paths needs to know the type of content to ' + 'process. Pass a :type to the filter call (:html for HTML, ' + ':xhtml for XHTML, :xml for XML, or :css for CSS).') end end |