-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
api_cite.js
1489 lines (1399 loc) · 66.8 KB
/
api_cite.js
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
/*global CSL: true */
CSL.Engine.prototype.previewCitationCluster = function (citation, citationsPre, citationsPost, newMode) {
// Generate output for a hypothetical citation at the current position,
// Leave the registry in the same state in which it was found.
//print("################### previewCitationCluster() #################");
var oldMode = this.opt.mode;
this.setOutputFormat(newMode);
var ret = this.processCitationCluster(citation, citationsPre, citationsPost, CSL.PREVIEW);
this.setOutputFormat(oldMode);
return ret[1];
};
CSL.Engine.prototype.appendCitationCluster = function (citation) {
var citationsPre = [];
var len = this.registry.citationreg.citationByIndex.length;
for (var pos = 0; pos < len; pos += 1) {
var c = this.registry.citationreg.citationByIndex[pos];
citationsPre.push(["" + c.citationID, c.properties.noteIndex]);
}
// Drop the data segment to return a list of pos/string pairs.
return this.processCitationCluster(citation, citationsPre, [])[1];
};
CSL.Engine.prototype.processCitationCluster = function (citation, citationsPre, citationsPost, flag) {
var c, i, ilen, j, jlen, k, klen, n, nlen, key, Item, item, noteCitations, textCitations, m, citationsInNote;
this.debug = false;
this.tmp.loadedItemIDs = {};
// Revert citation dereference from 2ffc4664ae
//citation = JSON.parse(JSON.stringify(citation));
//print("################### processCitationCluster() #################");
this.tmp.citation_errors = [];
var return_data = {"bibchange": false};
// make sure this citation has a unique ID, and register it in citationById.
this.setCitationId(citation);
var oldCitationList;
var oldItemList;
var oldAmbigs;
if (flag === CSL.PREVIEW) {
//SNIP-START
if (this.debug) {
CSL.debug("****** start state save *********");
}
//SNIP-END
//
// Simplify.
// Take a slice of existing citations.
oldCitationList = this.registry.citationreg.citationByIndex.slice();
// Take a slice of current items, for later use with update.
oldItemList = this.registry.reflist.slice();
// Make a list of preview citation ref objects
var newCitationList = citationsPre.concat([["" + citation.citationID, citation.properties.noteIndex]]).concat(citationsPost);
// Make a full list of desired ids, for use in preview update,
// and a hash list of same while we're at it.
var newItemIds = {};
var newItemIdsList = [];
for (var i = 0, ilen = newCitationList.length; i < ilen; i += 1) {
c = this.registry.citationreg.citationById[newCitationList[i][0]];
for (j = 0, jlen = c.citationItems.length; j < jlen; j += 1) {
newItemIds[c.citationItems[j].id] = true;
newItemIdsList.push("" + c.citationItems[j].id);
}
}
// Clone and save off disambigs of items that will be lost.
oldAmbigs = {};
for (var i = 0, ilen = oldItemList.length; i < ilen; i += 1) {
if (!newItemIds[oldItemList[i].id]) {
var oldAkey = this.registry.registry[oldItemList[i].id].ambig;
var ids = this.registry.ambigcites[oldAkey];
if (ids) {
for (j = 0, jlen = ids.length; j < jlen; j += 1) {
oldAmbigs[ids[j]] = CSL.cloneAmbigConfig(this.registry.registry[ids[j]].disambig);
}
}
}
}
// Update items. This will produce the base name data and sort things.
// Possibly unnecessary?
//this.updateItems(this.registry.mylist.concat(tmpItems));
//SNIP-START
if (this.debug) {
CSL.debug("****** end state save *********");
}
//SNIP-END
}
this.tmp.taintedCitationIDs = {};
var sortedItems = [];
// Styles that use note backreferencing with a by-cite
// givenname disambiguation rule include the note number
// in the cite for disambiguation purposes. Correct resolution
// of disambiguate="true" conditions on first-reference cites
// in certain editing scenarios (e.g. where a cite is moved across
// notes) requires that disambiguation be rerun on cites
// affected by the edit.
var rerunAkeys = {};
// retrieve item data and compose items for use in rendering
// attach pointer to item data to shared copy for good measure
for (var i = 0, ilen = citation.citationItems.length; i < ilen; i += 1) {
// Protect against caller-side overwrites to locator strings etc
item = {};
for (var key in citation.citationItems[i]) {
item[key] = citation.citationItems[i][key];
}
Item = this.retrieveItem("" + item.id);
if (Item.id) {
this.transform.loadAbbreviation("default", "hereinafter", Item.id);
}
item = CSL.parseLocator.call(this, item);
if (this.opt.development_extensions.static_statute_locator) {
this.remapSectionVariable([[Item,item]]);
}
if (this.opt.development_extensions.locator_label_parse) {
if (item.locator && ["bill","gazette","legislation","regulation","treaty"].indexOf(Item.type) === -1 && (!item.label || item.label === 'page')) {
var m = CSL.LOCATOR_LABELS_REGEXP.exec(item.locator);
if (m) {
var tryLabel = CSL.LOCATOR_LABELS_MAP[m[2]];
if (this.getTerm(tryLabel)) {
item.label = tryLabel;
item.locator = m[3];
}
}
}
}
var newitem = [Item, item];
sortedItems.push(newitem);
citation.citationItems[i].item = Item;
}
// ZZZ sort stuff moved from here.
// attach the sorted list to the citation item
citation.sortedItems = sortedItems;
// build reconstituted citations list in current document order
var citationByIndex = [];
for (var i = 0, ilen = citationsPre.length; i < ilen; i += 1) {
c = citationsPre[i];
//CSL.debug(" -- attempting to access Pre citation with ID: ("+c[0]+")");
try {
this.registry.citationreg.citationById[c[0]].properties.noteIndex = c[1];
} catch (e) {
var err = "CSL error\n";
err += " " + e + "\n";
err += " citationID=" + c[0] + "\n";
err += " noteIndex=" + c[1] + "\n";
err += " in array citationsPre at index " + i + ", document position " + i;
throw err;
}
citationByIndex.push(this.registry.citationreg.citationById[c[0]]);
}
citationByIndex.push(citation);
for (var i = 0, ilen = citationsPost.length; i < ilen; i += 1) {
c = citationsPost[i];
//CSL.debug(" -- attempting to access Post citation with ID: ("+c[0]+")");
try {
this.registry.citationreg.citationById[c[0]].properties.noteIndex = c[1];
} catch (e) {
var err = "CSL error\n";
err += " " + e + "\n";
err += " citationID=" + c[0] + "\n";
err += " noteIndex=" + c[1] + "\n";
err += " in array citationsPost at index " + i + ", document position " + (citationsPre.length + 1 + i);
throw err;
}
citationByIndex.push(this.registry.citationreg.citationById[c[0]]);
}
this.registry.citationreg.citationByIndex = citationByIndex;
//
// The processor provides three facilities to support
// updates following position reevaluation.
//
// (1) The updateItems() function reports tainted ItemIDs
// to state.tmp.taintedItemIDs.
//
// (2) The processor memos the type of style referencing as
// CSL.NONE, CSL.NUMERIC or CSL.POSITION in state.opt.update_mode.
//
// XXXX: NO LONGER
// (3) For citations containing cites with backreference note numbers,
// a string image of the rendered citation is held in
// citation.properties.backref_citation, and a list of
// ItemIDs to be used to update the backreference note numbers
// is memoed at citation.properties.backref_index. When such
// citations change position, they can be updated with a
// series of simple find and replace operations, without
// need for rerendering.
//
//
// Position evaluation!
//
// set positions in reconstituted list, noting taints
this.registry.citationreg.citationsByItemId = {};
if (this.opt.update_mode === CSL.POSITION) {
textCitations = [];
noteCitations = [];
citationsInNote = {};
}
var update_items = [];
for (var i = 0, ilen = citationByIndex.length; i < ilen; i += 1) {
if (!citationByIndex[i].properties) {
citationByIndex[i].properties = {};
}
citationByIndex[i].properties.index = i;
for (j = 0, jlen = citationByIndex[i].sortedItems.length; j < jlen; j += 1) {
item = citationByIndex[i].sortedItems[j];
if (!this.registry.citationreg.citationsByItemId[item[1].id]) {
this.registry.citationreg.citationsByItemId[item[1].id] = [];
update_items.push("" + item[1].id);
}
if (this.registry.citationreg.citationsByItemId[item[1].id].indexOf(citationByIndex[i]) === -1) {
this.registry.citationreg.citationsByItemId[item[1].id].push(citationByIndex[i]);
}
}
if (this.opt.update_mode === CSL.POSITION) {
if (citationByIndex[i].properties.noteIndex) {
noteCitations.push(citationByIndex[i]);
} else {
citationByIndex[i].properties.noteIndex = 0;
textCitations.push(citationByIndex[i]);
}
}
}
//
// update bibliography items here
//
if (flag !== CSL.ASSUME_ALL_ITEMS_REGISTERED) {
//SNIP-START
if (this.debug) {
CSL.debug("****** start update items *********");
}
//SNIP-END
// true signals implicit updateItems (will not rerun sys.retrieveItem())
this.updateItems(update_items, null, null, true);
//SNIP-START
if (this.debug) {
CSL.debug("****** endo update items *********");
}
//SNIP-END
}
if (!this.opt.citation_number_sort && sortedItems && sortedItems.length > 1 && this.citation_sort.tokens.length > 0) {
for (var i = 0, ilen = sortedItems.length; i < ilen; i += 1) {
sortedItems[i][1].sortkeys = CSL.getSortKeys.call(this, sortedItems[i][0], "citation_sort");
}
/*
* Grouped sort stuff (start)
*/
if (this.opt.grouped_sort && !citation.properties.unsorted) {
// Insert authorstring as key.
// Note the position of each ID that correspond to the relevant
// object in the myblobs list.
for (var i = 0, ilen = sortedItems.length; i < ilen; i += 1) {
var sortkeys = sortedItems[i][1].sortkeys;
this.tmp.authorstring_request = true;
// Run getAmbiguousCite() with the current disambig
// parameters, and pick up authorstring from the registry.
var mydisambig = this.registry.registry[sortedItems[i][0].id].disambig;
this.tmp.authorstring_request = true;
CSL.getAmbiguousCite.call(this, sortedItems[i][0], mydisambig);
var authorstring = this.registry.authorstrings[sortedItems[i][0].id];
this.tmp.authorstring_request = false;
sortedItems[i][1].sortkeys = [authorstring].concat(sortkeys);
}
sortedItems.sort(this.citation.srt.compareCompositeKeys);
// Replace authorstring key in items with same (authorstring) with the
// keystring of first normal key. This forces grouped sorts,
// as discussed here:
// https://github.com/citation-style-language/schema/issues/40
var lastauthor = false;
var thiskey = false;
var thisauthor = false;
for (var i = 0, ilen = sortedItems.length; i < ilen; i += 1) {
if (sortedItems[i][1].sortkeys[0] !== lastauthor) {
thisauthor = sortedItems[i][1].sortkeys[0];
thiskey = sortedItems[i][1].sortkeys[1];
}
sortedItems[i][1].sortkeys[0] = "" + thiskey + i;
lastauthor = thisauthor;
}
}
/*
* Grouped sort stuff (end)
*/
if (!citation.properties.unsorted) {
sortedItems.sort(this.citation.srt.compareCompositeKeys);
}
}
var citations;
if (this.opt.update_mode === CSL.POSITION) {
for (var i = 0; i < 2; i += 1) {
citations = [textCitations, noteCitations][i];
var first_ref = {};
var last_ref = {};
for (j = 0, jlen = citations.length; j < jlen; j += 1) {
var onecitation = citations[j];
if (!citations[j].properties.noteIndex) {
citations[j].properties.noteIndex = 0;
}
citations[j].properties.noteIndex = parseInt(citations[j].properties.noteIndex, 10);
if (j > 0 && citations[j - 1].properties.noteIndex > citations[j].properties.noteIndex) {
citationsInNote = {};
first_ref = {};
last_ref = {};
}
for (k = 0, klen = onecitation.sortedItems.length; k < klen; k += 1) {
if (!this.registry.registry[onecitation.sortedItems[k][1].id].parallel) {
if (!citationsInNote[onecitation.properties.noteIndex]) {
citationsInNote[onecitation.properties.noteIndex] = 1;
} else {
citationsInNote[onecitation.properties.noteIndex] += 1;
}
}
}
// Set the following:
//
// (1) position as required (as per current Zotero)
// (2) first-reference-note-number as required (on onecitation item)
// (3) near-note as required (on onecitation item, according to
// state.opt["near-note-distance"] parameter)
// (4) state.registry.citationreg.citationsByItemId.
//
// Any state changes caused by unsetting or resetting should
// trigger a single entry for the citations in
// state.tmp.taintedCitationIDs (can block on presence of
// state.registry.citationreg.citationsByItemId).
//
for (k = 0, klen = citations[j].sortedItems.length; k < klen; k += 1) {
item = citations[j].sortedItems[k];
var myid = item[0].id;
var mylocator = item[1].locator;
var mylabel = item[1].label;
if (item[0].legislation_id) {
myid = item[0].legislation_id;
}
var incitationid;
if (k > 0) {
// incitationid is only reached in the else branch
// following "undefined" === typeof first_ref[myid]
// below
if (onecitation.sortedItems[k - 1][0].legislation_id) {
incitationid = onecitation.sortedItems[k - 1][0].legislation_id;
} else {
incitationid = onecitation.sortedItems[k - 1][1].id;
}
}
// Don't touch item data of other cites when previewing
if (flag === CSL.PREVIEW) {
if (onecitation.citationID != citation.citationID) {
if ("undefined" === typeof first_ref[item[1].id]) {
first_ref[myid] = onecitation.properties.noteIndex;
last_ref[myid] = onecitation.properties.noteIndex;
} else {
last_ref[myid] = onecitation.properties.noteIndex;
}
continue;
}
}
var oldvalue = {};
oldvalue.position = item[1].position;
oldvalue["first-reference-note-number"] = item[1]["first-reference-note-number"];
oldvalue["near-note"] = item[1]["near-note"];
item[1]["first-reference-note-number"] = 0;
item[1]["near-note"] = false;
if (this.registry.citationreg.citationsByItemId[myid]) {
if (this.opt.xclass === 'note' && this.opt.has_disambiguate) {
var oldCount = this.registry.registry[myid]["citation-count"]
var newCount = this.registry.citationreg.citationsByItemId[myid].length;
this.registry.registry[myid]["citation-count"] = this.registry.citationreg.citationsByItemId[myid].length;
if ("number" === typeof oldCount) {
var oldCountCheck = (oldCount < 2);
var newCountCheck = (newCount < 2);
if (oldCountCheck !== newCountCheck) {
for (var l=0,llen=this.registry.citationreg.citationsByItemId[myid].length;l<llen;l++) {
rerunAkeys[this.registry.registry[myid].ambig] = true;
this.tmp.taintedCitationIDs[this.registry.citationreg.citationsByItemId[myid][l].citationID] = true;
}
}
} else {
for (var l=0,llen=this.registry.citationreg.citationsByItemId[myid].length;l<llen;l++) {
rerunAkeys[this.registry.registry[myid].ambig] = true;
this.tmp.taintedCitationIDs[this.registry.citationreg.citationsByItemId[myid][l].citationID] = true;
}
}
}
}
var oldlastid;
if ("undefined" === typeof first_ref[myid]) {
first_ref[myid] = onecitation.properties.noteIndex;
if (this.registry.registry[myid]) {
this.registry.registry[myid]['first-reference-note-number'] = onecitation.properties.noteIndex;
}
last_ref[myid] = onecitation.properties.noteIndex;
item[1].position = CSL.POSITION_FIRST;
} else {
//
// backward-looking position evaluation happens here.
//
//
//
var ibidme = false;
var suprame = false;
// XXX Ugly, but This is used in the second else-if branch condition below.
if (j > 0) {
try {
oldlastid = citations[j - 1].sortedItems.slice(-1)[0][1].id;
} catch (e) {
var err = "CSL Error\n";
err += " " + e;
err += " in citation object " + citations[j - 1].citationID + " at index " + (j - 1);
throw err;
}
if (citations[j - 1].sortedItems[0].slice(-1)[0].legislation_id) {
oldlastid = citations[j - 1].sortedItems[0].slice(-1)[0].legislation_id;
}
}
if (j > 0 && parseInt(k, 10) === 0 && citations[j - 1].properties.noteIndex !== citations[j].properties.noteIndex) {
// Case 1: source in previous onecitation
// (1) Threshold conditions
// (a) there must be a previous onecitation with one item
// (b) this item must be the first in this onecitation
// (c) the previous onecitation must contain a reference
// to the same item ...
// (d) the note numbers must be the same or consecutive.
// (this has some jiggery-pokery in it for parallels)
var items = citations[(j - 1)].sortedItems;
var useme = false;
// XXX Can oldid be equated with oldlastid, I wonder ...
var oldid = citations[j - 1].sortedItems[0][0].id;
if (citations[j - 1].sortedItems[0][0].legislation_id) {
oldid = citations[j - 1].sortedItems[0][0].legislation_id;
}
if ((oldid == myid && citations[j - 1].properties.noteIndex >= (citations[j].properties.noteIndex - 1)) || citations[j - 1].sortedItems[0][1].id == this.registry.registry[item[1].id].parallel) {
if (citationsInNote[citations[j - 1].properties.noteIndex] === 1 || citations[j - 1].properties.noteIndex === 0) {
useme = true;
}
}
for (n = 0, nlen = items.slice(1).length; n < nlen; n += 1) {
var itmp = items.slice(1)[n];
// XXXXX: This test can't be right. parallel stores an ID ... ?
if (!this.registry.registry[itmp[1].id].parallel || this.registry.registry[itmp[1].id].parallel == this.registry.registry[itmp[1].id]) {
// Does fire in some tests, as a matching undefined
// No apparent side effects of turning it off, though.
// For future consideration.
useme = false;
}
}
if (useme) {
ibidme = true;
} else {
suprame = true;
}
} else if (k > 0 && incitationid == myid) {
// Case 2: immediately preceding source in this onecitation
// (1) Threshold conditions
// (a) there must be an imediately preceding reference to the
// same item in this onecitation; and
ibidme = true;
} else if (k === 0 && citations[j - 1].properties.noteIndex == citations[j].properties.noteIndex
&& citations[j - 1].sortedItems.length
&& oldlastid == myid) {
// ... in case there are separate citations in the same note ...
// Case 2 [take 2]: immediately preceding source in this onecitation
// (1) Threshold conditions
// (a) there must be an imediately preceding reference to the
// same item in this onecitation; and
ibidme = true;
} else {
// everything else is definitely subsequent
suprame = true;
}
// conditions
var prev, prev_locator, prev_label, curr_locator, curr_label;
if (ibidme) {
if (k > 0) {
prev = onecitation.sortedItems[(k - 1)][1];
} else {
prev = citations[(j - 1)].sortedItems[0][1];
}
if (prev.locator) {
if (prev.label) {
prev_label = prev.label;
} else {
prev_label = "";
}
prev_locator = "" + prev.locator + prev_label;
} else {
prev_locator = prev.locator;
}
if (mylocator) {
if (mylabel) {
curr_label = mylabel;
} else {
curr_label = "";
}
curr_locator = "" + mylocator + curr_label;
} else {
curr_locator = mylocator;
}
}
// triage
if (ibidme && prev_locator && !curr_locator) {
ibidme = false;
suprame = true;
}
if (ibidme) {
if (!prev_locator && curr_locator) {
// (a) if the previous onecitation had no locator
// and this onecitation has one, use ibid+pages
item[1].position = CSL.POSITION_IBID_WITH_LOCATOR;
} else if (!prev_locator && !curr_locator) {
// (b) if the previous onecitation had no locator
// and this onecitation also has none, use ibid
item[1].position = CSL.POSITION_IBID;
//print("setting ibid in cmd_cite()");
} else if (prev_locator && curr_locator === prev_locator) {
// (c) if the previous onecitation had a locator
// (page number, etc.) and this onecitation has
// a locator that is identical, use ibid
item[1].position = CSL.POSITION_IBID;
//print("setting ibid in cmd_cite() [2]");
} else if (prev_locator && curr_locator && curr_locator !== prev_locator) {
// (d) if the previous onecitation had a locator,
// and this onecitation has one that differs,
// use ibid+pages
item[1].position = CSL.POSITION_IBID_WITH_LOCATOR;
} else {
// (e) if the previous onecitation had a locator
// and this onecitation has none, use subsequent
//
// ... and everything else would be subsequent also
ibidme = false; // just to be clear
suprame = true;
}
}
if (suprame) {
item[1].position = CSL.POSITION_SUBSEQUENT;
}
if (suprame || ibidme) {
if (first_ref[myid] != onecitation.properties.noteIndex) {
item[1]["first-reference-note-number"] = first_ref[myid];
if (this.registry.registry[myid]) {
// This is either the earliest recorded number, or the number of the current citation, whichever is smaller.
var oldFirst = this.registry.citationreg.citationsByItemId[myid][0].properties.noteIndex;
var newFirst = onecitation.properties.noteIndex;
this.registry.registry[myid]['first-reference-note-number'] = newFirst < oldFirst ? newFirst: oldFirst;
}
}
}
}
if (onecitation.properties.noteIndex) {
var note_distance = parseInt(onecitation.properties.noteIndex, 10) - parseInt(last_ref[myid], 10);
if (item[1].position !== CSL.POSITION_FIRST
&& note_distance <= this.citation.opt["near-note-distance"]) {
item[1]["near-note"] = true;
}
last_ref[myid] = onecitation.properties.noteIndex;
}
if (onecitation.citationID != citation.citationID) {
for (n = 0, nlen = CSL.POSITION_TEST_VARS.length; n < nlen; n += 1) {
var param = CSL.POSITION_TEST_VARS[n];
if (item[1][param] !== oldvalue[param]) {
if (this.registry.registry[myid]) {
if (param === 'first-reference-note-number') {
rerunAkeys[this.registry.registry[myid].ambig] = true;
this.tmp.taintedItemIDs[myid] = true;
}
}
this.tmp.taintedCitationIDs[onecitation.citationID] = true;
}
}
}
if (this.sys.variableWrapper) {
item[1].index = onecitation.properties.index;
item[1].noteIndex = onecitation.properties.noteIndex;
}
}
}
}
}
if (this.opt.citation_number_sort && sortedItems && sortedItems.length > 1 && this.citation_sort.tokens.length > 0) {
if (!citation.properties.unsorted) {
for (var i = 0, ilen = sortedItems.length; i < ilen; i += 1) {
sortedItems[i][1].sortkeys = CSL.getSortKeys.call(this, sortedItems[i][0], "citation_sort");
}
sortedItems.sort(this.citation.srt.compareCompositeKeys);
}
}
for (var key in this.tmp.taintedItemIDs) {
if (this.tmp.taintedItemIDs.hasOwnProperty(key)) {
citations = this.registry.citationreg.citationsByItemId[key];
// Current citation may be tainted but will not exist
// during previewing.
if (citations) {
for (var i = 0, ilen = citations.length; i < ilen; i += 1) {
this.tmp.taintedCitationIDs[citations[i].citationID] = true;
}
}
}
}
var ret = [];
if (flag === CSL.PREVIEW) {
// If previewing, return only a rendered string
//SNIP-START
if (this.debug) {
CSL.debug("****** start run processor *********");
}
//SNIP-END
try {
ret = this.process_CitationCluster.call(this, citation.sortedItems, citation.citationID);
} catch (e) {
CSL.error("Error running CSL processor for preview: "+e);
}
//SNIP-START
if (this.debug) {
CSL.debug("****** end run processor *********");
CSL.debug("****** start state restore *********");
}
//SNIP-END
// Wind out anything related to new items added for the preview.
// This means (1) names, (2) disambig state for affected items,
// (3) keys registered in the ambigs pool arrays, and (4) registry
// items.
//
// restore sliced citations
this.registry.citationreg.citationByIndex = oldCitationList;
this.registry.citationreg.citationById = {};
for (var i = 0, ilen = oldCitationList.length; i < ilen; i += 1) {
this.registry.citationreg.citationById[oldCitationList[i].citationID] = oldCitationList[i];
}
//SNIP-START
if (this.debug) {
CSL.debug("****** start final update *********");
}
//SNIP-END
var oldItemIds = [];
for (var i = 0, ilen = oldItemList.length; i < ilen; i += 1) {
oldItemIds.push("" + oldItemList[i].id);
}
this.updateItems(oldItemIds, null, null, true);
//SNIP-START
if (this.debug) {
CSL.debug("****** end final update *********");
}
//SNIP-END
// Roll back disambig states
for (var key in oldAmbigs) {
if (oldAmbigs.hasOwnProperty(key)) {
this.registry.registry[key].disambig = oldAmbigs[key];
}
}
//SNIP-START
if (this.debug) {
CSL.debug("****** end state restore *********");
}
//SNIP-END
} else {
// Rerun cites that have moved across citations or had a change
// in their number of subsequent references, so that disambiguate
// and subsequent-reference-count conditions are applied
// correctly in output.
for (var rerunAkey in rerunAkeys) {
this.disambiguate.run(rerunAkey, citation);
}
// Run taints only if not previewing
//
// Push taints to the return object
//
var obj;
for (var key in this.tmp.taintedCitationIDs) {
if (key == citation.citationID) {
continue;
}
var mycitation = this.registry.citationreg.citationById[key];
if (!mycitation.properties.unsorted) {
for (var i = 0, ilen = mycitation.sortedItems.length; i < ilen; i += 1) {
mycitation.sortedItems[i][1].sortkeys = CSL.getSortKeys.call(this, mycitation.sortedItems[i][0], "citation_sort");
}
mycitation.sortedItems.sort(this.citation.srt.compareCompositeKeys);
}
// For error reporting
this.tmp.citation_pos = mycitation.properties.index;
this.tmp.citation_note_index = mycitation.properties.noteIndex;
this.tmp.citation_id = "" + mycitation.citationID;
obj = [];
obj.push(mycitation.properties.index);
obj.push(this.process_CitationCluster.call(this, mycitation.sortedItems, mycitation.citationID));
obj.push(mycitation.citationID);
ret.push(obj);
}
this.tmp.taintedItemIDs = {};
this.tmp.taintedCitationIDs = {};
// For error reporting again
this.tmp.citation_pos = citation.properties.index;
this.tmp.citation_note_index = citation.properties.noteIndex;
this.tmp.citation_id = "" + citation.citationID;
obj = [];
obj.push(citationsPre.length);
obj.push(this.process_CitationCluster.call(this, sortedItems, citation.citationID));
obj.push(citation.citationID);
ret.push(obj);
//
// note for posterity: Rhino and Spidermonkey produce different
// sort results for items with matching keys. That discrepancy
// turned up a subtle bug in the parallel detection code, trapped
// at line 266, above, and in line 94 of util_parallel.js.
//
ret.sort(function (a, b) {
if (a[0] > b[0]) {
return 1;
} else if (a[0] < b[0]) {
return -1;
} else {
return 0;
}
});
//
// In normal rendering, return is a list of two-part arrays, with the first element
// a citation index number, and the second the text to be inserted.
//
}
return_data.citation_errors = this.tmp.citation_errors.slice();
return [return_data, ret];
};
CSL.Engine.prototype.process_CitationCluster = function (sortedItems, citationID) {
var str;
this.parallel.StartCitation(sortedItems);
str = CSL.getCitationCluster.call(this, sortedItems, citationID);
return str;
};
CSL.Engine.prototype.makeCitationCluster = function (rawList) {
var inputList, newitem, str, pos, len, item, Item;
inputList = [];
len = rawList.length;
for (pos = 0; pos < len; pos += 1) {
item = {};
for (var key in rawList[pos]) {
item[key] = rawList[pos][key];
}
Item = this.retrieveItem("" + item.id);
// Code block is copied from processCitationCluster() above
if (this.opt.development_extensions.locator_label_parse) {
if (item.locator && ["bill","gazette","legislation","regulation","treaty"].indexOf(Item.type) === -1 && (!item.label || item.label === 'page')) {
var m = CSL.LOCATOR_LABELS_REGEXP.exec(item.locator);
if (m) {
var tryLabel = CSL.LOCATOR_LABELS_MAP[m[2]];
if (this.getTerm(tryLabel)) {
item.label = tryLabel;
item.locator = m[3];
}
}
}
}
if (item.locator) {
item.locator = ("" + item.locator).replace(/\s+$/, '');
}
newitem = [Item, item];
inputList.push(newitem);
}
if (this.opt.development_extensions.static_statute_locator) {
this.remapSectionVariable(inputList);
}
if (inputList && inputList.length > 1 && this.citation_sort.tokens.length > 0) {
len = inputList.length;
for (pos = 0; pos < len; pos += 1) {
inputList[pos][1].sortkeys = CSL.getSortKeys.call(this, inputList[pos][0], "citation_sort");
}
inputList.sort(this.citation.srt.compareCompositeKeys);
}
this.tmp.citation_errors = [];
this.parallel.StartCitation(inputList);
str = CSL.getCitationCluster.call(this, inputList);
return str;
};
/**
* Get the undisambiguated version of a cite, without decorations
* <p>This is used internally by the Registry.</p>
*
* [object] CSL Item
* [object] disambiguation parameters
* [boolean] If true, include first-reference-note-number value in cite
*/
CSL.getAmbiguousCite = function (Item, disambig, visualForm, item) {
var use_parallels, ret;
var flags = this.tmp.group_context.tip;
var oldTermSiblingLayer = {
term_intended: flags.term_intended,
variable_attempt: flags.variable_attempt,
variable_success: flags.variable_success,
output_tip: flags.output_tip,
label_form: flags.label_form,
parallel_conditions: flags.parallel_conditions,
condition: flags.condition,
force_suppress: flags.force_suppress,
done_vars: flags.done_vars.slice()
}
if (disambig) {
this.tmp.disambig_request = disambig;
} else {
this.tmp.disambig_request = false;
}
var itemSupp = {
position: 1,
"near-note": true
};
if (item) {
itemSupp.locator = item.locator;
itemSupp.label = item.label;
}
if (this.registry.registry[Item.id]
&& this.registry.citationreg.citationsByItemId
&& this.registry.citationreg.citationsByItemId[Item.id]
&& this.registry.citationreg.citationsByItemId[Item.id].length
&& visualForm) {
if (this.citation.opt["givenname-disambiguation-rule"] === "by-cite") {
itemSupp['first-reference-note-number'] = this.registry.registry[Item.id]['first-reference-note-number'];
}
}
this.tmp.area = "citation";
this.tmp.root = "citation";
this.parallel.use_parallels = (this.parallel.use_parallels === true || this.parallel.use_parallels === null) ? null : false;
this.tmp.suppress_decorations = true;
this.tmp.just_looking = true;
// Do not reset shadow_numbers when running ambiguous cites
CSL.getCite.call(this, Item, itemSupp, null, false);
// !!!
for (var i=0,ilen=this.output.queue.length;i<ilen;i+=1) {
CSL.Output.Queue.purgeEmptyBlobs(this.output.queue[i]);
}
if (this.opt.development_extensions.clean_up_csl_flaws) {
for (var j=0,jlen=this.output.queue.length;j<jlen;j+=1) {
this.output.adjust.upward(this.output.queue[j]);
this.output.adjust.leftward(this.output.queue[j]);
this.output.adjust.downward(this.output.queue[j]);
this.output.adjust.fix(this.output.queue[j]);
}
}
var ret = this.output.string(this, this.output.queue);
this.tmp.just_looking = false;
this.tmp.suppress_decorations = false;
this.parallel.use_parallels = this.parallel.use_parallels === null ? true : false;
// Cache the result.
this.tmp.group_context.replace(oldTermSiblingLayer);
return ret;
};
/**
* Return delimiter for use in join
* <p>Splice evaluation is done during cite
* rendering, and this method returns the
* result. Evaluation requires three items
* of information from the preceding cite, if
* one is present: the names used; the years
* used; and the suffix appended to the
* citation. These details are copied into
* the state object before processing begins,
* and are cleared by the processor on
* completion of the run.</p>
*/
CSL.getSpliceDelimiter = function (last_locator, last_collapsed, pos) {
//print(pos + " after-collapse-delimiter="+this.citation.opt["after-collapse-delimiter"] + "\n cite_group_delimiter=" + this.tmp.use_cite_group_delimiter + "\n last_collapsed=" +last_collapsed + "\n have_collapsed=" +this.tmp.have_collapsed + "\n last_locator=" + last_locator)
if (undefined !== this.citation.opt["after-collapse-delimiter"]) {
if (last_locator) {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else if (last_collapsed && !this.tmp.have_collapsed) {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else if (!last_collapsed && !this.tmp.have_collapsed && this.citation.opt.collapse !== "year-suffix") {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else {
this.tmp.splice_delimiter = this.citation.opt.layout_delimiter;
}
} else if (this.tmp.use_cite_group_delimiter) {
this.tmp.splice_delimiter = this.citation.opt.cite_group_delimiter;
} else {
if (this.tmp.have_collapsed && this.opt.xclass === "in-text" && this.opt.update_mode !== CSL.NUMERIC) {
this.tmp.splice_delimiter = ", ";
} else if (this.tmp.cite_locales[pos - 1]) {
//
// Must have a value to take effect. Use zero width space to force empty delimiter.
var alt_affixes = this.tmp.cite_affixes[this.tmp.area][this.tmp.cite_locales[pos - 1]];
if (alt_affixes && alt_affixes.delimiter) {
this.tmp.splice_delimiter = alt_affixes.delimiter;
}
} else if (!this.tmp.splice_delimiter) {
// This happens when no delimiter is set on cs:layout under cs:citation
this.tmp.splice_delimiter = "";
}
}
/*
if (last_locator && "string" === typeof this.citation.opt["after-collapse-delimiter"]) {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else if (last_collapsed && !this.tmp.have_collapsed && "string" === typeof this.citation.opt["after-collapse-delimiter"]) {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else if (!last_collapsed && !this.tmp.have_collapsed && "string" === typeof this.citation.opt["after-collapse-delimiter"] && !this.citation.opt.collapse === "year-suffix") {
this.tmp.splice_delimiter = this.citation.opt["after-collapse-delimiter"];
} else if (this.tmp.use_cite_group_delimiter) {
this.tmp.splice_delimiter = this.citation.opt.cite_group_delimiter;
} else if (this.tmp.have_collapsed && this.opt.xclass === "in-text" && this.opt.update_mode !== CSL.NUMERIC) {
this.tmp.splice_delimiter = ", ";
} else if (this.tmp.cite_locales[pos - 1]) {
//
// Must have a value to take effect. Use zero width space to force empty delimiter.
var alt_affixes = this.tmp.cite_affixes[this.tmp.area][this.tmp.cite_locales[pos - 1]];
if (alt_affixes && alt_affixes.delimiter) {
this.tmp.splice_delimiter = alt_affixes.delimiter;
}
} else if (!this.tmp.splice_delimiter) {
// This happens when no delimiter is set on cs:layout under cs:citation
this.tmp.splice_delimiter = "";
}
*/
// Paranoia
//if (!this.tmp.splice_delimiter) {
// this.tmp.splice_delimiter = "";
//}
return this.tmp.splice_delimiter;
};
/*
* Compose individual cites into a single string, with
* flexible inter-cite splicing.
*/
CSL.getCitationCluster = function (inputList, citationID) {
var result, objects, myparams, len, pos, item, last_collapsed, params, empties, composite, compie, myblobs, Item, llen, ppos, obj, preceding_item, txt_esc, error_object;
inputList = inputList ? inputList : [];
this.tmp.last_primary_names_string = false;
txt_esc = CSL.getSafeEscape(this);
this.tmp.area = "citation";
this.tmp.root = "citation";
result = "";
objects = [];
this.tmp.last_suffix_used = "";
this.tmp.last_names_used = [];
this.tmp.last_years_used = [];
this.tmp.backref_index = [];
this.tmp.cite_locales = [];
this.output.checkNestedBrace = new CSL.checkNestedBrace(this);
var use_layout_prefix = this.output.checkNestedBrace.update(this.citation.opt.layout_prefix);
//var use_layout_prefix = this.citation.opt.layout_prefix;
var suppressTrailingPunctuation = false;
if (this.opt.xclass === "note" && this.citation.opt.suppressTrailingPunctuation) {
suppressTrailingPunctuation = true;
}
if (citationID) {
//this.registry.citationreg.citationById[citationID].properties.backref_index = false;
//this.registry.citationreg.citationById[citationID].properties.backref_citation = false;
if (this.registry.citationreg.citationById[citationID].properties["suppress-trailing-punctuation"]) {
suppressTrailingPunctuation = true;
}
}
// Adjust locator positions if that looks like a sensible thing to do.
if (this.opt.xclass === "note") {
var parasets = [];