Skip to content

Instantly share code, notes, and snippets.

@trcook
Last active August 29, 2015 14:09
Show Gist options
  • Save trcook/bffd316d8f85d970ef34 to your computer and use it in GitHub Desktop.
Save trcook/bffd316d8f85d970ef34 to your computer and use it in GitHub Desktop.
This is an easy rakefile to get rmd files in a directory, parse them through knitr and get them ready for processing by pandoc or something else as markdown.
### THE SCRIPT TO RUN -- MAKE SURE IT'S NOT INDENTED -- script goes bretween EOS's @input_file and @output_file are replaced with input and output files.
task :default=>"brownbag:install"
desc %{builds slides.md for use by reval}
task :reveal=>"brownbag:reveal"
namespace :brownbag do
###
SCRIPT_TO_RUN =<<EOS
Rscript -e 'require(knitr);options("revealout"="n");render_markdown();knit("@input_file", output="@output_file")'
EOS
REVEAL_SCRIPT_TO_RUN=<<EOS
Rscript -e 'require(knitr);options("revealout"="y");render_markdown();knit("@input_file", output="@output_file")'
EOS
my_rmd_files=FileList[Dir.glob("./*.rmd", File::FNM_CASEFOLD)]
my_reveal_md_files=my_rmd_files.ext("md")
my_pandoc_md_files=my_reveal_md_files.pathmap("%d/pandoc_%f")
out_file=my_pandoc_md_files.pathmap("%d/%{pandoc_,}n.pdf")
desc %{Build RMD files;}
task :install
task :install=>my_pandoc_md_files
task :install=>out_file
desc %{Builds reveal md variant of slides.rmd}
file my_reveal_md_files=>my_rmd_files
desc %{demands final pandoc pdf gets made for each of #{my_pandoc_md_files}}
# file out_file=>my_pandoc_md_files
# file my_pandoc_md_files=>my_rmd_files do |t|
# puts t.name
# proc_and_move(t)
# end
# task :reveal
task :reveal=>my_reveal_md_files
namespace :install do
rule (%r{pandoc_.*\.md}) => lambda{|objfile| my_rmd_files.find{objfile.pathmap("%{pandoc_,}n")}} do |t|
puts "#{t.source}#{t.name}"
proc_and_move(t)
end
end
namespace :reveal do
rule ".md" =>".rmd" do |t|
puts "#{t.source}#{t.name}"
proc_and_move(t,1)
end
end
rule ".pdf" =>->(objfile){objfile.pathmap("%{,pandoc_}n.md")} do |t|
sh %Q{pandoc #{t.source.ext("md")} -f markdown -t beamer --slide-level=2 -o #{t.name.ext('tex')} -V theme:Amsterdam -s --biblatex --bibliography="/s/dissandprojects.bib" --template=class_presentation.beamer --toc}
sh %Q{latexmk -pdf #{t.name.ext()} }
sh %Q{latexmk -c #{t.name.ext()}}
end
define_method(:proc_and_move) do |t,*args|
unless args.empty?
system %{
#{REVEAL_SCRIPT_TO_RUN.sub("@input_file",t.source).sub("@output_file",t.name)}
}
return
end
puts "other#{t.name},#{t.source}"
system %{
#{SCRIPT_TO_RUN.sub("@input_file",t.source).sub("@output_file",t.name)}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment