Mailing List Archive

Proposal for rendering math formulas
My idea is like this:
string render_math(string text)
{
tree parse_tree = parse_and_validate (text);
if (parse_tree != NULL)
{
string new_text = render_tex(parse_tree);
string hash = md5(new_text);
string file_name = file_prefix + hash;
if (file_exists(file_name))
return "<img ... alt=" + htmlattr_escape(text) + ">";
else
if (tex_render(new_text, file_name))
return "<img ... alt=" + htmlattr_escape(text) + ">";
else
return "<b>Warning: couldn't render: " + wiki_escape(text) + "</b>";
} else {
return text;
}
}

Where:
md5() - obvious
file_prefix - obvious
httmlattr_espace() - obvious
wiki_escape() - obvious
file_exists() - obvious
tex_render() - call external tex rendering program
parse_and_validate() - generated LALR parser which checks if TeX
is safe. If it doesn't understand it, then
it's NOT safe.
render_tex() - render TeX string from tree. Place to add
all extra features, alternative syntaxes etc.
In easiest version just copies first text.

What do you think ?
Re: Proposal for rendering math formulas [ In reply to ]
On Mon, Nov 25, 2002 at 11:30:09PM +0100, Tomasz Wegrzanowski wrote:
> My idea is like this:
> string render_math(string text)
> {
> tree parse_tree = parse_and_validate (text);
> if (parse_tree != NULL)
> {
> string new_text = render_tex(parse_tree);
> string hash = md5(new_text);
> string file_name = file_prefix + hash;
> if (file_exists(file_name))
> return "<img ... alt=" + htmlattr_escape(text) + ">";
> else
> if (tex_render(new_text, file_name))
> return "<img ... alt=" + htmlattr_escape(text) + ">";
> else
> return "<b>Warning: couldn't render: " + wiki_escape(text) + "</b>";
> } else {
> return text;
> }
> }
>
> Where:
> md5() - obvious
> file_prefix - obvious
> httmlattr_espace() - obvious
> wiki_escape() - obvious
> file_exists() - obvious
> tex_render() - call external tex rendering program
> parse_and_validate() - generated LALR parser which checks if TeX
> is safe. If it doesn't understand it, then
> it's NOT safe.
> render_tex() - render TeX string from tree. Place to add
> all extra features, alternative syntaxes etc.
> In easiest version just copies first text.
>
> What do you think ?

Hi,

I already hacked something similar, and I render "simple" formulas as HTML if
possible (e.g. only letters, +, -, *, /, exponents and indices). At the moment
I don't have the time to get it to a production level quality (and I think as
long as we have these performance problems we should not add the load of
generating lots of little images to the poor server). Are you interested in
taking a look at the code?

Regards,

JeLuF
Re: Proposal for rendering math formulas [ In reply to ]
On Mon, Nov 25, 2002 at 11:47:27PM +0100, Jens Frank wrote:
> Hi,
>
> I already hacked something similar, and I render "simple" formulas as HTML if
> possible (e.g. only letters, +, -, *, /, exponents and indices). At the moment
> I don't have the time to get it to a production level quality (and I think as
> long as we have these performance problems we should not add the load of
> generating lots of little images to the poor server). Are you interested in
> taking a look at the code?

Sure, send it to me.
Re: Proposal for rendering math formulas [ In reply to ]
On Mon, Nov 25, 2002 at 11:58:03PM +0100, Tomasz Wegrzanowski wrote:
> On Mon, Nov 25, 2002 at 11:47:27PM +0100, Jens Frank wrote:
> > Hi,
> >
> > I already hacked something similar, and I render "simple" formulas as HTML if
> > possible (e.g. only letters, +, -, *, /, exponents and indices). At the moment
> > I don't have the time to get it to a production level quality (and I think as
> > long as we have these performance problems we should not add the load of
> > generating lots of little images to the poor server). Are you interested in
> > taking a look at the code?
>
> Sure, send it to me.

Here you are. It works like this:

- Formulas are given in the normal article as [[math:a+b]].
- The PHP-Render-Code checks whether the formula is "simple",
whether it only contains constructs like letter, +, -, *, /,
exponents and indices. If yes, the formula is rendered as HTML,
=> Exit.
- The PHP-Render-Code checks whether an image for this formula exists.
As filename a MD5-Hash of the formula is used.
- If it does not exist, a TeX-File containing some header and footer is
generated and passed to a skript generating a PNG.
- The image link is added.

I do not parse the TeX-code. There are *much* to many allowed constructs.
I just grep for two not allowed constructs.

Both the HTML-Formula or the image are stored to the database for caching
purposes.

If latex fails to parse the file, I start up latex to render the error
message into a PNG. So only the tex2png-Skript has to handle exceptions.


Regards,

JeLuF