OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: XML pretty printing in HTML

[ Lists Home | Date Index | Thread Index ]
  • From: Warren Hedley <w.hedley@auckland.ac.nz>
  • To: Matt Sergeant <matt.sergeant@bbc.co.uk>
  • Date: Thu, 08 Jul 1999 09:16:27 +1200

Hi Matt

Thanks for your help with this problem. I've been playing around with
your code and it's pretty impressive (in its elegance, I mean). I haven't
had much of a chance to look at XML::Parser so far, but this really
blew me away.

Anyway, because of problems with empty elements (generating start and
end events) and whitespace handling, I may have to write a regexp-based
parser as you suggested.

As it happens, this wasn't anything close to legal HTML4. You're not
allowed to but <FONT> elements inside a <PRE> section anyway. So I'm
going to have to mess around with <P>'s, <BR />'s and &nbsp;'s.

Sounds like fun.


Matt Sergeant wrote:
> 
> OK. Here's an attempt:
> 
> use XML::Parser;
> 
> print XML::Parser->new(Handlers => {
>         # Handlers using closures, except Start 'cos it's more complex.
>         # $_[0] is the expat object where we store the HTML output
>         Init => sub { $_[0]->{html} = '<pre style="font:12pt monospace">' },
>         Final => sub { $_[0]->{html} .= </pre>; return($_[0]->{html}) },
>         Start => \&start,
>         End => sub { $_[0]->{html} .= '&lt;/<font color="green">' . $_[1] .
> '</font>&gt;' },
>         Char => sub { $_[0]->{html} .= "<b>$_[1]</b>" },
>         CdataStart => sub { $_[0]->{html} .= '<font color="yellow">' },
>         CdataEnd => sub { $_[0]->{html} .= '</font>' },
>         }
> )->parsefile($ARGV[0]);
> 
> sub start {
>         my $expat = shift;
>         my $element = shift;
>         my %attribs = @_;
> 
>         $expat->{html} .= '&lt;<font color="green">' . $element . '</font>';
>         if (%attribs) {
>                 foreach (keys %attribs) {
>                         $expat->{html} .= ' <font color="red">' . $_ .
>                                 '</font>=&quot;<font color="blue">' .
>                                 $attribs{$_}.
>                                 '</font>&quot;';
>                 }
>         }
>         $expat->{html} .= '&gt;';
> }




-- 
Warren Hedley

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo@ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@ic.ac.uk)






 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS