Skip to content

Instantly share code, notes, and snippets.

@bobthecow
Created October 10, 2012 16:06
Show Gist options
  • Save bobthecow/3866586 to your computer and use it in GitHub Desktop.
Save bobthecow/3866586 to your computer and use it in GitHub Desktop.
A proposal for the Nanoc 4.0 Rules DSL

A proposal for the Nanoc 4.0 Rules DSL

I'd love to see something very much like this:

# Rules

compile '/article/*' do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]

  rep :atom_feed do
    # Our atom feed rep doesn't need layout or #write,
    # since but it does need inline syntax highlighting
    filter :colorize_syntax,
      :default_colorizer => :coderay,
      :coderay => { :css => :style }
  end

  # A non-inline colorize (with a different colorizer) for all other reps
  filter :colorize_syntax

  rep :excerpt do
    layout 'excerpt'

    # Excerpt rep won't be written to a file, but
    # we'll need access to #compiled_content later,
    # so let's do an anonymous snapshot...
    snapshot
  end

  layout 'article'

  # "anonymous" rep for writing a PDF version
  rep do
    filter :pdf

    # write a PDF version of the article
    # note that this is chromeless because it's
    # before the 'default' layout.

    write item.identifier.chop + '.pdf'
  end

  layout 'default'

  write item.identifier + 'index.html'
end

Which would be strictly equivalent to this (old-style) Rules file:

# Rules

compile '/article/*', :rep => :atom_feed do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]

  filter :colorize_syntax,
    :default_colorizer => :coderay,
    :coderay => { :css => :style }
end
route '/article/*', :rep => :atom_feed do
  nil
end

compile '/article/*', :rep => :excerpt do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]
  filter :colorize_syntax

  layout 'excerpt'

  snapshot :some_arbitrary_name
end
route '/article/*', :rep => :excerpt do
  nil
end

compile '/article/*', :rep => :some_arbitrary_name do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]
  filter :colorize_syntax

  layout 'article'

  filter :pdf
end
route '/article/*', :rep => :some_arbitrary_name do
  item.identifier.chop + '.pdf'
end

compile '/article/*' do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]
  filter :colorize_syntax

  layout 'article'
  layout 'default'
end
route '/article/*' do
  item.identifier + 'index.html'
end

For comparison... here's that first rules file again:

(This time without comments)

# Rules

compile '/article/*' do
  filter :erb
  filter :redcarpet if ['md', 'mdown', 'markdown'].include? item[:extension]

  rep :atom_feed do
    filter :colorize_syntax,
      :default_colorizer => :coderay,
      :coderay => { :css => :style }
  end

  filter :colorize_syntax

  rep :excerpt do
    layout 'excerpt'
    snapshot
  end

  layout 'article'

  rep do
    filter :pdf
    write item.identifier.chop + '.pdf'
  end

  layout 'default'

  write item.identifier + 'index.html'
end

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment