Skip to content

Instantly share code, notes, and snippets.

@bsag
Created December 3, 2012 18:49
Show Gist options
  • Save bsag/4197052 to your computer and use it in GitHub Desktop.
Save bsag/4197052 to your computer and use it in GitHub Desktop.
Wrapper script for Pandoc. Sets some defaults and makes it easier to generate the small range of output formats I use. Requires the 'commander' gem.
#!/usr/bin/env ruby
require 'rubygems'
require 'commander/import'
program :version, '0.1'
program :description, 'Converts markdown documents into a variety of default formats using Pandoc'
CMD = "pandoc"
BIB = "$HOME/Dropbox/Documents/bibtex/all-refs.bib"
command :pdf do |c|
c.syntax = 'panda.rb pdf [options] file'
c.summary = 'Converts markdown to PDF'
c.description = 'Converts markdown to PDF via xelatex using nice fonts and margins'
c.example 'Number sections and just print result', 'panda.rb pdf --numbering --print myfile.md'
c.example 'Format bibliography using Proc R Soc style', 'panda.rb pdf --bibformat proc-r-soc-b myfile.md'
# appearance options
c.option '-n', '--numbering', 'Number sections'
c.option '-t', '--table-of-contents', 'Include table of contents'
c.option '-f', '--fontsize STRING', String, 'Font size in pts (default is 12pt)'
c.option '-m', '--margins STRING', String, 'Margins in cm (default is 3cm)'
# bib options
c.option '--bibformat STRING', String, "CSL bibformat to be used (turns on bibliography and uses bibliography file defined in source)"
# action options
c.option '-p', '--print', 'Print command without executing'
c.option '-o', '--open', 'Open the output file after it is built using default application'
c.action do |args, options|
# Set defaults which can be over-ridden by set options
options.default :fontsize => '12pt', :margin => '3cm'
filename = get_filename(args)
# Build basic command string then extend depending on options
cmd_options = "--variable mainfont='Minion Pro' --variable sansfont='Myriad Pro' --latex-engine=xelatex"
if options.toc
cmd_options.insert(0, "--toc ")
end
if options.fontsize
cmd_options.insert(0, "--variable fontsize=#{options.fontsize} ")
end
if options.margins
cmd_options.insert(0, "--variable geometry=margin=#{options.margins} ")
end
if options.numbering
cmd_options.insert(0, "-N ")
end
if options.bibformat
cmd_options.insert(0, "--bibliography=#{BIB} --csl=#{options.bibformat} ")
end
# Now put it all together with pandoc command and filenames
cmd_string = "#{CMD} #{cmd_options} #{filename}.md -o #{filename}.pdf"
# output command: via 'system' in normal case, or 'print' if options.print
if options.print
puts cmd_string
else
system cmd_string
end
# if options.open, open the output file with default application
if options.open
system "open #{filename}.pdf"
end
end
end
command :doc do |c|
c.syntax = 'panda.rb doc [options]'
c.summary = 'Converts markdown to Word (*.docx) format'
c.description = 'Converts markdown to Word'
c.example 'Number sections and add bibliography formatted in Proc R Soc style', 'panda.rb doc -n --bibformat proc-r-soc-b myfile.md'
# appearance options
c.option '--numbering', 'Number sections'
c.option '--table-of-contents', 'Include table of contents'
# bib options
c.option '--bibformat STRING', String, "CSL bibformat to be used (turns on bibliography and uses bibliography file defined in source)"
# action options
c.option '--print', 'Print command without executing'
c.option '--open', 'Open the output file after it is built using default application'
c.action do |args, options|
# Set defaults which can be over-ridden by set options
filename = get_filename(args)
cmd_options = "-s -S"
if options.toc
cmd_options.insert(0, "--toc ")
end
if options.numbering
cmd_options.insert(0, "-N ")
end
if options.bibformat
cmd_options.insert(0, "--bibliography=#{BIB} --csl=#{options.bibformat} ")
end
# Now put it all together with pandoc command and filenames
cmd_string = "#{CMD} #{cmd_options} #{filename}.md -o #{filename}.docx"
# output command: via 'system' in normal case, or 'print' if options.print
if options.print
puts cmd_string
else
system cmd_string
end
# if options.open, open the output file with default application
if options.open
system "open #{filename}.docx"
end
end
end
command :tex do |c|
c.syntax = 'panda.rb tex [options]'
c.summary = 'Converts markdown to LaTeX'
c.description = 'Converts markdown to LaTeX'
c.example '11pt font with bibliography formatted in Proc R Soc style', 'panda.rb text --fontsize 11pt --bibformat proc-r-soc-b myfile.md'
# appearance options
c.option '--numbering', 'Number sections'
c.option '--table-of-contents', 'Include table of contents'
c.option '--fontsize STRING', String, 'Font size in pts (default is 12pt)'
c.option '--margins STRING', String, 'Margins in cm (default is 3cm)'
# bib options
c.option '--bibformat STRING', String, "CSL bibformat to be used (turns on bibliography and uses bibliography file defined in source)"
# action options
c.option '--print', 'Print command without executing'
c.option '--open', 'Open the output file after it is built using default application'
c.action do |args, options|
# Set defaults which can be over-ridden by set options
options.default :fontsize => '12pt', :margin => '3cm'
filename = get_filename(args)
# Build basic command string then extend depending on options
cmd_options = "-s --variable mainfont='Minion Pro' --variable sansfont='Myriad Pro' --latex-engine=xelatex"
if options.toc
cmd_options.insert(0, "--toc ")
end
if options.fontsize
cmd_options.insert(0, "--variable fontsize=#{options.fontsize} ")
end
if options.margins
cmd_options.insert(0, "--variable geometry=margin=#{options.margins} ")
end
if options.numbering
cmd_options.insert(0, "-N ")
end
if options.bibformat
cmd_options.insert(0, "--bibliography=#{BIB} --csl=#{options.bibformat} ")
end
# Now put it all together with pandoc command and filenames
cmd_string = "#{CMD} #{cmd_options} #{filename}.md -o #{filename}.tex"
# output command: via 'system' in normal case, or 'print' if options.print
if options.print
puts cmd_string
else
system cmd_string
end
# if options.open, open the output file with default application
if options.open
system "open #{filename}.tex"
end
end
end
command :html do |c|
c.syntax = 'panda.rb html [options]'
c.summary = 'Converts markdown to standalone HTML document'
c.description = 'Converts markdown to standalone HTML document'
c.example 'Number sections and open using default browser after creation', 'panda.rb html -n --open myfile.md'
c.option '--numbering', 'Number sections'
c.option '--table-of-contents', 'Include table of contents'
# bib options
c.option '--bibformat STRING', String, "CSL bibformat to be used (turns on bibliography and uses bibliography file defined in source)"
# action options
c.option '--print', 'Print command without executing'
c.option '--open', 'Open the output file after it is built using default application'
c.action do |args, options|
# Set defaults which can be over-ridden by set options
options.default :fontsize => '12pt', :margin => '3cm'
filename = get_filename(args)
# Build basic command string then extend depending on options
cmd_options = "-s -S -c $HOME/.pandoc/templates/default.css"
if options.toc
cmd_options.insert(0, "--toc ")
end
if options.numbering
cmd_options.insert(0, "-N ")
end
if options.bibformat
cmd_options.insert(0, "--bibliography=#{BIB} --csl=#{options.bibformat} ")
end
# Now put it all together with pandoc command and filenames
cmd_string = "#{CMD} #{cmd_options} #{filename}.md -o #{filename}.html"
# output command: via 'system' in normal case, or 'print' if options.print
if options.print
puts cmd_string
else
system cmd_string
end
# if options.open, open the output file with default application
if options.open
system "open #{filename}.html"
end
end
end
# Return filename of first programme argument, without extension
def get_filename(args)
filename = File.basename(args[0],".*")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment