[Boulder.pm] Trickey (for a newbie) String Replacement
Luke Palmer
luke at luqui.org
Wed May 5 15:56:32 CDT 2004
Peter Hutnick writes:
> Hello,
>
> I'm writing a little script to do some of the grunt-work of converting a
> LaTeX document to XHTML.
>
> Consider the following block of code:
>
> @rule=( 's/\\emph{(.*?)}/\<em\>$1\<\/em\>/g',
> 's/\\chapter{(.*)}/\<h1\>$1\<\/h1\>/g');
>
> foreach (@rule) {
> print $_, ";\n";
> $text =~ $_;
> }
>
> As, I'm sure you can see, this does nothing. I think you can also see
> what I am driving at.
You had me staring at this for five minutes wondering why it didn't
work. I completely missed the fact that you didn't have an eval.
Here's what you want (note that I fixed your regexes a little, too):
@rule = (
's[\\\\emph{(.*?)}] [<em>$1</em>]g',
's[\\\\chapter{(.*?)}] [<h1>$1</h1>]g',
);
# this only works if the text is in $_
# you might have to do a little BS to get it there
foreach my $rule (@rule) {
eval $rule;
}
But I'm sure someone's written a LaTeX to XHTML converter before. Do a
little searching around (or mabye you have).
I'd also suggest using Parse::RecDescent if you want this to scale, or
if you plan on using this on multiple documents in the future. LaTeX
has a heirarchial structure, and Perl 5's regexes don't work very well
with that.
Luke
More information about the Boulder-pm
mailing list