我有以下rake文件来创建我的sinatra应用程序的静态版本, 从http://github.com/semanticart/stuff-site/blob/master/Rakefile
被盗class View
attr_reader :permalink
def initialize(path)
filename = File.basename(path)
@permalink = filename[0..-6]
end
end
view_paths = Dir.glob(File.join(File.dirname(__FILE__), 'views/pages', '*.haml'))
ALL_VIEWS = view_paths.map {|path| View.new(path) }
task :build do
def dump_request_to_file url, file
Dir.mkdir(File.dirname(file)) unless File.directory?(File.dirname(file))
File.open(file, 'w'){|f| f.print @request.get(url).body}
end
static_dir = File.join(File.dirname(__FILE__), 'public')
require 'sinatra'
require 'c4eo'
@request = Rack::MockRequest.new(Sinatra::Application)
ALL_VIEWS.each do |view|
puts view
dump_request_to_file("/#{view.permalink}", File.join(static_dir, view.permalink+'.html'))
end
end
ALL_VIEWS
现在是一个数组,其中包含我的'views / pages'目录根目录中的所有Haml文件。
如何修改ALL_VIEWS
和dump_request_to_file
方法以循环浏览views / pages目录中的所有子目录?
我的观察目录看起来有点像这样:http://i45.tinypic.com/167unpw.gif
如果它让生活变得更轻松,我可以将所有名为index.haml的文件放在目录中。
由于
答案 0 :(得分:2)
要循环浏览所有子目录,请将“views / pages”更改为“views / pages / **”
双重splats告诉它递归搜索,你可以在文档中看到它 http://ruby-doc.org/core/classes/Dir.html#M002322
请注意,我没有仔细查看您的用例,但初步看来您可能无法生成永久链接。当我检查结果时,我得到了:
[#<View:0x1010440a0 @permalink="hound">,
#<View:0x101044078 @permalink="index">,
#<View:0x101044000 @permalink="hound">,
#<View:0x101043f88 @permalink="index">,
#<View:0x101043f10 @permalink="references">,
#<View:0x101043e98 @permalink="do_find">,
#<View:0x101043e20 @permalink="index">,
#<View:0x101043da8 @permalink="README">]
这些是从这些文件生成的:
["/Users/josh/deleteme/fileutilstest/views/pages/bar/cheese/rodeo/hound.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/bar/cheese/rodeo/outrageous/index.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/hound.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/index.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/references.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/do_find.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/tutorials/index.haml",
"/Users/josh/deleteme/fileutilstest/views/pages/tutorials/README.haml"]
看起来您创建了以下链接: File.join(static_dir,view.permalink +'。html')
所以你可以看到,在这种情况下,会创建三个文件,如static_dir/index.html
一个相当明显的解决方案是包含链接的相对部分,因此它将成为
static_dir/bar/cheese/rodeo/outrageous/index.html
static_dir/bar/pizza/index.html
static_dir/tutorials/index.html
编辑:关于如何查找相对网址,这似乎有效
class View
attr_reader :permalink
def initialize( root_path , path )
root_path = File.expand_path(root_path).sub(/\/?$/,'/')
path = File.expand_path path
filename = path.gsub root_path , String.new
raise "#{path} does not appear to be a subdir of #{root_path}" unless root_path + filename == path
@permalink = filename[0..-6]
end
end
view_paths = Dir.glob(File.join(File.dirname(__FILE__), 'views/pages/**', '*.haml'))
ALL_VIEWS = view_paths.map { |path| View.new 'views/pages' , path }
require 'pp'
pp ALL_VIEWS
我不是那么热衷于[0 ..- 6]的事情,只有当你知道你的文件有一个后缀并且它是五个字符长时它才有用。但是我会不管它,因为我真的不知道你将如何处理我可能预期的不同的未来情况(即从haml生成一个html并提供服务,现在你有两个文件index.html和index.haml,在你删除它们的扩展名之后,它们都只是索引。或者styles.css在你试图通过拉入[0 ..- 6]
来删除它的扩展名时丢失了部分文件名。