Module: Texd::Helpers
- Defined in:
- lib/texd/helpers.rb
Constant Summary collapse
- ESCAPE_RE =
/([{}_$&%#])|([\\^~|<>])/.freeze
- ESC_MAP =
{ "\\" => "backslash", "^" => "asciicircum", "~" => "asciitilde", "|" => "bar", "<" => "less", ">" => "greater", }.freeze
- TYPOGRAPHIC_REPLACEMENTS =
[ # nested quotes [/(^|\W)"'\b/, '\1\\glqq{}\\glq{}'], [/\b'"(\W|$)/, '\\grq{}\\grqq{}\1'], # double quotes [/(^|\W)"\b/, '\1\\glqq{}'], [/\b"(\W|$)/, '\\grqq{}\1'], # single quotes [/(^|\W)'\b/, '\1\\glq{}'], [/\b'(\W|$)/, '\\grq{}\1'], ].freeze
- HYPHENATION_REPLACEMENTS =
[ # proper hyphenation [/(\w)-(\w)/, '\1"=\2'], ].freeze
Instance Method Summary collapse
-
#escape(text, line_break = "\\\\\\", typographic: true, hyphenation: typographic) ⇒ Object
Escapes the given text, making it safe for use in TeX documents.
Instance Method Details
#escape(text, line_break = "\\\\\\", typographic: true, hyphenation: typographic) ⇒ Object
Escapes the given text, making it safe for use in TeX documents.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/texd/helpers.rb', line 33 def escape(text, line_break = "\\\\\\", typographic: true, hyphenation: typographic) return "" if text.blank? text.to_s.dup.tap do |str| str.gsub!(ESCAPE_RE) do |m| if Regexp.last_match(1) "\\#{m}" else "\\text#{ESC_MAP[m]}{}" end end { TYPOGRAPHIC_REPLACEMENTS => typographic, HYPHENATION_REPLACEMENTS => hyphenation, }.each do |replacements, active| next unless active replacements.each do |re, replacement| str.gsub!(re, replacement) end end str.gsub!(/\r?\n/, line_break) end.freeze end |