MathType Input Translator 1.0: "HTML", "HTML input translator v1.00 by Design Science, Inc."

// "HTML input.tdl"

// Copyright (c) 2007-2010 by Design Science, Inc.
// License: You are allowed to create your own translators based on the contents of this file
// solely for use with MathType. It is recommended that you change the filename, title, and description
// in the first line of this file so as to reflect your modifications and the change of author and purpose.
// Design Science, Inc. is not responsible for any damages caused by the use of this file or derived works.

// $Header: /MathType/Translators/HTML input.tdl 16    1/19/10 12:34p Jimm $

input = "HTML";

rules {
	// image tags embedded in HTML (have texvc code in alt attribute)
//	img ? (exists(class) && class == "tex" && exists(alt)) => texvc { code = $0.alt; };
	// for images with tex in alt
	img ?(exists(alt)) char?(char==0x0020)* => texvc { code = $0.alt; };
	// for zoho writer
	img ?(exists(latexexpression)) char?(char==0x0020)* => texvc { code = $0.latexexpression; };

	// mark delimiter chars, for possible later matching
	char?(char=='(' || char==')' || char=='[' || char== ']' || char== '{' ||
		char== '|' || char== '}')@d => expr{kind="delimiter"; type=d.char;};
	// newlines and line feeds
	char?(char==0x000A) => expr{type=0x000A;};
	char?(char==0x000D) => expr{type=0x000D;};

	// named entities are in an auxiliary file
	#include "HTML input (chars).tdl"

		// variables show in italics
	i expr* `/i` => expr{type=0;style="variable";};
	em expr* `/em` => expr{type=0;style="variable";};
	// vectors in bold
	b expr* `/b` => expr{type=0;style="vector";};
	strong expr* `/b` => expr{type=0;style="vector";};
	// scripts
	sup expr* `/sup` => sup_expr;
	sub expr* `/sub` => sub_expr;

	// these combinations we recognize for templates
	expr?(type=='(') expr?(type!=')' && type!='(' && type!=']' && type!='[')+  expr?(type==')') => fenced_expr{type="paren";};
	expr?(type=='[') expr?(type!=')' && type!='(' && type!=']' && type!='[')+ expr?(type==']') => fenced_expr{type="brack";};
	expr?(type=='{') expr?(type!='}' && type!='{')+ expr?(type=='}') => fenced_expr{type="brace";};
	expr?(type=='|') expr?(type!='|')+ expr?(type=='|') => fenced_expr{type="bar";};
	expr?(type=='[') expr?(type!=')' && type!='(' && type!=']' && type!='[')+ expr?(type==')') => fenced_expr{type="intval"; opts="/lbrp";};
	expr?(type=='(') expr?(type!=')' && type!='(' && type!=']' && type!='[')+ expr?(type==']') => fenced_expr{type="intval"; opts="/lprb";};
	expr?(type=='[') expr?(type!=')' && type!=']' && type!='[')+ expr?(type=='[') => fenced_expr{type="intval"; opts="/lblb";};
	expr?(type==']') expr?(type!='(' && type!=']' && type!='[')+ expr?(type==']') => fenced_expr{type="intval"; opts="/rbrb";};
	expr?(type==']') expr?(type!='[' && type!=']')+ expr?(type=='[') => fenced_expr{type="intval"; opts="/rblb";};

	// make everything into an expr
	char | sup_expr | sub_expr | fenced_expr | texvc => expr{type=0;};
	// these we ignore, really, but to keep the tree correct...
	p expr* `/p` => expr{type=0;};
	span expr* `/span` => expr{type=0;};
}

outputs {
	_input_root => eqn(slot(*));
	// process the image tag
	texvc => apply( "Texvc input.tdl", tex_normalize(match.code) );

	// fenced expressions (note that we don't process first and last children (the fences)
	fenced_expr?(type=="paren")  => paren(slot([$1,$(-2) - $0]));
	fenced_expr?(type=="brack")  => brack(slot([$1,$(-2) - $0]));
	fenced_expr?(type=="brace")  => brace(slot([$1,$(-2) - $0]));
	fenced_expr?(type=="bar")    => bar(slot([$1,$(-2) - $0]));
	fenced_expr?(type=="intval") => intval(slot([$1,$(-2) - $0])){_options=match.opts;};
	// fenced_expr => paren(slot(*));

	sup_expr => sup(slot(*));
	sub_expr => sub(slot(*));

	// tables
	table_expr => matrix(*);
	tr_expr?(name(^this)!="table_expr") => matrix(row(*));
	tr_expr => row(*);
	td_expr => cell(*);

	// we don't (generally) want newlines and line feeds
	expr?(type==0x000A || type==0x000D) => ;
	// expr of non-zero type not processed above are from named entities
	expr?(type!=0) => char{mtcode=match.type;};
	// a generic expr
	expr => slot(*);

	// chars (exceptions go first)
	// there sometimes are newlines and line feeds
	char?(char==0x000A || char==0x000D) => ;

	// numeric chars
	char?(char>48 && char<58)  => char{mtcode=match.char;style=find_attribute(match,"style","text");};
	// alpha chars
	char?(char>64 && char<91)  => char{mtcode=match.char;style=find_attribute(match,"style","text");};
	char?(char>96 && char<123) => char{mtcode=match.char;style=find_attribute(match,"style","text");};
	// generic chars
	char => char{mtcode=match.char;style=find_attribute(match,"style","math");};
}

