| 1 | # This code is part of Perl distribution HTML-FromMail version 4.00. | ||
| 2 | # The POD got stripped from this file by OODoc version 3.05. | ||
| 3 | # For contributors see file ChangeLog. | ||
| 4 | |||
| 5 | # This software is copyright (c) 2003-2025 by Mark Overmeer. | ||
| 6 | |||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||
| 10 | |||
| 11 | |||
| 12 | package HTML::FromMail::Format::Magic;{ | ||
| 13 | our $VERSION = '4.00'; | ||
| 14 | } | ||
| 15 | |||
| 16 | use base 'HTML::FromMail::Format'; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use Log::Report 'html-frommail'; | ||
| 22 | |||
| 23 | BEGIN | ||
| 24 | { eval { require Template::Magic }; | ||
| 25 | $@ and error __x"install Template::Magic for this formatter."; | ||
| 26 | } | ||
| 27 | |||
| 28 | #-------------------- | ||
| 29 | |||
| 30 | #----------- | ||
| 31 | |||
| 32 | sub magic() { $_[0]->{HFFM_magic} } | ||
| 33 | |||
| 34 | #----------- | ||
| 35 | |||
| 36 | sub export($@) | ||
| 37 | { my ($self, $message, %args) = @_; | ||
| 38 | |||
| 39 | my $magic = $self->{HFFM_magic} = Template::Magic->new( | ||
| 40 | markers => 'HTML', | ||
| 41 | zone_handlers => sub { $self->lookupTemplate(\%args, @_) }, | ||
| 42 | ); | ||
| 43 | |||
| 44 | open my($out), ">", $args{output} | ||
| 45 | or fault __x"cannot write to {out}", 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 | sub lookupTemplate($$) | ||
| 57 | { my ($self, $args, $zone) = @_; | ||
| 58 | |||
| 59 | # Lookup the method to be called. | ||
| 60 | my $method = 'html' . ucfirst($zone->id); | ||
| 61 | my $prod = $args->{producer} or panic; | ||
| 62 | $prod->can($method) or return undef; | ||
| 63 | |||
| 64 | # Split zone attributes into hash. Added to %$args. | ||
| 65 | my $param = $zone->attributes || ''; | ||
| 66 | $param =~ s/^\s+//; | ||
| 67 | $param =~ s/\s+$//; | ||
| 68 | |||
| 69 | my %args = (%$args, zone => $zone); | ||
| 70 | if(length $param) | ||
| 71 | { foreach my $pair (split /\s*\,\s*/, $param) | ||
| 72 | { my ($k, $v) = split /\s*\=\>\s*/, $pair, 2; | ||
| 73 | $args{$k} = $v; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | my $value = $prod->$method($args{object}, \%args); | ||
| 78 | $zone->value = $value if defined $value; | ||
| 79 | } | ||
| 80 | |||
| 81 | our $msg_zone; # hack | ||
| 82 | sub containerText($) | ||
| 83 | { my ($self, $args) = @_; | ||
| 84 | my $zone = $args->{zone} or panic; | ||
| 85 | $msg_zone = $zone if $zone->id eq 'message'; # hack | ||
| 86 | $zone->content; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub processText($$) | ||
| 90 | { my ($self, $text, $args) = @_; | ||
| 91 | my $zone = $args->{zone} or panic; | ||
| 92 | |||
| 93 | # this hack is needed to get things to work :( | ||
| 94 | # but this will not work in the future. | ||
| 95 | $zone->_s = $msg_zone->_s; | ||
| 96 | $zone->_e = $msg_zone->_e; | ||
| 97 | $zone->merge; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub lookup($$) | ||
| 101 | { my ($self, $what, $args) = @_; | ||
| 102 | my $zone = $args->{zone} or panic; | ||
| 103 | $zone->lookup($what); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub onFinalToken($) | ||
| 107 | { my ($self, $args) = @_; | ||
| 108 | my $zone = $args->{zone} or panic; | ||
| 109 | ! defined $zone->content; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |