如何将嵌套文件的名称存储在变量中并在 rake 中循环遍历它们
我有以下 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
方法以循环浏览视图/页面目录中的所有子目录?
我的视图目录看起来有点像这样: http://i45.tinypic.com/167unpw.gif
如果这让生活变得更轻松,我可以将所有名为 index.haml 的文件放在目录中。
谢谢
I have the following rake file to create a static version of my sinatra app,
stolen from 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
is now an array containing all the Haml files in the root of my 'views/pages' directory.
How do I modify ALL_VIEWS
and the dump_request_to_file
method to cycle through all the subdirectories in my views/pages directory?
My views directory looks a bit like this: http://i45.tinypic.com/167unpw.gif
If it makes life a lot easier, I could have all my files named index.haml, inside directories.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要循环遍历所有子目录,请将“views/pages”更改为“views/pages/**”
双splats告诉它递归搜索,您可以在文档中看到它
http://ruby-doc.org/core/classes/Dir.html# M002322
请注意,我没有彻底研究您的用例,但初步看来您可能在生成永久链接时遇到问题。当我检查结果时,我得到:
哪些是从这些文件生成的:
看起来您使用以下命令创建链接:
File.join(static_dir, view.permalink+'.html')
所以你可以看到,在这种情况下,这将创建三个文件,如
static_dir/index.html
一个相当明显的解决方案是包含相对的链接的一部分,所以它会变成
编辑:关于解决如何查找相对网址,这似乎有效
我不太热衷于 [0..-6] 事情,仅当您知道文件有后缀并且长度为五个字符时,它才有效。但我不会管它,因为我真的不知道你会如何处理我可能预期的不同的未来情况(即从 haml 生成一个 html 并提供它,现在你有两个文件 index.html和index.haml,在您删除它们的扩展名后,它们都只是index。或者styles.css,当您尝试通过拉入[0..-6]来删除其扩展名时,它会丢失部分文件名。
To cycle through all subdirs, change 'views/pages' to 'views/pages/**'
The double splats tells it to search recursively, you can see it in the docs at
http://ruby-doc.org/core/classes/Dir.html#M002322
Note that I haven't looked thoroughly at your use case, but preliminarily it appears that you may have trouble generating a permalink. When I checked the results, I got:
Which were generated from these files:
And it looks like you create the link with:
File.join(static_dir, view.permalink+'.html')
So you can see that in this case, that would create three files like
static_dir/index.html
A fairly obvious solution is to include the relative portion of the link, so it would become
EDIT: In regards to addressing how to find the relative url, this seems to work
I'm not all that keen on the [0..-6] thing, it only works if you know your file has a suffix and that it is five characters long. But I'm going to leave it alone since I don't really know how you would want to handle the different future situations I might anticipate (ie generate an html from the haml and serve that up, now you have two files index.html and index.haml, which, after you remove their extensions, are both just index. Or styles.css which loses part of its filename when you attempt to remove its extension by pulling in [0..-6]