summaryrefslogtreecommitdiff
path: root/etc/todo/latex.demiurgo.rb
blob: 0a548721f22b5f9597cd0bff776170373697d379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module CodeRay
module Encoders

  # = LaTeX Encoder
  #
  # Encoder producing LaTeX.
  class Latex < Encoder

    include Streamable
    register_for :latex

    FILE_EXTENSION = 'tex'

    DEFAULT_OPTIONS = {
      :wrap => true,
    }

  protected
    def text_token text, kind
      @out <<
        if kind == :space
          text
        else
          text = escape_latex(text)
          "\\syn#{kind_to_command(kind)}{#{text}}"
        end
    end

    def block_token action, kind
      @out << super
    end

    def open_token kind
      "\\syn#{kind_to_command(kind)}{"
    end

    def close_token kind
      "}"
    end

    def kind_to_command kind
      kind.to_s.gsub(/[^a-z0-9]/i, '').to_sym
    end

    def finish options
      case options[:wrap]
      when true, 1, :semiverbatim
        @out = "\\begin{semiverbatim}\n#{@out}\n\\end{semiverbatim}\n"
      when false, 0
        # Nothing to do
      else
        raise ArgumentError, "Unknown :wrap option: '#{options[:wrap]}'"
      end

      super
    end

    # Escape text so it's interpreted literally by LaTeX compilers
    def escape_latex string
      string.to_s.gsub(/[$\\{}_%#&~^"]/) do |s|
        case s
        when '$'
          '\$'
        when '\\'
          '\synbs{}'
        when /[{}_%#&]/
          "\\#{s}"
        when /[~^]/
          "\\#{s}{}"
        when '"'
          '"{}'
        end
      end
    end

  end

end
end