Question

I use the following tests in a nanoc rule for compiling various kinds of content (including partials) in multiple directories by matching them with their identically-named layouts.

Now I've added index files to each content dir, but these need the default layout. This obviously works fine if I add an item named 'index:' to the metadata in the 'index.md' files…

---
title: 'This page title'
index: 'y'
---

…but checking for if @item[:index] seems a bit clunky, so I've been trying (well, hacking around) to find a way to omit 'index:' from the metadata and test by nanoc rep name or identifier - see the commented-out if statements in the code below:

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

What's wrong with the syntax/logic in my commented-out lines?

Edit:

I was missing the blindingly obvious here: simply add /content/dir_name.md at the same level as /content/dir_name/* to create /dir_name/index.html and /dir_name/*.html, and apply rules to those /content/dir_name.md files.

Was it helpful?

Solution

Did you change nanoc.yaml after nanoc create-site? Because I recall that by default, identifiers in nanoc don't contain the last index part of source file name.

Say, file content/dirA/index.markdown will have identifier /dirA/ or something, and compile to content/dirA/index.html. This may be the reason why your index regex didn't hit.

Yes, a little tricky, but nanoc is great.

update

I found a way to tell the content filename: item.raw_filename.

This document says it is only for binary files, while it also work on text files in my experiment.

# in compile block of Rules
item.identifier
# => "/aaa/"
item.raw_filename
# => "content/aaa.markdown"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top