-
Notifications
You must be signed in to change notification settings - Fork 10
/
ir.rs
582 lines (542 loc) · 19.1 KB
/
ir.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright © 2018 Corporation for Digital Scholarship
use crate::prelude::*;
use citeproc_io::output::markup::Markup;
use citeproc_io::output::LocalizedQuotes;
use csl::{Affixes, Choose, DateVariable, Formatting, GivenNameDisambiguationRule, TextElement};
use csl::{NumberVariable, StandardVariable, Variable};
use std::sync::Arc;
pub mod transforms;
pub type IrSum<O> = (IR<O>, GroupVars);
// Intermediate Representation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IR<O: OutputFormat = Markup> {
// no (further) disambiguation possible
Rendered(Option<CiteEdgeData<O>>),
// the name block,
Name(NameIR<O>),
Substitute,
/// a single <if disambiguate="true"> being tested once means the whole <choose> is re-rendered in step 4
/// or <choose><if><conditions><condition>
/// Should also include `if variable="year-suffix"` because that could change.
ConditionalDisamb(ConditionalDisambIR),
YearSuffix(YearSuffix),
// Think:
// <if disambiguate="true" ...>
// <text macro="..." />
// <text macro="..." />
// <text variable="year-suffix" />
// <text macro="..." />
// </if>
// = Seq[
// Rendered(...), // collapsed multiple nodes into one rendered
// YearSuffix(Explicit(Text(Variable::YearSuffix), T)),
// Rendered(..)
// ]
Seq(IrSeq),
/// Only exists to aggregate the counts of names
NameCounter(IrNameCounter<O>),
}
/// Simplified output that's more readable
impl<O: OutputFormat> std::fmt::Display for IR<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
IR::Name(_) => write!(f, "Name"),
IR::Seq(seq) => {
let mut dbg = f.debug_struct("Seq");
if let Some(pre) = seq.affixes.as_ref().map(|x| x.prefix.as_str()) {
dbg.field("prefix", &pre);
}
if let Some(delim) = seq.delimiter.as_ref() {
dbg.field("delimiter", &delim);
}
if let Some(suf) = seq.affixes.as_ref().map(|x| x.suffix.as_str()) {
dbg.field("suffix", &suf);
}
dbg.finish()
}
IR::Substitute => write!(f, "Substitute"),
IR::ConditionalDisamb(_) => write!(f, "ConditionalDisamb"),
IR::NameCounter(_) => write!(f, "NameCounter"),
IR::Rendered(None) => write!(f, "<empty>"),
IR::Rendered(Some(data)) => write!(f, "{:?}", data.build()),
IR::YearSuffix(_) => write!(f, "YearSuffix"),
}
}
}
/// # Disambiguation and group_vars
///
/// IrSeq needs to hold things
#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct IrSeq {
pub formatting: Option<Formatting>,
pub affixes: Option<Affixes>,
pub delimiter: Option<SmartString>,
pub display: Option<DisplayMode>,
pub quotes: Option<LocalizedQuotes>,
pub text_case: TextCase,
/// If this is None, this sequence is simply an implicit conditional
pub dropped_gv: Option<GroupVars>,
pub should_inherit_delim: bool,
pub is_layout: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DisambPass {
AddNames,
AddGivenName(GivenNameDisambiguationRule),
AddYearSuffix(u32),
Conditionals,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct YearSuffix {
// Has IR child.
// Clone element into here, because we already know it's a <text variable="" />
pub(crate) hook: YearSuffixHook,
pub(crate) suffix_num: Option<u32>,
}
impl<O: OutputFormat> IR<O> {
pub(crate) fn year_suffix(hook: YearSuffixHook) -> IrSum<O> {
(
IR::YearSuffix(YearSuffix {
hook,
suffix_num: None,
}),
GroupVars::Unresolved,
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum YearSuffixHook {
// Clone element into here, because we already know it's a <text variable="" />
Explicit(TextElement),
Plain,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum CiteEdgeData<O: OutputFormat = Markup> {
/// Used for [IR::leading_names_block_or_title]
Title(O::Build),
Output(O::Build),
Locator(O::Build),
LocatorLabel(O::Build),
/// Used for representing a YearSuffix that has actually been rendered during disambiguation.
YearSuffix(O::Build),
CitationNumber(O::Build),
CitationNumberLabel(O::Build),
Frnn(O::Build),
FrnnLabel(O::Build),
/// Accessed isn't really part of a reference -- it doesn't help disambiguating one from
/// another. So we will ignore it. Works for, e.g., date_YearSuffixImplicitWithNoDate.txt
Accessed(O::Build),
Year(O::Build),
Term(O::Build),
}
impl<O: OutputFormat> CiteEdgeData<O> {
pub fn from_number_variable(var: NumberVariable, label: bool) -> fn(O::Build) -> Self {
match (var, label) {
(NumberVariable::Locator, false) => CiteEdgeData::Locator,
(NumberVariable::Locator, true) => CiteEdgeData::LocatorLabel,
(NumberVariable::FirstReferenceNoteNumber, false) => CiteEdgeData::Frnn,
(NumberVariable::FirstReferenceNoteNumber, true) => CiteEdgeData::FrnnLabel,
(NumberVariable::CitationNumber, false) => CiteEdgeData::CitationNumber,
(NumberVariable::CitationNumber, true) => CiteEdgeData::CitationNumberLabel,
_ => CiteEdgeData::Output,
}
}
pub fn from_ordinary_variable(var: Variable) -> fn(O::Build) -> Self {
match var {
Variable::YearSuffix => CiteEdgeData::YearSuffix,
Variable::Title => CiteEdgeData::Title,
Variable::TitleShort => CiteEdgeData::Title,
_ => CiteEdgeData::Output,
}
}
pub fn from_standard_variable(var: StandardVariable, label: bool) -> fn(O::Build) -> Self {
match var {
StandardVariable::Number(nv) => CiteEdgeData::from_number_variable(nv, label),
StandardVariable::Ordinary(v) => CiteEdgeData::from_ordinary_variable(v),
}
}
pub fn from_date_variable(var: DateVariable) -> fn(O::Build) -> Self {
match var {
DateVariable::Accessed => CiteEdgeData::Accessed,
_ => CiteEdgeData::Output,
}
}
}
use crate::disamb::names::NameIR;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConditionalDisambIR {
// Has IR children
pub choose: Arc<Choose>,
pub done: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IrNameCounter<O: OutputFormat> {
pub name_irs: Vec<NameIR<O>>,
pub group_vars: GroupVars,
}
impl<O: OutputFormat> IrNameCounter<O> {
pub fn count<I: OutputFormat>(&self, ctx: &CiteContext<'_, O, I>) -> u32 {
self.name_irs.iter().map(|nir| nir.count(ctx)).sum()
}
pub fn render_cite<I: OutputFormat>(&self, ctx: &CiteContext<'_, O, I>) -> IrSum<O> {
let fmt = &ctx.format;
let count = self.count(ctx);
let built = if ctx.sort_key.is_some() {
fmt.affixed_text(
smart_format!("{:08}", count),
None,
Some(&crate::sort::natural_sort::num_affixes()),
)
} else {
// This isn't sort-mode, you can render NameForm::Count as text.
fmt.text_node(smart_format!("{}", count), None)
};
(
IR::Rendered(Some(CiteEdgeData::Output(built))),
GroupVars::Important,
)
}
}
// impl<O> Eq for IR<O> where O: OutputFormat + PartialEq + Eq {}
impl<O> IR<O>
where
O: OutputFormat + PartialEq,
{
#[allow(dead_code)]
fn deep_equals(
&self,
_self_gv: GroupVars,
self_id: NodeId,
self_arena: &IrArena<O>,
other: &Self,
_other_gv: GroupVars,
other_id: NodeId,
other_arena: &IrArena<O>,
) -> bool {
match (self, other) {
(IR::Rendered(a), IR::Rendered(b)) if a == b => return true,
(IR::Seq(a), IR::Seq(b)) if a == b => {}
(IR::YearSuffix(a), IR::YearSuffix(b)) if a == b => {}
(IR::ConditionalDisamb(a), IR::ConditionalDisamb(b)) if a == b => {}
(IR::Name(a), IR::Name(b)) if a == b => {}
(IR::Substitute, IR::Substitute) => {}
_ => return false,
}
self_id
.children(self_arena)
.zip(other_id.children(other_arena))
.all(|(a, b)| {
let (ai, ag) = self_arena.get(a).unwrap().get();
let (bi, bg) = other_arena.get(b).unwrap().get();
ai.deep_equals(*ag, a, self_arena, bi, *bg, b, other_arena)
})
}
}
impl<O: OutputFormat> Default for IR<O> {
fn default() -> Self {
IR::Rendered(None)
}
}
impl<O: OutputFormat<Output = SmartString>> CiteEdgeData<O> {
pub(crate) fn to_edge_data(&self, fmt: &O, formatting: Formatting) -> EdgeData {
match self {
CiteEdgeData::Output(x)
| CiteEdgeData::Title(x)
| CiteEdgeData::Year(x)
| CiteEdgeData::Term(x) => {
EdgeData::Output(fmt.output_in_context(x.clone(), formatting, None))
}
CiteEdgeData::YearSuffix(_) => EdgeData::YearSuffix,
CiteEdgeData::Frnn(_) => EdgeData::Frnn,
CiteEdgeData::FrnnLabel(_) => EdgeData::FrnnLabel,
CiteEdgeData::Locator(_) => EdgeData::Locator,
CiteEdgeData::LocatorLabel(_) => EdgeData::LocatorLabel,
CiteEdgeData::CitationNumber(_) => EdgeData::CitationNumber,
CiteEdgeData::CitationNumberLabel(_) => EdgeData::CitationNumberLabel,
CiteEdgeData::Accessed(_) => EdgeData::Accessed,
}
}
}
impl<O: OutputFormat> CiteEdgeData<O> {
pub(crate) fn inner(&self) -> O::Build {
self.build().clone()
}
fn build(&self) -> &O::Build {
match self {
Self::Title(b)
| Self::Output(b)
| Self::Locator(b)
| Self::LocatorLabel(b)
| Self::YearSuffix(b)
| Self::CitationNumber(b)
| Self::CitationNumberLabel(b)
| Self::Frnn(b)
| Self::FrnnLabel(b)
| Self::Accessed(b)
| Self::Year(b)
| Self::Term(b) => b,
}
}
}
impl IR<Markup> {
fn append_edges(
node: NodeId,
arena: &IrArena<Markup>,
edges: &mut Vec<EdgeData>,
fmt: &Markup,
formatting: Formatting,
inherit_delim: Option<&str>,
) {
let me = match arena.get(node) {
Some(x) => x.get(),
None => return,
};
let tree = IrTreeRef { node, arena };
match &me.0 {
IR::Rendered(None) => {}
IR::Rendered(Some(ed)) => edges.push(ed.to_edge_data(fmt, formatting)),
IR::YearSuffix(_ys) => {
if !tree.is_empty() {
edges.push(EdgeData::YearSuffix);
}
}
IR::Name(_) | IR::NameCounter(_) | IR::Substitute => {
IR::append_child_edges(node, arena, edges, fmt, formatting, None)
}
// Inherit the delimiter here.
IR::ConditionalDisamb(_) => {
IR::append_child_edges(node, arena, edges, fmt, formatting, inherit_delim)
}
IR::Seq(seq) => {
if IrSeq::overall_group_vars(seq.dropped_gv, tree)
.map_or(true, |x| x.should_render_tree())
{
seq.append_edges(node, arena, edges, fmt, formatting, inherit_delim)
}
}
}
}
fn append_child_edges(
node: NodeId,
arena: &IrArena<Markup>,
edges: &mut Vec<EdgeData>,
fmt: &Markup,
formatting: Formatting,
inherit_delim: Option<&str>,
) {
for child in node.children(arena) {
IR::append_edges(child, arena, edges, fmt, formatting, inherit_delim);
}
}
}
// impl<'a> From<&'a CiteEdgeData> for EdgeData {
// fn from(cite_edge: &CiteEdgeData) -> Self {
// match cite_edge {
// CiteEdgeData::Output(x) => EdgeData::Output(x.clone()),
// CiteEdgeData::YearSuffix(_) => EdgeData::YearSuffix,
// CiteEdgeData::Frnn(_) => EdgeData::Frnn,
// CiteEdgeData::Locator(_) => EdgeData::Locator,
// CiteEdgeData::CitationNumber(_) => EdgeData::CitationNumber,
// }
// }
// }
impl IrSeq {
pub(crate) fn overall_group_vars<O: OutputFormat>(
dropped_gv: Option<GroupVars>,
tree: IrTreeRef<O>,
) -> Option<GroupVars> {
dropped_gv.map(|dropped| {
let acc = tree.children().fold(dropped, |acc, child| {
let gv = child.get_node().unwrap().get().1;
acc.neighbour(gv)
});
// Replicate GroupVars::implicit_conditional
if acc != GroupVars::Missing {
GroupVars::Important
} else {
GroupVars::Plain
}
})
}
}
/// Currently, flattening into EdgeData(String) only works when the Output type is String
/// So Pandoc isn't ready yet; maybe you can flatten Pandoc structure into a string.
impl<'a, O: OutputFormat<Output = SmartString>> IrTreeRef<'a, O> {
pub(crate) fn flatten_or_plain(&self, fmt: &O, if_empty: &str) -> O::Build {
self.flatten(fmt, None)
.unwrap_or_else(|| fmt.plain(if_empty))
}
/// Assumes any group vars have been resolved, so every item touched by flatten should in fact
/// be rendered
pub(crate) fn flatten(&self, fmt: &O, override_delim: Option<&str>) -> Option<O::Build> {
// must clone
match self.arena.get(self.node)?.get().0 {
IR::Rendered(None) => None,
IR::Rendered(Some(ref x)) => Some(x.inner()),
IR::ConditionalDisamb(_) => self.flatten_children(fmt, override_delim),
IR::YearSuffix(_) | IR::NameCounter(_) | IR::Name(_) | IR::Substitute => {
self.flatten_children(fmt, None)
}
IR::Seq(ref seq) => seq.flatten_seq(*self, fmt, override_delim),
}
}
pub(crate) fn flatten_children(
&self,
fmt: &O,
override_delim: Option<&str>,
) -> Option<O::Build> {
let mut group = Vec::new();
for child in self
.children()
.filter_map(|child| child.flatten(fmt, override_delim))
{
group.push(child)
}
if group.is_empty() {
return None;
}
Some(fmt.group(group, "", None))
}
}
impl<'a> IrTreeRef<'a, Markup> {
pub fn to_edge_stream(&self, fmt: &Markup) -> Vec<EdgeData> {
let mut edges = Vec::new();
IR::append_edges(
self.node,
self.arena,
&mut edges,
fmt,
Formatting::default(),
None,
);
edges
}
}
impl IrSeq {
// TODO: Groupvars
fn flatten_seq<O: OutputFormat<Output = SmartString>>(
&self,
tree: IrTreeRef<O>,
fmt: &O,
override_delim: Option<&str>,
) -> Option<O::Build> {
// Do this where it won't require mut access
// self.recompute_group_vars();
if !IrSeq::overall_group_vars(self.dropped_gv, tree)
.map_or(true, |x| x.should_render_tree())
{
return None;
}
let IrSeq {
formatting,
ref delimiter,
ref affixes,
ref quotes,
display,
text_case,
dropped_gv: _,
should_inherit_delim,
is_layout: _,
} = *self;
let xs: Vec<_> = tree
.children()
.filter_map(|child| child.flatten(fmt, delimiter.as_opt_str()))
.collect();
if xs.is_empty() {
return None;
}
let delim = override_delim
.filter(|_| should_inherit_delim)
.or(delimiter.as_opt_str())
.unwrap_or("");
let grp = fmt.group(xs, delim, formatting);
let grp = fmt.affixed_quoted(grp, affixes.as_ref(), quotes.clone());
// TODO: pass in_bibliography from ctx
let mut grp = fmt.with_display(grp, display, true);
fmt.apply_text_case(
&mut grp,
&IngestOptions {
text_case,
..Default::default()
},
);
Some(grp)
}
fn append_edges(
&self,
node: NodeId,
arena: &IrArena<Markup>,
edges: &mut Vec<EdgeData>,
fmt: &Markup,
format_context: Formatting,
override_delim: Option<&str>,
) {
// Currently recreates the whole markup-formatting infrastructure, but keeps the same
// granularity of edges that RefIR will produce.
if node.children(arena).next().is_none() {
return;
}
let IrSeq {
ref affixes,
ref delimiter,
formatting,
display,
// TODO: use these
quotes: _,
text_case: _,
dropped_gv: _,
should_inherit_delim,
is_layout: _,
} = *self;
let delimiter = override_delim
.filter(|_| should_inherit_delim)
.or(delimiter.as_opt_str());
let affixes = affixes.as_ref();
// TODO: move display out of tag_stack, so that quotes can go inside it.
// Damn those macros.
let stack = fmt.tag_stack(formatting.unwrap_or_else(Default::default), display);
let sub_formatting = formatting
.map(|mine| format_context.override_with(mine))
.unwrap_or(format_context);
let mut open_tags = SmartString::new();
let mut close_tags = SmartString::new();
fmt.stack_preorder(&mut open_tags, &stack);
fmt.stack_postorder(&mut close_tags, &stack);
if !affixes.map_or(true, |a| a.prefix.is_empty()) {
edges.push(EdgeData::Output(affixes.unwrap().prefix.as_str().into()));
}
if !open_tags.is_empty() {
edges.push(EdgeData::Output(open_tags));
}
// push the innards
let mut seen = false;
let mut sub = Vec::new();
for child in node.children(arena) {
IR::append_edges(child, arena, &mut sub, fmt, sub_formatting, delimiter);
if !sub.is_empty() {
if seen {
if let Some(delimiter) = delimiter {
edges.push(EdgeData::Output(fmt.output_in_context(
fmt.plain(delimiter),
sub_formatting,
None,
)));
}
} else {
seen = true;
}
edges.extend(sub.drain(..));
}
}
if !close_tags.is_empty() {
edges.push(EdgeData::Output(close_tags));
}
if !affixes.map_or(true, |a| a.suffix.is_empty()) {
edges.push(EdgeData::Output(affixes.unwrap().suffix.as_str().into()));
}
}
}