Skip to content

Commit

Permalink
move json serialization from get_citation_strings to new of Context +…
Browse files Browse the repository at this point in the history
… add logs when it fails
  • Loading branch information
Elena Krippner committed Dec 4, 2023
1 parent 3e1b519 commit 9ff6bd4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion commons/src/config/preamble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use clap::Args;
use icu_locid::Locale;
use logid::err;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};

use super::{
locale, log_id::ConfigErr, parse_to_hashset, parse_to_locale_pathbuf_hashset, ConfigFns,
Expand Down
2 changes: 1 addition & 1 deletion render/src/html/citeproc/csl_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn get_style_string(path: PathBuf) -> String {
_ => {
log!(
GeneralWarning::UnsupportedCslStyle,
format!("The csl style '{:?}' is is not supported", path.to_str()),
format!("The csl style '{:?}' is not supported", path.to_str()),
);
IEEE.to_string()
}
Expand Down
10 changes: 6 additions & 4 deletions render/src/html/citeproc/js/citeproc_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let citationsPre = [];
const citationsPost = [];
let citationResults = [];
let footnoteResults = [];
let forPagedjs;

function getCitations(citationText) {
const citationData = JSON.parse(citationText);
Expand Down Expand Up @@ -127,15 +128,16 @@ function getFootnoteStyles() {
return stylesString.join("\n");
}

export function initProcessor(citationText, localeText, styleText) {
export function initProcessor(citationText, localeText, styleText, forPagedjsParam) {
citations = getCitations(citationText);
let citeprocSys = getCiteprocSys(localeText);
citeproc = getProcessor(citeprocSys, styleText);
forPagedjs = forPagedjsParam;
}

export function getCitationStrings(citationIds, for_pagedjs) {
export function getCitationStrings(citationIds) {
let counter = 1;
for (let citationId of JSON.parse(citationIds)) {
for (let citationId of citationIds) {
let newStrings = appendCitation(counter, citationId);
let lastResult = newStrings[newStrings.length - 1];
if (citeproc.opt.xclass === "in-text") {
Expand All @@ -144,7 +146,7 @@ export function getCitationStrings(citationIds, for_pagedjs) {
citationResults[newStrings[i][0]] = newStrings[i][1];
}
} else {
if (for_pagedjs) {
if (forPagedjs) {
citationResults.push(`<span className=\"footnote\">${lastResult[1]}</span>`);
for (let i = 0; i < newStrings.length - 1; ++i) {
citationResults[newStrings[i][0]] = `<span className=\"footnote\">${newStrings[i][1]}</span>`;
Expand Down
18 changes: 12 additions & 6 deletions render/src/html/citeproc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,21 @@ impl CiteprocWrapper {
doc_locale: Locale,
citation_locales: HashMap<Locale, PathBuf>,
style_id: PathBuf,
citation_id_vectors: &Vec<Vec<String>>,
citation_id_vectors: &[serde_json::Value],
for_pagedjs: bool,
) -> Result<Vec<String>, CiteError> {
let citation_text = get_csl_string(citation_paths);
let locale = get_locale_string(doc_locale, citation_locales);
let style = get_style_string(style_id);

self.module
.call::<()>("initProcessor", json_args!(citation_text, locale, style))
.call::<()>("initProcessor", json_args!(citation_text, locale, style, for_pagedjs))
.map_err(|_| CiteError::ProcessorInitializationError)?;

self.module
.call(
"getCitationStrings",
json_args!(
serde_json::to_string(citation_id_vectors).unwrap(),
for_pagedjs
),
citation_id_vectors,
)
.map_err(|_| CiteError::CitationError)
}
Expand Down Expand Up @@ -79,6 +76,15 @@ fn get_csl_string(references: &HashSet<PathBuf>) -> String {
let mut citation_items: Vec<CslItem> = vec![];
for reference in references {
if let Ok(citation_string) = fs::read_to_string(reference.clone().into_os_string()) {
match serde_json::from_str::<CslData>(&citation_string) {
Ok(mut citation_data) => citation_items.append(&mut citation_data.items),
Err(e) => {
log!(
GeneralWarning::JSONDeserialization,
format!("JSON deserializaion failed with error: '{:?}'", e)
);
}
}
let mut citation_data: CslData =
serde_json::from_str::<CslData>(&citation_string).unwrap();
citation_items.append(&mut citation_data.items);
Expand Down
6 changes: 6 additions & 0 deletions render/src/log_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub enum GeneralWarning {

/// Log-id denoting the attempt to use an unsupported csl style
UnsupportedCslStyle,

/// Log-id denoting a failed json deserialization
JSONDeserialization,

/// Log-id denoting a failed json serialization
JSONSerialization,
}

#[derive(Debug, Clone, ErrLogId, Error)]
Expand Down
28 changes: 19 additions & 9 deletions render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use logid::log;
use std::path::PathBuf;
use unimarkup_commons::{
config::icu_locid::{locale, Locale},
lexer::{span::Span, token::iterator::Itertools},
lexer::span::Span,
};
use unimarkup_inline::element::{
base::{EscapedNewline, EscapedPlain, EscapedWhitespace, Newline, Plain},
Expand All @@ -26,7 +26,7 @@ use unimarkup_parser::{
},
};

use crate::log_id::RenderError;
use crate::log_id::{GeneralWarning, RenderError};

pub struct Context<'a> {
doc: &'a Document,
Expand Down Expand Up @@ -74,40 +74,50 @@ impl<'a> Context<'a> {
.clone()
.unwrap_or(locale!("en-US"));
let citation_locales = doc.config.preamble.cite.citation_locales.clone();
let citation_ids = match serde_json::to_value(&doc.citations) {

This comment has been minimized.

Copy link
@mhatzl

mhatzl Dec 4, 2023

Contributor

I think you have to convert each individual entry in the outer Vec to serde_json::Value.
e.g. using doc.citations.iter().map(|c| match serde_json::to_value::<Vec<String>>(c) ...
Then instead of &[citation_ids] you would use &citation_ids, because &Vec<> is compatible with &[].

Otherwise the created JSON might not be correct.
But not sure how citeproc-js handles it.

Ok(citation_ids) => citation_ids,
Err(e) => {
log!(
GeneralWarning::JSONSerialization,
format!("JSON serialization failed with error: '{:?}'", e)
);
serde_json::to_value::<Vec<Vec<String>>>(vec![]).unwrap()
}
};
rendered_citations = match citeproc.get_citation_strings(
&doc.config.preamble.cite.references,
doc_locale,
citation_locales,
style,
&doc.citations,
&[citation_ids],
for_pagedjs,
) {
Ok(rendered_citations) => rendered_citations,
Err(e) => {
log!(e);
vec!["CITATION ERROR".to_string(); doc.citations.len()]
vec!["########### CITATION ERROR ###########".to_string(); doc.citations.len()]
}
};
footnotes = match citeproc.get_footnotes() {
Ok(footnotes) => footnotes,
Err(e) => {
log!(e);
"CITATION ERROR".to_string()
"########### CITATION ERROR ###########".to_string()
}
};
bibliography = match citeproc.get_bibliography() {
Ok(bibliography) => bibliography,
Err(e) => {
log!(e);
"CITATION ERROR".to_string()
"########### CITATION ERROR ###########".to_string()
}
};
}
Err(e) => {
log!(e);
rendered_citations = vec!["CITATION_ERROR".to_string(); doc.citations.len()];
footnotes = "CITATION_ERROR".to_string();
bibliography = "CITATION_ERROR".to_string();
rendered_citations = vec!["########### CITATION ERROR ###########".to_string(); doc.citations.len()];
footnotes = "########### CITATION ERROR ###########".to_string();
bibliography = "########### CITATION ERROR ###########".to_string();
}
}

Expand Down

0 comments on commit 9ff6bd4

Please sign in to comment.