Sha256: bf85637366dff9ac52783054b5b191f74a778dcd5c3c434fc0a9bece2eac3d2a
Contents?: true
Size: 1.77 KB
Versions: 1
Compression:
Stored size: 1.77 KB
Contents
class LatexToPdf # Converts a string of LaTeX +code+ into a binary string of PDF. # # pdflatex is used to convert the file and creates the directory +#{Rails.root}/tmp/rails-latex+ to store intermediate # files. def self.generate_pdf(code,parse_twice=false) dir=File.join(Rails.root,'tmp','rails-latex',"#{Process.pid}-#{Thread.current.hash}") input=File.join(dir,'input.tex') FileUtils.mkdir_p(dir) File.open(input,'wb') {|io| io.write(code) } (parse_twice ? 2 : 1).times { system('pdflatex','-output-directory',dir,'-interaction','batchmode',input, :umask => 7,:out => :close, :err => :close, :in => :close) } result=File.read(input.sub(/\.tex$/,'.pdf')) FileUtils.rm_rf(dir) result end # Escapes LaTex special characters in text so that they wont be interpreted as LaTex commands. # # This method will use RedCloth to do the escaping if available. def self.escape_latex(text) # :stopdoc: unless @latex_escaper if defined?(RedCloth::Formatters::LATEX) class << (@latex_escaper=RedCloth.new('')) include RedCloth::Formatters::LATEX end else class << (@latex_escaper=Object.new) ESCAPE_RE=/([{}_$&%#])|([\\^~|<>])/ ESC_MAP={ '\\' => 'backlash', '^' => 'asciicircum', '~' => 'asciitilde', '|' => 'bar', '<' => 'less', '>' => 'greater', } def latex_esc(text) # :nodoc: text.gsub(ESCAPE_RE) {|m| if $1 "\\#{m}" else "\\text#{ESC_MAP[m]}{}" end } end end end # :startdoc: end @latex_escaper.latex_esc(text.to_s).html_safe end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rails-latex-1.0.1 | lib/latex_to_pdf.rb |