2

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.

Community
  • 1
  • 1
Dave Everitt
  • 17,193
  • 6
  • 67
  • 97

1 Answers1

2

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"
Jokester
  • 5,501
  • 3
  • 31
  • 39
  • Thanks, but I'm making sure my /dirname/index.md files are written to /dirname/index.html in my loop. My problem is understanding how to identify index files by the word 'index' in their content name, for the dirs I'm looping through... and yes, nanoc is great! – Dave Everitt Nov 15 '13 at 13:54
  • 1
    @DaveEveritt Ahh I found a (less document supported) way to tell `content/dirname/index.md`. See the update part. – Jokester Nov 15 '13 at 16:08
  • Thanks for the extra research, but from my attempts (in the commented out lines) it seems `item.identifier` can't take a statement with interpolated variables: `if item.identifier == "/#{dir}/index/"` and this is what I need. BUT I'll try `raw_filename`, report back and 'accept' if it works :-) – Dave Everitt Nov 16 '13 at 13:31
  • 2
    This is the correct answer. "index" is magic in nanoc, much like it's magic in web servers. The identifier for `/foo/index.md` is `/foo/`. `item.identifier` *never* has `/index/` at the end of it. – bobthecow Nov 17 '13 at 02:27
  • I know about the index magic, but your comment made me realise I've been a fool. I could simply add `/content/dir_name.md` at the same level as `/content/dir_name/*` to create `/dir_name/index.html` and `/dir_name/*.html`. D'oh. – Dave Everitt Nov 17 '13 at 21:50