Using nanoc for blogging

132 views
Skip to first unread message

Schof

unread,
Dec 28, 2009, 1:44:43 PM12/28/09
to nanoc
As I see it, there are two essential bits of functionality required to
use nanoc as a blogging engine (three, but tagging is already
implemented and well-documented):

1) The ability to have archive pages autogenerated.

2) The ability to have a "home" page for the blog containing either
summaries of the last N articles or the full text of the last N
articles. (Summaries would be the first X paragraphs of the article,
or the contents of the article up to a "SPLIT" tag of some kind.)

I've seen examples for how to do #1 above. (Although I'm not sure if
the examples, such as h3rald's site, are still valid for Nanoc 3.) I
haven't seen any examples of #2 above.

What's the canonical/best practices way of doing both tasks?

Thanks very much!


John

PS: I'm documenting the process of building a site with nanoc on the
site I'm building with nanoc. Current docs to date are here: http://sch0f.com/

Matthew Helmick

unread,
Dec 28, 2009, 2:51:46 PM12/28/09
to na...@googlegroups.com
Comments? Unless moderated you would have to use a third-party comments engine. No?


--
You received this message because you are subscribed to the nanoc discusssion group.

To post to this group, send email to na...@googlegroups.com
To unsubscribe from this group, send email to nanoc+un...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/nanoc

Denis Defreyne

unread,
Dec 28, 2009, 3:22:19 PM12/28/09
to na...@googlegroups.com
On 28 Dec 2009, at 19:44, Schof wrote:

> As I see it, there are two essential bits of functionality required to
> use nanoc as a blogging engine (three, but tagging is already
> implemented and well-documented):
>
> 1) The ability to have archive pages autogenerated.

Hi,

You can auto-generate archive pages as well as pages for tags, categories, … using the “preprocess” block (http://nanoc.stoneship.org/manual/#preprocessing-the-site-data). The code in the preprocess block will be executed before the site is compiled, and it is the ideal place to create auto-generated pages.

The nanoc web site does this with the blog archives. There is a “page_size” configuration attribute that determines the number of items on a pageination page; the preprocess block divides the articles in groups that are page_size big. The rules file for the nanoc site can be found at http://projects.stoneship.org/hg/sites-nanoc/file/1afd7af8238d/Rules so you may want to take a look at that.

This way of generating extra items will not create physical items in the content/ directory; they’ll only be present in memory. If you want to generate the files on disk, you can use a Rake tasks that does the same.

> 2) The ability to have a "home" page for the blog containing either
> summaries of the last N articles or the full text of the last N
> articles. (Summaries would be the first X paragraphs of the article,
> or the contents of the article up to a "SPLIT" tag of some kind.)

If you simply want to display the latest article, you could do something like this on the home page (compare to the Stoneship index page at http://projects.stoneship.org/hg/sites-stoneship/file/5fd41bd0ee0d/content/index.erb):

<% latest_article = sorted_article.first %>
<h2><%= latest_article[:title] %></h2>
<%= latest_article.compiled_content(:snapshot => :pre) %>

(This uses the #compiled_content convenience function, which is not available in nanoc 3.0 by default, but can easily be added. See http://github.com/ddfreyne/nanoc/commit/4f5ad42665312089db0758ca63c961170e16982d and http://groups.google.co.uk/group/nanoc/browse_thread/thread/d03777d22c02018f for details.)

If you want to display a predefined article excerpt, you could define the excerpt in the page metadata and then print it, like this:

<% latest_article = sorted_article.first %>
<h2><%= latest_article[:title] %></h2>
<%= latest_article[:excerpt] %>

If you want to take the first N paragraphs (three or so) and then display a “read full article” link, you’ll probably have to do some Ruby string processing and perhaps even some HTML parsing (for which I recommend nokogiri -- see http://nokogiri.org/).

Hope this helps,

Denis

--
Denis Defreyne
denis.d...@stoneship.org

Denis Defreyne

unread,
Dec 28, 2009, 3:29:32 PM12/28/09
to na...@googlegroups.com
On 28 Dec 2009, at 20:51, Matthew Helmick wrote:

> Comments? Unless moderated you would have to use a third-party comments engine. No?

Hi,

There are two options if you want comments: you can either use some of the third-party comment engines, or you can use a dynamic server-side comment system that integrates with nanoc.

For the first approach, there is IntenseDebate (http://intensedebate.com/) and Disqus (http://disqus.com/). I’ve played with both of them for a little bit, but I don’t really have an opinion about which one is better.

The second approach involves letting nanoc write out files that are processed at the server side (for example, .php files). These files then connect to the database, fetch the comments for a page with a given ID that is hardcoded in the page. I implemented comments for Stoneship this way a long time ago, but I’m not a fan of this approach (and to be honest, I’m not a fan of comments in general).

So, comments are possible. :)

Regards,

John Mark Schofield

unread,
Dec 28, 2009, 4:57:31 PM12/28/09
to na...@googlegroups.com
On Mon, Dec 28, 2009 at 11:51 AM, Matthew Helmick <matt.h...@gmail.com> wrote:
Comments? Unless moderated you would have to use a third-party comments engine. No?


On Mon, Dec 28, 2009 at 10:44 AM, Schof <jscho...@gmail.com> wrote:
As I see it, there are two essential bits of functionality required to
use nanoc as a blogging engine (three, but tagging is already
implemented and well-documented):

If you want to comment on one of my blog entries, get your own blog and write about it there. ;-)

I'm actually considering some javascript that would load google search results of pages that link to the blog entry in question, to make that more seamless.



John

Schof

unread,
Dec 28, 2009, 5:13:30 PM12/28/09
to nanoc
On Dec 28, 12:22 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:

> > 1) The ability to have archive pages autogenerated.

> You can auto-generate archive pages as well as pages for tags, categories, … using the “preprocess” block (http://nanoc.stoneship.org/manual/#preprocessing-the-site-data). The code in the preprocess block will be executed before the site is compiled, and it is the ideal place to create auto-generated pages.
>
> The nanoc web site does this with the blog archives. There is a “page_size” configuration attribute that determines the number of items on a pageination page; the preprocess block divides the articles in groups that are page_size big. The rules file for the nanoc site can be found athttp://projects.stoneship.org/hg/sites-nanoc/file/1afd7af8238d/Rulesso you may want to take a look at that.


>
> This way of generating extra items will not create physical items in the content/ directory; they’ll only be present in memory. If you want to generate the files on disk, you can use a Rake tasks that does the same.

I'll try this out. I see no reason this should generate files in the
content/ directory; I think your solution will work perfectly.


> > 2) The ability to have a "home" page for the blog containing either
> > summaries of the last N articles or the full text of the last N
> > articles. (Summaries would be the first X paragraphs of the article,
> > or the contents of the article up to a "SPLIT" tag of some kind.)
>

> If you simply want to display the latest article, you could do something like this on the home page (compare to the Stoneship index page athttp://projects.stoneship.org/hg/sites-stoneship/file/5fd41bd0ee0d/co...


>
>         <% latest_article = sorted_article.first %>
>         <h2><%= latest_article[:title] %></h2>
>         <%= latest_article.compiled_content(:snapshot => :pre) %>
>

> (This uses the #compiled_content convenience function, which is not available in nanoc 3.0 by default, but can easily be added. Seehttp://github.com/ddfreyne/nanoc/commit/4f5ad42665312089db0758ca63c96...andhttp://groups.google.co.uk/group/nanoc/browse_thread/thread/d03777d22...for details.)


>
> If you want to display a predefined article excerpt, you could define the excerpt in the page metadata and then print it, like this:
>
>         <% latest_article = sorted_article.first %>
>         <h2><%= latest_article[:title] %></h2>
>         <%= latest_article[:excerpt] %>
>
> If you want to take the first N paragraphs (three or so) and then display a “read full article” link, you’ll probably have to do some Ruby string processing and perhaps even some HTML parsing (for which I recommend nokogiri -- seehttp://nokogiri.org/).

Thanks very much, Denis. However, this appears to get the "N latest
posts" when N is 1. For simplicity, let's say the full text of the N
latest articles on one page, where "N" is 10. Is this feasible? (In
the meantime, I'll read up on compiled_content and see if that will
get me where I'm going.)


Thanks very much for the quick replies. I appreciate your help, Denis!

John

Denis Defreyne

unread,
Dec 28, 2009, 5:18:34 PM12/28/09
to na...@googlegroups.com
On 28 Dec 2009, at 23:13, Schof wrote:

> Thanks very much, Denis. However, this appears to get the "N latest
> posts" when N is 1. For simplicity, let's say the full text of the N
> latest articles on one page, where "N" is 10. Is this feasible? (In
> the meantime, I'll read up on compiled_content and see if that will
> get me where I'm going.)

Hi,

You can do this: sorted_articles (a method in the Blogging helper) is an array of all articles ordered by date; to get the first five you’d do sorted_articles[0..4].

Regards,

Schof

unread,
Dec 29, 2009, 12:08:35 AM12/29/09
to nanoc
On Dec 28, 12:22 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> On 28 Dec 2009, at 19:44, Schof wrote:
> > 1) The ability to have archive pages autogenerated.

> You can auto-generate archive pages as well as pages for tags, categories, … using the “preprocess” block (http://nanoc.stoneship.org/manual/#preprocessing-the-site-data). The code in the preprocess block will be executed before the site is compiled, and it is the ideal place to create auto-generated pages.
>
> The nanoc web site does this with the blog archives. There is a “page_size” configuration attribute that determines the number of items on a pageination page; the preprocess block divides the articles in groups that are page_size big. The rules file for the nanoc site can be found athttp://projects.stoneship.org/hg/sites-nanoc/file/1afd7af8238d/Rulesso you may want to take a look at that.


>
> This way of generating extra items will not create physical items in the content/ directory; they’ll only be present in memory. If you want to generate the files on disk, you can use a Rake tasks that does the same.

I tried this tonight, and was unable to get it to produce output.
There are no archive pages generated in the output folder. Even with
the -V verbose option, nanoc3 simply compiles the site without
complaint. Stuff that was working previously still works. But no
archives.

I'm undoubtedly doing something stupid -- most likely I didn't copy
enough context from your Rules file, or the ERB layout files I copied
aren't being processed for some reason.

My entire site is up on BitBucket: http://bitbucket.org/schof/sch0f.com/src/
If I could impose, could you take a look and let me know what I'm
doing wrong?

I'm a little hesitant to ask this, because it seems like pushing my
luck as a question-asker a little too far. But at the moment, I'm
stuck. If your answer is "learn Ruby better" I won't be upset. ;-


Thanks very much!


John

Eric Sunshine

unread,
Dec 29, 2009, 12:28:41 AM12/29/09
to na...@googlegroups.com
On 12/29/2009 12:08 AM, Schof wrote:
> I tried this tonight, and was unable to get it to produce output.
> There are no archive pages generated in the output folder. Even with
> the -V verbose option, nanoc3 simply compiles the site without
> complaint. Stuff that was working previously still works. But no
> archives.
> My entire site is up on BitBucket: http://bitbucket.org/schof/sch0f.com/src/

Examining a bit of code you pulled from Denis's site source, one finds:

# Get articles
articles = items.select { |i| i[:kind] == 'article' }

This statement is looking through all items in your site which have a
"kind: article" property set in the .yaml file. Since you do not have
any such items in your website (at least not in the BitBucket
repository), you get no results.

To resolve, you need to add items of "kind: article"; each one
containing a single blog entry.

-- ES

Schof

unread,
Dec 29, 2009, 12:53:03 AM12/29/09
to nanoc

Thanks very much, Eric! That helped a great deal. I also had to add a
"created_at" property to the yaml files. One step forward. However,
now nanoc is barfing on the render method:

$ nanoc3 -V co
Loading site data...
Compiling site...

+--- /!\ ERROR /!\ -------------------------------------------+
| An exception occured while compiling the site. If you think |
| this is a bug in nanoc, please do report it at |
| <http://projects.stoneship.org/trac/nanoc/newticket> -- |
| thanks in advance! |
+-------------------------------------------------------------+

=== MESSAGE:

NoMethodError: undefined method `render' for #<Nanoc3::Extra::Context:
0x8397224>

=== COMPILATION STACK:

- [item] /news/1/ (rep default)

=== BACKTRACE:

0. item /news/1/ (rep default):1:in `get_binding'
1. /home/private/nanoc/lib/nanoc3/extra/context.rb:20:in
`get_binding'
2. /home/private/nanoc/lib/nanoc3/filters/erb.rb:15:in `run'
3. /home/private/nanoc/lib/nanoc3/base/item_rep.rb:166:in `filter'
4. /home/private/nanoc/lib/nanoc3/base/rule_context.rb:51:in
`filter'
5. (eval):34:in `load_rules'
6. /home/private/nanoc/lib/nanoc3/base/rule.rb:32:in `instance_eval'
7. /home/private/nanoc/lib/nanoc3/base/rule.rb:32:in `apply_to'
8. /home/private/nanoc/lib/nanoc3/base/compiler.rb:175:in
`compile_rep'
9. /home/private/nanoc/lib/nanoc3/base/compiler.rb:121:in
`compile_reps'
10. /home/private/nanoc/lib/nanoc3/base/compiler.rb:70:in `run'
11. /home/private/nanoc/lib/nanoc3/cli/commands/compile.rb:84:in
`run'
12. /home/private/.gem/ruby/1.8/gems/cri-1.0.1/lib/cri/base.rb:91:in
`run'
13. /home/private/nanoc/bin/nanoc3:16


Thanks very much, Eric!


John

Eric Sunshine

unread,
Dec 29, 2009, 1:01:30 AM12/29/09
to na...@googlegroups.com
On 12/29/2009 12:53 AM, Schof wrote:
> Thanks very much, Eric! That helped a great deal. I also had to add a
> "created_at" property to the yaml files. One step forward. However,
> now nanoc is barfing on the render method:
> $ nanoc3 -V co
> NoMethodError: undefined method `render' for #<Nanoc3::Extra::Context:
> 0x8397224>

You need to add the following line to your lib/default.rb file so that
the 'render' method is defined in your project:

include Nanoc3::Helpers::Rendering

For documentation on Nanoc3::Helpers::Rendering and 'render', see:
http://nanoc.stoneship.org/doc/3.0.0/Nanoc3/Helpers/Rendering.html

-- ES

Schof

unread,
Dec 31, 2009, 5:58:07 PM12/31/09
to nanoc


Thanks very much, Eric and Denis!

I also had to add the lib/sites-nanoc.rb file from the nanoc site to
get the format_as_date function. But I did that, and now it's working
like a charm! (Well, not quite like a charm -- there's some quirks to
be ironed out -- but it IS working.)

Thanks very much!

John Schofield

Reply all
Reply to author
Forward
0 new messages