diff --git a/Rules b/Rules index 46e8215..b865676 100644 --- a/Rules +++ b/Rules @@ -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' diff --git a/lib/default.rb b/lib/default.rb index 076c858..487f79d 100644 --- a/lib/default.rb +++ b/lib/default.rb @@ -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 diff --git a/nanoc.yaml b/nanoc.yaml index aa56556..38207cd 100644 --- a/nanoc.yaml +++ b/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