Sha256: d9aa05f0adc08b1ff89eceab45ef6d4c36b3b9a151e2662760c448ef70c4b1a1

Contents?: true

Size: 850 Bytes

Versions: 3

Compression:

Stored size: 850 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(/(src|href)=(['"]?)(\/.+?)\2([ >])/) do
          $1 + '=' + $2 + relative_path_to($3) + $2 + $4
        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

3 entries across 3 versions & 1 rubygems

Version Path
nanoc3-3.0.2 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.1 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.0 lib/nanoc3/filters/relativize_paths.rb