Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby nanoc:按项名称或标识符测试编译规则时出现问题_Ruby_Nanoc - Fatal编程技术网

Ruby nanoc:按项名称或标识符测试编译规则时出现问题

Ruby nanoc:按项名称或标识符测试编译规则时出现问题,ruby,nanoc,Ruby,Nanoc,我在nanoc规则中使用以下测试,通过将各种内容(包括部分内容)与其同名布局进行匹配,来编译多个目录中的各种内容(包括部分内容) 现在,我已经将索引文件添加到每个内容目录中,但它们需要默认布局。如果我在'index.md'文件中的元数据中添加一个名为'index:'的项,这显然可以很好地工作 --- title: 'This page title' index: 'y' --- …但是检查if@item[:index]似乎有点笨拙,所以我一直在尝试(好吧,黑客入侵)从元数据中忽略“index:

我在nanoc规则中使用以下测试,通过将各种内容(包括部分内容)与其同名布局进行匹配,来编译多个目录中的各种内容(包括部分内容)

现在,我已经将索引文件添加到每个内容目录中,但它们需要默认布局。如果我在'index.md'文件中的元数据中添加一个名为'index:'的项,这显然可以很好地工作

---
title: 'This page title'
index: 'y'
---
…但是检查
if@item[:index]
似乎有点笨拙,所以我一直在尝试(好吧,黑客入侵)从元数据中忽略“index:”,并通过nanoc rep name或identifier进行测试-请参阅下面代码中注释掉的
if
语句:

layouts = ['layoutone','layouttwo','layoutetc']

layouts.each  do |dir|
  compile "/#{dir}/*" do
    # if item.identifier == "/#{dir}/index/"
    # if item.identifier =~ %r{/\w/index/}
    # if @item.rep_named(:index)
    if @item[:index]
     filter :kramdown
      layout "default"
    elsif @item[:inc]
      filter :erb
      filter :kramdown
      layout "#{dir}"
    else
      filter :kramdown
      layout "#{dir}"
    end
  end
end
我的注释行中的语法/逻辑有什么问题

编辑:
我在这里忽略了一个显而易见的问题:只需在与
/content/dir\u name/*
相同的级别添加
/content/dir\u name/*
即可创建
/dir\u name/index.html
/dir\u name/*.html
,并将规则应用于那些
/content/dir\u name.md
文件。

nanoc create site
之后,您是否更改了
nanoc.yaml
?因为我记得默认情况下,
nanoc
中的标识符不包含源文件名的最后一个
索引部分

比如说,文件
content/dirA/index.markdown
将具有标识符
/dirA/
或其他内容,并编译为
content/dirA/index.html
。这可能是您的
索引
正则表达式没有命中的原因

是的,有点棘手,但是nanoc很棒

更新 我找到了一种告诉内容文件名的方法:
item.raw\u filename

说它只适用于二进制文件,而在我的实验中它也适用于文本文件

# in compile block of Rules
item.identifier
# => "/aaa/"
item.raw_filename
# => "content/aaa.markdown"

谢谢,但是我要确保我的/dirname/index.md文件被写入循环中的/dirname/index.html。我的问题是理解如何通过内容名中的“index”一词来识别索引文件,对于我循环使用的目录。。。是的,nanoc很棒@DaveEveritt Ahh我找到了一种(不太支持文档)告诉
content/dirname/index.md
的方法。请参阅更新部分。感谢您的额外研究,但从我的尝试(在注释行中)来看,似乎
item.identifier
无法接受带有插值变量的语句:
if item.identifier==“/#{dir}/index/”
,这就是我需要的。但是我会尝试
raw\u filename
,返回报告并“接受”它是否有效:-)这是正确的答案。“索引”在nanoc中很神奇,就像它在web服务器中的魔力一样。
/foo/index.md
的标识符是
/foo/
<代码>项目。标识符
从来没有在它的末尾添加过
/index/
。我知道索引的魔力,但你的评论让我意识到我是个傻瓜。我可以简单地将
/content/dir\u name.md
添加到与
/content/dir\u name/*
相同的级别,以创建
/dir\u name/index.html
/dir\u name/*.html
。哦。