如何将嵌套文件的名称存储在变量中并在 rake 中循环遍历它们

发布于 2024-09-08 13:20:28 字数 1381 浏览 3 评论 0原文

我有以下 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_VIEWSdump_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2024-09-15 13:20:28

要循环遍历所有子目录,请将“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,在您删除它们的扩展名后,它们都只是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:

[#<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">]

Which were generated from these files:

["/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"]

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

static_dir/bar/cheese/rodeo/outrageous/index.html
static_dir/bar/pizza/index.html
static_dir/tutorials/index.html

EDIT: In regards to addressing how to find the relative url, this seems to work

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

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文