-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCiteProc.php
1775 lines (1554 loc) · 58.8 KB
/
CiteProc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* CiteProc-PHP
*
* Copyright (C) 2010 - 2011 Ron Jerome, all rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class citeproc {
public $bibliography;
public $citation;
public $style;
protected $macros;
private $info;
protected $locale;
protected $style_locale;
public $quash;
private $mapper = NULL;
function __construct($csl = NULL, $lang = 'en') {
if ($csl) {
$this->init($csl, $lang);
}
}
function init($csl, $lang) {
// define field values appropriate to your data in the csl_mapper class and un-comment the next line.
$this->mapper = new csl_mapper();
$this->quash = array();
$csl_doc = new DOMDocument();
if ($csl_doc->loadXML($csl)) {
$style_nodes = $csl_doc->getElementsByTagName('style');
if ($style_nodes) {
foreach ($style_nodes as $style) {
$this->style = new csl_style($style);
}
}
$info_nodes = $csl_doc->getElementsByTagName('info');
if ($info_nodes) {
foreach ($info_nodes as $info) {
$this->info = new csl_info($info);
}
}
$this->locale = new csl_locale($lang);
$this->locale->set_style_locale($csl_doc);
$macro_nodes = $csl_doc->getElementsByTagName('macro');
if ($macro_nodes) {
$this->macros = new csl_macros($macro_nodes, $this);
}
$citation_nodes = $csl_doc->getElementsByTagName('citation');
foreach ($citation_nodes as $citation) {
$this->citation = new csl_citation($citation, $this);
}
$bibliography_nodes = $csl_doc->getElementsByTagName('bibliography');
foreach ($bibliography_nodes as $bibliography) {
$this->bibliography = new csl_bibliography($bibliography, $this);
}
}
}
function render($data, $mode = NULL) {
$text = '';
switch ($mode) {
case 'citation':
$text .= (isset($this->citation))? $this->citation->render($data) : '';
break;
case 'bibliography':
default:
$text .= (isset($this->bibliography))? $this->bibliography->render($data) : '';
break;
}
return $text;
}
function render_macro($name, $data, $mode) {
return $this->macros->render_macro($name, $data, $mode);
}
function get_locale($type, $arg1, $arg2 = NULL, $arg3 = NULL) {
return $this->locale->get_locale($type, $arg1, $arg2, $arg3);
}
function map_field($field) {
if ($this->mapper) {
return $this->mapper->map_field($field);
}
return ($field);
}
function map_type($field) {
if ($this->mapper) {
return $this->mapper->map_type($field);
}
return ($field);
}
}
class csl_factory {
public static function create($dom_node, $citeproc = NULL) {
$class_name = 'csl_' . str_replace('-', '_', $dom_node->nodeName);
if (class_exists($class_name)) {
return new $class_name($dom_node, $citeproc);
}
else {
return NULL;
}
}
}
class csl_collection {
protected $elements = array();
function add_element($elem) {
if (isset($elem)) $this->elements[] = $elem;
}
function render($data, $mode = NULL) {}
function format($text) {return $text;}
}
class csl_element extends csl_collection {
protected $attributes = array();
protected $citeproc;
function __construct($dom_node = NULL, $citeproc = NULL) {
$this->citeproc = &$citeproc;
$this->set_attributes($dom_node);
$this->init($dom_node, $citeproc);
}
function init($dom_node, $citeproc) {
if (!$dom_node) return;
foreach ($dom_node->childNodes as $node) {
if ($node->nodeType == 1) {
$this->add_element(csl_factory::create($node, $citeproc));
}
}
}
function __set($name, $value) {
$this->attributes[$name] = $value;
}
function __isset($name) {
return isset($this->attributes[$name]);
}
function __unset($name) {
unset($this->attributes[$name]);
}
function &__get($name = NULL) {
$null = NULL;
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}
if (isset($this->{$name})) {
return $this->{$name};
}
return $null;
}
function set_attributes($dom_node) {
$att = array();
$element_name = $dom_node->nodeName;
if (isset($dom_node->attributes->length)) {
for ($i=0; $i < $dom_node->attributes->length; $i++) {
$value = $dom_node->attributes->item($i)->value;
$name = str_replace(' ', '_', $dom_node->attributes->item($i)->name);
if ($name == 'type' ) {
$value = $this->citeproc->map_type($value);
}
if (($name == 'variable' || $name == 'is-numeric') && $element_name != 'label') {
$value = $this->citeproc->map_field($value);
}
$this->{$name} = $value;
}
}
}
function get_attributes() {
return $this->attributes;
}
function get_hier_attributes() {
$hier_attr = array();
$hier_names = array('and', 'delimiter-precedes-last', 'et-al-min', 'et-al-use-first',
'et-al-subsequent-min', 'et-al-subsequent-use-first', 'initialize-with',
'name-as-sort-order', 'sort-separator', 'name-form', 'name-delimiter',
'names-delimiter');
foreach ($hier_names as $name) {
if (isset($this->attributes[$name])) {
$hier_attr[$name] = $this->attributes[$name];
}
}
return $hier_attr;
}
function name($name = NULL) {
if ($name) {
$this->name = $name;
}
else {
return str_replace(' ', '_', $this->name);
}
}
}
class csl_rendering_element extends csl_element {
function render($data, $mode = NULL) {
$text = '';
$text_parts = array();
$delim = $this->delimiter;
foreach ($this->elements as $element) {
$text_parts[] = $element->render($data, $mode);
}
$text = implode($delim, $text_parts); // insert the delimiter if supplied.
return $this->format($text);
}
}
class csl_format extends csl_rendering_element {
protected $no_op;
protected $format;
function __construct($dom_node = NULL, $citeproc = NULL) {
parent::__construct($dom_node, $citeproc);
$this->init_formatting();
}
function init_formatting() {
$this->no_op = TRUE;
$this->format = '';
if (isset($this->quotes) && strtolower($this->quotes) == "true") {
$this->quotes = array();
$this->quotes['punctuation-in-quote'] = $this->citeproc->get_locale('style_option', 'punctuation-in-quote');
$this->quotes['open-quote'] = $this->citeproc->get_locale('term', 'open-quote');
$this->quotes['close-quote'] = $this->citeproc->get_locale('term', 'close-quote');
$this->quotes['open-inner-quote'] = $this->citeproc->get_locale('term', 'open-inner-quote');
$this->quotes['close-inner-quote'] = $this->citeproc->get_locale('term', 'close-inner-quote');
$this->no_op = FALSE;
}
if (isset($this->{'prefix'})) $this->no_op = FALSE;
if (isset($this->{'suffix'})) $this->no_op = FALSE;
if (isset($this->{'display'})) $this->no_op = FALSE;
$this->format .= (isset($this->{'font-style'})) ? 'font-style: ' . $this->{'font-style'} . ';' : '';
$this->format .= (isset($this->{'font-family'})) ? 'font-family: ' . $this->{'font-family'} . ';' : '';
$this->format .= (isset($this->{'font-weight'})) ? 'font-weight: ' . $this->{'font-weight'} . ';' : '';
$this->format .= (isset($this->{'font-variant'})) ? 'font-variant: ' . $this->{'font-variant'} . ';' : '';
$this->format .= (isset($this->{'text-decoration'})) ? 'text-decoration: ' . $this->{'text-decoration'} . ';' : '';
$this->format .= (isset($this->{'vertical-align'})) ? 'vertical-align: ' . $this->{'vertical-align'} . ';' : '';
// $this->format .= (isset($this->{'display'}) && $this->{'display'} == 'indent') ? 'padding-left: 25px;' : '';
if (isset($this->{'text-case'}) ||
!empty($this->format) ||
!empty($this->span_class) ||
!empty($this->div_class)) {
$this->no_op = FALSE;
}
}
function format($text) {
if (empty($text) || $this->no_op) return $text;
$quotes = $this->quotes;
$quotes = is_array($quotes) ? $quotes : array();
if (isset($this->{'text-case'})) {
switch ($this->{'text-case'}) {
case 'uppercase':
$text = mb_strtoupper($text);
break;
case 'lowercase':
$text = mb_strtolower($text);
break;
case 'capitalize-all':
case 'title':
$text = mb_convert_case($text, MB_CASE_TITLE);
break;
case 'capitalize-first':
$chr1 = mb_strtoupper(mb_substr($text, 0, 1));
$text = $chr1 . mb_substr($text, 1);
break;
}
}
$prefix = $this->prefix;
$prefix .= isset($quotes['open-quote']) ? $quotes['open-quote'] : '';
$suffix = $this->suffix;
if (isset($quotes['close-quote']) && !empty($suffix) && isset($quotes['punctuation-in-quote'])) {
if (strpos($suffix, '.') !== FALSE || strpos($suffix, ',') !== FALSE) {
$suffix = $suffix . $quotes['close-quote'];
}
}
elseif (isset($quotes['close-quote'])) {
$suffix = $quotes['close-quote'] . $suffix;
}
if (!empty($suffix)) { // gaurd against repeaded suffixes...
$no_tags = strip_tags($text);
if (strlen($no_tags) && ($no_tags[(strlen($no_tags) - 1)] == $suffix[0]) ) {
$suffix = substr($suffix, 1);
}
}
if (!empty($this->format) || !empty($this->span_class)) {
$style = (!empty($this->format)) ? 'style="' . $this->format . '" ' : '';
$class = (!empty($this->span_class)) ? 'class="' . $this->span_class . '"' : '';
$text = '<span ' . $class . $style . '>' . $text . '</span>';
}
$div_class = $div_style = '';
if (!empty($this->div_class)) {
$div_class = (!empty($this->div_class)) ? 'class="' . $this->div_class . '"' : '';
}
if ($this->display == 'indent') {
$div_style = 'style="text-indent: 0px; padding-left: 45px;"';
}
if ($div_class || $div_style) {
return '<div ' . $div_class . $div_style . '>' . $prefix . $text . $suffix . '</div>';
}
return $prefix . $text . $suffix;
}
}
class csl_info {
public $title;
public $id;
public $authors = array();
public $links = array();
function __construct($dom_node) {
$name = array();
foreach ($dom_node->childNodes as $node) {
if ($node->nodeType == 1) {
switch ($node->nodeName) {
case 'author':
case 'contributor':
foreach ($node->childNodes as $authnode) {
if ($node->nodeType == 1) {
$name[$authnode->nodeName] = $authnode->nodeValue;
}
}
$this->authors[] = $name;
break;
case 'link':
foreach ($node->attributes as $attribute) {
$this->links[] = $attribute->value;
}
break;
default:
$this->{$node->nodeName} = $node->nodeValue;
}
}
}
}
}
class csl_terms {
}
class csl_name extends csl_format {
private $name_parts = array();
private $attr_init = FALSE;
function __construct($dom_node, $citeproc = NULL) {
$tags = $dom_node->getElementsByTagName('name-part');
if ($tags) {
foreach ($tags as $tag) {
$name_part = $tag->getAttribute('name');
$tag->removeAttribute('name');
for ($i=0; $i < $tag->attributes->length; $i++) {
$value = $tag->attributes->item($i)->value;
$name = str_replace(' ', '_', $tag->attributes->item($i)->name);
$this->name_parts[$name_part][$name] = $value;
}
}
}
parent::__construct($dom_node, $citeproc);
}
function init_formatting() {
$this->no_op = array();
$this->format = array();
$this->base = $this->get_attributes();
$this->format['base'] = '';
$this->format['family'] = '';
$this->format['given'] = '';
$this->no_op['base'] = TRUE;
$this->no_op['family'] = TRUE;
$this->no_op['given'] = TRUE;
if (isset($this->prefix)) {
$this->no_op['base'] = FALSE;
}
if (isset($this->suffix)) {
$this->no_op['base'] = FALSE;
}
$this->init_format($this->base);
if (!empty($this->name_parts)) {
foreach ($this->name_parts as $name => $formatting) {
$this->init_format($formatting, $name);
}
}
}
function init_attrs($mode) {
// $and = $this->get_attributes('and');
if (isset($this->citeproc)) {
$style_attrs = $this->citeproc->style->get_hier_attributes();
$mode_attrs = $this->citeproc->{$mode}->get_hier_attributes();
$this->attributes = array_merge($style_attrs, $mode_attrs, $this->attributes);
}
if (isset($this->and)) {
if ($this->and == 'text') {
$this->and = $this->citeproc->get_locale('term', 'and');
}
elseif ($this->and == 'symbol') {
$this->and = '&';
}
}
if (!isset($this->delimiter)) {
$this->delimiter = $this->{'name-delimiter'} ;
}
if (!isset($this->alnum)) {
list($this->alnum, $this->alpha, $this->cntrl, $this->dash,
$this->digit, $this->graph, $this->lower, $this->print,
$this->punct, $this->space, $this->upper, $this->word,
$this->patternModifiers) = $this->get_regex_patterns();
}
$this->dpl = $this->{'delimiter-precedes-last'};
$this->sort_separator = isset($this->{'sort-separator'}) ? $this->{'sort-separator'} : ', ';
$this->delimiter = isset($this->{'name-delimiter'}) ? $this->{'name-delimiter'} : (isset($this->delimiter) ? $this->delimiter : ', ');
$this->form = isset($this->{'name-form'}) ? $this->{'name-form'} : (isset($this->form) ? $this->form : 'long');
$this->attr_init = $mode;
}
function init_format($attribs, $part = 'base') {
if (!isset($this->{$part})) {
$this->{$part} = array();
}
if (isset($attribs['quotes']) && strtolower($attribs['quotes']) == 'true') {
$this->{$part}['open-quote'] = $this->citeproc->get_locale('term', 'open-quote');
$this->{$part}['close-quote'] = $this->citeproc->get_locale('term', 'close-quote');
$this->{$part}['open-inner-quote'] = $this->citeproc->get_locale('term', 'open-inner-quote');
$this->{$part}['close-inner-quote'] = $this->citeproc->get_locale('term', 'close-inner-quote');
$this->no_op[$part] = FALSE;
}
if (isset($attribs['prefix'])) $this->{$part}['prefix'] = $attribs['prefix'];
if (isset($attribs['suffix'])) $this->{$part}['suffix'] = $attribs['suffix'];
$this->format[$part] .= (isset($attribs['font-style'])) ? 'font-style: ' . $attribs['font-style'] . ';' : '';
$this->format[$part] .= (isset($attribs['font-family'])) ? 'font-family: ' . $attribs['font-family'] . ';' : '';
$this->format[$part] .= (isset($attribs['font-weight'])) ? 'font-weight: ' . $attribs['font-weight'] . ';' : '';
$this->format[$part] .= (isset($attribs['font-variant'])) ? 'font-variant: ' . $attribs['font-variant'] . ';' : '';
$this->format[$part] .= (isset($attribs['text-decoration'])) ? 'text-decoration: ' . $attribs['text-decoration'] . ';' : '';
$this->format[$part] .= (isset($attribs['vertical-align'])) ? 'vertical-align: ' . $attribs['vertical-align'] . ';' : '';
if (isset($attribs['text-case'])) {
$this->no_op[$part] = FALSE;
$this->{$part}['text-case'] = $attribs['text-case'];
}
if (!empty($this->format[$part])) $this->no_op[$part] = FALSE;
}
function format($text, $part = 'base') {
if (empty($text) || $this->no_op[$part]) return $text;
if (isset($this->{$part}['text-case'])) {
switch ($this->{$part}['text-case']) {
case 'uppercase':
$text = mb_strtoupper($text);
break;
case 'lowercase':
$text = mb_strtolower($text);
break;
case 'capitalize-all':
$text = mb_convert_case($text, MB_CASE_TITLE);
break;
case 'capitalize-first':
$chr1 = mb_strtoupper(mb_substr($text, 0, 1));
$text = $chr1 . mb_substr($text, 1);
break;
}
}
$open_quote = isset($this->{$part}['open-quote']) ? $this->{$part}['open-quote'] : '';
$close_quote = isset($this->{$part}['close-quote']) ? $this->{$part}['close-quote'] : '';
$prefix = isset($this->{$part}['prefix']) ? $this->{$part}['prefix'] : '';
$suffix = isset($this->{$part}['suffix']) ? $this->{$part}['suffix'] : '';
if ($text[(strlen($text) -1)] == $suffix) unset($suffix);
if (!empty($this->format[$part])) {
$text = '<span style="' . $this->format[$part] . '">' . $text . '</span>';
}
return $prefix . $open_quote . $text . $close_quote . $suffix;
}
function render($names, $mode = NULL) {
$text = '';
$authors = array();
$count = 0;
$auth_count = 0;
$et_al_triggered = FALSE;
if (!$this->attr_init || $this->attr_init != $mode) $this->init_attrs($mode);
$initialize_with = $this->{'initialize-with'};
foreach ($names as $rank => $name) {
$count++;
//$given = (!empty($name->firstname)) ? $name->firstname : '';
if (!empty($name->given) && isset($initialize_with)) {
$name->given = preg_replace("/([$this->upper])[$this->lower]+/$this->patternModifiers", '\\1', $name->given);
$name->given = preg_replace("/(?<=[-$this->upper]) +(?=[-$this->upper])/$this->patternModifiers", "", $name->given);
if(isset($name->initials)) {
$name->initials = $name->given . $name->initials;
}
$name->initials = $name->given;
}
if (isset($name->initials)) {
// within initials, remove any dots:
$name->initials = preg_replace("/([$this->upper])\.+/$this->patternModifiers", "\\1", $name->initials);
// within initials, remove any spaces *between* initials:
$name->initials = preg_replace("/(?<=[-$this->upper]) +(?=[-$this->upper])/$this->patternModifiers", "", $name->initials);
if ($this->citeproc->style->{'initialize-with-hyphen'} == 'false') {
$name->initials = preg_replace("/-/", '', $name->initials);
}
// within initials, add a space after a hyphen, but only if ...
if (preg_match("/ $/", $initialize_with)) {// ... the delimiter that separates initials ends with a space
// $name->initials = preg_replace("/-(?=[$this->upper])/$this->patternModifiers", " -", $name->initials);
}
// then, separate initials with the specified delimiter:
$name->initials = preg_replace("/([$this->upper])(?=[^$this->lower]+|$)/$this->patternModifiers", "\\1$initialize_with", $name->initials);
// $shortenInitials = (isset($options['numberOfInitialsToKeep'])) ? $options['numberOfInitialsToKeep'] : FALSE;
// if ($shortenInitials) $given = drupal_substr($given, 0, $shortenInitials);
if (isset($initialize_with) ) {
$name->given = $name->initials;
}
elseif(!empty($name->given)) {
$name->given = $name->given .' '. $name->initials;
}
elseif(empty($name->given)) {
$name->given = $name->initials;
}
}
$ndp = (isset($name->{'non-dropping-particle'})) ? $name->{'non-dropping-particle'} . ' ' : '';
$suffix = (isset($name->{'suffix'})) ? ' ' . $name->{'suffix'} : '';
if (isset($name->given)) {
$given = $this->format($name->given, 'given');
}
else {
$given = '';
}
if(isset($name->family)) {
$name->family = $this->format($name->family, 'family');
if ($this->form == 'short') {
$text = $ndp . $name->family;
}
else {
switch ($this->{'name-as-sort-order'}) {
case 'first' && $rank == 0:
case 'all':
$text = $ndp . $name->family . $this->sort_separator . $given;
break;
default:
$text = $given .' '. $ndp . $name->family . $suffix;
}
}
$authors[] = trim($this->format($text));
}
if (isset($this->{'et-al-min'}) && $count >= $this->{'et-al-min'}) break;
}
if (isset($this->{'et-al-min'}) &&
$count >= $this->{'et-al-min'} &&
isset($this->{'et-al-use-first'}) &&
$count >= $this->{'et-al-use-first'} &&
count($names) > $this->{'et-al-use-first'}) {
if ($this->{'et-al-use-first'} < $this->{'et-al-min'}) {
for ($i = $this->{'et-al-use-first'}; $i < $count; $i++) {
unset($authors[$i]);
}
}
if ($this->etal) {
$etal = $this->etal->render();
}
else {
$etal = $this->citeproc->get_locale('term', 'et-al');
}
$et_al_triggered = TRUE;
}
if (!empty($authors) && !$et_al_triggered) {
$auth_count = count($authors);
if (isset($this->and) && $auth_count > 1) {
$authors[$auth_count-1] = $this->and . ' ' . $authors[$auth_count-1]; //stick an "and" in front of the last author if "and" is defined
}
}
$text = implode($this->delimiter, $authors);
if (!empty($authors) && $et_al_triggered) {
switch($this->{'delimiter-precedes-et-al'}) {
case 'never':
$text = $text . " $etal";
break;
case 'always':
$text = $text . "$this->delimiter$etal";
break;
default:
$text = count($authors) == 1 ? $text . " $etal" : $text . "$this->delimiter$etal";
}
}
if ($this->form == 'count') {
if (!$et_al_triggered) {
return (int)count($authors);
}
else {
return (int)(count($authors) - 1);
}
}
// strip out the last delimiter if not required
if (isset($this->and) && $auth_count > 1) {
$last_delim = strrpos($text, $this->delimiter . $this->and);
switch ($this->dpl) { //dpl == delimiter proceeds last
case 'always':
return $text;
break;
case 'never':
return substr_replace($text, ' ', $last_delim, strlen($this->delimiter));
break;
case 'contextual':
default:
if ($auth_count < 3) {
return substr_replace($text, ' ', $last_delim, strlen($this->delimiter));
}
}
}
return $text ;
}
function get_regex_patterns() {
// Checks if PCRE is compiled with UTF-8 and Unicode support
if (!@preg_match('/\pL/u', 'a')) {
// probably a broken PCRE library
return $this->get_latin1_regex();
}
else {
// Unicode safe filter for the value
return $this->get_utf8_regex();
}
}
function get_latin1_regex() {
$alnum = "[:alnum:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Matches ISO-8859-1 letters:
$alpha = "[:alpha:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Matches ISO-8859-1 control characters:
$cntrl = "[:cntrl:]";
// Matches ISO-8859-1 dashes & hyphens:
$dash = "-–";
// Matches ISO-8859-1 digits:
$digit = "[\d]";
// Matches ISO-8859-1 printing characters (excluding space):
$graph = "[:graph:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Matches ISO-8859-1 lower case letters:
$lower = "[:lower:]äåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Matches ISO-8859-1 printing characters (including space):
$print = "[:print:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Matches ISO-8859-1 punctuation:
$punct = "[:punct:]";
// Matches ISO-8859-1 whitespace (separating characters with no visual representation):
$space = "[\s]";
// Matches ISO-8859-1 upper case letters:
$upper = "[:upper:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆ";
// Matches ISO-8859-1 "word" characters:
$word = "_[:alnum:]ÄÅÁÀÂÃÇÉÈÊËÑÖØÓÒÔÕÜÚÙÛÍÌÎÏÆäåáàâãçéèêëñöøóòôõüúùûíìîïæÿß";
// Defines the PCRE pattern modifier(s) to be used in conjunction with the above variables:
// More info: <http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php>
$patternModifiers = "";
return array($alnum, $alpha, $cntrl, $dash, $digit, $graph, $lower,
$print, $punct, $space, $upper, $word, $patternModifiers);
}
function get_utf8_regex() {
// Matches Unicode letters & digits:
$alnum = "\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}"; // Unicode-aware equivalent of "[:alnum:]"
// Matches Unicode letters:
$alpha = "\p{Ll}\p{Lu}\p{Lt}\p{Lo}"; // Unicode-aware equivalent of "[:alpha:]"
// Matches Unicode control codes & characters not in other categories:
$cntrl = "\p{C}"; // Unicode-aware equivalent of "[:cntrl:]"
// Matches Unicode dashes & hyphens:
$dash = "\p{Pd}";
// Matches Unicode digits:
$digit = "\p{Nd}"; // Unicode-aware equivalent of "[:digit:]"
// Matches Unicode printing characters (excluding space):
$graph = "^\p{C}\t\n\f\r\p{Z}"; // Unicode-aware equivalent of "[:graph:]"
// Matches Unicode lower case letters:
$lower = "\p{Ll}\p{M}"; // Unicode-aware equivalent of "[:lower:]"
// Matches Unicode printing characters (including space):
$print = "\P{C}"; // same as "^\p{C}", Unicode-aware equivalent of "[:print:]"
// Matches Unicode punctuation (printing characters excluding letters & digits):
$punct = "\p{P}"; // Unicode-aware equivalent of "[:punct:]"
// Matches Unicode whitespace (separating characters with no visual representation):
$space = "\t\n\f\r\p{Z}"; // Unicode-aware equivalent of "[:space:]"
// Matches Unicode upper case letters:
$upper = "\p{Lu}\p{Lt}"; // Unicode-aware equivalent of "[:upper:]"
// Matches Unicode "word" characters:
$word = "_\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}"; // Unicode-aware equivalent of "[:word:]" (or "[:alnum:]" plus "_")
// Defines the PCRE pattern modifier(s) to be used in conjunction with the above variables:
// More info: <http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php>
$patternModifiers = "u"; // the "u" (PCRE_UTF8) pattern modifier causes PHP/PCRE to treat pattern strings as UTF-8
return array($alnum, $alpha, $cntrl, $dash, $digit, $graph, $lower,
$print, $punct, $space, $upper, $word, $patternModifiers);
}
}
class csl_names extends csl_format {
private $substitutes;
function init_formatting() {
// $this->span_class = 'authors';
parent::init_formatting();
}
function init($dom_node, $citeproc) {
$etal = '';
$tag = $dom_node->getElementsByTagName('substitute')->item(0);
if ($tag) {
$this->substitutes = csl_factory::create($tag, $citeproc);
$dom_node->removeChild($tag);
}
$tag = $dom_node->getElementsByTagName('et-al')->item(0);
if ($tag) {
$etal = csl_factory::create($tag, $citeproc);
$dom_node->removeChild($tag);
}
$var = $dom_node->getAttribute('variable');
foreach ($dom_node->childNodes as $node) {
if ($node->nodeType == 1) {
$element = csl_factory::create($node, $citeproc);
if (($element instanceof csl_label)) $element->variable = $var;
if (($element instanceof csl_name) && $etal) {
$element->etal = $etal;
}
$this->add_element($element);
}
}
}
function render($data, $mode = NULL) {
$matches = array();
$variable_parts = array();
if (!isset($this->delimiter)) {
$style_delimiter = $this->citeproc->style->{'names-delimiter'};
$mode_delimiter = $this->citeproc->{$mode}->{'names-delimiter'};
$this->delimiter = (isset($mode_delimiter)) ? $mode_delimiter : (isset($style_delimiter) ? $style_delimiter : '');
}
$variables = explode(' ', $this->variable);
foreach ($variables as $var) {
if (in_array($var, $this->citeproc->quash)) continue;
if (isset($data->{$var}) && (!empty($data->{$var}))) {
$matches[] = $var;
}
}
if (empty($matches)) { // we don't have any primary suspects, so lets check the substitutes...
if (isset($this->substitutes)) {
foreach ($this->substitutes->elements as $element) {
if (($element instanceof csl_names)) { //test to see if any of the other names variables has content
$sub_variables = explode(' ', $element->variable);
foreach ($sub_variables as $var) {
if (isset($data->{$var}) ) {
$matches[] = $var;
$this->citeproc->quash[] = $var;
}
}
}
else { // if it's not a "names" element, just render it
$text = $element->render($data, $mode);
$this->citeproc->quash[] = isset($element->variable) ? $element->variable : $element->var;
if (!empty($text)) $variable_parts[] = $text;
}
if (!empty($matches)) break;
}
}
}
foreach ($matches as $var) {
if (in_array($var, $this->citeproc->quash) && in_array($var, $variables)) continue;
$text = '';
if (!empty($data->{$var})) {
foreach ($this->elements as $element) {
if(is_a($element, 'csl_label')) {
$element->variable = $var;
$text .= $element->render($data, $mode);
}
elseif (is_a($element, 'csl_name')) {
$text .= $element->render($data->{$var}, $mode);
}
}
}
if (!empty($text)) $variable_parts[] = $text;
}
if (!empty($variable_parts)) {
$text = implode($this->delimiter, $variable_parts);
return $this->format($text);
}
return ;
}
}
class csl_date extends csl_format {
function init($dom_node, $citeproc) {
$locale_elements = array();
if ($form = $this->form) {
$local_date = $this->citeproc->get_locale('date_options', $form);
$dom_elem = dom_import_simplexml($local_date[0]);
if ($dom_elem) {
foreach ($dom_elem->childNodes as $node) {
if ($node->nodeType == 1) {
$locale_elements[] = csl_factory::create($node, $citeproc);
}
}
}
foreach ($dom_node->childNodes as $node) {
if ($node->nodeType == 1) {
$element = csl_factory::create($node, $citeproc);
foreach ($locale_elements as $key => $locale_element) {
if ($locale_element->name == $element->name) {
$locale_elements[$key]->attributes = array_merge($locale_element->attributes, $element->attributes);
$locale_elements[$key]->format = $element->format;
break;
}
else {
$locale_elements[] = $element;
}
}
}
}
if ($date_parts = $this->{'date-parts'}) {
$parts = explode('-', $date_parts);
foreach ($locale_elements as $key => $element) {
if (array_search($element->name, $parts) === FALSE) {
unset($locale_elements[$key]);
}
}
if (count($locale_elements) != count($parts)) {
foreach ($parts as $part) {
$element = new csl_date_part();
$element->name = $part;
$locale_elements[] = $element;
}
}
// now re-order the elements
foreach ($parts as $part) {
foreach ($locale_elements as $key => $element)
if ($element->name == $part) {
$this->elements[] = $element;
unset($locale_elements[$key]);
}
}
}
else {
$this->elements = $locale_elements;
}
}
else {
parent::init($dom_node, $citeproc);
}
}
function render($data, $mode = NULL) {
$date_parts = array();
$date = '';
$text = '';
if (($var = $this->variable) && isset($data->{$var})) {
$date = $data->{$var}->{'date-parts'}[0];
foreach ($this->elements as $element) {
$date_parts[] = $element->render($date, $mode);
}
$text = implode('', $date_parts);
}
return $this->format($text);
}
}
class csl_date_part extends csl_format {
function render($date, $mode = NULL) {
$text = '';
switch ($this->name) {
case 'year':
$text = (isset($date[0])) ? $date[0] : '';
if ($text > 0 && $text < 500) {
$text = $text . $this->citeproc->get_locale('term', 'ad');
}
elseif ($text < 0) {
$text = $text * -1;
$text = $text . $this->citeproc->get_locale('term', 'bc');
}
//return ((isset($this->prefix))? $this->prefix : '') . $date[0] . ((isset($this->suffix))? $this->suffix : '');
break;
case 'month':
$text = (isset($date[1])) ? $date[1] : '';
if (empty($text) || $text < 1 || $text > 12) return;
// $form = $this->form;
switch ($this->form) {
case 'numeric': break;
case 'numeric-leading-zeros':
if ($text < 10) {
$text = '0' . $text;
break;
}
break;
case 'short':
$month = 'month-' . sprintf('%02d', $text);
$text = $this->citeproc->get_locale('term', $month, 'short');
break;
default:
$month = 'month-' . sprintf('%02d', $text);
$text = $this->citeproc->get_locale('term', $month);
break;
}
break;
case 'day':
$text = (isset($date[2])) ? $date[2] : '';
break;
}
return $this->format($text);
}
}