| 1 | # Copyrights 2003-2018 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution HTML-FromMail. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package HTML::FromMail::Format::Magic; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '0.12'; | ||
| 12 | |||
| 13 | use base 'HTML::FromMail::Format'; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | |||
| 20 | BEGIN | ||
| 21 | { eval { require Template::Magic }; | ||
| 22 | die "Install Bundle::Template::Magic for this formatter\n" | ||
| 23 | if $@; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sub init($) | ||
| 28 | { my ($self, $args) = @_; | ||
| 29 | |||
| 30 | $self->SUPER::init($args) or return; | ||
| 31 | |||
| 32 | $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub export($@) | ||
| 36 | { my ($self, %args) = @_; | ||
| 37 | |||
| 38 | my $magic = $self->{HFFM_magic} | ||
| 39 | = Template::Magic->new | ||
| 40 | ( markers => 'HTML' | ||
| 41 | , zone_handlers => sub { $self->lookupTemplate(\%args, @_) } | ||
| 42 | ); | ||
| 43 | |||
| 44 | $self->log(ERROR => "Cannot write to $args{output}: $!"), return | ||
| 45 | unless open my($out), ">", $args{output}; | ||
| 46 | |||
| 47 | my $oldout = select $out; | ||
| 48 | $magic->print($args{input}); | ||
| 49 | select $oldout; | ||
| 50 | |||
| 51 | close $out; | ||
| 52 | $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | sub magic() { shift->{HFFM_magic} } | ||
| 58 | |||
| 59 | |||
| 60 | sub lookupTemplate($$) | ||
| 61 | { my ($self, $args, $zone) = @_; | ||
| 62 | |||
| 63 | # Lookup the method to be called. | ||
| 64 | my $method = 'html' . ucfirst($zone->id); | ||
| 65 | my $prod = $args->{producer}; | ||
| 66 | return undef unless $prod->can($method); | ||
| 67 | |||
| 68 | # Split zone attributes into hash. Added to %$args. | ||
| 69 | my $param = $zone->attributes || ''; | ||
| 70 | $param =~ s/^\s+//; | ||
| 71 | $param =~ s/\s+$//; | ||
| 72 | |||
| 73 | my %args = (%$args, zone => $zone); | ||
| 74 | if(length $param) | ||
| 75 | { foreach my $pair (split /\s*\,\s*/, $param) | ||
| 76 | { my ($k, $v) = split /\s*\=\>\s*/, $pair, 2; | ||
| 77 | $args{$k} = $v; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | my $value = $prod->$method($args{object}, \%args); | ||
| 82 | $zone->value = $value if defined $value; | ||
| 83 | } | ||
| 84 | |||
| 85 | our $msg_zone; # hack | ||
| 86 | sub containerText($) | ||
| 87 | { my ($self, $args) = @_; | ||
| 88 | my $zone = $args->{zone}; | ||
| 89 | $msg_zone = $zone if $zone->id eq 'message'; # hack | ||
| 90 | $zone->content; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub processText($$) | ||
| 94 | { my ($self, $text, $args) = @_; | ||
| 95 | my $zone = $args->{zone}; | ||
| 96 | |||
| 97 | # this hack is needed to get things to work :( | ||
| 98 | # but this will not work in the future. | ||
| 99 | $zone->_s = $msg_zone->_s; | ||
| 100 | $zone->_e = $msg_zone->_e; | ||
| 101 | $zone->merge; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub lookup($$) | ||
| 105 | { my ($self, $what, $args) = @_; | ||
| 106 | my $zone = $args->{zone} or confess; | ||
| 107 | $zone->lookup($what); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub onFinalToken($) | ||
| 111 | { my ($self, $args) = @_; | ||
| 112 | my $zone = $args->{zone} or confess; | ||
| 113 | ! defined $zone->content; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |