中间人父母,兄弟姐妹,儿童方法

Lea*_*cim 0 ruby 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.
Run Code Online (Sandbox Code Playgroud)

这是父方法 http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#parent-instance_method

这是子方法 http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#children-instance_method

这是兄弟姐妹的方法

http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#siblings-instance_method

Bri*_*ung 8

有些洞穴探险到中间人的代码后,我发现,描述了一个黄瓜测试#parent,#children#siblings方法.

middleman/middleman-core/features/sitemap_traversal.feature:

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... 
Run Code Online (Sandbox Code Playgroud)
  • 简而言之,似乎父资源可以在文件名'index.html'中找到一级,因此如果您正在查看/foo/bar/some_resource.html,可以在其中找到其父级/foo/index.html.
  • 它的兄弟姐妹与请求处于同一级别(注意"同名目录"被转换为'索引文件',例如/foo/bar/变成/foo/bar.html,使其兄弟姐妹在该/foo/*级别上任何东西.)
  • 它的孩子在下面的水平.

通过将在各自的位置上的任意文件,在文件的层次结构,你应该能够引用文件,或一组,其中包括该文件,通过调用#parent,#children#siblings.

在阅读测试时要注意,在配置文件中设置了几条"假"路由(/sub/fake.htmlfake2.html,/directory-indexed/fake.htmlfake2.html).


更深入的潜水

如果你不能以面值进行黄瓜测试(我不怪你),还有更多!毕竟,这是什么,"我应该看到"和"我不应该看到"废话?那么,有一个答案.

在固定装置试验(中间人/中间人核/装置/遍历应用内/),layout.erb是不与任何内容的唯一文件.在其中,您可以看到Child,Parent和Sibling路径打印在html文档的正文中.

middleman/middleman-core/fixtures/traversal-app/source/layout.erb:

Path: <%= current_page.path %>

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

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

...continued...
Run Code Online (Sandbox Code Playgroud)

这有助于解释测试,这些测试只是在响应体中查找源自布局的字符串.您可以在Cucumber步骤定义(middleman/middleman-core/lib/middleman-core/step_definitions /)中查看测试的确切行为.

最后,布局中使用我们设置了摆在首位来描述方法,#parent,#children,和#siblings,这是在中间人核心定义.

middleman/middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb中:

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
Run Code Online (Sandbox Code Playgroud)

我将不再解释Ruby代码,因为我必须快点上班.如果您希望我进一步调查,请在评论中告诉我,我会回复它.