I'm still having a lot of trouble getting tags to working properly
within Nanoc 2.1. There has been a lot of really great work done on this
since earlier versions. However, it's still not quite at the point of
working "out of the box".. yet.
I'm working towards having tag index pages automatically generated. At
the moment, I'm trying to do this via a Rake task and prodding around
with the Nanoc API. Seems simple enough but I am missing something!
I took the basic skeleton for trawling tags out of my site via this
article here:
https://nanoc.stoneship.org/blog/2008-07-20/
And applied some modification to then inline compile the pages rather
than generating an intermediate tag page in content. That bit, I think
works fine. I also hacked it slightly to add a different layout
attribute for tags index pages and dumped this into a Rake task:
# Build pages for each tag
tags_without_pages.each do |t|
# Buid page
puts t
page = Nanoc::Page.new("Tag: #{t}", { :tags => t, :layout => "tag"
}, "/tags/#{t}")
page.site = site
page.build_reps
pages << page
site.compiler.run([page])
end
But, and this is where I'm falling over, when attempting to compile the
page above I run into two key problems:
1) I can manage to generate an index page of pages titles only. If I try
to use any other attributes of the page, I get Nil problems all over the
place.
E.g. my regular front page "render-a-summary-of-the-last-few-articles"
ERB looks like this:
<% sorted_articles.last(5).each do |article| %>
<div class="article">
<h1 class="title"><%= article.page_id %></h1>
<p class="date"><%= article.created_at.strftime("%A %d %B %Y")
%></p>
<p class="excerpt"><%= auto_excerpt(article) %>... <%= link_to
"read " + article.page_id, article.page_id %></p>
<p class="tags">Tags: <%= tags_for(article, :base_url =>
'/tags/') %>.</p>
</div>
<% end %>
This works fine. But if I try to do the same thing within a tags index
ERB template tags.erb I get:
<% pages_with_tag(@page.tags).each do |article| %>
<div class="article">
<h1 class="title"><%= article.page_id %></h1>
<p class="date"><%= article.created_at.strftime("%A %d %B %Y")
%></p>
<p class="excerpt"><%= auto_excerpt(article) %>... <%= link_to
"read " + article.page_id, article.page_id %></p>
<p class="tags">Tags: <%= tags_for(article, :base_url =>
'/tags/') %>.</p>
</div>
<% end %>
I see compile errors of "can't covert Nil into String" which go away if
I remove references to page attributes other than "page_id" (which seems
to be the only attribute I can safely reference). i.e. there is
something different about the non-tags index articles somewhere...
So, what's the proper way to generate tag index pages?
2) A really annoying problem is also that my 'tags_for' links are all
now broken too when using the autocompiler.
E.g. say I have an article 'foo' that lives in
/output/articles/foo/index.html - I can link to this fine as
'/articles/foo/' via 'link_to' and Firefox follows it fine etc. all
works as it should.
But, if I try to do the same with my tags index pages, it doesn't work,
despite the <a href..> targets being identical:
<p class="excerpt"><a href="http://nanoc.stoneship.org/">Nanoc</a> in
itself is great for generating templatised static HTML pages. It's very
powerful and flexible with good built-in filter support and easy user...
<a href="/articles/nanoc-rake/">read Expanding the Power of Nanoc with
Rake</a></p>
<p class="tags">Tags: <a href="/tags/nanoc/">nanoc</a>, <a
href="/tags/rake/">rake</a>, <a href="/tags/sass/">sass</a>, <a
href="/tags/sitecopy/">sitecopy</a>.</p>
In the first line:
<a href="/articles/nanoc-rake/">read Expanding the Power of Nanoc with
Rake</a>
Links perfectly. Cool.
But, in the second line:
<a href="/tags/nanoc/">nanoc</a>
Gives a 404 when clicked on. If I instead type explicitly:
.../tags/nanoc/index.html it works fine and the page loads.
Is this a bug in the autocompiler? In Firefox?!?
Many thanks in advance for any advice you can offer!
Rob
> 1) I can manage to generate an index page of pages titles only. If I
> try
> to use any other attributes of the page, I get Nil problems all over
> the
> place.
The issue here is that the @pages array when compiling a page with ERB
is actually an array of page *proxies* (Nanoc::PageProxy). @page is a
proxy as well, and so are @assets and @asset (Nanoc::AssetProxy).
These proxies basically "rewrite" calls like "pageproxy.foo" to
"page.attribute_named(:foo)".
So, when you're getting a list of pages or assets straight from a
Nanoc::Site instance, you should know that these are actual
Nanoc::Pages or Nanoc::Assets, and in order to get attributes off
them, you need to use attribute_named(:something).
I would love nothing more than to simply get rid of proxies entirely.
In retrospect, this was a bad design decision. Unfortunately, it's
hard to revert this one; if I suddenly start passing Pages (with an
appropriate method_missing) instead of PageProxies around, then I risk
getting backward compatibility issues.
This doesn't meant that it's impossible; it simply would require a lot
of thought to make sure everything still works (fortunately nanoc has
an extensive test case). It's definitely on my todo list! Possibly for
2.2...
> 2) A really annoying problem is also that my 'tags_for' links are all
> now broken too when using the autocompiler. [..] But, in the second
> line:
>
> <a href="/tags/nanoc/">nanoc</a>
>
> Gives a 404 when clicked on. If I instead type explicitly:
> .../tags/nanoc/index.html it works fine and the page loads.
>
> Is this a bug in the autocompiler? In Firefox?!?
A bug in the autocompiler. When requesting a path for which no page
exists (e.g. /tags/nanoc/), but which *does* contain an index filename
(e.g. index.html), then the autocompiler should serve the index.html
file.
This bug will be fixed soon. Thanks for letting me know! :)
Regards,
Denis
--
Denis Defreyne
denis.d...@stoneship.org
Got it working now and it rocks.
I was just getting a bit confused between the Page and PageProxy objects
and that was catching me out.
I'll post some further notes on this in case other people are interested
or have a better approach.
Many thanks again! :)
> For anyone interested in this and how I've implemented it, you can
> find full details and code snippets here:
>
> http://www.remerson.plus.com/articles/nanoc-tags/
That's quite neat. I've added a link to that article on the nanoc Trac.
The original idea of generating placeholder tag pages is indeed much
better than letting rake generate temporary pages and compile those.
This is the technique I use for the archive pages for the nanoc site,
but I'm not really liking it either, so I may actually change the way
I create these archive pages some day.
By the way, "nanoc" is spelled "nanoc" and not "Nanoc", i.e. a
lowercase n. :)
> By the way, "nanoc" is spelled "nanoc" and not "Nanoc", i.e. a > lowercase n. :)