citum_engine/processor/bibliography/
mod.rs1mod compound;
13mod grouping;
14
15use super::matching::Matcher;
16use super::rendering::{CompoundRenderData, Renderer, RendererResources};
17use super::{ProcessedReferences, Processor};
18use crate::api::AnnotationStyle;
19use crate::reference::Reference;
20use crate::render::format::OutputFormat;
21use crate::render::{ProcEntry, ProcTemplate};
22use std::collections::{HashMap, HashSet};
23
24#[derive(Debug, Clone, Default)]
26pub(crate) struct RenderedBibliographyGroup {
27 pub(crate) heading: Option<String>,
29 pub(crate) body: String,
31}
32
33impl Processor {
34 fn with_bibliography_renderer<T>(&self, render: impl FnOnce(Renderer<'_>) -> T) -> T {
36 let bibliography_shared_config = self.get_bibliography_config();
37 let bibliography_config = self.get_bibliography_options().into_owned();
38 let renderer = Renderer::new(
39 RendererResources {
40 style: &self.style,
41 bibliography: &self.bibliography,
42 locale: &self.locale,
43 config: &bibliography_shared_config,
44 bibliography_config: Some(bibliography_config),
45 first_note_by_id: None,
46 },
47 &self.hints,
48 &self.citation_numbers,
49 CompoundRenderData {
50 set_by_ref: &self.compound_set_by_ref,
51 member_index: &self.compound_member_index,
52 sets: &self.compound_sets,
53 },
54 self.show_semantics,
55 self.inject_ast_indices,
56 self.abbreviation_map.as_ref(),
57 );
58
59 render(renderer)
60 }
61
62 fn process_sorted_refs<'a, I, F>(
69 &self,
70 sorted_refs: I,
71 process_fn: impl Fn(&Reference, usize) -> Option<ProcTemplate>,
72 ) -> Vec<ProcEntry>
73 where
74 I: Iterator<Item = &'a Reference>,
75 F: OutputFormat<Output = String>,
76 {
77 let mut bibliography = Vec::new();
78 let mut previous_reference: Option<&Reference> = None;
79
80 let bibliography_options = self.get_bibliography_options();
81 let substitute = bibliography_options.subsequent_author_substitute.as_ref();
82
83 for (index, reference) in sorted_refs.enumerate() {
84 let ref_id = reference.id().unwrap_or_default().to_string();
85 let entry_number = self
86 .citation_numbers
87 .borrow()
88 .get(&ref_id)
89 .copied()
90 .unwrap_or(index + 1);
91
92 if let Some(mut processed) = process_fn(reference, entry_number) {
93 if let Some(substitute_string) = substitute
94 && let Some(previous) = previous_reference
95 && self.contributors_match(previous, reference)
96 {
97 self.with_bibliography_renderer(|renderer| {
98 renderer.apply_author_substitution_with_format::<F>(
99 &mut processed,
100 substitute_string,
101 );
102 });
103 }
104
105 bibliography.push(ProcEntry {
106 id: ref_id,
107 template: processed,
108 metadata: self.extract_metadata(reference),
109 });
110 previous_reference = Some(reference);
111 }
112 }
113
114 bibliography
115 }
116
117 pub fn process_references(&self) -> ProcessedReferences {
122 self.initialize_numeric_bibliography_numbers();
123 let sorted_refs = self.sort_references(self.bibliography.values().collect());
124 let bibliography = self.process_sorted_refs::<_, crate::render::plain::PlainText>(
125 sorted_refs.iter().copied(),
126 |reference, entry_number| self.process_bibliography_entry(reference, entry_number),
127 );
128
129 ProcessedReferences {
130 bibliography,
131 citations: None,
132 }
133 }
134
135 pub fn process_bibliography_entry(
137 &self,
138 reference: &Reference,
139 entry_number: usize,
140 ) -> Option<ProcTemplate> {
141 self.with_bibliography_renderer(|renderer| {
142 renderer.process_bibliography_entry(reference, entry_number)
143 })
144 }
145
146 pub fn process_bibliography_entry_with_format<F>(
148 &self,
149 reference: &Reference,
150 entry_number: usize,
151 ) -> Option<ProcTemplate>
152 where
153 F: OutputFormat<Output = String>,
154 {
155 self.with_bibliography_renderer(|renderer| {
156 renderer.process_bibliography_entry_with_format::<F>(reference, entry_number)
157 })
158 }
159
160 pub fn contributors_match(&self, prev: &Reference, current: &Reference) -> bool {
164 let matcher = Matcher::new(&self.style, &self.default_config);
165 matcher.contributors_match(prev, current)
166 }
167
168 pub fn apply_author_substitution(&self, proc: &mut ProcTemplate, substitute: &str) {
172 self.with_bibliography_renderer(|renderer| {
173 renderer.apply_author_substitution(proc, substitute);
174 });
175 }
176
177 pub fn render_bibliography_with_format<F>(&self) -> String
179 where
180 F: OutputFormat<Output = String>,
181 {
182 self.render_bibliography_with_format_and_annotations::<F>(None, None)
183 }
184
185 pub fn render_bibliography_with_format_and_annotations<F>(
187 &self,
188 annotations: Option<&HashMap<String, String>>,
189 annotation_style: Option<&AnnotationStyle>,
190 ) -> String
191 where
192 F: OutputFormat<Output = String>,
193 {
194 self.render_selected_bibliography_with_format_and_annotations::<F, _>(
195 self.bibliography.keys().cloned().collect::<Vec<_>>(),
196 annotations,
197 annotation_style,
198 )
199 }
200
201 pub fn render_selected_bibliography_with_format<F, I>(&self, item_ids: I) -> String
203 where
204 F: OutputFormat<Output = String>,
205 I: IntoIterator<Item = String>,
206 {
207 self.render_selected_bibliography_with_format_and_annotations::<F, _>(item_ids, None, None)
208 }
209
210 pub fn render_selected_bibliography_with_format_and_annotations<F, I>(
217 &self,
218 item_ids: I,
219 annotations: Option<&HashMap<String, String>>,
220 annotation_style: Option<&AnnotationStyle>,
221 ) -> String
222 where
223 F: OutputFormat<Output = String>,
224 I: IntoIterator<Item = String>,
225 {
226 let selected: HashSet<String> = item_ids.into_iter().collect();
227
228 if let Some(groups) = self
230 .style
231 .bibliography
232 .as_ref()
233 .filter(|bibliography| bibliography.groups_enabled)
234 .and_then(|bibliography| bibliography.groups.as_ref())
235 {
236 let all_entries = self.process_references().bibliography;
237 return self.render_with_custom_groups_filtered::<F>(
238 &all_entries,
239 groups,
240 &selected,
241 annotations,
242 annotation_style,
243 );
244 }
245
246 let bibliography_options = self.get_bibliography_options();
248 if let Some(partitioning) = bibliography_options.sort_partitioning.as_ref()
249 && crate::sort_partitioning::should_render_sections(partitioning)
250 {
251 self.initialize_numeric_bibliography_numbers();
252 let all_sorted = self.sort_references(self.bibliography.values().collect());
253 let selected_sorted: Vec<&Reference> = all_sorted
254 .into_iter()
255 .filter(|r| r.id().as_deref().is_some_and(|id| selected.contains(id)))
256 .collect();
257 return self.render_with_partition_sections::<F>(
258 selected_sorted,
259 partitioning,
260 annotations,
261 annotation_style,
262 );
263 }
264
265 self.initialize_numeric_bibliography_numbers();
267 let sorted_refs = self.sort_references(self.bibliography.values().collect());
268
269 let bibliography = self.process_sorted_refs::<_, F>(
270 sorted_refs
271 .iter()
272 .filter(|r| r.id().as_deref().is_some_and(|id| selected.contains(id)))
273 .copied(),
274 |reference, entry_number| {
275 self.process_bibliography_entry_with_format::<F>(reference, entry_number)
276 },
277 );
278
279 let bibliography = self.merge_compound_entries::<F>(bibliography);
280 crate::render::refs_to_string_with_format::<F>(bibliography, annotations, annotation_style)
281 }
282
283 pub fn render_bibliography(&self) -> String {
285 self.render_bibliography_with_format::<crate::render::plain::PlainText>()
286 }
287}