> One last thing, is there any way to access the extension of the
> files in /contents? It would simplify my rules a lot...
Hi,
Not directly; a Nanoc3::Item does not store the extension of the file
where it was created from. A Nanoc3::Item can be created from
arbitrary data, not just files on the disk, so this makes sense (e.g.
a database table containing items wouldn't store file extensions
either).
There is a way around this, if you feel like hacking a bit. You could
make the filesystem data source that you use (filesystem,
filesystem_combined, filesystem_compact) store the extension as an
attribute. For example, in lib/nanoc3/data_sources/
filesystem_compact.rb in the #items method, you have this:
attributes = meta.merge(:file =>
Nanoc3::Extra::FileProxy.new(content_filename))
Replace this line with something like
attributes = meta.merge({
:file => Nanoc3::Extra::FileProxy.new(content_filename),
:extension => File.extname(content_filename)
})
Now you will be able to access item[:extension] for all items. The
extension will be in the format ".md" or ".html"; if you want to get
rid of the dot, use File.extname(...)[1..-1] or so.
Note that the :file and :extension attribute will override any
existing metadata; if you want the automatically determined extension
to have lower priority, you could use something like this:
attributes = {
:file => Nanoc3::Extra::FileProxy.new(content_filename),
:extension => File.extname(content_filename)
}.merge(meta)
Hope this helps!
Denis
--
Denis Defreyne
denis.d...@stoneship.org
> This would greatly simplify my rules, more than anything. I understand
> your "philosophy" of extreme flexibility, and I do believe it's one of
> Nanoc's main selling points (stay tuned for an upcoming in-depth
> article on how I migrated h3rald.com from Typo to Nanoc), but in this
> case is there any chance to have this TINY modification right in the
> core?
>
> It wouldn't change a lot overall (at worst it would be left blank if
> no extension is available), but it could GREATLY simplify rule logic
> for someone like me!
Yep, you're right. I've made the necessary changes to the filesystem
data sources; see <http://projects.stoneship.org/hg/nanoc/rev/eb379813b7d9
> for details. The 'extension' attribute will always be overrideable
in the metadata itself, if necessary.
Regards,