Class: Rouge::Formatters::Tex

Inherits:
Rouge::Formatter show all
Defined in:
lib/rouge/formatters/tex.rb

Constant Summary collapse

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

{
  '&' => '\&',
  '%' => '\%',
  '$' => '\$',
  '#' => '\#',
  '_' => '\_',
  '{' => '\{',
  '}' => '\}',
  '~' => '{\textasciitilde}',
  '^' => '{\textasciicircum}',
  '|' => '{\textbar}',
  '\\' => '{\textbackslash}',
  "\t" => '{\tab}',
}
ESCAPE_REGEX =
/[#{ESCAPE.keys.map(&Regexp.method(:escape)).join}]/om

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

disable_escape!, enable_escape!, #escape?, escape_enabled?, #filter_escapes, find, #format, format, #render, tag, #token_lines, with_escape

Constructor Details

#initialize(opts = {}) ⇒ Tex

Returns a new instance of Tex


27
28
29
# File 'lib/rouge/formatters/tex.rb', line 27

def initialize(opts={})
  @prefix = opts.fetch(:prefix) { 'RG' }
end

Instance Method Details

#escape_tex(str) ⇒ Object


31
32
33
# File 'lib/rouge/formatters/tex.rb', line 31

def escape_tex(str)
  str.gsub(ESCAPE_REGEX, ESCAPE)
end

#render_line(line, &b) ⇒ Object


58
59
60
61
62
63
64
65
66
# File 'lib/rouge/formatters/tex.rb', line 58

def render_line(line, &b)
  head, *rest = line
  return unless head

  tag_first(*head, &b)
  rest.each do |(tok, val)|
    yield tag(tok, val)
  end
end

#stream(tokens) {|"\\begin{#{@prefix}*}%\n"| ... } ⇒ Object

Yields:

  • ("\\begin{#{@prefix}*}%\n")

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rouge/formatters/tex.rb', line 35

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) ⇒ Object


79
80
81
82
83
84
85
86
87
# File 'lib/rouge/formatters/tex.rb', line 79

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

#tag_first(tok, val) {|"\\hphantom{#{'x' * leading}}"| ... } ⇒ Object

special handling for the first token of a line. we replace all initial spaces with \hphantomxxxx, which renders an empty space equal to the size of the x's.

Yields:

  • ("\\hphantom{#{'x' * leading}}")

72
73
74
75
76
77
# File 'lib/rouge/formatters/tex.rb', line 72

def tag_first(tok, val)
  leading = nil
  val.sub!(/^[ ]+/) { leading = $&.size; '' }
  yield "\\hphantom{#{'x' * leading}}" if leading
  yield tag(tok, val)
end