#!/usr/bin/perl -w # html2ascii - prosty konwerter HTML -> ASCII # (C) 2000 Marcin Owsiany my $buff; # tymczasowo przechowuje caly plik my @buff; # przechowuje wszystkie linie pliku my $debug = 0; # debugging my $ullvl = 0; # poziom wciecia przy listach my $hrefbase = ""; # baza odnosnikow wzglednych my @hrefs = ('INVALID'); # odnosniki w pliku my @listtype = ("*"); # hierarchia typow wypunktowania list my $IGNORE = 0; # komentarz zawiera tagi # funkcja do wypisywania tematu po prawej stronie sub printright { my $len = length($_[0]); print " " x (76 - $len); print "$_[0]\n"; } # budowanie tymczasowego skalara zawierajacego caly plik # '\n' -> ' ' while (<>) { chomp; s/\r//g; $buff .= "$_ "; } # usuwanie nadmiarowych spacji $buff =~ s/\s{2,}/ /g; # podzial pliku na linie postaci: # ewentualny tekst $buff =~ s/(.*)/; print $1; $IGNORE = 0; } # linie sa postaci # ewentualny tekst ... if ($line =~ /^<([^>]*)>(.*)/) { $tag = $1; $tekst = $2; # zamiana HTML 'entities' $tekst =~ s/<?//g; $tekst =~ s/&?/&/g; $tekst =~ s/"?/"/g; # poszczegolne tagi: # tytul wydrukuj po prawej if ($tag =~ /^title$/i) { printright $tekst; # nie drukuj zawartosci } elsif ($tag =~ /^style/i) { # dla obrazkow pokazuj alt lub nazwe pliku albo 'image' } elsif ($tag =~ /^img\s+/i) { if ($tag =~ /alt="([^"]*)"/i) { print "[$1]"; } else { if ($tag =~ /href="([^"]*)"/i) { my $adr = $1; $adr =~ m,([^/]+)$,; print "[$1]"; } else { print "[image]"; } } # H1, H2, ... } elsif ($tag =~ m,^/?h\d,i) { print "\n"; print $tekst unless $tekst =~ /^\s*$/; #

} elsif ($tag =~ /^p(\s|$)/i) { print "\n\n"; print $tekst unless $tekst =~ /^\s*$/; #

} elsif ($tag =~ m,^/p$,i) { print "\n"; #
(bierze pod uwage biezace wciecie listy) } elsif ($tag =~ /^br$/i) { print "\n"; print " " x (1 + $ullvl) if $ullvl; print $tekst unless $tekst =~ /^\s*$/; # lista numerowana # tworzy nowy poziom hierarchii } elsif ($tag =~ /^ol/i) { $ullvl++; if ($listtype[$#listtype] eq "*") { push @listtype, "0"; } else { push @listtype, $listtype[$#listtype] . ".0"; } print "$tekst\n" unless $tekst =~ /^\s*$/; # lista nienumerowana # tworzy nowy poziom hierarchii } elsif ($tag =~ /^ul/i) { $ullvl++; push @listtype, "*"; print "$tekst\n" unless $tekst =~ /^\s*$/; # element listy # bierze pod uwage rodzaj bierzacej hierarchii # drukuje "*" albo odpowiedni numer } elsif ($tag =~ /^li(\s|$)/i) { print "\n"; print " " x $ullvl; $tekst =~ s/^\s+//; if ($listtype[$#listtype] ne "*") { my $listtype = pop @listtype; print STDERR "$listtype\n" if $debug; $listtype =~ /(\d+)$/; my $tmp = $1; ++$tmp; $listtype =~ s/\d+$//; $listtype .= $tmp; push @listtype, $listtype; } print "$listtype[$#listtype] $tekst"; # koniec listy numerowanej # usuwa bierzaca hierarchie } elsif ($tag =~ m,^/ol,i) { $ullvl--; pop @listtype; print "\n$tekst"; # koniec listy nienumerowanej # usuwa bierzaca hierarchie } elsif ($tag =~ m,^/ul,i) { $ullvl--; pop @listtype; print "\n$tekst"; # ustawia baze odnosnikow wzglednych } elsif ($tag =~ m,^base,i) { $tag =~ /href="([^"]+)"/i; $hrefbase = $1; print $tekst unless $tekst =~ /^\s*$/; # drukuje numer kolejnego odnosnika # dodaje URL do listy # bierze pod uwage baze przy odnosnikach wzglednych } elsif ($tag =~ m,^a\s,i) { $tag =~ /href="?([^"\s]+)"?/i; if (my $href = $1) { if (($href !~ m,^http://,) && $hrefbase) { $href = $hrefbase . $href; } push @hrefs, $href; print "[$#hrefs]"; } print $tekst unless $tekst =~ /^\s*$/; #
} elsif ($tag =~ m,^hr\b,i) { print "\n\n"; print $tekst unless $tekst =~ /^\s*$/; # komentarz # bierze pod uwage komentarze obejmujace inne tagi (czyli # rozciagajace sie na kilka linii) } elsif ($tag =~ m,^!--,) { if ($tag !~ m,--$,) { if ($tekst !~ m,-->(.*),) { $IGNORE = 1; } else { print "$1;" } } # nieznany tag, drukuje tylko tekst } else { print $tekst unless $tekst =~ /^\s*$/; } # cos jest zle :-( } else { die "Wrong line: $line\n"; } } # Drukuje liste URL-i odnosnikow wystepujacych w tekscie print "\n\nReferences:\n"; for ($x=1; $x <= $#hrefs; ++$x){ print "[$x] $hrefs[$x]\n"; }