/
letter
executable file
·134 lines (108 loc) · 3.27 KB
/
letter
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
#!/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.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('-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
rawbody.slice!(0..(sallength-1))
closingmatch = rawbody.match(closings)
closing, closingoffset =
if closingmatch
[PandocRuby.new(closingmatch[0]).to_latex(:smart), closingmatch.begin(0)]
else
["",0]
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]{phil-letter}
\\usepackage[pdftex]{graphicx}
\\usepackage{pxfonts}
\\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\\\\
Professor of Philosophy}
\\setlength{\\parindent}{0pt}
\\setlength{\\parskip}{6pt plus 2pt minus 1pt}
\\begin{document}
\\begin{letter}{<%= address %>}
\\opening{<%= salutation %>}
<%= latexbody %>
\\closing{<%= closing %>
<% if sign %>\\\\ \\vspace{10pt}{\\scalebox{.55}{\\includegraphics{<%= sigfile %>}}}<% 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
output = IO.popen("rubber-pipe --pdf --module graphicx", "w+") do |pipe|
pipe.puts latexall
pipe.close_write
pipe.read
end
if $? == 0
File.open(basename + '.pdf', 'wb').write(output)
puts "Created #{basename}.pdf"
exit 0
else
exit 1
end