Ruby 中间人父母、兄弟姐妹、子女方法

Ruby 中间人父母、兄弟姐妹、子女方法,ruby,middleman,Ruby,Middleman,在middleman中创建页面时,如何指示哪些页面是父母/兄弟姐妹/孩子?文档提供了一些指示,说明了如何使用父兄弟姐妹和子方法来构建导航和面包屑,但没有说明如何在目录中排列页面,以便它们以适当的方式响应这些方法父、兄弟姐妹和子方法 Each resource in the sitemap is a Resource object. Pages can tell you all kinds of interesting things about themselves. You can access

在middleman中创建页面时,如何指示哪些页面是父母/兄弟姐妹/孩子?文档提供了一些指示,说明了如何使用父兄弟姐妹和子方法来构建导航和面包屑,但没有说明如何在目录中排列页面,以便它们以适当的方式响应这些方法父、兄弟姐妹和子方法

Each resource in the sitemap is a Resource object. Pages can tell you all kinds of interesting things about themselves. You can access frontmatter data, file extension, source and output paths, a linkable url, its mime type, etc. Some of the properties of the Page are mostly useful for Middleman's rendering internals, but you could imagine filtering pages on file extension to find all .html files, for example.

Each page can also find other pages related to it in the site hierarchy. The parent, siblings, and children methods are particularly useful in building navigation menus and breadcrumbs.
这是父方法

这是孩子们的方法

这是兄弟姐妹的方法


在深入研究了一下中间人的代码之后,我发现了一个Cucumber测试,它描述了父母、孩子和兄弟姐妹的方法

:

简言之,似乎可以在文件名为“index.html”的一级找到父资源,因此如果您正在查看/foo/bar/some_resource.html,则可以在/foo/index.html找到其父资源。 它的同级目录与请求处于同一级别请注意,同名目录被转换为“索引文件”,例如/foo/bar/beans/foo/bar.html,使它的同级目录处于/foo/*级别。 它的孩子们在下面的水平。 通过将任何文件放置在文件层次结构中各自的位置,您应该能够通过调用父级、子级或同级来引用该文件或包含该文件的集合

阅读测试时请注意,存在两个已设置的假路由/sub/fake.html和fake2.html,以及/directory index/fake.html和fake2.html

更深的潜水 如果你不能从表面上接受黄瓜测试,我不怪你,还有更多!毕竟,这是什么,我应该看到,我不应该看到胡说八道?嗯,这是有答案的

在测试夹具中,是唯一包含任何内容的文件。在其中,您可以看到子路径、父路径和同级路径打印在html文档的主体中

:


我不再解释Ruby代码了,因为我得赶紧去上班了。如果您希望我进一步调查,请在评论中告诉我,我会回复。

非常感谢。这太棒了。我会看看这个,然后再告诉你它的价值,我能够通过克隆中间人源代码,然后使用RubyMine的自动方法定义查找来导航,从而非常轻松地挖掘源代码。好的,我知道如何组织文件,但我在使用父、子、子、,显示站点结构的同级方法。有关于如何编写的提示吗?这是我不知道的,但是递归调用根目录及其所有子目录上的子目录如何?这样,您就可以映射出顶级目录所有子目录的子目录。另外,为了澄清,您正在尝试动态生成导航栏?谢谢,是的,导航栏,但是,它作为站点内容的视觉地图,也就是说,它显示文件之间的父、兄弟、子关系。
Feature: Step through sitemap as a tree

  Scenario: Root
    Given the Server is running at "traversal-app"
    When I go to "/index.html"
    Then I should see "Path: index.html"
    Then I should not see "Parent: index.html"
    Then I should see "Child: sub/index.html"
    Then I should see "Child: root.html"
    Then I should not see "Child: proxied.html"

...continued... 
Path: <%= current_page.path %>

Source: <%= current_page.source_file.sub(root + "/", "") %>

<% if current_page.parent %>
  Parent: <%= current_page.parent.path %>
<% end %>

...continued...
module Traversal
  # This resource's parent resource
  # @return [Middleman::Sitemap::Resource, nil]
  def parent
    parts = path.split("/")
    parts.pop if path.include?(app.index_file)

    return nil if parts.length < 1

    parts.pop
    parts << app.index_file

    parent_path = "/" + parts.join("/")

    store.find_resource_by_destination_path(parent_path)
  end

  # This resource's child resources
  # @return [Array<Middleman::Sitemap::Resource>]
  def children
    return [] unless directory_index?

    if eponymous_directory?
      base_path = eponymous_directory_path
      prefix    = %r|^#{base_path.sub("/", "\\/")}|
    else
      base_path = path.sub("#{app.index_file}", "")
      prefix    = %r|^#{base_path.sub("/", "\\/")}|
    end

    store.resources.select do |sub_resource|
      if sub_resource.path == self.path || sub_resource.path !~ prefix
        false
      else
        inner_path = sub_resource.path.sub(prefix, "")
        parts = inner_path.split("/")
        if parts.length == 1
          true
        elsif parts.length == 2
          parts.last == app.index_file
        else
          false
        end
      end
    end
  end

  # This resource's sibling resources
  # @return [Array<Middleman::Sitemap::Resource>]
  def siblings
    return [] unless parent
    parent.children.reject { |p| p == self }
  end

  ...continued...
end