Sha256: 4b82bf69cc38fcc8134ee1978a137fdf753f49a65361e3968bb8e8379ca971c6

Contents?: true

Size: 861 Bytes

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
nanoc3-3.0.5 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.4 lib/nanoc3/filters/relativize_paths.rb
nanoc3-3.0.3 lib/nanoc3/filters/relativize_paths.rb