1package Biber::SortList;
2use v5.16;
3use strict;
4use warnings;
5
6use Biber::Utils;
7use Biber::Constants;
8use List::Util qw( first );
9
10=encoding utf-8
11
12=head1 NAME
13
14Biber::SortList
15
16=head2 new
17
18    Initialize a Biber::SortList object
19
20=cut
21
22sub new {
23  my ($class, %params) = @_;
24  my $self = bless {%params}, $class;
25  return $self;
26}
27
28
29=head2 set_section
30
31    Sets the section of a sort list
32
33=cut
34
35sub set_section {
36  my $self = shift;
37  my $section = shift;
38  $self->{section} = lc($section);
39  return;
40}
41
42=head2 get_section
43
44    Gets the section of a sort list
45
46=cut
47
48sub get_section {
49  my $self = shift;
50  return $self->{section};
51}
52
53
54=head2 set_sortschemename
55
56    Sets the sortscheme name of a sort list
57
58=cut
59
60sub set_sortschemename {
61  my $self = shift;
62  my $ssn = shift;
63  $self->{sortschemename} = lc($ssn);
64  return;
65}
66
67=head2 get_sortschemename
68
69    Gets the sortschemename of a sort list
70
71=cut
72
73sub get_sortschemename {
74  my $self = shift;
75  return $self->{sortschemename};
76}
77
78=head2 set_name
79
80    Sets the name of a sort list
81
82=cut
83
84sub set_name {
85  my $self = shift;
86  my $name = shift;
87  $self->{name} = lc($name);
88  return;
89}
90
91=head2 get_name
92
93    Gets the name of a sort list
94
95=cut
96
97sub get_name {
98  my $self = shift;
99  return $self->{name};
100}
101
102
103=head2 set_type
104
105    Sets the type of a sort list
106
107=cut
108
109sub set_type {
110  my $self = shift;
111  my $type = shift;
112  $self->{type} = lc($type);
113  return;
114}
115
116=head2 get_type
117
118    Gets the type of a section list
119
120=cut
121
122sub get_type {
123  my $self = shift;
124  return $self->{type};
125}
126
127=head2 set_keys
128
129    Sets the keys for the list
130
131=cut
132
133sub set_keys {
134  my ($self, $keys) = @_;
135  $self->{keys} = $keys;
136  return;
137}
138
139=head2 get_keys
140
141    Gets the keys for the list
142
143=cut
144
145sub get_keys {
146  my $self = shift;
147  return @{$self->{keys}};
148}
149
150=head2 count_keys
151
152    Count the keys for the list
153
154=cut
155
156sub count_keys {
157  my $self = shift;
158  return $#{$self->{keys}} + 1;
159}
160
161
162=head2 get_listdata
163
164    Gets all of the list metadata
165
166=cut
167
168sub get_listdata {
169  my $self = shift;
170  return [ $self->{sortscheme},
171           $self->{keys},
172           $self->{sortinitdata},
173           $self->{extrayeardata},
174           $self->{extraalphadata} ];
175}
176
177=head2 set_extrayeardata_for_key
178
179  Saves extrayear field data for a key
180
181=cut
182
183sub set_extrayeardata_for_key {
184  my ($self, $key, $ed) = @_;
185  return unless defined($key);
186  $self->{extrayeardata}{$key} = $ed;
187  return;
188}
189
190=head2 set_extrayeardata
191
192    Saves extrayear field data for all keys
193
194=cut
195
196sub set_extrayeardata {
197  my ($self, $ed) = @_;
198  $self->{extrayeardata} = $ed;
199  return;
200}
201
202
203=head2 get_extrayeardata
204
205    Gets the extrayear field data for a key
206
207=cut
208
209sub get_extrayeardata {
210  my ($self, $key) = @_;
211  return unless defined($key);
212  return $self->{extrayeardata}{$key};
213}
214
215=head2 set_extratitledata_for_key
216
217  Saves extratitle field data for a key
218
219=cut
220
221sub set_extratitledata_for_key {
222  my ($self, $key, $ed) = @_;
223  return unless defined($key);
224  $self->{extratitledata}{$key} = $ed;
225  return;
226}
227
228=head2 set_extratitledata
229
230    Saves extratitle field data for all keys
231
232=cut
233
234sub set_extratitledata {
235  my ($self, $ed) = @_;
236  $self->{extratitledata} = $ed;
237  return;
238}
239
240
241=head2 get_extratitledata
242
243    Gets the extratitle field data for a key
244
245=cut
246
247sub get_extratitledata {
248  my ($self, $key) = @_;
249  return unless defined($key);
250  return $self->{extratitledata}{$key};
251}
252
253
254=head2 set_extratitleyeardata_for_key
255
256  Saves extratitleyear field data for a key
257
258=cut
259
260sub set_extratitleyeardata_for_key {
261  my ($self, $key, $ed) = @_;
262  return unless defined($key);
263  $self->{extratitleyeardata}{$key} = $ed;
264  return;
265}
266
267=head2 set_extratitleyeardata
268
269    Saves extratitleyear field data for all keys
270
271=cut
272
273sub set_extratitleyeardata {
274  my ($self, $ed) = @_;
275  $self->{extratitleyeardata} = $ed;
276  return;
277}
278
279
280=head2 get_extratitleyeardata
281
282    Gets the extratitleyear field data for a key
283
284=cut
285
286sub get_extratitleyeardata {
287  my ($self, $key) = @_;
288  return unless defined($key);
289  return $self->{extratitleyeardata}{$key};
290}
291
292
293=head2 set_extraalphadata_for_key
294
295    Saves extraalpha field data for a key
296
297=cut
298
299sub set_extraalphadata_for_key {
300  my ($self, $key, $ed) = @_;
301  return unless defined($key);
302  $self->{extraalphadata}{$key} = $ed;
303  return;
304}
305
306=head2 set_extraalphadata
307
308    Saves extraalpha field data for all keys
309
310=cut
311
312sub set_extraalphadata {
313  my ($self, $ed) = @_;
314  $self->{extraalphadata} = $ed;
315  return;
316}
317
318=head2 get_extraalphadata
319
320    Gets the extraalpha field data for a key
321
322=cut
323
324sub get_extraalphadata {
325  my ($self, $key) = @_;
326  return unless defined($key);
327  return $self->{extraalphadata}{$key};
328}
329
330=head2 set_sortdata
331
332    Saves sorting data in a list for a key
333
334=cut
335
336sub set_sortdata {
337  my ($self, $key, $sd) = @_;
338  return unless defined($key);
339  $self->{sortdata}{$key} = $sd;
340  return;
341}
342
343=head2 get_sortdata
344
345    Gets the sorting data in a list for a key
346
347=cut
348
349sub get_sortdata {
350  my ($self, $key) = @_;
351  return unless defined($key);
352  return $self->{sortdata}{$key};
353}
354
355
356=head2 set_sortinitdata_for_key
357
358 Saves sortinit data for a specific key
359
360=cut
361
362sub set_sortinitdata_for_key {
363  my ($self, $key, $init, $inithash) = @_;
364  return unless defined($key);
365  $self->{sortinitdata}{$key} = {init     => $init,
366                                 inithash => $inithash};
367  return;
368}
369
370=head2 set_sortinitdata
371
372 Saves sortinit data for all keys
373
374=cut
375
376sub set_sortinitdata {
377  my ($self, $sid) = @_;
378  $self->{sortinitdata} = $sid;
379  return;
380}
381
382
383=head2 get_sortinit_for_key
384
385    Gets the sortinit in a list for a key
386
387=cut
388
389sub get_sortinit_for_key {
390  my ($self, $key) = @_;
391  return unless defined($key);
392  return $self->{sortinitdata}{$key}{init};
393}
394
395=head2 get_sortinithash_for_key
396
397    Gets the sortinit hash in a list for a key
398
399=cut
400
401sub get_sortinithash_for_key {
402  my ($self, $key) = @_;
403  return unless defined($key);
404  return $self->{sortinitdata}{$key}{inithash};
405}
406
407=head2 set_sortscheme
408
409    Sets the sortscheme of a list
410
411=cut
412
413sub set_sortscheme {
414  my $self = shift;
415  my $sortscheme = shift;
416  $self->{sortscheme} = $sortscheme;
417  return;
418}
419
420=head2 get_sortscheme
421
422    Gets the sortscheme of a list
423
424=cut
425
426sub get_sortscheme {
427  my $self = shift;
428  return $self->{sortscheme};
429}
430
431
432=head2 add_filter
433
434    Adds a filter to a list object
435
436=cut
437
438sub add_filter {
439  my $self = shift;
440  my ($type, $values) = @_;
441  # Disjunctive filters are not simple values
442  if ($type eq 'orfilter') {
443    $self->{filters}{$type} = $values;
444  }
445  else {
446    $self->{filters}{$type} = [ split(/\s*,\s*/,$values) ];
447  }
448  return;
449}
450
451=head2 get_filter
452
453    Gets a specific filter from a list object
454
455=cut
456
457sub get_filter {
458  my $self = shift;
459  my $type = shift;
460  return $self->{filters}{$type};
461}
462
463=head2 get_filters
464
465    Gets all filters for a list object
466
467=cut
468
469sub get_filters {
470  my $self = shift;
471  return $self->{filters};
472}
473
474=head2 instantiate_entry
475
476  Do any dynamic information replacement for information
477  which varies in an entry between lists. This is information which
478  needs to be output to the .bbl for an entry but which is a property
479  of the sorting list and not the entry per se so it cannot be stored
480  statically in the entry and must be pulled from the specific list
481  when outputting the entry.
482
483  Currently this means:
484
485  * sortinit
486  * extrayear
487  * extraalpha
488
489=cut
490
491sub instantiate_entry {
492  my $self = shift;
493  my $entry = shift;
494  my $key = shift;
495  return '' unless $entry;
496
497  my $entry_string = $$entry;
498
499  # sortinit
500  my $sinit = $self->get_sortinit_for_key($key);
501  if (defined($sinit)) {
502    my $str = "\\field{sortinit}{$sinit}";
503    $entry_string =~ s|<BDS>SORTINIT</BDS>|$str|gxms;
504  }
505
506  # sortinithash
507  my $sinithash = $self->get_sortinithash_for_key($key);
508  if (defined($sinithash)) {
509    my $str = "\\field{sortinithash}{$sinithash}";
510    $entry_string =~ s|<BDS>SORTINITHASH</BDS>|$str|gxms;
511  }
512
513  # extrayear
514  my $eys;
515  if (my $e = $self->get_extrayeardata($key)) {
516    $eys = "      \\field{extrayear}{$e}\n";
517    $entry_string =~ s|^\s*<BDS>EXTRAYEAR</BDS>\n|$eys|gxms;
518  }
519
520  # extratitle
521  my $ets;
522  if (my $e = $self->get_extratitledata($key)) {
523    $ets = "      \\field{extratitle}{$e}\n";
524    $entry_string =~ s|^\s*<BDS>EXTRATITLE</BDS>\n|$ets|gxms;
525  }
526
527  # extratitle
528  my $etys;
529  if (my $e = $self->get_extratitleyeardata($key)) {
530    $etys = "      \\field{extratitleyear}{$e}\n";
531    $entry_string =~ s|^\s*<BDS>EXTRATITLEYEAR</BDS>\n|$etys|gxms;
532  }
533
534  # extraalpha
535  my $eas;
536  if (my $e = $self->get_extraalphadata($key)) {
537    $eas = "      \\field{extraalpha}{$e}\n";
538    $entry_string =~ s|^\s*<BDS>EXTRAALPHA</BDS>\n|$eas|gxms;
539  }
540
541  return $entry_string;
542}
543
5441;
545
546__END__
547
548=head1 AUTHORS
549
550François Charette, C<< <firmicus at ankabut.net> >>
551Philip Kime C<< <philip at kime.org.uk> >>
552
553=head1 BUGS
554
555Please report any bugs or feature requests on our Github tracker at
556L<https://github.com/plk/biber/issues>.
557
558=head1 COPYRIGHT & LICENSE
559
560Copyright 2009-2015 François Charette and Philip Kime, all rights reserved.
561
562This module is free software.  You can redistribute it and/or
563modify it under the terms of the Artistic License 2.0.
564
565This program is distributed in the hope that it will be useful,
566but without any warranty; without even the implied warranty of
567merchantability or fitness for a particular purpose.
568
569=cut
570