Sha256: 5cacbcfe496c769e9d50903c4b671870c7c1fffa14922898fa32a4bfed9fd0b2

Contents?: true

Size: 870 Bytes

Versions: 1

Compression:

Stored size: 870 Bytes

Contents

module Latexpdf
  class Escaper
    ESCAPE_RE=/([{}_$&%#])|([\\^~|<>])/
    ESC_MAP = {
        '\\' => 'backslash',
        '^' => 'asciicircum',
        '~' => 'asciitilde',
        '|' => 'bar',
        '<' => 'less',
        '>' => 'greater'
    }

    def tab_to_space(text)
      text.gsub(/\x09/, " ")
    end

    def remove_non_printable_chars(text)
      pattern = /([\x00-\x08\x0B-\x1F\x7F])/
      text.gsub(pattern, "")
    end

    def tex_safe(text)
      text = text.gsub(ESCAPE_RE) { |m|
        if $1
          "\\#{m}"
        else
          "\\text#{ESC_MAP[m]}{}"
        end
      }
      text = tab_to_space(text)
      text = remove_non_printable_chars(text)
      text.html_safe
    end
  end

  def self.escape_latex(text)
    latex_escaper.tex_safe(text)
  end

  private

  def self.latex_escaper
    @latex_escaper ||= Escaper.new
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
latexpdf-0.4.9 lib/latexpdf/escaper.rb