Skip to content
This repository

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 141 lines (118 sloc) 3.977 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#!/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
Something went wrong with that request. Please try again.