Add helper method for LaTeX-escaping strings in ERB

This commit is contained in:
Paul van Tilburg 2015-06-05 22:26:59 +02:00
parent d4985a2242
commit ac551fcf7a
1 changed files with 21 additions and 0 deletions

View File

@ -1386,6 +1386,27 @@ module StopTime::Controllers
#
private
# Escapes the given string such that it can be used as in in
# LaTeX.
#
# @param [String] string the given string
def _escape_latex(string)
escape_chars = { '#' => '\#',
'$' => '\$',
'%' => '\%',
'&' => '\&',
'\\' => '\textbackslash{}',
'^' => '\textasciicircum{}',
'_' => '\_',
'{' => '\{',
'}' => '\}',
'~' => '\textasciitilde{}' }
regexp_str = escape_chars.keys.map { |c| Regexp.escape(c) }.join('|')
regexp = Regexp.new(regexp_str)
string.to_s.gsub(regexp, escape_chars)
end
alias_method :l, :_escape_latex
# Generates a LaTex document for the invoice with the given number.
#
# @param [Fixnum] number number of the invoice