Skip to content

Commit

Permalink
Add :knitr filter
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Feb 8, 2014
1 parent b39ebcd commit 9196358
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Rules
Expand Up @@ -23,6 +23,9 @@ compile '/blog/' do
end

compile '/blog/*' do
if item[:extension] == 'Rmd'
filter :knitr
end
filter :pandoc, ['mathjax']
filter :colorize_syntax, :default_colorizer => :pygmentsrb
layout 'post'
Expand Down
13 changes: 13 additions & 0 deletions lib/default.rb
Expand Up @@ -10,3 +10,16 @@ def ShortBlogName(item)
title = item[item.attributes.has_key?(:short_url) ? :short_url : :title]
title.split(%r{\W+}).join('_')
end


class KnitrFilter < Nanoc::Filter
identifier :knitr

def run(content, params={})
# Escape content to fit inside R's double quotes inside bash single quotes.
escaped_content = content.gsub(%r{\\}, "\\\\\\").gsub(%r{"}, '\"').gsub(%r{'}, "'\"'\"'").gsub(%r{\n}, "\\n")
command = ('Rscript -e \'library(knitr); cat(knit(quiet=TRUE, text="' +
escaped_content + '"))\'')
`#{command}`
end
end
2 changes: 1 addition & 1 deletion nanoc.yaml
@@ -1,7 +1,7 @@
# A list of file extensions that nanoc will consider to be textual rather than
# binary. If an item with an extension not in this list is found, the file
# will be considered as binary.
text_extensions: [ 'coffee', 'css', 'erb', 'haml', 'handlebars', 'hb', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'ms', 'mustache', 'pandoc', 'php', 'rb', 'sass', 'scss', 'txt', 'xhtml', 'xml' ]
text_extensions: [ 'coffee', 'css', 'erb', 'haml', 'handlebars', 'hb', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'ms', 'mustache', 'pandoc', 'php', 'Rmd', 'rb', 'sass', 'scss', 'txt', 'xhtml', 'xml' ]

# The path to the directory where all generated files will be written to. This
# can be an absolute path starting with a slash, but it can also be path
Expand Down

0 comments on commit 9196358

Please sign in to comment.