| 1 | # WebFetch::Output::TT | ||||||||||
| 2 | # ABSTRACT: save data from WebFetch via the Perl Template Toolkit | ||||||||||
| 3 | # | ||||||||||
| 4 | # Copyright (c) 1998-2022 Ian Kluft. This program is free software; you can | ||||||||||
| 5 | # redistribute it and/or modify it under the terms of the GNU General Public | ||||||||||
| 6 | # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html | ||||||||||
| 7 | |||||||||||
| 8 | # pragmas to silence some warnings from Perl::Critic | ||||||||||
| 9 | ## no critic (Modules::RequireExplicitPackage) | ||||||||||
| 10 | # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first | ||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | ## use critic (Modules::RequireExplicitPackage) | ||||||||||
| 15 | |||||||||||
| 16 | package WebFetch::Output::TT; | ||||||||||
| 17 | 1 | $WebFetch::Output::TT::VERSION = '0.1.0'; | |||||||||
| 18 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use base "WebFetch"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use Template; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | # define exceptions/errors | ||||||||||
| 25 | 1 | ▶ | use Exception::Class ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | "WebFetch::Output::TT::Exception::Template" => { | |||||||||
| 27 | isa => "WebFetch::TracedException", | ||||||||||
| 28 | alias => "throw_template", | ||||||||||
| 29 | description => "error during template processing", | ||||||||||
| 30 | }, | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ); | |||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | # set defaults | ||||||||||
| 36 | |||||||||||
| 37 | 1 | our @Options = ( "template=s", "tt_include:s" ); | |||||||||
| 38 | 1 | our $Usage = "--template template-file [--tt_include include-path]"; | |||||||||
| 39 | |||||||||||
| 40 | # no user-servicable parts beyond this point | ||||||||||
| 41 | |||||||||||
| 42 | # register capabilities with WebFetch | ||||||||||
| 43 | 1 | __PACKAGE__->module_register( "cmdline", "output:tt" ); | |||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | # Perl Template Toolkit format handler | ||||||||||
| 47 | sub fmt_handler_tt | ||||||||||
| 48 | 0 | ▶ | { | ||||||||
|
Subroutine
fmt_handler_tt: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 49 | 0 | my $self = shift; | |||||||||
| 50 | 0 | my $filename = shift; | |||||||||
| 51 | 0 | my $output; | |||||||||
| 52 | |||||||||||
| 53 | # configure and create template object | ||||||||||
| 54 | 0 | my %tt_config = ( | |||||||||
| 55 | ABSOLUTE => 1, | ||||||||||
| 56 | RELATIVE => 1, | ||||||||||
| 57 | ); | ||||||||||
| 58 | 0 | ▶ | if ( exists $self->{tt_include}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | $tt_config{INCLUDE_PATH} = $self->{tt_include} | ||||||||||
| 60 | 0 | } | |||||||||
| 61 | 0 | my $template = Template->new( \%tt_config ); | |||||||||
| 62 | |||||||||||
| 63 | # process template | ||||||||||
| 64 | $template->process( $self->{template}, { data => $self->{data}}, | ||||||||||
| 65 | 0 | ▶ | \$output, { binmode => ':utf8'} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | or throw_template $template->error(); | ||||||||||
| 67 | |||||||||||
| 68 | 0 | $self->raw_savable( $filename, $output ); | |||||||||
| 69 | 0 | return 1; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | 1; | |||||||||
| 73 | |||||||||||
| 74 | =pod | ||||||||||
| 75 | |||||||||||
| 76 | =encoding UTF-8 | ||||||||||
| 77 | |||||||||||
| 78 | =head1 NAME | ||||||||||
| 79 | |||||||||||
| 80 | WebFetch::Output::TT - save data from WebFetch via the Perl Template Toolkit | ||||||||||
| 81 | |||||||||||
| 82 | =head1 VERSION | ||||||||||
| 83 | |||||||||||
| 84 | version 0.1.0 | ||||||||||
| 85 | |||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||
| 87 | |||||||||||
| 88 | In perl scripts: | ||||||||||
| 89 | |||||||||||
| 90 | C<use WebFetch::Output::TT;> | ||||||||||
| 91 | |||||||||||
| 92 | From the command line: | ||||||||||
| 93 | |||||||||||
| 94 | C<perl -w -MWebFetch::Output::TT -e "&fetch_main" -- | ||||||||||
| 95 | [...WebFetch input options...] --dir directory | ||||||||||
| 96 | --dest_format tt --dest dest-path --template tt-file > | ||||||||||
| 97 | |||||||||||
| 98 | =head1 DESCRIPTION | ||||||||||
| 99 | |||||||||||
| 100 | This module saves output via the Perl Template Toolkit. | ||||||||||
| 101 | |||||||||||
| 102 | =over 4 | ||||||||||
| 103 | |||||||||||
| 104 | =item $obj->fmt_handler_tt( $filename ) | ||||||||||
| 105 | |||||||||||
| 106 | This function formats the data according to the Perl Template Toolkit | ||||||||||
| 107 | template provided in the --template parameter. | ||||||||||
| 108 | |||||||||||
| 109 | =back | ||||||||||
| 110 | |||||||||||
| 111 | =head1 SEE ALSO | ||||||||||
| 112 | |||||||||||
| 113 | L<WebFetch> | ||||||||||
| 114 | L<https://github.com/ikluft/WebFetch> | ||||||||||
| 115 | |||||||||||
| 116 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 117 | |||||||||||
| 118 | Please report bugs via GitHub at L<https://github.com/ikluft/WebFetch/issues> | ||||||||||
| 119 | |||||||||||
| 120 | Patches and enhancements may be submitted via a pull request at L<https://github.com/ikluft/WebFetch/pulls> | ||||||||||
| 121 | |||||||||||
| 122 | =head1 AUTHOR | ||||||||||
| 123 | |||||||||||
| 124 | Ian Kluft <https://github.com/ikluft> | ||||||||||
| 125 | |||||||||||
| 126 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 127 | |||||||||||
| 128 | This software is Copyright (c) 1998-2022 by Ian Kluft. | ||||||||||
| 129 | |||||||||||
| 130 | This is free software, licensed under: | ||||||||||
| 131 | |||||||||||
| 132 | The GNU General Public License, Version 3, June 2007 | ||||||||||
| 133 | |||||||||||
| 134 | =cut | ||||||||||
| 135 | |||||||||||
| 136 | __END__ | ||||||||||