| 1 | package Shebangml::FromHTML; | ||
| 2 | $VERSION = v0.0.1; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use Class::Accessor::Classy; | ||
| 9 | ro 'parser'; | ||
| 10 | lo 'output'; | ||
| 11 | no Class::Accessor::Classy; | ||
| 12 | |||
| 13 | use constant DEBUG => 0; | ||
| 14 | |||
| 15 | use HTML::Parser; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Shebangml::FromHTML - HBML via HTML::Parser | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | |||
| 26 | =head2 new | ||
| 27 | |||
| 28 | my $parser = Shebangml::FromHTML->new; | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $package = shift; | ||
| 34 | my $class = ref($package) || $package; | ||
| 35 | my $self = {parser => HTML::Parser->new}; | ||
| 36 | bless($self, $class); | ||
| 37 | $self->_setup; | ||
| 38 | return($self); | ||
| 39 | } # end subroutine new definition | ||
| 40 | ######################################################################## | ||
| 41 | |||
| 42 | =head2 _setup | ||
| 43 | |||
| 44 | $self->_setup; | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub _setup { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | my @output; | ||
| 52 | $self->{output} = \@output; | ||
| 53 | my @ctx = ({root => 1, had_content => 1}); | ||
| 54 | |||
| 55 | my %def_empty = map({$_ => 1} qw( | ||
| 56 | img meta br link input | ||
| 57 | )); | ||
| 58 | my %shortcuts = ( | ||
| 59 | br => '\n;', | ||
| 60 | ); | ||
| 61 | my $start = sub { | ||
| 62 | my ($el) = @_; | ||
| 63 | |||
| 64 | my $short; | ||
| 65 | $el = $short if($short = $shortcuts{$el}); | ||
| 66 | unless($ctx[-1]->{had_content}) { | ||
| 67 | $ctx[-1]->{had_content} = 1; | ||
| 68 | push(@output, '{', $el); | ||
| 69 | return($short); | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | if($short) { | ||
| 73 | push(@output, $short); | ||
| 74 | return($short); | ||
| 75 | } | ||
| 76 | push(@output, | ||
| 77 | (@output && $output[-1] =~ m/[\.\w]$/ ? '\\' : ''), | ||
| 78 | $el); | ||
| 79 | return(); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | my $out = sub { | ||
| 83 | push(@output, @_); | ||
| 84 | }; | ||
| 85 | my $eh; | ||
| 86 | my $sh = sub { | ||
| 87 | my ($p, $el, $original_string, @atts) = @_; | ||
| 88 | |||
| 89 | DEBUG and warn "start handler $el ($original_string)"; | ||
| 90 | |||
| 91 | $start->($el) and return; | ||
| 92 | |||
| 93 | push(@ctx, my $e = { }); | ||
| 94 | if(@atts) { | ||
| 95 | @atts = $self->_reduce_atts(@atts); | ||
| 96 | |||
| 97 | # TODO sorting, quoting, and such | ||
| 98 | my $att = join(' ', @atts); | ||
| 99 | $out->("[$att]"); | ||
| 100 | $e->{had_atts} = 1; | ||
| 101 | } | ||
| 102 | elsif($original_string =~ m#/>$#) { | ||
| 103 | # do I even need to tag it as empty? | ||
| 104 | $e->{had_atts} = 2; | ||
| 105 | $out->('[]'); | ||
| 106 | } | ||
| 107 | |||
| 108 | if($def_empty{$el}) { | ||
| 109 | unless($e->{had_atts}) { | ||
| 110 | $out->('[]'); | ||
| 111 | $e->{had_atts} = 3; | ||
| 112 | } | ||
| 113 | $eh->($p, $el) unless($original_string =~ m#/>$#); | ||
| 114 | } | ||
| 115 | |||
| 116 | }; | ||
| 117 | $eh = sub { | ||
| 118 | my ($p, $el) = @_; | ||
| 119 | $shortcuts{$el} and return; # never closing shortcuts | ||
| 120 | DEBUG and warn "end handler $el";# at ", $p->current_byte; | ||
| 121 | my $e = pop(@ctx); | ||
| 122 | if($e->{had_content}) { | ||
| 123 | $out->('}'); | ||
| 124 | } | ||
| 125 | elsif(not $e->{had_atts}) { | ||
| 126 | # we still have to make it an entity | ||
| 127 | $out->('{}'); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | # XXX may need to output [] or {} to indicate br-like tag? | ||
| 131 | } | ||
| 132 | }; | ||
| 133 | my $ch = sub { | ||
| 134 | my ($p, $s) = @_; | ||
| 135 | DEBUG and warn "text ", length($s), "\n"; | ||
| 136 | |||
| 137 | my $e = $ctx[-1]; | ||
| 138 | $out->('{') unless($e->{had_content}); | ||
| 139 | $e->{had_content} = 1; | ||
| 140 | |||
| 141 | # escaping the string... | ||
| 142 | # TODO smarter [] and {} matched handling | ||
| 143 | $s =~ s/([\{\}\[\]])/\\$1/g; | ||
| 144 | $s =~ s/\\/\\\\/g; | ||
| 145 | # TODO < and such need to transform too | ||
| 146 | my %translate = ( | ||
| 147 | amp => '&', | ||
| 148 | 'lt' => '<', | ||
| 149 | 'gt' => '>', | ||
| 150 | nbsp => '\_;', | ||
| 151 | ); | ||
| 152 | my $re = sub { | ||
| 153 | my $code = shift; | ||
| 154 | my $out = $translate{$code}; | ||
| 155 | return($out || "\\$code;"); | ||
| 156 | }; | ||
| 157 | my $match = join('|', keys %translate, '\w+', '#[0-9]+'); | ||
| 158 | $s =~ s/&($match);/$re->($1)/eg; | ||
| 159 | |||
| 160 | $out->($s); | ||
| 161 | }; | ||
| 162 | my $dt = sub { | ||
| 163 | DEBUG and warn "doctype @_"; | ||
| 164 | }; | ||
| 165 | my $parser = $self->parser; | ||
| 166 | $parser->handler(start => $sh, 'self, tagname, text, @attr'); | ||
| 167 | $parser->handler(end => $eh, 'self, tagname'); | ||
| 168 | $parser->handler(text => $ch, 'self, text'); | ||
| 169 | $parser->empty_element_tags(1); | ||
| 170 | |||
| 171 | #Doctype => $dt, | ||
| 172 | #Default => sub { | ||
| 173 | # DEBUG and warn 'hit default: ', | ||
| 174 | # "@_ (", $output[-1] ? $output[-1] : (), ") ", | ||
| 175 | # "line ", $parser->current_line, | ||
| 176 | # ", column ", $parser->current_column, | ||
| 177 | # ", byte ", $parser->current_byte; | ||
| 178 | #}, | ||
| 179 | # ExternEnt => sub {warn "an externEnt?!"}, | ||
| 180 | # Entity => sub {warn "an Entity?! @_"}, | ||
| 181 | } # end subroutine _setup definition | ||
| 182 | ######################################################################## | ||
| 183 | |||
| 184 | =head2 parse | ||
| 185 | |||
| 186 | $parser->parse($source); | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub parse { | ||
| 191 | my $self = shift; | ||
| 192 | my ($source) = @_; | ||
| 193 | $self->parser->parse_file($source); | ||
| 194 | } # end subroutine parse definition | ||
| 195 | ######################################################################## | ||
| 196 | |||
| 197 | =head2 _reduce_atts | ||
| 198 | |||
| 199 | my @atts = $self->_reduce_atts(@atts); | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | { | ||
| 204 | my %shortcut = (name => ':', id => '=', class => '@'); | ||
| 205 | sub _reduce_atts { | ||
| 206 | my $self = shift; | ||
| 207 | my (@atts) = @_; | ||
| 208 | |||
| 209 | # yank the id, name, class to the front -- with shortcuts | ||
| 210 | my %num = map({my $n = $_*2; ($atts[$n] => $n)} 0..($#atts/2)); | ||
| 211 | my @cuts; | ||
| 212 | my @out; | ||
| 213 | foreach my $k (sort keys(%shortcut)) { | ||
| 214 | if(exists($num{$k})) { | ||
| 215 | push(@cuts, $num{$k}); | ||
| 216 | my $att = $atts[$num{$k}+1]; | ||
| 217 | if($att =~ m/^\w+$/) { | ||
| 218 | push(@out, $shortcut{$k} . $att); | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | push(@out, $k . qq(="$att")); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | foreach my $n (sort({$b <=> $a} @cuts)) { | ||
| 226 | splice(@atts, $n, 2); | ||
| 227 | } | ||
| 228 | return(@out, | ||
| 229 | @atts ? | ||
| 230 | map({my $n = $_*2; $atts[$n].'="'.$atts[$n+1].'"'} 0..int($#atts/2)) | ||
| 231 | : () | ||
| 232 | ); | ||
| 233 | }} # end subroutine _reduce_atts definition | ||
| 234 | ######################################################################## | ||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||
| 241 | |||
| 242 | http://scratchcomputing.com/ | ||
| 243 | |||
| 244 | =head1 BUGS | ||
| 245 | |||
| 246 | If you found this module on CPAN, please report any bugs or feature | ||
| 247 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||
| 248 | notified, and then you'll automatically be notified of progress on your | ||
| 249 | bug as I make changes. | ||
| 250 | |||
| 251 | If you pulled this development version from my /svn/, please contact me | ||
| 252 | directly. | ||
| 253 | |||
| 254 | =head1 COPYRIGHT | ||
| 255 | |||
| 256 | Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved. | ||
| 257 | |||
| 258 | =head1 NO WARRANTY | ||
| 259 | |||
| 260 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||
| 261 | offered with this software. You use this software at your own risk. In | ||
| 262 | case of loss, no person or entity owes you anything whatsoever. You | ||
| 263 | have been warned. | ||
| 264 | |||
| 265 | =head1 LICENSE | ||
| 266 | |||
| 267 | This program is free software; you can redistribute it and/or modify it | ||
| 268 | under the same terms as Perl itself. | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | # vi:ts=2:sw=2:et:sta | ||
| 273 | 1; |