From: Jens J. M. <je...@us...> - 2005-01-28 15:07:59
|
Update of /cvsroot/docutils/sandbox/jensj/latex_math/tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31714/tools Modified Files: rst2mathml.py Log Message: Added the remaining symbols and a not command. Updated documentation. Index: rst2mathml.py =================================================================== RCS file: /cvsroot/docutils/sandbox/jensj/latex_math/tools/rst2mathml.py,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- rst2mathml.py 28 Jan 2005 09:30:54 -0000 1.1 +++ rst2mathml.py 28 Jan 2005 15:07:24 -0000 1.2 @@ -267,74 +267,26 @@ return [self.text] -escaped = {'{': 'lbrace', - '}': 'rbrace'} - over = {'tilde': '~', 'hat': '^', - 'bar': '_'} - -special = {'sum': u'\u03A3', - 'int': u'\u222B', - 'prod': u'\u03A0', - 'leftarraw': u'\u00D7', - 'rightarrow': u'\u00D7', - 'pm': u'\u00D7', - 'mp': u'\u00D7', - 'cdot': u'\u00D7', - 'leq': u'\u00D7', - 'geq': u'\u00D7', - 'cdots': u'\u00D7', - 'infty': u'\u00D7', - 'hbar': u'\u00D7', - 'uparrow': u'\u00D7', - 'downarrow': u'\u00D7', - 'nabla': u'\u2207', - 'times': u'\u00D7', - # Greek letters: - 'alpha': u'\u03B1', 'beta': u'\u03B2', 'gamma': u'\u03B3', - 'delta': u'\u03B4', 'epsilon': u'\u03F5', 'varepsilon': u'\u03B5', - 'zeta': u'\u03B6', - 'eta': u'\u03B7', - 'theta': u'\u03B8', - 'vartheta': u'\u03D1', - 'iota': u'\u03B9', - 'kappa': u'\u03BA', - 'varkappa': u'\u03F0', - 'lambda': u'\u03BB', - 'mu': u'\u03BC', - 'nu': u'\u03BD', - 'xi': u'\u03BE', - 'pi': u'\u03C0', - 'varpi': u'\u03D6', - 'rho': u'\u03C1', - 'varrho': u'\u03F1', - 'sigma': u'\u03C3', - 'varsigma': u'\u03C2', - 'tau': u'\u03C4', - 'upsilon': u'\u03C5', - 'phi': u'\u03D5', - 'varphi': u'\u03C6', - 'chi': u'\u03C7', - 'psi': u'\u03C8', - 'omega': u'\u03C9', - 'Gamma': u'\u0393', - 'Delta': u'\u0394', - 'Theta': u'\u0398', - 'Lambda': u'\u039B', - 'Xi': u'\u039E', - 'Pi': u'\u03A0', - 'Sigma': u'\u03A3', - 'Upsilon': u'\u03D2', - 'Phi': u'\u03A6', - 'Psi': u'\u03A8', - 'Omega': u'\u03A9', - # Braces: - 'lbrace': u'\u007B', - 'rbrace': u'\u007D', - 'langle': u'\u2329', - 'rangle': u'\u232A'} + 'bar': '_', + 'vec': u'\u20D7'} +special = { + # Greek letters: + 'tau': u'\u03c4', 'phi': u'\u03d5', 'xi': u'\u03be', 'iota': u'\u03b9', 'epsilon': u'\u03f5', 'varrho': u'\u03f1', 'Delta': u'\u0394', 'Omega': u'\u03a9', 'Upsilon': u'\u03d2', 'varsigma': u'\u03c2', 'beta': u'\u03b2', 'psi': u'\u03c8', 'Psi': u'\u03a8', 'rho': u'\u03c1', 'delta': u'\u03b4', 'alpha': u'\u03b1', 'zeta': u'\u03b6', 'Pi': u'\u03a0', 'omega': u'\u03c9', 'Gamma': u'\u0393', 'Lambda': u'\u039b', 'varepsilon': u'\u03b5', 'Phi': u'\u03a6', 'Xi': u'\u039e', 'kappa': u'\u03ba', 'vartheta': u'\u03d1', 'Sigma': u'\u03a3', 'varkappa': u'\u03f0', 'upsilon': u'\u03c5', 'varphi': u'\u03c6', 'varpi': u'\u03d6', 'mu': u'\u03bc', 'eta': u'\u03b7', 'chi': u'\u03c7', 'Theta': u'\u0398', 'theta': u'\u03b8', 'pi': u'\u03c0', 'sigma': u'\u03c3', 'nu': u'\u03bd', 'gamma': u'\u03b3', 'lambda': u'\u03bb', + # Binary operation symbols: + 'wedge': u'\u2227', 'diamond': u'\u22c4', 'star': u'\u22c6', 'amalg': u'\u2a3f', 'ast': u'\u2217', 'odot': u'\u2299', 'triangleleft': u'\u25c1', 'bigtriangleup': u'\u25b3', 'ominus': u'\u2296', 'ddagger': u'\u2021', 'wr': u'\u2240', 'otimes': u'\u2297', 'sqcup': u'\u2294', 'oplus': u'\u2295', 'bigcirc': u'\u25cb', 'oslash': u'\u2298', 'sqcap': u'\u2293', 'bullet': u'\u2219', 'cup': u'\u222a', 'cdot': u'\u22c5', 'cap': u'\u2229', 'bigtriangledown': u'\u25bd', 'times': u'\xd7', 'setminus': u'\u2216', 'circ': u'\u2218', 'vee': u'\u2228', 'uplus': u'\u228e', 'mp': u'\u2213', 'dagger': u'\u2020', 'triangleright': u'\u25b7', 'div': u'\xf7', 'pm': u'\xb1', + # Relation symbols: + 'subset': u'\u2282', 'propto': u'\u221d', 'geq': u'\u2265', 'sqsubset': u'\u228f', 'Join': u'\u2a1d', 'frown': u'\u2322', 'models': u'\u22a7', 'supset': u'\u2283', 'in': u'\u2208', 'doteq': u'\u2250', 'dashv': u'\u22a3', 'gg': u'\u226b', 'leq': u'\u2264', 'succ': u'\u227b', 'vdash': u'\u22a2', 'cong': u'\u2245', 'simeq': u'\u2243', 'subseteq': u'\u2286', 'parallel': u'\u2225', 'equiv': u'\u2261', 'ni': u'\u220b', 'le': u'\u2264', 'approx': u'\u2248', 'preceq': u'\u2aaf', 'sqsupset': u'\u2290', 'll': u'\u226a', 'sqsupseteq': u'\u2292', 'mid': u'\u2223', 'prec': u'\u227a', 'succeq': u'\u2ab0', 'bowtie': u'\u22c8', 'perp': u'\u27c2', 'sqsubseteq': u'\u2291', 'asymp': u'\u224d', 'smile': u'\u2323', 'supseteq': u'\u2287', 'sim': u'\u223c', 'neq': u'\u2260', + # Arrow symbols: + 'searrow': u'\u2198', 'updownarrow': u'\u2195', 'Uparrow': u'\u21d1', 'longleftrightarrow': u'\u27f7', 'Leftarrow': u'\u21d0', 'longmapsto': u'\u27fc', 'Longleftarrow': u'\u27f8', 'nearrow': u'\u2197', 'hookleftarrow': u'\u21a9', 'downarrow': u'\u2193', 'Leftrightarrow': u'\u21d4', 'longrightarrow': u'\u27f6', 'rightharpoondown': u'\u21c1', 'longleftarrow': u'\u27f5', 'rightarrow': u'\u2192', 'Updownarrow': u'\u21d5', 'rightharpoonup': u'\u21c0', 'Longleftrightarrow': u'\u27fa', 'leftarrow': u'\u2190', 'mapsto': u'\u21a6', 'nwarrow': u'\u2196', 'uparrow': u'\u2191', 'leftharpoonup': u'\u21bc', 'leftharpoondown': u'\u21bd', 'Downarrow': u'\u21d3', 'leftrightarrow': u'\u2194', 'Longrightarrow': u'\u27f9', 'swarrow': u'\u2199', 'hookrightarrow': u'\u21aa', 'Rightarrow': u'\u21d2', + # Miscellaneous symbold: + 'infty': u'\u221e', 'surd': u'\u221a', 'partial': u'\u2202', 'ddots': u'\u22f1', 'exists': u'\u2203', 'flat': u'\u266d', 'diamondsuit': u'\u2662', 'wp': u'\u2118', 'spadesuit': u'\u2660', 'Re': u'\u211c', 'vdots': u'\u22ee', 'aleph': u'\u2135', 'clubsuit': u'\u2663', 'sharp': u'\u266f', 'angle': u'\u2220', 'prime': u'\u2032', 'natural': u'\u266e', 'ell': u'\u2113', 'neg': u'\xac', 'top': u'\u22a4', 'nabla': u'\u2207', 'bot': u'\u22a5', 'heartsuit': u'\u2661', 'cdots': u'\u22ef', 'Im': u'\u2111', 'forall': u'\u2200', 'imath': u'\u0131', 'hbar': u'\u210f', 'emptyset': u'\u2205', + # Variable-sized symbols: + 'bigotimes': u'\u2a02', 'coprod': u'\u2210', 'int': u'\u222b', 'sum': u'\u2211', 'bigodot': u'\u2a00', 'bigcup': u'\u22c3', 'biguplus': u'\u2a04', 'bigcap': u'\u22c2', 'bigoplus': u'\u2a01', 'oint': u'\u222e', 'bigvee': u'\u22c1', 'bigwedge': u'\u22c0', 'prod': u'\u220f', + # Braces: + 'langle': u'\u2329', 'rangle': u'\u232A'} functions = ['arccos', 'arcsin', 'arctan', 'arg', 'cos', 'cosh', 'cot', 'coth', 'csc', 'deg', 'det', 'dim', @@ -345,6 +297,7 @@ 'injlim', 'varinjlim', 'varlimsup', 'projlim', 'varliminf', 'varprojlim'] + def parse_latex_math(string, inline=True): """parse_latex_math(string [,inline]) -> MathML-tree @@ -375,8 +328,8 @@ if c == ' ': pass elif c == '\\': - if c2 in escaped: - node = node.append(mo(escaped[c2])) + if c2 in '{}': + node = node.append(mo(c2)) skip = 2 elif c2 == ' ': node = node.append(mspace()) @@ -463,12 +416,20 @@ 'Y': u'\U0001D550', 'Z': u'\u2124'} +negatables = {'=': u'\u2260', + '\in': u'\u2209', + '\equiv': u'\u2262'} + + def handle_keyword(name, node, string): skip = 0 + if len(string) > 0 and string[0] == ' ': + string = string[1:] + skip = 1 if name == 'begin': if not string.startswith('{matrix}'): raise SyntaxError, 'Expected "\begin{matrix}"!' - skip = 8 + skip += 8 entry = mtd() table = mtable(mtr(entry)) node.append(table) @@ -476,7 +437,7 @@ elif name == 'end': if not string.startswith('{matrix}'): raise SyntaxError, 'Expected "\end{matrix}"!' - skip = 8 + skip += 8 node = node.close().close().close() elif name == 'text': if string[0] != '{': @@ -485,7 +446,7 @@ if i == -1: raise SyntaxError, 'Expected "\text{...}"!' node = node.append(mtext(string[1:i])) - skip = i + 1 + skip += i + 1 elif name == 'sqrt': sqrt = msqrt() node.append(sqrt) @@ -495,9 +456,6 @@ node.append(frac) node = frac elif name == 'left': - if string[0] == ' ': - string = string[1:] - skip = 1 for par in ['(', '[', '|', '\\{', '\\langle', '.']: if string.startswith(par): break @@ -508,9 +466,6 @@ node = fenced skip += len(par) elif name == 'right': - if string[0] == ' ': - string = string[1:] - skip = 1 for par in [')', ']', '|', '\\}', '\\rangle', '.']: if string.startswith(par): break @@ -519,6 +474,14 @@ node.closepar = par node = node.close() skip += len(par) + elif name == 'not': + for operator in negatables: + if string.startswith(operator): + break + else: + raise SyntaxError, 'Expected something to negate: "\\not ..."!' + node = node.append(mo(negatables[operator])) + skip += len(operator) elif name == 'mathbf': style = mstyle(nchildren=1, fontweight='bold') node.append(style) @@ -527,7 +490,7 @@ if string[0] != '{' or not string[1].isupper() or string[2] != '}': raise SyntaxError, 'Expected something like "\mathbb{A}"!' node = node.append(mi(mathbb[string[1]])) - skip = 3 + skip += 3 else: code = special.get(name) if code is not None: |