| 1 | package Treex::Block::Write::BaseTextWriter; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 4 | $Treex::Block::Write::BaseTextWriter::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Encode 'decode'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | extends 'Treex::Block::Write::BaseWriter'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | has encoding => ( | |||||||||||||||||||||||||||
| 13 | isa => 'Str', | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | default => 'utf8', | ||||||||||||||||||||||||||||
| 16 | documentation => 'Output encoding. \'utf8\' by default.', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | has '+to' => ( | |||||||||||||||||||||||||||
| 20 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 21 | builder => '_build_to', | ||||||||||||||||||||||||||||
| 22 | lazy_build => 1 | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | has '+compress' => ( | |||||||||||||||||||||||||||
| 26 | default => 0 | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Set the right text encoding when opening a handle. | ||||||||||||||||||||||||||||
| 30 | around '_open_file_handle' => sub { | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my ( $orig, $self, $filename ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # actually open the file handle | ||||||||||||||||||||||||||||
| 35 | 1 | my $handle = $self->$orig($filename); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # set the right encoding | ||||||||||||||||||||||||||||
| 38 | 1 | binmode( $handle, ':' . $self->encoding ); | |||||||||||||||||||||||||||
| 39 | 1 | return $handle; | |||||||||||||||||||||||||||
| 40 | 4 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Default to standard output if no output file is set. | ||||||||||||||||||||||||||||
| 43 | sub _build_to { | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_to: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if ( !defined( $self->path ) && !defined( $self->file_stem ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->path or defined $self->file_stem
MC/DC0%0/2
defined $self->path or defined $self->file_stem
defined $self->path
defined $self->file_stem
Truth table33%1/3
A or B
A= defined $self->pathB= defined $self->file_stem
| |||||||||||||||||||||||||||||
| 48 | 1 | return '-'; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Append everything to one file if the 'to' parameter is set just to one file. | ||||||||||||||||||||||||||||
| 54 | override '_get_next_filename' => sub { | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | return $self->to if ( $self->to !~ m/[ ,]/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | return super(); | |||||||||||||||||||||||||||
| 60 | 4 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Default process_document method for all Writer blocks. | ||||||||||||||||||||||||||||
| 63 | override 'process_document' => sub { | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my ( $self, $document ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # set _file_handle properly (this MUST be called if process_document is overridden) | ||||||||||||||||||||||||||||
| 68 | 0 | $self->_prepare_file_handle($document); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # allow header printing (in overrides) | ||||||||||||||||||||||||||||
| 71 | 0 | $self->print_header($document); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # call the original process_document with _file_handle set | ||||||||||||||||||||||||||||
| 74 | 0 | $self->Treex::Core::Block::process_document($document); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # allow footer printing (in overrides) | ||||||||||||||||||||||||||||
| 77 | 0 | $self->print_footer($document); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||||||||||
| 80 | 4 | }; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub print_header { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ( $self, $document ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
print_header: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub print_footer { | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my ( $self, $document ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
print_footer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 4 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 NAME | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Treex::Block::Write::BaseTextWriter | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 VERSION | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | This is a base class for all text-based output formats, which adds printing to standard output | ||||||||||||||||||||||||||||
| 109 | by default and the possibility to select the output file character encoding (defaulting to | ||||||||||||||||||||||||||||
| 110 | UTF-8). | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Also, if multiple documents are read and only one output file given in the C<to> parameter, | ||||||||||||||||||||||||||||
| 113 | all input documents will be appended to a single file. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =over | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item C<encoding> | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | The output encoding, C<utf8> by default. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =back | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 DERIVED CLASSES | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Before creating a class derived from C<BaseTextWriter>, please see the instructions in | ||||||||||||||||||||||||||||
| 128 | L<Treex::Block::Write::BaseWriter>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | There is a possibility to override the following two methods in addition to those described in | ||||||||||||||||||||||||||||
| 131 | L<Treex::Block::Write::BaseWriter>: | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =over | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item C<<$self->print_header($document)>> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Print a document header. Called before the standard C<process_document> is launched. Will not be | ||||||||||||||||||||||||||||
| 138 | called if C<process_document> is overridden (and C<super> not called)! | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item C<<$self->print_footer($document)>> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Print a document footer. Called after the standard C<process_document> is launched. Will not be | ||||||||||||||||||||||||||||
| 143 | called if C<process_document> is overridden (and C<super> not called)! | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Ondřej Dušek <odusek@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||