Module: RedCloth::Formatters::LATEX

Includes:
Base
Defined in:
lib/redcloth/formatters/latex.rb

Defined Under Namespace

Modules: Settings

Constant Summary collapse

ENTITIES =
YAML::load(File.read(File.dirname(__FILE__)+'/latex_entities.yml'))

Instance Method Summary collapse

Methods included from Base

#ignore, #inline_redcloth_version, #redcloth_version

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RedCloth::Formatters::Base

Instance Method Details

#acronym(opts) ⇒ Object

acronyms



58
59
60
# File 'lib/redcloth/formatters/latex.rb', line 58

def acronym(opts)
  "#{opts[:title]} (#{opts[:text]})"
end

#arrow(opts) ⇒ Object



259
260
261
# File 'lib/redcloth/formatters/latex.rb', line 259

def arrow(opts)
  "\\rightarrow{}"
end

#bc_close(opts) ⇒ Object



183
184
185
# File 'lib/redcloth/formatters/latex.rb', line 183

def bc_close(opts)
  end_chunk("verbatim") + "\n"
end

#bc_open(opts) ⇒ Object

code blocks



178
179
180
181
# File 'lib/redcloth/formatters/latex.rb', line 178

def bc_open(opts)
  opts[:block] = true
  begin_chunk("verbatim") + "\n"
end

#bq_close(opts) ⇒ Object



193
194
195
# File 'lib/redcloth/formatters/latex.rb', line 193

def bq_close(opts)
  "\\end{quotation}\n\n"
end

#bq_open(opts) ⇒ Object

block quotations



188
189
190
191
# File 'lib/redcloth/formatters/latex.rb', line 188

def bq_open(opts)
  opts[:block] = true
  "\\begin{quotation}\n"
end

#code(opts) ⇒ Object

inline code



53
54
55
# File 'lib/redcloth/formatters/latex.rb', line 53

def code(opts)
  opts[:block] ? opts[:text] : "\\verb@#{opts[:text]}@"
end


271
272
273
# File 'lib/redcloth/formatters/latex.rb', line 271

def copyright(opts)
  "\\copyright{}"
end

#dim(opts) ⇒ Object



282
283
284
285
286
287
# File 'lib/redcloth/formatters/latex.rb', line 282

def dim(opts)
  opts[:text].gsub!('x', '\times')
  opts[:text].gsub!('"', "''")
  period = opts[:text].slice!(/\.$/)
  "$#{opts[:text]}$#{period}"
end

#ellipsis(opts) ⇒ Object



247
248
249
# File 'lib/redcloth/formatters/latex.rb', line 247

def ellipsis(opts)
  "#{opts[:text]}\\ldots{}"
end

#emdash(opts) ⇒ Object



251
252
253
# File 'lib/redcloth/formatters/latex.rb', line 251

def emdash(opts)
  "---"
end

#endash(opts) ⇒ Object



255
256
257
# File 'lib/redcloth/formatters/latex.rb', line 255

def endash(opts)
  " -- "
end

#entity(opts) ⇒ Object

TODO: what do we do with (unknown) unicode entities ?



277
278
279
280
# File 'lib/redcloth/formatters/latex.rb', line 277

def entity(opts)
  text = opts[:text][0..0] == '#' ? opts[:text][1..-1] : opts[:text] 
  ENTITIES[text]
end

#fn(opts) ⇒ Object



230
231
232
# File 'lib/redcloth/formatters/latex.rb', line 230

def fn(opts)
  "\\footnotetext[#{opts[:id]}]{#{opts[:text]}}"
end

#footno(opts) ⇒ Object

footnotes



224
225
226
227
228
# File 'lib/redcloth/formatters/latex.rb', line 224

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

#image(opts) ⇒ Object

FIXME: use includegraphics with security verification

Remember to use ‘RequirePackagegraphicx’ 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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/redcloth/formatters/latex.rb', line 208

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

#inline_html(opts) ⇒ Object

TODO: what do we do with HTML?



290
291
292
# File 'lib/redcloth/formatters/latex.rb', line 290

def inline_html(opts)
  opts[:text] || ""
end

#li_close(opts = nil) ⇒ Object



107
108
109
# File 'lib/redcloth/formatters/latex.rb', line 107

def li_close(opts=nil)
  "\n"
end

#li_open(opts) ⇒ Object



103
104
105
# File 'lib/redcloth/formatters/latex.rb', line 103

def li_open(opts)
  "  \\item #{opts[:text]}"
end

links



198
199
200
# File 'lib/redcloth/formatters/latex.rb', line 198

def link(opts)
  "\\href{#{opts[:href]}}{#{opts[:name]}}"
end

#p(opts) ⇒ Object

paragraphs



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/redcloth/formatters/latex.rb', line 112

def p(opts)
  case opts[:align]
  when 'left' then
    "\\begin{flushleft}#{opts[:text]}\\end{flushleft}\n\n" 
  when 'right' then
    "\\begin{flushright}#{opts[:text]}\\end{flushright}\n\n" 
  when 'center' then
    "\\begin{center}#{opts[:text]}\\end{center}\n\n" 
  else
    "#{opts[:text]}\n\n"
  end
end

#quote1(opts) ⇒ Object



239
240
241
# File 'lib/redcloth/formatters/latex.rb', line 239

def quote1(opts)
  "`#{opts[:text]}'"
end

#quote2(opts) ⇒ Object



243
244
245
# File 'lib/redcloth/formatters/latex.rb', line 243

def quote2(opts)
  "``#{opts[:text]}''"
end

#registered(opts) ⇒ Object



267
268
269
# File 'lib/redcloth/formatters/latex.rb', line 267

def registered(opts)
  "\\textregistered{}"
end

#snip(opts) ⇒ Object

inline verbatim



235
236
237
# File 'lib/redcloth/formatters/latex.rb', line 235

def snip(opts)
  "\\verb`#{opts[:text]}`"
end

#table_close(opts) ⇒ Object

FIXME: need caption and label elements similar to image -> figure



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/redcloth/formatters/latex.rb', line 165

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

#table_open(opts) ⇒ Object

We need to know the column count before opening tabular context.



157
158
159
160
161
162
# File 'lib/redcloth/formatters/latex.rb', line 157

def table_open(opts)
  @table = []
  @table_multirow = {}
  @table_multirow_next = {}
  return ""
end

#td(opts) ⇒ Object

tables



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/redcloth/formatters/latex.rb', line 126

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

#tr_close(opts) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/redcloth/formatters/latex.rb', line 144

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

#tr_open(opts) ⇒ Object



139
140
141
142
# File 'lib/redcloth/formatters/latex.rb', line 139

def tr_open(opts)
  @table_row = []
  return ""
end

#trademark(opts) ⇒ Object



263
264
265
# File 'lib/redcloth/formatters/latex.rb', line 263

def trademark(opts)
  "\\texttrademark{}"
end