Skip to content

Commit

Permalink
feat(proc): add ProcReferences
Browse files Browse the repository at this point in the history
Add a struct to handle intermediately rendered output.

The intention is something similar to the haskell citeproc server json.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Nov 23, 2023
1 parent f59c675 commit 13f10f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use csln::bibliography::HasFile;
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::style::Style;
use processor::{refs_to_string, Processor};
use processor::Processor;

#[derive(Parser, Default, Debug)]
#[clap(author = "Bruce D'Arcus", version, about = "A CLI for CSLN")]
Expand All @@ -27,6 +27,6 @@ fn main() {
let locale = csln::style::locale::Locale::from_file(&opts.locale);
let processor: Processor = Processor::new(style, bibliography, citations, locale);
let rendered_refs = processor.process_references();
println!("{}", refs_to_string(rendered_refs));
//println!("{}", serde_json::to_string_pretty(&rendered_refs).unwrap());
//println!("{}", refs_to_string(rendered_refs));
println!("{}", serde_json::to_string_pretty(&rendered_refs).unwrap());
}
46 changes: 43 additions & 3 deletions processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SPDX-FileCopyrightText: © 2023 Bruce D'Arcus
use csln::bibliography::reference::InputReference;
use csln::bibliography::reference::{EdtfString, RefID};
use csln::bibliography::InputBibliography as Bibliography;
use csln::citation::Citation;
use csln::citation::{Citation, CitationItem};
use csln::style::locale::Locale;
use csln::style::options::{Config, MonthFormat, SortKey, SubstituteKey};
use csln::style::template::{
Expand Down Expand Up @@ -619,17 +619,57 @@ impl ComponentValues for TemplateDate {
// assert_eq!(rendered_date, "2020");
// }

/// The intermediate representation of renderered citations and bibliography..
#[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)]
pub struct ProcReferences {
pub bibliography: Vec<ProcTemplate>,
pub citations: Option<Vec<ProcTemplate>>,
}

impl Processor {
/// Render references to AST.
#[inline]
pub fn process_references(&self) -> Vec<ProcTemplate> {
pub fn process_references(&self) -> ProcReferences {
let sorted_references = self.sort_references(self.get_references());
sorted_references
let bibliography = sorted_references
.par_iter()
.map(|reference| self.process_reference(reference))
.collect();
let citations = self.process_citations(&self.citations);
ProcReferences { bibliography, citations: Some(citations) }
}

fn process_citations(
&self,
citations: &[Citation],
) -> Vec<Vec<ProcTemplateComponent>> {
citations
.iter()
.map(|citation| self.process_citation(citation))
.collect()
}

fn process_citation(&self, citation: &Citation) -> Vec<ProcTemplateComponent> {
// map the citation items to a vector of ProcTemplateComponents
citation
.citation_items
.iter()
.filter_map(|citation_item| self.process_citation_item(citation_item))
.flatten() // Flatten the nested vectors
.collect()
}

fn process_citation_item(
&self,
citation_item: &CitationItem,
) -> Option<Vec<ProcTemplateComponent>> {
let citation_style = self.style.citation.clone();
let reference = self.get_reference(&citation_item.ref_id)?;
let proc_template =
self.process_template(&reference, citation_style?.template.as_slice());
Some(proc_template)
}

/// Render a reference to AST.
fn process_reference(
&self,
Expand Down

0 comments on commit 13f10f8

Please sign in to comment.