Newbie Question - How do I use the static datasource?

928 views
Skip to first unread message

Andrew Myers

unread,
May 20, 2012, 7:50:47 AM5/20/12
to na...@googlegroups.com
Hi,

I think I am going to need to use the static datasource as per https://github.com/cboone/nanoc-static-data-source, however I'm not sure how to set it up?

Where do you need to save the static.rb file to?  Is it to /lib or somewhere else?

Also should the assets/ dir be under content/, or a sibling to content/

Sorry for the silly newbie question.  I'm sort of learning Ruby as I go, so it's all a little bit overwhelming at the moment ;-)

REgards,
Andrew.

Eric Sunshine

unread,
May 21, 2012, 5:53:51 AM5/21/12
to na...@googlegroups.com
You should place static.rb in lib/ or a subdirectory of lib/.

Your assets/ directory should be a sibling of content/, and you need
to configure the static data-source to point at your assets/ directory
via config.yaml. For instance, your config.yaml might contain this:

data_sources:
- type: filesystem_unified
items_root: /
layouts_root: /
- type: static
items_root: /assets

This configures the data-source "filesystem_unified" in the typical
fashion, and then configures the additional static data-source
"assets". In your Rules file, if you need special handling of static
assets, you can name them via identifiers like this:

compile '/assets/*/' do
# ...process...
end

route '/assets/*/' do
# ...compose name...
end

-- ES
> --
> 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?hl=en

Andrew Myers

unread,
May 25, 2012, 8:46:14 PM5/25/12
to na...@googlegroups.com
Thank you!

Eric Sunshine

unread,
Jul 9, 2012, 5:56:33 PM7/9/12
to na...@googlegroups.com
Hi Nathaniel,

Comments below...

On Sun, Jul 8, 2012 at 5:37 PM, Nathanael Phillips <na...@coffeebot.net> wrote:
> * My folder structure has content and assets as siblings ("/nanoc/content"
> and "/nanoc/assets").
> * config.yaml is:
> -
> type: static
> items_root: /assets

It's not clear from this excerpt if this configuration information
resides under the 'data_sources' key. If not, that could be a problem.

> * rules.rb has:
> route '/assets/*/' do
> "/assets/" + File.basename(item.identifier.chop) + item[:extension]
> end

This looks a bit suspect. For the static data-source, item.identifier
already contains the file extension, so there is no need to add it
again. Moreover, this rule is flattening the source /assets hierarchy
into a single output /assets directory. Is this really intended?
Typically, you could get by with the following much simpler rule which
retains the directory hierarchy:

route '/assets/*/' do
item.identifier.chop
end

> There are no compile errors, but at the same time, the assets don't appear
> in the output folder.

You could try examining the output of the nanoc subcommand
'show-plugins' to see if your static data-source is even recognized.
If it is, subcommands 'show-data' and 'show-rules' might tell you what
nanoc intends to do with the static items.

-- ES

Nathanael Phillips

unread,
Jul 9, 2012, 11:40:42 PM7/9/12
to na...@googlegroups.com
Thanks for the quick response, Eric!

the static configuration is under the data_sources key. 
from show-plugins:
Data Sources:

  builtin:
    delicious                                                   (Nanoc::DataSources::Delicious)
    filesystem, filesystem_verbose                              (Nanoc::DataSources::FilesystemVerbose)
    filesystem_combined, filesystem_compact, filesystem_unified (Nanoc::DataSources::FilesystemUnified)
    last_fm                                                     (Nanoc::DataSources::LastFM)
    twitter                                                     (Nanoc::DataSources::Twitter)
  custom:
    static                                                      (Nanoc::DataSources::Static)

I changed the routing item, too -- I'm building this off of another template, which is why I used that odd line; it's what they did ;)

Nothing for assets shows up in show-data or show-rules, though.  Any thoughts?

I am wondering if some of this is due to the setup the other folks did. I'm basing my site setup off of this: https://github.com/futuresimple/futuresimple.github.com

The main purpose is that they've built their site quite close to how I'm structuring mine, and it was a great way to learn nanoc. Now that I have some time with it under my belt, I'm wondering if I need to start from scratch....

Eric Sunshine

unread,
Jul 10, 2012, 12:15:06 AM7/10/12
to na...@googlegroups.com
The problem is that the static data-source assumes that the source
directory containing static assets is named "static". If you want to
use a source directory named "assets", then you need to configure the
data source as such:

data_sources:
- ...other...
- type: static
items_root: /assets
prefix: assets

Note the new 'prefix' key. Its value is relative to your top-level
project directory (hence no leading "/").

(On my own site, I use the default source name "static", so I neither
ran into the issue nor thought about it when I replied originally to
Andrew.)

-- ES

Eric Sunshine

unread,
Jul 10, 2012, 12:18:22 AM7/10/12
to na...@googlegroups.com
One other point: Rules match from top to bottom (first to last). Make
sure that the 'compile' and 'route' rules for your static assets
appear in Rules file before the catch-all compile/route rules match
wildcard '*' to ensure nanoc does not ignore your static asset rules.

-- ES

Nathanael Phillips

unread,
Jul 10, 2012, 11:02:37 PM7/10/12
to na...@googlegroups.com
Perfect, thanks! The prefix option worked like a charm :)

Thanks for the tip, and pointing out the power of the data-source and other subfunctions!


On Monday, July 9, 2012 9:18:22 PM UTC-7, Eric Sunshine wrote:
One other point: Rules match from top to bottom (first to last). Make
sure that the 'compile' and 'route' rules for your static assets
appear in Rules file before the catch-all compile/route rules match
wildcard '*' to ensure nanoc does not ignore your static asset rules.

-- ES


Matija Marohnić

unread,
Oct 29, 2012, 7:57:38 PM10/29/12
to na...@googlegroups.com
Is it possible to filter files from the static data source? My static folder is named "assets", I tried this:

compile "/assets/*/" do
  puts item.identifier.chop
  case item[:extension]
  when 'scss'
    if File.basename(item.identifier) == "app.scss"
      filter :sass, sass_options
      filter :relativize_paths, type: :css
    end
  when "coffee"
    filter :coffeescript
  end
end

but when I try to compile, an error is raised:

Nanoc::Errors::CannotUseTextualFilter: The “Nanoc::Filters::Sass” filter cannot be used to filter the “/assets/stylesheets/app.scss/” item (rep “default”), because textual filters cannot be used on binary items.

Any idea why nanoc considers app.scss as a binary file?

Eric Sunshine

unread,
Oct 29, 2012, 8:52:13 PM10/29/12
to na...@googlegroups.com
The static datasource is quite simple-minded and presumes that all
resources are binary:

https://github.com/cboone/nanoc-static-data-source/blob/master/static.rb#L27

You could reprogram the datasource to assign binary status
dynamically. Alternately, a work-around would be to add a 'preprocess'
block to your Rules file which assigns 'false' to the :binary
attribute of each item with .scss extension.

-- ES

Matija Marohnić

unread,
Oct 29, 2012, 9:11:17 PM10/29/12
to na...@googlegroups.com
I tried doing both. First I tried:
preprocess do
  items.each do |item|
    if /scss|coffee/.match(item[:extension])
      item[:binary] = false
    end
  end
end
but the error remained. Then I modified a part of static.rb:
filenames.map do |filename|
  attributes = {
    :extension => File.extname(filename)[1..-1],
    :filename  => filename,
  }
  identifier = filename[(prefix.length+1)..-1] + '/'

  binary     = /scss|coffee/.match(attributes[:extension])
  mtime      = File.mtime(filename)
  checksum   = checksum_for(filename)

  Nanoc3::Item.new(
    filename,
    attributes,
    identifier,
    :binary => binary, :mtime => mtime, :checksum => checksum
  )
end
but the error still remained. What am I doing wrong?

Eric Sunshine

unread,
Oct 29, 2012, 10:39:35 PM10/29/12
to na...@googlegroups.com
Have you verified that the :binary attribute actually has been set to
'false'? (Print out the item ID and :binary attribute for each matched
item in 'preprocess' and/or the static data source.) Did you clear the
nanoc cache? (The cache should have been recognized as out-of-date
automatically, but, if not, you might try deleting the 'tmp' and
'output' folders manually.)

-- ES


On Mon, Oct 29, 2012 at 9:11 PM, Matija Marohnić

Denis Defreyne

unread,
Oct 30, 2012, 2:27:18 AM10/30/12
to na...@googlegroups.com
On 30 Oct 2012, at 00:57, Matija Marohnić <matija....@gmail.com> wrote:

> Is it possible to filter files from the static data source?

The static data source’s intention is to be used in case you want to pass-through files and simply copy them to output/ without filtering them. I recommend that you move your CSS/SCSS/… files that you want to filter into content/assets/ instead.

Cheers

Denis

Matija Marohnić

unread,
Oct 30, 2012, 3:40:03 AM10/30/12
to na...@googlegroups.com
Deleting the contents of tmp also didn't work. Ok, I guess I'll move them to content/assets, I just wanted to have content/index.haml and content/assets/stylesheets/index.scss file without having to prefix stylesheets with an underscore.

Matija Marohnić

unread,
Oct 30, 2012, 6:01:11 PM10/30/12
to na...@googlegroups.com
I just found out I was using Ruby's string#match method like it was regex#match, so no wonder it was false.

Dave Everitt

unread,
Oct 31, 2012, 12:09:30 PM10/31/12
to na...@googlegroups.com
many posts seem to try over-complex code as a way of solving issues... but as Denis wrote:

The static data source’s intention is to be used in case you want to pass-through files and simply copy them to output/ without filtering them. I recommend that you move your CSS/SCSS/… files that you want to filter into content/assets/ instead.

which is exactly what I did on first using Nanoc :-)
Reply all
Reply to author
Forward
0 new messages