class Rouge::Formatters::Tex
Constants
- ESCAPE
A map of TeX escape characters. Newlines are handled specially by using
token_lines
spaces are preserved as long as they aren’t at the beginning of a line. see tag_first for our initial-space strategy- ESCAPE_REGEX
Public Class Methods
new(opts={})
click to toggle source
# File lib/rouge/formatters/tex.rb, line 32 def initialize(opts={}) @prefix = opts.fetch(:prefix) { 'RG' } end
Public Instance Methods
escape_tex(str)
click to toggle source
# File lib/rouge/formatters/tex.rb, line 36 def escape_tex(str) str.gsub(ESCAPE_REGEX, ESCAPE) end
hphantom_tag(tok, val) { |"\\hphantom{#{'x' * leading}}"| ... }
click to toggle source
Special handling for leading spaces, since they may be gobbled by a previous command. We replace all initial spaces with hphantom{xxxx}, which renders an empty space equal to the size of the x’s.
# File lib/rouge/formatters/tex.rb, line 73 def hphantom_tag(tok, val) leading = nil val.sub!(/^[ ]+/) { leading = $&.size; '' } yield "\\hphantom{#{'x' * leading}}" if leading yield tag(tok, val) unless val.empty? end
render_line(line, &b)
click to toggle source
# File lib/rouge/formatters/tex.rb, line 63 def render_line(line, &b) line.each do |(tok, val)| hphantom_tag(tok, val, &b) end end
stream(tokens) { |"\\begin{#{prefix}*}%\n"| ... }
click to toggle source
# File lib/rouge/formatters/tex.rb, line 40 def stream(tokens, &b) # surround the output with \begin{RG*}...\end{RG*} yield "\\begin{#{@prefix}*}%\n" # we strip the newline off the last line to avoid # an extra line being rendered. we do this by yielding # the \newline tag *before* every line group except # the first. first = true token_lines tokens do |line| if first first = false else yield "\\newline%\n" end render_line(line, &b) end yield "%\n\\end{#{@prefix}*}%\n" end
tag(tok, val)
click to toggle source
# File lib/rouge/formatters/tex.rb, line 80 def tag(tok, val) if escape?(tok) val elsif tok == Token::Tokens::Text escape_tex(val) else "\\#@prefix{#{tok.shortname}}{#{escape_tex(val)}}" end end