Best way to do cool urls with nanoc and apache multiviews

12 views
Skip to first unread message

Pradeep

unread,
Nov 17, 2009, 12:25:11 AM11/17/09
to nanoc
Hi,

I am developing a site with nanoc and want to take advantage of apache
multiviews feature. http://httpd.apache.org/docs/1.3/content-negotiation.html

here is how I am trying to use it.

The rules file has a route that generates .html files with the content
filename as the output filename as well like.
content/article1.html
content/article2.html
content/posts/post1.html
becomes

output/article1.html
output/article2.html
output/posts/post1.html

Now if I have a link to article1.html and article2.html from
post1.html I dont want the .html to be there in the link.

I just want the links to be generated to be pointed as /article1 and /
article2. On deployment Apache multiviews will take care of locating
the right files article1.html and article2.html respectively when the
above links are accessed.

Now nanoc generates the links with the same path as it was written
given by item_rep.path. I have been fixing this by writing a custom
helper function mimicing link_to only it does a

path.gsub!(/.html/,'')
to strip of the .html

But this is a poor solution as I see there are other helper methods
(sitemap, atom_feed) that use item_rep.path and I need to override
them all to keep chopping.

Is there a cleaner way to do this instead of overriding the helpers?

Some solutions that I thought of..

1. have a another property that contains the preferred link. like
item_rep.linkpath. but then howto tell rest of the code that generates
links to use this linkpath property instead of the path property.?

2. override item_rep.path so that it does what I want (chop
extensions). Is this is possible will it have any bad consequences?

Note: I know that the generated links with .html extension will also
work but I want it to be generated without them.



Denis Defreyne

unread,
Nov 17, 2009, 6:28:19 AM11/17/09
to na...@googlegroups.com
On 17 Nov 2009, at 06:25, Pradeep wrote:

> Hi,
>
> I am developing a site with nanoc and want to take advantage of apache
> multiviews feature. http://httpd.apache.org/docs/1.3/content-negotiation.html
>
> [..]
>
> 2. override item_rep.path so that it does what I want (chop
> extensions). Is this is possible will it have any bad consequences?

Hi,

The second possibility (overriding #path) is probably the best way to
solve this problem. It’s fairly easy, too. #path is simply an
accessor, so you can override it and make it return a modified @path
instead of @path itself. For example:

class Nanoc3::ItemRep
def path
@path.sub(/\.[a-z]+$/, '')
end
end

Here’s a variant that uses File.extname to get the file extension,
which may work better:

class Nanoc3::ItemRep
def path
@path.sub(/#{Regexp.quote(File.extname(@path))}/, '')
end
end

It is not the cleanest solution and it might break at some point in
the future, but for now this is the best approach.

Hope this helps,

Denis

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

Pradeep

unread,
Nov 18, 2009, 4:24:18 AM11/18/09
to nanoc
Hi,

class Nanoc3::ItemRep
def path
@path.sub(/#{Regexp.quote(File.extname(@path))}/, '')
end


worked for me. No issues so far. Will post any updates over here.

Thanks again Denis.

Pradeep


On Nov 17, 4:28 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> On 17 Nov 2009, at 06:25, Pradeep wrote:
>
> > Hi,
>
> > I am developing a site with nanoc and want to take advantage of apache
> > multiviews feature.http://httpd.apache.org/docs/1.3/content-negotiation.html
> denis.defre...@stoneship.org
Reply all
Reply to author
Forward
0 new messages