Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fed78ebb1c
Fetching contributors…

Cannot retrieve contributors at this time

executable file 144 lines (118 sloc) 3.977 kb
#!/usr/bin/env ruby
# converts markdown letter to PDF
# TODO - rewrite as wrapper around markdown2pdf w/ custom template
require 'optparse'
require 'ostruct'
require 'erb'
require 'rubygems'
#require 'peg_markdown'
require 'pandoc-ruby'
require 'rtex'
# Set defaults
options = OpenStruct.new
options.fontsize = '11pt'
options.font = 'lmodern'
options.linespread = '1.1'
options.margin = '1.3in'
options.signature = ENV['HOME'] + '/Private/sig.pdf'
options.sign = true
options.template = nil
options.latex = false
opts = OptionParser.new
opts.on('-h', '--help') { puts opts }
opts.on('-f', '--fontsize FONTSIZE') { |s| options.fontsize = s }
opts.on('--font times|garamond|lmodern') { |s| options.font = s }
opts.on('--linespread NUMBER)') { |s| options.linespread = s }
opts.on('-m', '--margin MARGIN') { |m| options.margin = m }
opts.on('-s', '--signature SIGFILE') { |s| options.signature = s; options.sign = true }
opts.on('-n', '--no-sign') { options.sign = false }
opts.on('-l', '--latex') { options.latex = true }
opts.parse!(ARGV)
infile = ARGV[0]
if (not infile) || infile.empty?
puts "You must specify a file to convert."
exit 1
end
basename = File.basename(infile,File.extname(infile))
salutations = /^(Dear .*[:,]|To whom it may concern:|Recommendation for (.*):|Letter of recommendation for (.*):)\s*$/
closings = /^((Sincerely|Yours|Best|Sincerely yours),)/
rawbody = File.open(infile).read
salmatch = rawbody.match(salutations)
salutation, saloffset, sallength =
if salmatch
[PandocRuby.new(salmatch[0]).to_latex(:smart), salmatch.begin(0), salmatch[0].length]
else
["",0,0]
end
address = if saloffset > 0
PandocRuby.new(rawbody.slice!(0..(saloffset-1)).split("\n").join(" \n").strip).to_latex(:smart)
else
""
end
if sallength > 0
rawbody.slice!(0..(sallength-1))
end
closingmatch = rawbody.match(closings)
closing, closingoffset =
if closingmatch
[PandocRuby.new(closingmatch[0]).to_latex(:smart), closingmatch.begin(0)]
else
["$\\quad$",rawbody.length - 1]
end
endstuff = rawbody.slice!(closingoffset, rawbody.length - 1)
sign = options.sign
sigfile = options.signature
latexbody = PandocRuby.new(rawbody).to_latex(:smart)
defaulttemplate = <<EOF
\\documentclass[<%= options.fontsize %>,letterpaper,oneside]{ucbletter}
\\linespread{<%= options.linespread %>}
\\usepackage[pdftex]{graphicx}
<% if options.font == "garamond" %>
\\usepackage[expert,urw-garamond]{mathdesign}
<% elsif options.font == "times" %>
\\usepackage{txfonts}
<% elsif options.font == "lmodern" %>
\\usepackage{lmodern}
<% end %>
\\usepackage[mathletters]{ucs}
\\usepackage[utf8x]{inputenc}
\\usepackage[margin=<%= options.margin %>]{geometry}
\\usepackage{enumerate}
\\usepackage{hyperref}
\\email{jgm@berkeley.edu\\vspace{5pt}}
\\sigspace=5pt
\\name{John MacFarlane} % this only affects memos
\\location{John MacFarlane} % name under DEPARTMENT OF PHILOSOPHY
\\email{jgm@berkeley.edu} % email address under phone
\\signature{John MacFarlane\\\\Professor of Philosophy} % after the closing
\\department{DEPARTMENT OF PHILOSOPHY}
\\telephone{TEL: (510) 642-2722\\\\FAX: (510) 642-4164}
\\setlength{\\parindent}{0pt}
\\setlength{\\parskip}{6pt plus 2pt minus 1pt}
\\begin{document}
\\begin{letter}{<%= address %>}
\\opening{<%= salutation %>}
<%= latexbody %>
\\closing{<%= closing %><% if sign %>{\\vspace{20pt}\\\\\\scalebox{.55}{\\includegraphics{<%= sigfile %>}}}<% else %>\\vspace{30pt}<% end %>}
%\\encl{}
\\end{letter}
\\end{document}
EOF
template = if options.template
ERB.new(File.open(options.template).read)
else
ERB.new(defaulttemplate)
end
latexall = template.result
if options.latex
puts latexall
exit 0
end
output = RTeX::Document.new(latexall).to_pdf
if $? == 0
File.open(basename + '.pdf', 'wb').write(output)
puts "Created #{basename}.pdf"
exit 0
else
exit 1
end
Jump to Line
Something went wrong with that request. Please try again.