Sha256: 614f6b7f5b80cb7e970e51df3b794ca57313a134958fd40cc9fa13b465cf7b3b

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

#
# $Id: latexutils.rb 2204 2006-03-18 06:10:26Z aamine $
#
# Copyright (c) 2002-2006 Minero Aoki
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of the GNU LGPL, see the file "COPYING".
#

module ReVIEW

  module LaTeXUtils

    MATACHARS = {
      '#'  => '\#',
      "$"  => '\textdollar{}',
      '%' => '\%',
      '&' => '\&',
      '{' => '\{',
      '}' => '\}',
      '_'  => '\textunderscore{}',
      '^' => '\textasciicircum{}',
      '~' => '\textasciitilde{}',
      '|' => '\textbar{}',
      '<'  => '\textless{}',
      '>'  => '\textgreater{}',
      "\\" => '\reviewbackslash{}'
    }

    METACHARS_RE = /[#{Regexp.escape(MATACHARS.keys.join(''))}]/

    MATACHARS_INVERT = MATACHARS.invert

    def escape_latex(str)
      str.gsub(METACHARS_RE) {|s|
        MATACHARS[s] or raise "unknown trans char: #{s}"
      }
    end

    alias escape escape_latex

    def unescape_latex(str)
      metachars_invert_re = Regexp.new(MATACHARS_INVERT.keys.collect{|key|  Regexp.escape(key)}.join('|'))
      str.gsub(metachars_invert_re) {|s|
        MATACHARS_INVERT[s] or raise "unknown trans char: #{s}"
      }
    end

    def escape_index(str)
      str.gsub(/[@!|"]/) {|s| '"' + s }
    end

    def macro(name, *args)
      "\\#{name}" + args.map {|a| "{#{a}}" }.join('')
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
review-0.9.0 lib/review/latexutils.rb