def _create_tag(self, code, block): tree = parse_latex_math(code, inline=(not block)) html = ''.join(tree.xml()) tag = html_to_tags(html)[0] def strip_ns(tag): del tag.attrib['xmlns'] for child in tag: strip_ns(child) for child in tag: strip_ns(child) return tag
def visit_math(self, node, math_env=''): # If the method is called from visit_math_block(), math_env != ''. # As there is no native HTML math support, we provide alternatives: # LaTeX and MathJax math_output modes simply wrap the content, # HTML and MathML math_output modes also convert the math_code. if self.math_output not in ('mathml', 'html', 'mathjax', 'latex'): self.document.reporter.error( 'math-output format "%s" not supported ' 'falling back to "latex"'% self.math_output) self.math_output = 'latex' # # HTML container tags = {# math_output: (block, inline, class-arguments) 'mathml': ('div', '', ''), 'html': ('div', 'span', 'formula'), 'mathjax': ('div', 'span', 'math'), 'latex': ('pre', 'tt', 'math'), } tag = tags[self.math_output][math_env == ''] clsarg = tags[self.math_output][2] # LaTeX container wrappers = {# math_mode: (inline, block) 'mathml': (None, None), 'html': ('$%s$', u'\\begin{%s}\n%s\n\\end{%s}'), 'mathjax': ('\(%s\)', u'\\begin{%s}\n%s\n\\end{%s}'), 'latex': (None, None), } wrapper = wrappers[self.math_output][math_env != ''] # get and wrap content math_code = node.astext().translate(unichar2tex.uni2tex_table) if wrapper and math_env: math_code = wrapper % (math_env, math_code, math_env) elif wrapper: math_code = wrapper % math_code # settings and conversion if self.math_output in ('latex', 'mathjax'): math_code = self.encode(math_code) if self.math_output == 'mathjax' and not self.math_header: if self.math_output_options: self.mathjax_url = self.math_output_options[0] self.math_header = [self.mathjax_script % self.mathjax_url] elif self.math_output == 'html': if self.math_output_options and not self.math_header: self.math_header = [self.stylesheet_call( utils.find_file_in_dirs(s, self.settings.stylesheet_dirs)) for s in self.math_output_options[0].split(',')] # TODO: fix display mode in matrices and fractions math2html.DocumentParameters.displaymode = (math_env != '') math_code = math2html.math2html(math_code) elif self.math_output == 'mathml': self.doctype = self.doctype_mathml self.content_type = self.content_type_mathml try: mathml_tree = parse_latex_math(math_code, inline=not(math_env)) math_code = ''.join(mathml_tree.xml()) except SyntaxError as err: err_node = self.document.reporter.error(err, base_node=node) self.visit_system_message(err_node) self.body.append(self.starttag(node, 'p')) self.body.append(u','.join(err.args)) self.body.append('</p>\n') self.body.append(self.starttag(node, 'pre', CLASS='literal-block')) self.body.append(self.encode(math_code)) self.body.append('\n</pre>\n') self.depart_system_message(err_node) raise nodes.SkipNode # append to document body if tag: self.body.append(self.starttag(node, tag, suffix='\n'*bool(math_env), CLASS=clsarg)) self.body.append(math_code) if math_env: self.body.append('\n') if tag: self.body.append('</%s>\n' % tag) # Content already processed: raise nodes.SkipNode