| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
import unicodedata |
| 4 |
|
| 5 |
from . import (parse_argument, eat_whitespace, parse_macro_name, |
| 6 |
OPEN_SCOPE, CLOSE_SCOPE, START_MACRO) |
| 7 |
|
| 8 |
|
| 9 |
__all__ = ['MACROS', 'NewCommand', 'Macro'] |
| 10 |
|
| 11 |
|
| 12 |
class MacroBase(object): |
| 13 |
def parse_arguments(self, tokens): |
| 14 |
raise NotImplementedError |
| 15 |
|
| 16 |
def parse_arguments_and_expand(self, tokens): |
| 17 |
raise NotImplementedError |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class NewCommand(MacroBase): |
| 22 |
r""" \newcommand{cmd}[args]{def} """ |
| 23 |
|
| 24 |
def __init__(self, macros): |
| 25 |
self.macros = macros |
| 26 |
|
| 27 |
@staticmethod |
| 28 |
def _parse_macro_name(tokens): |
| 29 |
eat_whitespace(tokens) |
| 30 |
token = next(tokens) |
| 31 |
in_group = token.type == OPEN_SCOPE |
| 32 |
if in_group: |
| 33 |
eat_whitespace(tokens) |
| 34 |
token = next(tokens) |
| 35 |
assert token.type == START_MACRO |
| 36 |
name = parse_macro_name(tokens) |
| 37 |
if in_group: |
| 38 |
eat_whitespace(tokens) |
| 39 |
assert next(tokens).type == CLOSE_SCOPE |
| 40 |
return name |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
def _parse_optional_arguments(tokens, macros): |
| 44 |
eat_whitespace(tokens) |
| 45 |
if tokens.peek().value == '[': |
| 46 |
next(tokens) |
| 47 |
num_args = '' |
| 48 |
for token in tokens: |
| 49 |
if token.value == ']': |
| 50 |
break |
| 51 |
num_args += token.value |
| 52 |
else: |
| 53 |
num_args = 0 |
| 54 |
return int(num_args) |
| 55 |
|
| 56 |
def parse_arguments_and_expand(self, tokens, macros): |
| 57 |
name = self._parse_macro_name(tokens) |
| 58 |
num_args = self._parse_optional_arguments(tokens, macros) |
| 59 |
definition = parse_argument(tokens, macros) |
| 60 |
for i in range(10): |
| 61 |
definition = definition.replace('#{}'.format(i + 1), |
| 62 |
'{' + str(i) + '}') |
| 63 |
self.macros[name] = Macro(num_args, definition) |
| 64 |
return '' |
| 65 |
|
| 66 |
|
| 67 |
class Macro(object): |
| 68 |
def __init__(self, num_args, format_string): |
| 69 |
self.num_args = num_args |
| 70 |
self.format_string = format_string |
| 71 |
|
| 72 |
def parse_arguments_and_expand(self, tokens, macros): |
| 73 |
args = [parse_argument(tokens, macros) for _ in range(self.num_args)] |
| 74 |
return self.expand(args) |
| 75 |
|
| 76 |
def expand(self, arguments): |
| 77 |
assert len(arguments) == self.num_args |
| 78 |
return self.format_string.format(*arguments) |
| 79 |
|
| 80 |
|
| 81 |
class Symbol(Macro): |
| 82 |
def __init__(self, symbol): |
| 83 |
super(Symbol, self).__init__(0, symbol) |
| 84 |
|
| 85 |
|
| 86 |
class SymbolByName(Macro): |
| 87 |
def __init__(self, unicode_symbol_name): |
| 88 |
unicode_symbol = unicodedata.lookup(unicode_symbol_name) |
| 89 |
super(SymbolByName, self).__init__(0, unicode_symbol) |
| 90 |
|
| 91 |
|
| 92 |
class Combining(Macro): |
| 93 |
def __init__(self, unicode_accent_name): |
| 94 |
unicode_accent = unicodedata.lookup('COMBINING ' + unicode_accent_name) |
| 95 |
super(Combining, self).__init__(1, '{0}' + unicode_accent) |
| 96 |
|
| 97 |
DOTTED_CHARS = {'ı': 'i', |
| 98 |
'ȷ': 'j'} |
| 99 |
|
| 100 |
def expand(self, arguments): |
| 101 |
assert len(arguments) == self.num_args |
| 102 |
accented, rest = arguments[0][0], arguments[0][1:] |
| 103 |
accented = self.DOTTED_CHARS.get(accented, accented) |
| 104 |
expanded = super(Combining, self).expand([accented]) |
| 105 |
return unicodedata.normalize('NFC', expanded) + rest |
| 106 |
|
| 107 |
|
| 108 |
MACROS = { |
| 109 |
# accents |
| 110 |
'`': Combining('GRAVE ACCENT'), |
| 111 |
"'": Combining('ACUTE ACCENT'), |
| 112 |
'^': Combining('CIRCUMFLEX ACCENT'), |
| 113 |
'"': Combining('DIAERESIS'), |
| 114 |
'H': Combining('DOUBLE ACUTE ACCENT'), |
| 115 |
'~': Combining('TILDE'), |
| 116 |
'c': Combining('CEDILLA'), |
| 117 |
'k': Combining('OGONEK'), |
| 118 |
'=': Combining('MACRON'), |
| 119 |
'b': Combining('MACRON BELOW'), |
| 120 |
'.': Combining('DOT ABOVE'), |
| 121 |
'd': Combining('DOT BELOW'), |
| 122 |
'r': Combining('RING ABOVE'), |
| 123 |
'u': Combining('BREVE'), |
| 124 |
'v': Combining('CARON'), |
| 125 |
# '|': Combining('VERTICAL LINE ABOVE'), |
| 126 |
# 'h': Combining('HOOK ABOVE'), |
| 127 |
'G': Combining('DOUBLE GRAVE ACCENT'), |
| 128 |
'U': Combining('DOUBLE VERTICAL LINE ABOVE'), |
| 129 |
't': Combining('DOUBLE INVERTED BREVE'), |
| 130 |
'textcircled': Combining('ENCLOSING CIRCLE'), |
| 131 |
|
| 132 |
# symbols |
| 133 |
'o': SymbolByName('LATIN SMALL LETTER O WITH STROKE'), |
| 134 |
'O': SymbolByName('LATIN CAPITAL LETTER O WITH STROKE'), |
| 135 |
'i': SymbolByName('LATIN SMALL LETTER DOTLESS I'), |
| 136 |
'l': SymbolByName('LATIN SMALL LETTER L WITH STROKE'), |
| 137 |
'L': SymbolByName('LATIN CAPITAL LETTER L WITH STROKE'), |
| 138 |
'oe': SymbolByName('LATIN SMALL LIGATURE OE'), |
| 139 |
'OE': SymbolByName('LATIN CAPITAL LIGATURE OE'), |
| 140 |
'ae': SymbolByName('LATIN SMALL LETTER AE'), |
| 141 |
'AE': SymbolByName('LATIN CAPITAL LETTER AE'), |
| 142 |
'aa': SymbolByName('LATIN SMALL LETTER A WITH RING ABOVE'), |
| 143 |
'AA': SymbolByName('LATIN CAPITAL LETTER A WITH RING ABOVE'), |
| 144 |
'ss': SymbolByName('LATIN SMALL LETTER SHARP S'), |
| 145 |
'dh': SymbolByName('LATIN SMALL LETTER ETH'), |
| 146 |
'DH': SymbolByName('LATIN CAPITAL LETTER ETH'), |
| 147 |
'dj': SymbolByName('LATIN SMALL LETTER D WITH STROKE'), |
| 148 |
'DJ': SymbolByName('LATIN CAPITAL LETTER D WITH STROKE'), |
| 149 |
'ng': SymbolByName('LATIN SMALL LETTER ENG'), |
| 150 |
'NG': SymbolByName('LATIN CAPITAL LETTER ENG'), |
| 151 |
'th': SymbolByName('LATIN SMALL LETTER THORN'), |
| 152 |
'TH': SymbolByName('LATIN CAPITAL LETTER THORN'), |
| 153 |
|
| 154 |
'emspace': SymbolByName('EM SPACE'), |
| 155 |
'enspace': SymbolByName('EN SPACE'), |
| 156 |
|
| 157 |
'dag': SymbolByName('DAGGER'), |
| 158 |
'ddag': SymbolByName('DOUBLE DAGGER'), |
| 159 |
'dots': SymbolByName('HORIZONTAL ELLIPSIS'), |
| 160 |
'ldots': SymbolByName('HORIZONTAL ELLIPSIS'), |
| 161 |
'P': SymbolByName('PILCROW SIGN'), |
| 162 |
'S': SymbolByName('SECTION SIGN'), |
| 163 |
'copyright': SymbolByName('COPYRIGHT SIGN'), |
| 164 |
'pounds': SymbolByName('POUND SIGN'), |
| 165 |
'guillemotleft': SymbolByName('LEFT-POINTING DOUBLE ANGLE QUOTATION MARK'), |
| 166 |
'guillemotright': SymbolByName('RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK'), |
| 167 |
'guilsinglleft': SymbolByName('SINGLE LEFT-POINTING ANGLE QUOTATION MARK'), |
| 168 |
'guilsinglright': SymbolByName('SINGLE RIGHT-POINTING ANGLE QUOTATION MARK'), |
| 169 |
'quotedblbase': SymbolByName('DOUBLE LOW-9 QUOTATION MARK'), |
| 170 |
'quotesinglbase': SymbolByName('SINGLE LOW-9 QUOTATION MARK'), |
| 171 |
|
| 172 |
'thinspace': SymbolByName('THIN SPACE'), |
| 173 |
|
| 174 |
'textasciicircum': Symbol('^'), |
| 175 |
'textasciitilde': Symbol('~'), |
| 176 |
'textasteriskcentered': Symbol('*'), |
| 177 |
'textbackslash': Symbol('\\'), |
| 178 |
'textbar': Symbol('|'), |
| 179 |
'textbraceleft': Symbol('{'), |
| 180 |
'textbraceright': Symbol('}'), |
| 181 |
'textbullet': SymbolByName('BULLET'), |
| 182 |
'textcopyright': SymbolByName('COPYRIGHT SIGN'), |
| 183 |
'textdagger': SymbolByName('DAGGER'), |
| 184 |
'textdaggerdbl': SymbolByName('DOUBLE DAGGER'), |
| 185 |
'textdollar': Symbol('$'), |
| 186 |
'textellipsis': SymbolByName('HORIZONTAL ELLIPSIS'), |
| 187 |
'textemdash': SymbolByName('EM DASH'), |
| 188 |
'textendash': SymbolByName('EN DASH'), |
| 189 |
'textexclamdown': SymbolByName('INVERTED EXCLAMATION MARK'), |
| 190 |
'textgreater': Symbol('>'), |
| 191 |
'textless': Symbol('<'), |
| 192 |
'textordfeminine': SymbolByName('FEMININE ORDINAL INDICATOR'), |
| 193 |
'textordmasculine': SymbolByName('MASCULINE ORDINAL INDICATOR'), |
| 194 |
'textparagraph': SymbolByName('PILCROW SIGN'), |
| 195 |
'textperiodcentered': SymbolByName('MIDDLE DOT'), |
| 196 |
'textquestiondown': SymbolByName('INVERTED QUESTION MARK'), |
| 197 |
'textquotesingle': Symbol("'"), # from the textcomp package |
| 198 |
'textquotedbl': Symbol('"'), |
| 199 |
'textquotedblleft': SymbolByName('LEFT DOUBLE QUOTATION MARK'), |
| 200 |
'textquotedblright': SymbolByName('RIGHT DOUBLE QUOTATION MARK'), |
| 201 |
'textquoteleft': SymbolByName('LEFT SINGLE QUOTATION MARK'), |
| 202 |
'textquoteright': SymbolByName('RIGHT SINGLE QUOTATION MARK'), |
| 203 |
'textregistered': SymbolByName('REGISTERED SIGN'), |
| 204 |
'textsection': SymbolByName('SECTION SIGN'), |
| 205 |
'textsterling': SymbolByName('POUND SIGN'), |
| 206 |
'texttrademark': SymbolByName('TRADE MARK SIGN'), |
| 207 |
'textunderscore': Symbol('_'), |
| 208 |
'textvisiblespace': SymbolByName('OPEN BOX'), |
| 209 |
|
| 210 |
'TeX': Macro(0, 'TeX'), |
| 211 |
|
| 212 |
# escaped characters |
| 213 |
' ': Symbol(' '), |
| 214 |
'&': Symbol('&'), |
| 215 |
'$': Symbol('$'), |
| 216 |
'{': Symbol('{'), |
| 217 |
'}': Symbol('}'), |
| 218 |
'%': Symbol('%'), |
| 219 |
'#': Symbol('#'), |
| 220 |
'_': Symbol('_'), |
| 221 |
} |