Skip to content

Commit

Permalink
Merge pull request #107 from zotero/issue-91
Browse files Browse the repository at this point in the history
Error on unrecognised output format (JS)
  • Loading branch information
cormacrelf authored Apr 21, 2021
2 parents c7f6975 + 9fd1689 commit 0164b40
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 143 deletions.
53 changes: 48 additions & 5 deletions crates/proc/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,21 +1154,64 @@ fn built_cluster(
cluster_id: ClusterId,
) -> Arc<<Markup as OutputFormat>::Output> {
let fmt = db.get_formatter();
let build = built_cluster_before_output(db, cluster_id);
let build = built_cluster_before_output(db, cluster_id, &fmt);
let string = fmt.output(build, get_piq(db));
Arc::new(string)
}

pub fn built_cluster_preview(
db: &dyn IrDatabase,
cluster_id: ClusterId,
formatter: &Markup,
fmt: &Markup,
) -> Arc<<Markup as OutputFormat>::Output> {
let build = built_cluster_before_output(db, cluster_id);
let string = formatter.output(build, get_piq(db));
let build = built_cluster_before_output(db, cluster_id, &fmt);
let string = fmt.output(build, get_piq(db));
Arc::new(string)
}

#[test]
pub fn test_preview_unicode_escape_issue_91() {
use crate::test::{MockProcessor, test_style_layout};
use csl::{CslType, Variable, NameVariable, NumberVariable};
use citeproc_io::{Reference, NumberLike};

// ugh. this should be easier.

let mut proc = MockProcessor::rtf();

let style = test_style_layout(r#"
<group delimiter=", ">
<text prefix="text: " variable="title" />
<names prefix="name: " variable="author" />
<number prefix="number: " variable="page" />
</group>
"#);
proc.set_style_text(&style);

let mut r = Reference::empty("id".into(), CslType::Book);
r.ordinary.insert(Variable::Title, "Čotar".into());
r.name.insert(NameVariable::Author, vec![citeproc_io::Name::Person(citeproc_io::PersonName {
family: Some("Čotar".into()),
..Default::default()
})]);
r.number.insert(NumberVariable::Page, NumberLike::Str("Čotar".into()));
proc.insert_references(vec![r]);

let mut interner = string_interner::StringInterner::<ClusterId>::new();
let cluster = interner.get_or_intern("cluster");
proc.init_clusters(vec![(cluster, ClusterNumber::Note(IntraNote::Single(1)), vec![Cite::basic("id")])]);

// check the rtf (default)
let built = proc.built_cluster(cluster);
println!("{}", built);
assert_eq!(built.as_str(), "text: \\uc0\\u268 otar, name: \\uc0\\u268 otar, number: \\uc0\\u268 otar");

let plain = Markup::plain();
let preview = built_cluster_preview(&proc, cluster, &plain);
println!("{}", preview);
assert_eq!(preview.as_str(), "text: Čotar, name: Čotar, number: Čotar");
}

fn cluster_cites_sorted(db: &dyn IrDatabase, cluster_id: ClusterId) -> Option<Arc<Vec<CiteId>>> {
db.cluster_data_sorted(cluster_id)
.map(|data| data.cites.clone())
Expand All @@ -1179,8 +1222,8 @@ use crate::ir::transforms;
pub fn built_cluster_before_output(
db: &dyn IrDatabase,
cluster_id: ClusterId,
fmt: &Markup,
) -> <Markup as OutputFormat>::Build {
let fmt = db.get_formatter();
let cite_ids = if let Some(x) = db.cluster_cites_sorted(cluster_id) {
x
} else {
Expand Down
3 changes: 1 addition & 2 deletions crates/proc/src/disamb/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ fn test() {
db.insert_references(vec![refr, refr2]);
let mut interner = string_interner::StringInterner::default();
let id = interner.get_or_intern("1");
db.init_clusters(vec![(id, vec![Cite::basic("ref_id")])]);
db.set_cluster_note_number(id, Some(ClusterNumber::Note(IntraNote::Single(1))));
db.init_clusters(vec![(id, ClusterNumber::Note(IntraNote::Single(1)), vec![Cite::basic("ref_id")])]);
let cite_ids = db.cluster_cites(id);

let get_stream = |ind: usize| {
Expand Down
27 changes: 19 additions & 8 deletions crates/proc/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use citeproc_db::{CiteData, ClusterId, LocaleFetcher, PredefinedLocales, StyleDatabase};
use citeproc_db::{CiteData, ClusterId, ClusterNumber, LocaleFetcher, PredefinedLocales, StyleDatabase};
use citeproc_io::{output::markup::Markup, Cite, Reference};
use csl::Atom;

Expand All @@ -18,8 +18,8 @@ pub fn with_test_style<T>(s: &str, f: impl Fn(Style) -> T) -> T {
f(sty)
}

pub fn with_test_citation<T>(f: impl Fn(Style) -> T, s: &str) -> T {
let sty = Style::parse_for_test(&format!(
pub fn test_style_layout(s: &str) -> String {
format!(
r#"<?xml version="1.0" encoding="utf-8"?>
<style class="note" version="1.0.1">
<citation>
Expand All @@ -30,7 +30,11 @@ pub fn with_test_citation<T>(f: impl Fn(Style) -> T, s: &str) -> T {
</style>
"#,
s
))
)
}

pub fn with_test_citation<T>(mut f: impl FnMut(Style) -> T, s: &str) -> T {
let sty = Style::parse_for_test(&test_style_layout(s))
.unwrap();
f(sty)
}
Expand All @@ -45,13 +49,14 @@ pub fn with_test_citation<T>(f: impl Fn(Style) -> T, s: &str) -> T {
pub struct MockProcessor {
storage: salsa::Storage<Self>,
fetcher: Arc<dyn LocaleFetcher>,
formatter: Markup,
}

impl salsa::Database for MockProcessor {}

impl ImplementationDetails for MockProcessor {
fn get_formatter(&self) -> Markup {
Markup::html()
self.formatter.clone()
}
fn lookup_interned_string(
&self,
Expand All @@ -68,11 +73,17 @@ impl citeproc_db::HasFetcher for MockProcessor {
}

impl MockProcessor {
pub fn rtf() -> Self {
let mut new = Self::new();
new.formatter = Markup::rtf();
new
}
pub fn new() -> Self {
let fetcher = Arc::new(PredefinedLocales::bundled_en_us());
let mut db = MockProcessor {
storage: Default::default(),
fetcher,
formatter: Markup::html(),
};
citeproc_db::safe_default(&mut db);
crate::safe_default(&mut db);
Expand All @@ -85,10 +96,10 @@ impl MockProcessor {
self.set_style_with_durability(Arc::new(style), Durability::MEDIUM);
}

pub fn init_clusters(&mut self, clusters: Vec<(ClusterId, Vec<Cite<Markup>>)>) {
pub fn init_clusters(&mut self, clusters: Vec<(ClusterId, ClusterNumber, Vec<Cite<Markup>>)>) {
let mut cluster_ids = Vec::new();
for cluster in clusters {
let (cluster_id, cites) = cluster;
let (cluster_id, note_number, cites) = cluster;
let mut ids = Vec::new();
for (index, cite) in cites.into_iter().enumerate() {
let cite_id = self.cite(CiteData::RealCite {
Expand All @@ -99,7 +110,7 @@ impl MockProcessor {
ids.push(cite_id);
}
self.set_cluster_cites(cluster_id, Arc::new(ids));
self.set_cluster_note_number(cluster_id, None);
self.set_cluster_note_number(cluster_id, Some(note_number));
cluster_ids.push(cluster_id);
}
self.set_cluster_ids(Arc::new(cluster_ids));
Expand Down
4 changes: 4 additions & 0 deletions crates/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ let positions = [ ... before, { note: 34 }, ... after ];
let preview = driver.previewCitationCluster(cites, positions, "html").unwrap();
```

The format argument is like the format passed to `Driver.new`: one of `"html"`,
`"rtf"` or `"plain"`. The driver will use that instead of its normal output
format.

The positions array is exactly like a call to `setClusterOrder`, except exactly
one of the positions omits the id field. This could either:

Expand Down
Loading

0 comments on commit 0164b40

Please sign in to comment.