summaryrefslogtreecommitdiff
path: root/sphinx/util/texescape.py
blob: c0619f4634b55b0b0b5b1f53bab86655de361fae (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
"""
    sphinx.util.texescape
    ~~~~~~~~~~~~~~~~~~~~~

    TeX escaping helper.

    :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

tex_replacements = [
    # map TeX special chars
    (u'$', ur'\$'),
    (u'%', ur'\%'),
    (u'&', ur'\&'),
    (u'#', ur'\#'),
    (u'_', ur'\_'),
    (u'{', ur'\{'),
    (u'}', ur'\}'),
    (u'[', ur'{[}'),
    (u']', ur'{]}'),
    (u'`', ur'{}`'),
    (u'\\',ur'\textbackslash{}'),
    (u'~', ur'\textasciitilde{}'),
    (u'<', ur'\textless{}'),
    (u'>', ur'\textgreater{}'),
    (u'^', ur'\textasciicircum{}'),
    # map special Unicode characters to TeX commands
    (u'¶', ur'\P{}'),
    (u'§', ur'\S{}'),
    (u'€', ur'\texteuro{}'),
    (u'∞', ur'\(\infty\)'),
    (u'±', ur'\(\pm\)'),
    (u'→', ur'\(\rightarrow\)'),
    (u'‣', ur'\(\rightarrow\)'),
    # used to separate -- in options
    (u'', ur'{}'),
    # map some special Unicode characters to similar ASCII ones
    (u'─', ur'-'),
    (u'⎽', ur'\_'),
    (u'╲', ur'\textbackslash{}'),
    (u'|', ur'\textbar{}'),
    (u'│', ur'\textbar{}'),
    (u'ℯ', ur'e'),
    (u'ⅈ', ur'i'),
    (u'₁', ur'1'),
    (u'₂', ur'2'),
    # map Greek alphabet
    (u'α', ur'\(\alpha\)'),
    (u'β', ur'\(\beta\)'),
    (u'γ', ur'\(\gamma\)'),
    (u'δ', ur'\(\delta\)'),
    (u'ε', ur'\(\epsilon\)'),
    (u'ζ', ur'\(\zeta\)'),
    (u'η', ur'\(\eta\)'),
    (u'θ', ur'\(\theta\)'),
    (u'ι', ur'\(\iota\)'),
    (u'κ', ur'\(\kappa\)'),
    (u'λ', ur'\(\lambda\)'),
    (u'μ', ur'\(\mu\)'),
    (u'ν', ur'\(\nu\)'),
    (u'ξ', ur'\(\xi\)'),
    (u'ο', ur'o'),
    (u'π', ur'\(\pi\)'),
    (u'ρ', ur'\(\rho\)'),
    (u'σ', ur'\(\sigma\)'),
    (u'τ', ur'\(\tau\)'),
    (u'υ', u'\\(\\upsilon\\)'),
    (u'φ', ur'\(\phi\)'),
    (u'χ', ur'\(\chi\)'),
    (u'ψ', ur'\(\psi\)'),
    (u'ω', ur'\(\omega\)'),
    (u'Α', ur'A'),
    (u'Β', ur'B'),
    (u'Γ', ur'\(\Gamma\)'),
    (u'Δ', ur'\(\Delta\)'),
    (u'Ε', ur'E'),
    (u'Ζ', ur'Z'),
    (u'Η', ur'H'),
    (u'Θ', ur'\(\Theta\)'),
    (u'Ι', ur'I'),
    (u'Κ', ur'K'),
    (u'Λ', ur'\(\Lambda\)'),
    (u'Μ', ur'M'),
    (u'Ν', ur'N'),
    (u'Ξ', ur'\(\Xi\)'),
    (u'Ο', ur'O'),
    (u'Π', ur'\(\Pi\)'),
    (u'Ρ', ur'P'),
    (u'Σ', ur'\(\Sigma\)'),
    (u'Τ', ur'T'),
    (u'Υ', u'\\(\\Upsilon\\)'),
    (u'Φ', ur'\(\Phi\)'),
    (u'Χ', ur'X'),
    (u'Ψ', ur'\(\Psi\)'),
    (u'Ω', ur'\(\Omega\)'),
    (u'Ω', ur'\(\Omega\)'),
]

tex_escape_map = {}
tex_replace_map = {}
tex_hl_escape_map_new = {}

def init():
    for a, b in tex_replacements:
        tex_escape_map[ord(a)] = b
        tex_replace_map[ord(a)] = u'_'

    for a, b in tex_replacements:
        if a in u'[]{}\\': continue
        tex_hl_escape_map_new[ord(a)] = b