tags:

views:

350

answers:

2

I'm writing a complex script that takes the XML backup of a Blogger blog and converts it to InDesign Tagged Text to be laid out in a book. I'm using a whole bunch of regular expressions to clean out the HTML tags of each blog post and convert them to InDesign tags. For example:

<p>A really long paragraph.</p> -> <ParaStyle:Main text>A really long paragraph.
<em>Whatever</em> -> <CharStyle:Italic>Whatever<CharStyle:>

For the most part the script is working great. However, InDesign can't handle nested tags. <CharStyle:Small><CharStyle:Italic>This is small italic text<CharStyle:><CharStyle:> will not work and needs to end up as <CharStyle:Small italic>This is small italic text<CharStyle:>

I'm trying to use variables in regex search patterns to find anywhere where character style tags are doubled up, but when I use the variables, nothing is found. If I hard code the InDesign tags into the regex, though, it works. What is making the variables unfindable?

Here's a working excerpt from my code (in real life $input isn't a string variable, but a LibXML object that the script parses...this is just for illustration)

#!/usr/bin/perl -w
use strict;

my $IDitalic = "<~~CharStyle:Italic>";
my $IDsmall = "<~~CharStyle:Small>";
my $IDsmallitalic = "<~~CharStyle:Small italic>";
my $IDcharend = "<~~CharStyle:>";

sub cleanText {
    my $text = $_[0];

    # Replace any span with a font size attribute with "small" character style
    $text =~ s/<span[^>]*?font-size[^>]*>(.*?)<\/span>/$IDsmall$1$IDcharend/gis;

    # Replace <em> tags with "italic" character style
    $text =~ s/<em>(.*?)<\/em>/$IDitalic$1$IDcharend/gis;

    #--------------------------------------------------------
    # Problem section
    #
    # The following works since everything is hard coded
    # $text =~ s/<~~CharStyle:Small><~~CharStyle:Italic>/$IDsmallitalic/gi;
    # $text =~ s/<~~CharStyle:><~~CharStyle:>/$IDcharend/gi;

    # When I use variables, though, it doesn't work...
    $text =~ s/{$IDsmall}{$IDitalic}/$IDsmallitalic/gi;
    $text =~ s/({$IDcharend})\1+/$1/gi;

    #--------------------------------------------------------


    # Clear out all tags that aren't the InDesign tags, take out the dummy ~~ and rebuild the actual tag
    $text =~ s/<[^~~](?:[^>'"]*|(['"]).*?\1)*>//gs;
    $text =~ s/<~~/</gs;

    return $text;
}

my $input = "<~~ParaStyle:Main text>In sodales malesuada nisi quis varius. Proin a ligula mauris. Proin ac justo est, vitae sollicitudin tortor. Proin auctor, <span style=\"font-size:78%\">augue eu</span> fringilla imperdiet, nisi sapien tempus libero, sed aliquet quam metus vel risus. Curabitur feugiat tristique porttitor. Integer malesuada volutpat accumsan. <span class=\"dummy\"In egestas</span> metus ut erat placerat tempus. <em>Nam vestibulum</em>, est quis scelerisque tincidunt, enim est lacinia ligula, vel accumsan ante nisl consectetur massa. Nullam velit nisi, viverra quis viverra ac, dictum ac enim. Sed nisl magna, fringilla at placerat quis, facilisis id nibh. Mauris eget sapien mauris, nec sollicitudin urna. Curabitur ac nunc a arcu vulputate tincidunt.\n<~~ParaStyle:Main text><span style=\"font-size:78%\"><em>**This is really small text</em></span>\n<ParaStyle:Comments\:Comment author>Andrew\n<~~ParaStyle:Comments\:Comment date>Friday, May 29, 2009— 8:15 PM";


print cleanText($input);

So, what's going wrong?

Also, is there a better way to maintain the InDesign tags without having the dummy tildes in the variable name?

Thanks!

Author apparently decided to parse the HTML, for more information go to the parsing follow-up question.

+3  A: 

Try putting your '$' outside the '{'.. like so:

$text =~ s/${IDsmall}${IDitalic}/$IDsmallitalic/gi;
$text =~ s/(${IDcharend})\1+/$1/gi;
kbosak
That did the trick! Strange, though, since elsewhere in the script I have a $ in the braces and it works. What's the real standard?
Andrew
@Andrew the standard is to only use braces in cases where Perl cannot tell what the variable name is. Take a look at my answer for an example.
Chas. Owens
@Andrew: in other parts of the script you were probably doing something different. In this example the braces don't really do much for you since there is no ambiguity in the variable names's ends.
brian d foy
+11  A: 

The first thing you are doing wrong is trying to use regexes on XML, as you have noticed, it doesn't work. This is a fundamental limitation of regexes. You should be using a parser instead. I like XML::Twig.

The second thing you are doing wrong is saying {$IDsmall} in the regex. That means a literal { the variable's contents then a literal }. Since the literal curly braces are in your text versions I assume you meant to type ${IDsmall}, however, that is unnecessary because the curly braces are only needed when you must make it clear what is a variable and what is text like this /${IDsmall}some other text/. In this case, without the curly braces Perl would think you were referring to a variable named $IDsmallsome.

The third thing you are doing wrong is not using \Q and \E to prevent special characters in your variables from affecting the match: /\Q$IDsmall\E/. Of course, if you meant for the special characters to affect the match, then you shouldn't be using a normal string. You should be using a quoted regex made by the qr// operator.

The fourth thing you are doing wrong is trying to use a negated character class to match more than one character: <[^~~](?:[^>'"]*|(['"]).*?\1)*>. /[^~~]/ means the same thing as /[^~]/. You probably want /[^~]{2}/.

There may be other problems, those were just what I saw on a first glance.

Chas. Owens
Good points, I should have brought some of these up in my answer.
kbosak
In actuality I am parsing the XML. The blog post HTML is stored in a node named <content> -- in the actual script it is parsed out and saved as a variable that then gets regexified.I think my biggest problem was the \Q \E thing. I've also come across quotemeta(); is there a difference?And oops on the double negated character thing. Fixing that now...And I'm sure there are plenty of other problems with the script. It's my first real large Perl script...Thanks!
Andrew
Ah, I see what you mean by parsing the XML... So, to best handle the text, I should use another parser on the HTML content that I already parsed out of the XML file, right?
Andrew
@Andrew: no, quotemeta does to an expression what \Q...\E does to its contents in a string (after interpolation).
ysth
@Andrew \Q\E in a regex should be calling quotemeta behind the scenes for you, so, as long as I am not insane, they should be equivalent. As for parsing the HTML, yes, you should be using a parser for that. XML::Twig can handle HTML as well as XML. Given that you already have the HTML in a string, take a look at the parse_html method.
Chas. Owens
Alrighty then... now I need to figure out how to use LibXML to parse HTML (since I'm already using it for the XML), and replace all my regexes with parse statements. What fun! :) Thanks!
Andrew
InDesign has an XML format, but it also a different XML-like format which is the one you see here. I just play with it with regexes. I don't have a general solution, but I'm not going to waste time with that either. For this sort of transformation, a regex gets the job done nicely.
brian d foy