Sha256: c700258f2c8096c29600464b96c33b70e10effec3b0e593d0b5c9c2903fbc172

Contents?: true

Size: 715 Bytes

Versions: 1

Compression:

Stored size: 715 Bytes

Contents

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

    def remove_invalid_utf8(text)
      text.gsub("\u007F", "")
    end

    def tex_safe(text)
      text = text.gsub(ESCAPE_RE) { |m|
        if $1
          "\\#{m}"
        else
          "\\text#{ESC_MAP[m]}{}"
        end
      }
      text = remove_invalid_utf8(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.8 lib/latexpdf/escaper.rb