def _latex_preparse(self, s, locals): """ Replace instances of '\sage{x}' in s with the LaTeX version of x in the running session. EXAMPLES: sage: s = 2 sage: sage.misc.latex.Latex()._latex_preparse('\sage{s}', locals()) '2' """ i0 = -1 while True: i = s.find('\\sage{') if i == -1 or i == i0: return s i0 = i t = s[i+6:] j = t.find('}') if j == -1: return s var = t[:j] try: k = latex(sage_eval.sage_eval(var, locals)) except Exception, msg: print msg k = '\\mbox{\\rm [%s undefined]}'%var s = s[:i] + k + t[j+1:]
def preparse_imports_from_sage(self, line): """ Finds occurrences of strings such as ``sage(object)`` in *line*, converts ``object`` to :attr:`shell.interface`, and replaces those strings with their identifier in the new system. This also works with strings such as ``maxima(object`` if :attr:`shell.interface` is ``maxima``. :param line: the line to transform :type line: string .. warning:: This does not parse nested parentheses correctly. Thus, lines like ``sage(a.foo())`` will not work correctly. This can't be done in generality with regular expressions. EXAMPLES:: sage: from sage.misc.interpreter import interface_shell_embed, InterfaceShellTransformer sage: shell = interface_shell_embed(maxima) sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, prefilter_manager=shell.prefilter_manager) sage: ift.shell.ex('a = 3') sage: ift.preparse_imports_from_sage('2 + sage(a)') '2 + sage0 ' sage: maxima.eval('sage0') '3' sage: ift.preparse_imports_from_sage('2 + maxima(a)') '2 + sage1 ' sage: ift.preparse_imports_from_sage('2 + gap(a)') '2 + gap(a)' """ from sage_eval import sage_eval for sage_code in self._sage_import_re.findall(line): expr = preparse(sage_code) result = self.shell.interface(sage_eval(expr, self.shell.user_ns)) self.temporary_objects.append(result) line = self._sage_import_re.sub(' ' + result.name() + ' ', line, 1) return line