/
latex.rb
287 lines (242 loc) · 6.19 KB
/
latex.rb
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'yaml'
module RedCloth::Formatters::LATEX
include RedCloth::Formatters::Base
ENTITIES = YAML::load(File.read(File.dirname(__FILE__)+'/latex_entities.yml'))
module Settings
# Maps CSS style names to latex formatting options
def latex_image_styles
@latex_image_class_styles ||= {}
end
end
RedCloth::TextileDoc.send(:include, Settings)
# headers
{ :h1 => 'section',
:h2 => 'subsection',
:h3 => 'subsubsection',
:h4 => 'paragraph',
:h5 => 'subparagraph',
:h6 => 'textbf',
}.each do |m,tag|
define_method(m) do |opts|
"\\#{tag}{#{opts[:text]}}\n\n"
end
end
# commands
{ :strong => 'textbf',
:em => 'emph',
:i => 'textit',
:b => 'textbf',
:ins => 'underline',
:del => 'sout',
}.each do |m,tag|
define_method(m) do |opts|
"\\#{tag}{#{opts[:text]}}"
end
end
# inline verbatim
define_method(:code) do |opts|
opts[:block] ? opts[:text] : "\\verb@#{opts[:text]}@"
end
# sub/superscripts
{ :sup => '\textsuperscript{#1}',
:sub => '\textsubscript{#1}',
}.each do |m, expr|
define_method(m) do |opts|
expr.sub('#1', opts[:text])
end
end
# environments
{ :pre => 'verbatim',
:cite => 'quote',
}.each do |m, env|
define_method(m) do |opts|
begin_chunk(env) + opts[:text] + end_chunk(env)
end
end
# ignore (or find a good solution later)
[ :span,
:div,
:acronym,
:caps
].each do |m|
define_method(m) do |opts|
opts[:text].to_s
end
end
{ :ol => 'enumerate',
:ul => 'itemize',
}.each do |m, env|
define_method("#{m}_open") do |opts|
opts[:block] = true
"\\begin{#{env}}\n"
end
define_method("#{m}_close") do |opts|
"#{li_close}\\end{#{env}}\n\n"
end
end
def li_open(opts)
"#{li_close unless opts.delete(:first)} \\item #{opts[:text]}"
end
def li_close(opts=nil)
"\n"
end
def p(opts)
opts[:text] + "\n\n"
end
def td(opts)
column = @table_row.size
if opts[:colspan]
opts[:text] = "\\multicolumn{#{opts[:colspan]}}{ #{"l " * opts[:colspan].to_i}}{#{opts[:text]}}"
end
if opts[:rowspan]
@table_multirow_next[column] = opts[:rowspan].to_i - 1
opts[:text] = "\\multirow{#{opts[:rowspan]}}{*}{#{opts[:text]}}"
end
@table_row.push(opts[:text])
return ""
end
def tr_open(opts)
@table_row = []
return ""
end
def tr_close(opts)
multirow_columns = @table_multirow.find_all {|c,n| n > 0}
multirow_columns.each do |c,n|
@table_row.insert(c,"")
@table_multirow[c] -= 1
end
@table_multirow.merge!(@table_multirow_next)
@table_multirow_next = {}
@table.push(@table_row)
return ""
end
# We need to know the column count before opening tabular context.
def table_open(opts)
@table = []
@table_multirow = {}
@table_multirow_next = {}
return ""
end
# FIXME: need caption and label elements similar to image -> figure
def table_close(opts)
output = "\\begin{table}\n"
output << " \\centering\n"
output << " \\begin{tabular}{ #{"l " * @table[0].size }}\n"
@table.each do |row|
output << " #{row.join(" & ")} \\\\\n"
end
output << " \\end{tabular}\n"
output << "\\end{table}\n"
output
end
def bc_open(opts)
opts[:block] = true
begin_chunk("verbatim") + "\n"
end
def bc_close(opts)
end_chunk("verbatim") + "\n"
end
def bq_open(opts)
opts[:block] = true
"\\begin{quotation}\n"
end
def bq_close(opts)
"\\end{quotation}\n\n"
end
def link(opts)
"\\href{#{opts[:href]}}{#{opts[:name]}}"
end
# FIXME: use includegraphics with security verification
#
# Remember to use '\RequirePackage{graphicx}' in your LaTeX header
#
# FIXME: Look at dealing with width / height gracefully as this should be
# specified in a unit like cm rather than px.
def image(opts)
# Don't know how to use remote links, plus can we trust them?
return "" if opts[:src] =~ /^\w+\:\/\//
# Resolve CSS styles if any have been set
styling = opts[:class].to_s.split(/\s+/).collect { |style| latex_image_styles[style] }.compact.join ','
# Build latex code
[ "\\begin{figure}",
" \\centering",
" \\includegraphics[#{styling}]{#{opts[:src]}}",
(" \\caption{#{escape opts[:title]}}" if opts[:title]),
(" \\label{#{escape opts[:alt]}}" if opts[:alt]),
"\\end{figure}",
].compact.join "\n"
end
def footno(opts)
# TODO: insert a placeholder until we know the footnote content.
# For this to work, we need some kind of post-processing...
"\\footnotemark[#{opts[:text]}]"
end
def fn(opts)
"\\footnotetext[#{opts[:id]}]{#{opts[:text]}}"
end
def snip(opts)
"\\verb`#{opts[:text]}`"
end
def quote1(opts)
"`#{opts[:text]}'"
end
def quote2(opts)
"``#{opts[:text]}\""
end
def ellipsis(opts)
"#{opts[:text]}\\ldots{}"
end
def emdash(opts)
"---"
end
def endash(opts)
"--"
end
def arrow(opts)
"\\rightarrow{}"
end
def trademark(opts)
"\\texttrademark{}"
end
def registered(opts)
"\\textregistered{}"
end
def copyright(opts)
"\\copyright{}"
end
# TODO: what do we do with (unknown) unicode entities ?
#
def entity(opts)
text = opts[:text][0..0] == '#' ? opts[:text][1..-1] : opts[:text]
ENTITIES[text]
end
def dim(opts)
opts[:text].gsub!('x', '\times')
opts[:text].gsub!('"', "''")
period = opts[:text].slice!(/\.$/)
"$#{opts[:text]}$#{period}"
end
private
def escape(text)
latex_esc(text)
end
def escape_pre(text)
text
end
# Use this for block level commands that use \begin
def begin_chunk(type)
chunk_counter[type] += 1
return "\\begin{#{type}}" if 1 == chunk_counter[type]
''
end
# Use this for block level commands that use \end
def end_chunk(type)
chunk_counter[type] -= 1
raise RuntimeError, "Bad latex #{type} nesting detected" if chunk_counter[type] < 0 # This should never need to happen
return "\\end{#{type}}" if 0 == chunk_counter[type]
''
end
def chunk_counter
@chunk_counter ||= Hash.new 0
end
end