| 1 | |||
| 2 | # Download source from http://tidy.sourceforge.net/docs/Overview.html#Download | ||
| 3 | # http://tidy.sourceforge.net/ | ||
| 4 | ### XML TOOLS ! | ||
| 5 | # http://www.garshol.priv.no/download/xmltools/cat_ix.html#SC_GNConv | ||
| 6 | |||
| 7 | # http://www.chami.com/html-kit/ | ||
| 8 | # --output-xml yes --output-xhtml yes --add-xml-decl yes | ||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | package WWW::Scraper::TidyXML; | ||
| 13 | $VERSION = sprintf("%d.%02d", q$Revision: 1.05 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | my $DaveRaggettVersion = 0; # Different versions of tidy.exe require different processing. | ||
| 17 | |||
| 18 | { package WWW::Scraper::TidyXML::_struct_; | ||
| 19 | use Class::Struct; | ||
| 20 | struct ( | ||
| 21 | 'm_asXML' => '$' | ||
| 22 | ,'m_asString' => '$' | ||
| 23 | ,'m_isTidyd' => '$' # indicates this string has been "Tidy.exe"-d. | ||
| 24 | ,'m_xmlParser' => '$' | ||
| 25 | ,'m_TRACE' => '$' | ||
| 26 | ,'m_context' => '$' | ||
| 27 | ,'m_found_context' => '$' | ||
| 28 | ,'artifactFolder' => '$' | ||
| 29 | ,'parentScraper' => '$' | ||
| 30 | ,'frameType' => '$' | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | use base qw(WWW::Scraper::TidyXML::_struct_); | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $self = new WWW::Scraper::TidyXML::_struct_; | ||
| 37 | bless $self, shift; | ||
| 38 | |||
| 39 | my $scraper; | ||
| 40 | my $string = ''; | ||
| 41 | for my $parm ( @_ ) { | ||
| 42 | if ( $parm and not ref $parm ) { | ||
| 43 | open TMP, "<$parm" or die "Can't open '$parm': $!\n"; | ||
| 44 | my $tmp = join '', <TMP>; close TMP; | ||
| 45 | $string = \$tmp; | ||
| 46 | } else { | ||
| 47 | if ( ref($parm) eq 'HASH' ) { | ||
| 48 | map { $self->$_($$parm{$_}) } keys %$parm; | ||
| 49 | } elsif ( ref($parm) =~ m/WWW::Scraper/ ) { | ||
| 50 | $self->parentScraper($parm); | ||
| 51 | } elsif ( ref($parm) eq 'SCALAR' ) { | ||
| 52 | $string = $parm; | ||
| 53 | } else { | ||
| 54 | $string = \$parm; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | # Stash the original HTML into the artifacts folder. | ||
| 60 | if ( $string && $self->artifactFolder && (my $scraper = $self->parentScraper) ) { | ||
| 61 | my $artName = $self->artifactFolder().'/'.$scraper->{'scraperName'}.'_pg_'.$scraper->pageNumber.'.htm'; | ||
| 62 | open TMP, ">$artName" || warn "Can't open '$artName' to write: $!"; | ||
| 63 | print TMP $$string; | ||
| 64 | close TMP; | ||
| 65 | } | ||
| 66 | |||
| 67 | if ( ($self->frameType eq 'TidyXML') and $string and not ($$string =~ m-<meta name="generator" content="HTML Tidy, see www.w3.org" />-s) ) { | ||
| 68 | open TMP, ">temp.html" or die "Can't open 'temp.html': $!"; | ||
| 69 | print TMP fixupHtmlForTidy($string); | ||
| 70 | close TMP; | ||
| 71 | |||
| 72 | my $rslt; | ||
| 73 | if ( $self->getTidyVersion() == 1 ) { | ||
| 74 | $rslt = `tidy -upper -asxml -numeric -clean temp.html 2>temp.err`; | ||
| 75 | unless ( $rslt ) { | ||
| 76 | open TMP, "<temp.err"; my $err = join '',<TMP>; close TMP; | ||
| 77 | unless ( $err =~ m/Parsing "temp\.html"/s ) { | ||
| 78 | warn "$err"; | ||
| 79 | die "This Scraper engine requires 'Tidy' to scrub HTML before parsing.\nGet this program from 'http://tidy.sourceforge.net/docs/Overview.html#Download'\n"; | ||
| 80 | } | ||
| 81 | # warn "$err"; | ||
| 82 | } | ||
| 83 | unlink 'temp.html'; | ||
| 84 | $string = \$rslt; | ||
| 85 | } elsif ( $WWW::Scraper::TidyXML::DaveRaggettVersion == 2 ) { | ||
| 86 | $rslt = `tidy -upper -asxml -numeric --show-warnings no --write-back yes --clean yes --force-output yes --error-file temp.err temp.html >tidy.stdout 2>tidy.stderr`; | ||
| 87 | if ( $? ) { | ||
| 88 | open TMP, "<temp.err"; my $err = join '',<TMP>; close TMP; | ||
| 89 | unless ( $err =~ m/Parsing "temp\.html"/s ) { | ||
| 90 | warn "$err"; | ||
| 91 | die "This Scraper engine requires 'Tidy' to scrub HTML before parsing.\nGet this program from 'http://tidy.sourceforge.net/docs/Overview.html#Download'\n"; | ||
| 92 | } | ||
| 93 | # warn "$err"; | ||
| 94 | } | ||
| 95 | open TMP, "<temp.html" or die "Can't reopen 'temp.html': $!"; | ||
| 96 | my $xml = join '',<TMP>; | ||
| 97 | $string = \$xml; | ||
| 98 | close TMP; | ||
| 99 | unlink 'temp.html'; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | die "This Scraper engine requires 'Tidy' to scrub HTML before parsing.\nGet this program from 'http://tidy.sourceforge.net/docs/Overview.html#Download'\n"; | ||
| 103 | } | ||
| 104 | # Stash the tidied XML into the artifacts folder. | ||
| 105 | if ( $self->artifactFolder && (my $scraper = $self->parentScraper) ) { | ||
| 106 | my $artName = $self->artifactFolder().'/'.$scraper->{'scraperName'}.'_pg_'.$scraper->pageNumber.'.xml'; | ||
| 107 | open TMP, ">$artName" || warn "Can't open '$artName' to write: $!"; | ||
| 108 | print TMP $$string; | ||
| 109 | close TMP; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | $self->m_asString($string); | ||
| 114 | $self->m_isTidyd($WWW::Scraper::TidyXML::DaveRaggettVersion); | ||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | # Here are some common rewrites that should be done before handing the string to tidy.exe. | ||
| 119 | sub fixupHtmlForTidy { | ||
| 120 | my ($string) = @_; | ||
| 121 | $$string =~ s-<noframe>.*?</noframe>--gsi; | ||
| 122 | return $$string; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | # Extract the given xml path from the given TidyXML. Return reference to the string version. | ||
| 127 | sub asString { | ||
| 128 | my ($self, $xmlPath) = @_; | ||
| 129 | |||
| 130 | unless ( defined $xmlPath ) { | ||
| 131 | my $result; | ||
| 132 | if ( $self->m_context() ) { | ||
| 133 | my $xml; | ||
| 134 | $xml = $self->m_context()->toString(); | ||
| 135 | $result = \$xml; | ||
| 136 | } else { | ||
| 137 | $result = $self->m_asString(); | ||
| 138 | } | ||
| 139 | return $result; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $node; | ||
| 143 | |||
| 144 | #### | ||
| 145 | #### XML::XPath-wise parsing . . . | ||
| 146 | #### | ||
| 147 | # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 148 | #return \undef unless $parsedXML = $elements->item($count-1); | ||
| 149 | my $parsedXML = $self->m_asXML(); | ||
| 150 | unless ( $parsedXML ) { | ||
| 151 | # Just-in-time parsing of the TidyXML string. | ||
| 152 | my $xml = $self->m_asString(); | ||
| 153 | return \undef unless $$xml; | ||
| 154 | $$xml =~ s-<!DOCTYPE.*?>--s; | ||
| 155 | eval { | ||
| 156 | use XML::XPath; | ||
| 157 | $parsedXML = XML::XPath->new('xml' => $$xml); | ||
| 158 | $self->m_asXML($parsedXML); | ||
| 159 | $self->m_context(''); | ||
| 160 | }; | ||
| 161 | die "Can't XML parse : $@" if $@; | ||
| 162 | $self->m_asXML($parsedXML); | ||
| 163 | } | ||
| 164 | |||
| 165 | my $context = $self->m_context(); | ||
| 166 | $context = undef unless $context; | ||
| 167 | my $nodeset = $parsedXML->find($xmlPath, $context); # find all paragraphs | ||
| 168 | my @node = $nodeset->get_nodelist(); | ||
| 169 | $node = $node[0]; | ||
| 170 | |||
| 171 | unless ( $parsedXML = $node ) { | ||
| 172 | $self->m_found_context(''); | ||
| 173 | return \undef; | ||
| 174 | } | ||
| 175 | |||
| 176 | $self->m_found_context($node); | ||
| 177 | |||
| 178 | my $result = $parsedXML->toString(); | ||
| 179 | return \$result; | ||
| 180 | } | ||
| 181 | |||
| 182 | # Extract the given xml path from the given TidyXML. Return the xml version. | ||
| 183 | sub asXML { | ||
| 184 | my ($self, $xmlPath) = @_; | ||
| 185 | |||
| 186 | return $self->m_asXML() unless defined $xmlPath; | ||
| 187 | |||
| 188 | die "TidyXML::asXML(path) is not implemented."; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | # Returns the marked up text from the referenced string, as designated by the given tag. | ||
| 194 | # This algorithm extracts the contents of the first <$tag> element it encounters, | ||
| 195 | # taking into consideration that it may contain <$tag> elements within it. | ||
| 196 | # It removes the marked text from the original string, strips off the markup tags, | ||
| 197 | # and returns that result. | ||
| 198 | # (if wantarray, will return result and first tag, with brackets removed) | ||
| 199 | # | ||
| 200 | sub getMarkedText { | ||
| 201 | my ($self, $tag, $withContent) = @_; | ||
| 202 | |||
| 203 | my ($sub_string, $theRest) = WWW::Scraper::getMarkedText(undef, $tag, $self->asString(), $withContent); | ||
| 204 | |||
| 205 | $self->m_asXML(''); # By calling getMarkedText(), we lost any pre-parsing XPath may have set up. | ||
| 206 | $self->m_context(''); | ||
| 207 | $self->m_found_context(''); | ||
| 208 | |||
| 209 | return ($sub_string, $theRest) if wantarray; | ||
| 210 | return $sub_string; | ||
| 211 | } | ||
| 212 | |||
| 213 | my $AttributeGrabber = qr/(?:\s*\w[\w\d]*=(?:(?:"(?:(?:\\")|(?:[^"]))*")|(?:'(?:(?:\\')|(?:[^']))*')|(?:[^'"][^ >]*)))/; | ||
| 214 | sub Attributes { | ||
| 215 | my ($self, $attrs) = @_; | ||
| 216 | |||
| 217 | my $attributes = {}; | ||
| 218 | my (@attrs) = ($attrs =~ m{($AttributeGrabber)}gs); | ||
| 219 | for ( @attrs ) { | ||
| 220 | my ($name, $attr) = (m{\s*(\w[\w\d]*)=(.*)$}); | ||
| 221 | $attr = '' if !defined $attr; | ||
| 222 | $attr =~ s{^'(.*)'$}{$1} unless $attr =~ s{^"(.*)"$}{$1}; | ||
| 223 | $attributes->{lc $name} = $attr; | ||
| 224 | } | ||
| 225 | return $attributes; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub getMarkedTextAndAttributes { | ||
| 229 | my ($self, $tag, $withContent) = @_; | ||
| 230 | |||
| 231 | my ($sub_string, $attrs) = $self->getMarkedText($tag, $withContent); | ||
| 232 | return undef unless $sub_string; | ||
| 233 | |||
| 234 | my $attributes = $self->Attributes($attrs); | ||
| 235 | return ($sub_string, $attributes) if wantarray; | ||
| 236 | return $attributes; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub isNotTestable { | ||
| 240 | my ($self, $scraperEngine) = @_; | ||
| 241 | my $rslt = `tidy -version 2>tidy.stderr`; | ||
| 242 | if ( $? ) { | ||
| 243 | return ($scraperEngine?$scraperEngine:"This Scraper engine")." requires 'HTML Tidy' to scrub HTML before parsing.\nGet this program from 'http://tidy.sourceforge.net/docs/Overview.html#Download'\nMake sure it is in your execution search path.\n"; | ||
| 244 | } | ||
| 245 | return ''; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub getTidyVersion { | ||
| 249 | |||
| 250 | unless ( $WWW::Scraper::TidyXML::DaveRaggettVersion ) { | ||
| 251 | my $rslt = `tidy -version 2>tidy.version`; | ||
| 252 | if ( $? ) { | ||
| 253 | $WWW::Scraper::TidyXML::DaveRaggettVersion = -1; | ||
| 254 | } else { | ||
| 255 | open TMP, "<tidy.version"; $rslt = join "\n",<TMP>; close TMP; | ||
| 256 | # Dave Raggett | ||
| 257 | #HTML Tidy release date: 30th April 2000 | ||
| 258 | #See http://www.w3.org/People/Raggett for details | ||
| 259 | if ( $rslt =~ m{HTML Tidy release date: 30th April 2000} ) { | ||
| 260 | $WWW::Scraper::TidyXML::DaveRaggettVersion = 1; | ||
| 261 | } else { | ||
| 262 | $WWW::Scraper::TidyXML::DaveRaggettVersion = 2; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | return $WWW::Scraper::TidyXML::DaveRaggettVersion; | ||
| 267 | } | ||
| 268 | |||
| 269 | # Pick an input parameter according to the version of tidy.exe. | ||
| 270 | sub accordingToTidyVersion { | ||
| 271 | WWW::Scraper::TidyXML::getTidyVersion(); | ||
| 272 | if ( $WWW::Scraper::TidyXML::DaveRaggettVersion > 0 ) { | ||
| 273 | return $_[$WWW::Scraper::TidyXML::DaveRaggettVersion-1]; | ||
| 274 | } else { | ||
| 275 | return undef; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | 1; | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | __END__ |