| 1 | package Wikibase::Datatype::Print::Utils; | ||||||||||
| 2 | |||||||||||
| 3 | 48 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 48 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 48 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 48 | ▶ | use Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 48 | ▶ | use List::Util 1.33 qw(all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 48 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 48 | ▶ | use Wikibase::Datatype::Print::Texts qw(text_keys texts); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 48 | Readonly::Array our @EXPORT_OK => qw(defaults print_aliases print_common print_descriptions | |||||||||
| 13 | print_forms print_glosses print_labels print_references print_senses | ||||||||||
| 14 | print_sitelinks print_statements); | ||||||||||
| 15 | |||||||||||
| 16 | 48 | our $VERSION = 0.21; | |||||||||
| 17 | |||||||||||
| 18 | 148 | ▶ | sub defaults { | ||||||||
|
Subroutine
defaults: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 19 | 148 | my ($obj, $opts_hr) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 148 | ▶ | if (! defined $opts_hr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 33 | $opts_hr = {}; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 148 | ▶ | if (! exists $opts_hr->{'lang'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 45 | $opts_hr->{'lang'} = 'en'; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 148 | ▶ | if (! exists $opts_hr->{'texts'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 36 | $opts_hr->{'texts'} = texts($opts_hr->{'lang'}); | |||||||||
| 31 | |||||||||||
| 32 | # Check 'texts' keys if are right. | ||||||||||
| 33 | } else { | ||||||||||
| 34 | 112 | ▶ | if (! all { exists $opts_hr->{'texts'}->{$_} } text_keys()) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 35 | 1 | err 'Defined text keys are bad.'; | |||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 147 | return $opts_hr; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 10 | ▶ | sub print_aliases { | ||||||||
|
Subroutine
print_aliases: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 43 | 10 | my ($obj, $opts_hr, $alias_cb) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 10 | ▶ | if ($opts_hr->{'lang'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | return print_common($obj, $opts_hr, 'aliases', $alias_cb, | ||||||||||
| 47 | 41 | ▶ | $opts_hr->{'texts'}->{'aliases'}, sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 41 | grep { $_->language eq $opts_hr->{'lang'} } @_ | |||||||||
| 49 | }, | ||||||||||
| 50 | 8 | ); | |||||||||
| 51 | } else { | ||||||||||
| 52 | return print_common($obj, $opts_hr, 'aliases', $alias_cb, | ||||||||||
| 53 | 2 | $opts_hr->{'texts'}->{'aliases'}); | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 120 | ▶ | sub print_common { | ||||||||
|
Subroutine
print_common: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 58 | 120 | my ($obj, $opts_hr, $list_method, $print_cb, $title, $input_cb, | |||||||||
| 59 | $flag_one_line) = @_; | ||||||||||
| 60 | |||||||||||
| 61 | 120 | my @input; | |||||||||
| 62 | 120 | ▶ | if (defined $input_cb) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 36 | @input = map { $input_cb->($_) } @{$obj->$list_method}; | |||||||||
| 64 | } else { | ||||||||||
| 65 | 84 | @input = @{$obj->$list_method}; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 120 | my @ret; | |||||||||
| 69 | my @values; | ||||||||||
| 70 | 120 | my $separator = ' '; | |||||||||
| 71 | 120 | ▶ | if ($flag_one_line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 22 | $separator = ' '; | |||||||||
| 73 | } | ||||||||||
| 74 | 120 | foreach my $list_item (@input) { | |||||||||
| 75 | 140 | push @values, map { $separator.$_ } $print_cb->($list_item, $opts_hr); | |||||||||
| 76 | } | ||||||||||
| 77 | 120 | ▶ | if (@values) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 85 | ▶ | if ($flag_one_line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 19 | ▶ | if (@values > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 1 | err "Multiple values are printed to one line."; | |||||||||
| 81 | } | ||||||||||
| 82 | 18 | push @ret, $title.':'.$values[0]; | |||||||||
| 83 | } else { | ||||||||||
| 84 | 66 | push @ret, ( | |||||||||
| 85 | $title.':', | ||||||||||
| 86 | @values, | ||||||||||
| 87 | ); | ||||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 119 | return @ret; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 12 | ▶ | sub print_descriptions { | ||||||||
|
Subroutine
print_descriptions: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 12 | my ($obj, $opts_hr, $desc_cb) = @_; | |||||||||
| 96 | |||||||||||
| 97 | 12 | ▶ | if ($opts_hr->{'lang'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | return print_common($obj, $opts_hr, 'descriptions', $desc_cb, | ||||||||||
| 99 | 11 | ▶ | $opts_hr->{'texts'}->{'description'}, sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 11 | grep { $_->language eq $opts_hr->{'lang'} } @_ | |||||||||
| 101 | 10 | }, 1, | |||||||||
| 102 | ); | ||||||||||
| 103 | } else { | ||||||||||
| 104 | return print_common($obj, $opts_hr, 'descriptions', $desc_cb, | ||||||||||
| 105 | 2 | $opts_hr->{'texts'}->{'description'}); | |||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 4 | ▶ | sub print_forms { | ||||||||
|
Subroutine
print_forms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 110 | 4 | my ($obj, $opts_hr, $form_cb) = @_; | |||||||||
| 111 | |||||||||||
| 112 | return print_common($obj, $opts_hr, 'forms', $form_cb, | ||||||||||
| 113 | 4 | $opts_hr->{'texts'}->{'forms'}); | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 6 | ▶ | sub print_glosses { | ||||||||
|
Subroutine
print_glosses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 117 | 6 | my ($obj, $opts_hr, $glosse_cb) = @_; | |||||||||
| 118 | |||||||||||
| 119 | return print_common($obj, $opts_hr, 'glosses', $glosse_cb, | ||||||||||
| 120 | 6 | $opts_hr->{'texts'}->{'glosses'}); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 12 | ▶ | sub print_labels { | ||||||||
|
Subroutine
print_labels: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 124 | 12 | my ($obj, $opts_hr, $label_cb) = @_; | |||||||||
| 125 | |||||||||||
| 126 | 12 | ▶ | if ($opts_hr->{'lang'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | return print_common($obj, $opts_hr, 'labels', $label_cb, | ||||||||||
| 128 | 14 | ▶ | $opts_hr->{'texts'}->{'label'}, sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 129 | 14 | grep { $_->language eq $opts_hr->{'lang'} } @_ | |||||||||
| 130 | 10 | }, 1, | |||||||||
| 131 | ); | ||||||||||
| 132 | } else { | ||||||||||
| 133 | return print_common($obj, $opts_hr, 'labels', $label_cb, | ||||||||||
| 134 | 2 | $opts_hr->{'texts'}->{'label'}); | |||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 37 | ▶ | sub print_references { | ||||||||
|
Subroutine
print_references: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 139 | 37 | my ($obj, $opts_hr, $reference_cb) = @_; | |||||||||
| 140 | |||||||||||
| 141 | return print_common($obj, $opts_hr, 'references', $reference_cb, | ||||||||||
| 142 | 37 | $opts_hr->{'texts'}->{'references'}); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 4 | ▶ | sub print_senses { | ||||||||
|
Subroutine
print_senses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 146 | 4 | my ($obj, $opts_hr, $sense_cb) = @_; | |||||||||
| 147 | |||||||||||
| 148 | return print_common($obj, $opts_hr, 'senses', $sense_cb, | ||||||||||
| 149 | 4 | $opts_hr->{'texts'}->{'senses'}); | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 4 | ▶ | sub print_sitelinks { | ||||||||
|
Subroutine
print_sitelinks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 153 | 4 | my ($obj, $opts_hr, $sitelink_cb) = @_; | |||||||||
| 154 | |||||||||||
| 155 | return print_common($obj, $opts_hr, 'sitelinks', $sitelink_cb, | ||||||||||
| 156 | 4 | ▶ | $opts_hr->{'texts'}->{'sitelinks'}, sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 157 | 4 | my @sitelinks = @_; | |||||||||
| 158 | 4 | my $l = $opts_hr->{'lang'}; | |||||||||
| 159 | 4 | ▶ | if (defined $l) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 3 | return grep { $_->site =~ m/${l}wiki/ms } @sitelinks; | |||||||||
| 161 | } else { | ||||||||||
| 162 | 1 | return @sitelinks; | |||||||||
| 163 | } | ||||||||||
| 164 | 4 | }); | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 24 | ▶ | sub print_statements { | ||||||||
|
Subroutine
print_statements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 168 | 24 | my ($obj, $opts_hr, $statement_cb) = @_; | |||||||||
| 169 | |||||||||||
| 170 | return print_common($obj, $opts_hr, 'statements', $statement_cb, | ||||||||||
| 171 | 24 | $opts_hr->{'texts'}->{'statements'}); | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 48 | 1; | |||||||||
| 175 | |||||||||||
| 176 | __END__ | ||||||||||