From ac551fcf7a037e7f5d9af6ec3169effcb13d5672 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Fri, 5 Jun 2015 22:26:59 +0200 Subject: [PATCH] Add helper method for LaTeX-escaping strings in ERB --- stoptime.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/stoptime.rb b/stoptime.rb index 332b961..b16a9d6 100644 --- a/stoptime.rb +++ b/stoptime.rb @@ -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