Filtering by file extensions

57 views
Skip to first unread message

Satheesh Babu Vattekkat

unread,
Dec 11, 2009, 5:47:13 AM12/11/09
to nanoc
I'm making a site that is fed by different sources. Some of the
content is in html. The content I want to enter manually, I'd like to
enter in markdown or textile. I am not able to come up with a rules
file that can compile based on different extensions. What I could do
is to put all txt files in some folders and put a rule like

compile '/whatever/*' do
filter maruku
...

But when I do like

compile '*.txt' do
filter maruku
...
end

compile '*.html' do
filter erb

It complains that nothing is there for /. So I added a rule for '/'
too with filter as erb after these two rules. That compiles .txt with
erb as well.

Any thoughts?

Denis Defreyne

unread,
Dec 14, 2009, 4:17:12 AM12/14/09
to na...@googlegroups.com
Hi,

I believe that you are confusing item identifiers with paths. When the data source loads an item, the item is given an identifier in the form “/about/” or “/games/torchlight/”. These identifiers correspond with the files “content/about.html” and “content/games/torchlight.html” (when using the default data source).

Compilation and routing rules take a pattern that is matched against identifiers, not paths. Therefore, you can’t directly create a pattern that matches items with a given extension. (This would’t really be possible for all situations; after all, an item that is loaded from a database does not have an extension.)

Fortunately, the filesystem data sources set an “extension” attribute, which you can use to conditionally compile items:

compile '*' do
case item[:extension]
when '.txt'

when '.md'

when '.html'

end

Hope this helps,

Denis

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

Eric Sunshine

unread,
Dec 14, 2009, 12:47:37 PM12/14/09
to nanoc
On Dec 14, 4:17 am, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> Fortunately, the filesystem data sources set an “extension”
> attribute, which you can use to conditionally compile items:
>   compile '*' do
>   case item[:extension]
>     when '.txt'
>   ...
> end

This :extension attribute seems to exist only in nanoc 2.x, but is
absent (unfortunately, perhaps) from nanoc 3.x.

-- ES

Denis Defreyne

unread,
Dec 14, 2009, 12:51:14 PM12/14/09
to na...@googlegroups.com
Ah, yikes… you’re right. It’s not present in nanoc 3.0.x, but it will be there in nanoc 3.1.x. There’s a mailinglist discussion about this from a few months back, though; see <http://groups.google.com/group/nanoc/browse_thread/thread/6c82039ecb32c377/daa1719f04c74cf5?lnk=gst&q=meta+merge#daa1719f04c74cf5> for details. It talks about adding the extension to the data sources as an :extension attribute.

Eric Sunshine

unread,
Dec 14, 2009, 1:04:19 PM12/14/09
to nanoc
On Dec 14, 12:51 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> On 14 Dec 2009, at 18:47, Eric Sunshine wrote:
> > This :extension attribute seems to exist only in nanoc 2.x, but is
> > absent (unfortunately, perhaps) from nanoc 3.x.
> Ah, yikes… you’re right. It’s not present in nanoc 3.0.x, but it will be there in nanoc 3.1.x.

In the meantime, Satheesh Babu Vattekkat, you can take a hint from the
nanoc's own website sources. In each asset's .yaml file, indicate the
type of filtering which should be applied to that asset. If you look
at the .yaml file of nanoc's news items, for instance, you will see
"markdown: true" (or false), and the compile rule looks something
like:

compile '*' do
filter :maruku if item[:markdown]
...
end

Even better, the .yaml file could specify which filter to apply. For
instance "filter: maruku" or "filter: erb".

compile '*' do
filter item[:filter].intern
...
end

Even more general would be to allow a list of filters to be specifies
in the .yaml file. For instance "filters: [erb, maruku]".

-- ES

sickill

unread,
Dec 15, 2009, 3:48:05 AM12/15/09
to nanoc
Thanks for the tip! This is especially useful when compiling blog
posts in different formats (textile, markup). I have "markup: sth"
property in all my posts, but now I'll use this one, thanks!

Marcin

On Dec 14, 10:17 am, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> denis.defre...@stoneship.org

Satheesh Babu Vattekkat

unread,
Dec 16, 2009, 11:30:28 AM12/16/09
to nanoc
Thanks! That really worked!

On Dec 14, 11:04 pm, Eric Sunshine <sunsh...@sunshineco.com> wrote:
>     filter item[:filter].intern
Reply all
Reply to author
Forward
0 new messages