Created
December 9, 2009 17:17
-
-
Save mattstevens/252609 to your computer and use it in GitHub Desktop.
Webby filter to create paths readable by the file system for CHM and Mac help bundles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Webby | |
class Renderer | |
attr_reader :page | |
end | |
module Filters | |
# To use add localpath to the end of the list of filters in the layout. | |
# Converts all links starting with '/' to use relative paths and appends | |
# 'index.html' to all links both starting and ending with '/'. | |
class LocalPath | |
def initialize( str, dir ) | |
@str = str | |
@dir = dir | |
end | |
def rel_path(base, path) | |
base.gsub!(/\\/,'/') | |
path.gsub!(/\\/,'/') | |
base = base.split('/') | |
path = path.split('/') | |
i = 0 | |
while ((base[i] == path[i]) && (i < base.size)) | |
i += 1 | |
end | |
'../'*(base.size - i) + path[i..-1].join('/') | |
end | |
def filter | |
rgxp = %r/="\/([^"]*)"/ | |
@str.gsub!(rgxp) do | |
path = $1 | |
path << "index.html" if path[-1,1] == '/' || path.empty? | |
path = rel_path(@dir, path) | |
%Q(="#{path}") | |
end | |
end | |
end | |
register :localpath do |input, cursor| | |
LocalPath.new(input, cursor.renderer.page.dir).filter | |
end | |
end # module Filters | |
end # module Webby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment