Skip to content

Instantly share code, notes, and snippets.

@denisdefreyne
Created September 7, 2022 20:02
Show Gist options
  • Save denisdefreyne/432e1b50d52a403ae16cef6f567e79fc to your computer and use it in GitHub Desktop.
Save denisdefreyne/432e1b50d52a403ae16cef6f567e79fc to your computer and use it in GitHub Desktop.
Nanoc example: Creating separate pages for each photo

content/photos/dog.jpg

content/photos/dog.yaml:

alt: A *very* good doggo. Who’s a good girl? woof woof yes you are YOU ARE

Rules:

# …

compile '/photos/*' do
  # Write it to the exact same location
  write item.identifier
end

# For each photo, additionaly create another representation (called `detail` here but it could be anything).
compile '/photos/*', rep: :detail do
  filter :alt_to_content
  filter :kramdown
  layout '/default.*'
  write ext: 'html'
end

lib/filters/alt-to-content.rb:

Class.new(Nanoc::Filter) do
  identifier :alt_to_content

  # This filter applies to binary items, but returns textual content.
  type :binary => :text

  def run(content, params = {})
    # Extract the alt text from the attributes
    @item[:alt]
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment