Sha256: ec496dc87eb4388a4ad8c4dcc697dcbf9ca8130947dffabaf7dfb6ae0b815c5f

Contents?: true

Size: 861 Bytes

Versions: 8

Compression:

Stored size: 861 Bytes

Contents

# encoding: utf-8

module Nanoc3::Filters
  class RelativizePaths < Nanoc3::Filter

    require 'nanoc3/helpers/link_to'
    include Nanoc3::Helpers::LinkTo

    def run(content, params={})
      # Set assigns so helper function can be used
      @item_rep = assigns[:item_rep] if @item_rep.nil?

      # Filter
      case params[:type]
      when :html
        content.gsub(/(<[^>]+\s+(src|href))=(['"]?)(\/.*?)\3([ >])/) do
          $1 + '=' + $3 + relative_path_to($4) + $3 + $5
        end
      when :css
        content.gsub(/url\((['"]?)(\/.*?)\1\)/) do
          'url(' + $1 + relative_path_to($2) + $1 + ')'
        end
      else
        raise RuntimeError.new(
          "The relativize_paths needs to know the type of content to " +
          "process. Pass :type => :html for HTML or :type => :css for CSS."
        )
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nanoc3-3.1.0b1 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.1.0a3 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.9 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.8 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.1.0a2 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.1.0a1 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.7 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.6 lib/nanoc3/filters/relativize_paths.rb