module RedCloth::Formatters::LATEX
Public Class Methods
entities()
click to toggle source
# File lib/redcloth/formatters/latex.rb 6 def self.entities 7 @entities ||= YAML.load(File.read(File.dirname(__FILE__)+'/latex_entities.yml')) 8 end
Public Instance Methods
acronym(opts)
click to toggle source
acronyms
# File lib/redcloth/formatters/latex.rb 60 def acronym(opts) 61 "#{opts[:title]} (#{opts[:text]})" 62 end
arrow(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 268 def arrow(opts) 269 "\\rightarrow{}" 270 end
auto_link(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 204 def auto_link(opts) 205 return opts[:href] unless auto_link_urls 206 href_with_proto = opts[:href] 207 href_with_proto = 'http://' + href_with_proto unless href_with_proto.index('http') == 0 208 "\\href{#{href_with_proto}}{#{opts[:href]}}" 209 end
bc_close(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 185 def bc_close(opts) 186 end_chunk("verbatim") + "\n" 187 end
bc_open(opts)
click to toggle source
code blocks
# File lib/redcloth/formatters/latex.rb 180 def bc_open(opts) 181 opts[:block] = true 182 begin_chunk("verbatim") + "\n" 183 end
bq_close(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 195 def bq_close(opts) 196 "\\end{quotation}\n\n" 197 end
bq_open(opts)
click to toggle source
block quotations
# File lib/redcloth/formatters/latex.rb 190 def bq_open(opts) 191 opts[:block] = true 192 "\\begin{quotation}\n" 193 end
code(opts)
click to toggle source
inline code
# File lib/redcloth/formatters/latex.rb 55 def code(opts) 56 opts[:block] ? opts[:text] : "\\verb@#{opts[:text]}@" 57 end
copyright(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 280 def copyright(opts) 281 "\\copyright{}" 282 end
dim(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 291 def dim(opts) 292 opts[:text].gsub!('x', '\times') 293 opts[:text].gsub!('"', "''") 294 period = opts[:text].slice!(/\.$/) 295 "$#{opts[:text]}$#{period}" 296 end
ellipsis(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 256 def ellipsis(opts) 257 "#{opts[:text]}\\ldots{}" 258 end
emdash(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 260 def emdash(opts) 261 "---" 262 end
endash(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 264 def endash(opts) 265 " -- " 266 end
entity(opts)
click to toggle source
TODO: what do we do with (unknown) unicode entities ?
# File lib/redcloth/formatters/latex.rb 286 def entity(opts) 287 text = opts[:text][0..0] == '#' ? opts[:text][1..-1] : opts[:text] 288 RedCloth::Formatters::LATEX.entities[text] 289 end
fn(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 239 def fn(opts) 240 "\\footnotetext[#{opts[:id]}]{#{opts[:text]}}" 241 end
footno(opts)
click to toggle source
footnotes
# File lib/redcloth/formatters/latex.rb 233 def footno(opts) 234 # TODO: insert a placeholder until we know the footnote content. 235 # For this to work, we need some kind of post-processing... 236 "\\footnotemark[#{opts[:text]}]" 237 end
image(opts)
click to toggle source
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.
# File lib/redcloth/formatters/latex.rb 217 def image(opts) 218 # Don't know how to use remote links, plus can we trust them? 219 return "" if opts[:src] =~ /^\w+\:\/\// 220 # Resolve CSS styles if any have been set 221 styling = opts[:class].to_s.split(/\s+/).collect { |style| latex_image_styles[style] }.compact.join ',' 222 # Build latex code 223 [ "\\begin{figure}", 224 " \\centering", 225 " \\includegraphics[#{styling}]{#{opts[:src]}}", 226 (" \\caption{#{escape opts[:title]}}" if opts[:title]), 227 (" \\label{#{escape opts[:alt]}}" if opts[:alt]), 228 "\\end{figure}", 229 ].compact.join "\n" 230 end
inline_html(opts)
click to toggle source
TODO: what do we do with HTML
?
# File lib/redcloth/formatters/latex.rb 299 def inline_html(opts) 300 opts[:text] || "" 301 end
li_close(opts=nil)
click to toggle source
# File lib/redcloth/formatters/latex.rb 109 def li_close(opts=nil) 110 "\n" 111 end
li_open(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 105 def li_open(opts) 106 " \\item #{opts[:text]}" 107 end
link(opts)
click to toggle source
links
# File lib/redcloth/formatters/latex.rb 200 def link(opts) 201 "\\href{#{opts[:href]}}{#{opts[:name]}}" 202 end
p(opts)
click to toggle source
paragraphs
# File lib/redcloth/formatters/latex.rb 114 def p(opts) 115 case opts[:align] 116 when 'left' then 117 "\\begin{flushleft}#{opts[:text]}\\end{flushleft}\n\n" 118 when 'right' then 119 "\\begin{flushright}#{opts[:text]}\\end{flushright}\n\n" 120 when 'center' then 121 "\\begin{center}#{opts[:text]}\\end{center}\n\n" 122 else 123 "#{opts[:text]}\n\n" 124 end 125 end
quote1(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 248 def quote1(opts) 249 "`#{opts[:text]}'" 250 end
quote2(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 252 def quote2(opts) 253 "``#{opts[:text]}''" 254 end
registered(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 276 def registered(opts) 277 "\\textregistered{}" 278 end
snip(opts)
click to toggle source
inline verbatim
# File lib/redcloth/formatters/latex.rb 244 def snip(opts) 245 "\\verb`#{opts[:text]}`" 246 end
table_close(opts)
click to toggle source
FIXME: need caption and label elements similar to image -> figure
# File lib/redcloth/formatters/latex.rb 167 def table_close(opts) 168 output = "\\begin{table}\n" 169 output << " \\centering\n" 170 output << " \\begin{tabular}{ #{"l " * @table[0].size }}\n" 171 @table.each do |row| 172 output << " #{row.join(" & ")} \\\\\n" 173 end 174 output << " \\end{tabular}\n" 175 output << "\\end{table}\n" 176 output 177 end
table_open(opts)
click to toggle source
We need to know the column count before opening tabular context.
# File lib/redcloth/formatters/latex.rb 159 def table_open(opts) 160 @table = [] 161 @table_multirow = {} 162 @table_multirow_next = {} 163 return "" 164 end
td(opts)
click to toggle source
tables
# File lib/redcloth/formatters/latex.rb 128 def td(opts) 129 column = @table_row.size 130 if opts[:colspan] 131 opts[:text] = "\\multicolumn{#{opts[:colspan]}}{ #{"l " * opts[:colspan].to_i}}{#{opts[:text]}}" 132 end 133 if opts[:rowspan] 134 @table_multirow_next[column] = opts[:rowspan].to_i - 1 135 opts[:text] = "\\multirow{#{opts[:rowspan]}}{*}{#{opts[:text]}}" 136 end 137 @table_row.push(opts[:text]) 138 return "" 139 end
tr_close(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 146 def tr_close(opts) 147 multirow_columns = @table_multirow.find_all {|c,n| n > 0} 148 multirow_columns.each do |c,n| 149 @table_row.insert(c,"") 150 @table_multirow[c] -= 1 151 end 152 @table_multirow.merge!(@table_multirow_next) 153 @table_multirow_next = {} 154 @table.push(@table_row) 155 return "" 156 end
tr_open(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 141 def tr_open(opts) 142 @table_row = [] 143 return "" 144 end
trademark(opts)
click to toggle source
# File lib/redcloth/formatters/latex.rb 272 def trademark(opts) 273 "\\texttrademark{}" 274 end
Private Instance Methods
begin_chunk(type)
click to toggle source
Use this for block level commands that use begin
# File lib/redcloth/formatters/latex.rb 314 def begin_chunk(type) 315 chunk_counter[type] += 1 316 return "\\begin{#{type}}" if 1 == chunk_counter[type] 317 '' 318 end
chunk_counter()
click to toggle source
# File lib/redcloth/formatters/latex.rb 328 def chunk_counter 329 @chunk_counter ||= Hash.new 0 330 end
end_chunk(type)
click to toggle source
Use this for block level commands that use end
# File lib/redcloth/formatters/latex.rb 321 def end_chunk(type) 322 chunk_counter[type] -= 1 323 raise RuntimeError, "Bad latex #{type} nesting detected" if chunk_counter[type] < 0 # This should never need to happen 324 return "\\end{#{type}}" if 0 == chunk_counter[type] 325 '' 326 end
escape(text)
click to toggle source
# File lib/redcloth/formatters/latex.rb 305 def escape(text) 306 latex_esc(text) 307 end
escape_pre(text)
click to toggle source
# File lib/redcloth/formatters/latex.rb 309 def escape_pre(text) 310 text 311 end