Skip to content

Commit

Permalink
treat unknown citation styles as file names to load custom CSL files.
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
SillyFreak committed Feb 12, 2025
1 parent ba6d872 commit 454d857
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ See the [manual](docs/manual.pdf) for details.

## License

Parts of the Rust plugin that interfaces with the [Hayagriva](https://github.com/typst/hayagriva) citation library are based on [`typst-library`'s `bibliography.rs`](https://github.com/typst/typst/blob/26e65bfef5b1da7f6c72e1409237cf03fb5d6069/crates/typst-library/src/model/bibliography.rs). Likewise, the example bibliographies are taken or adapted from [`typst-dev-assets`' `works.bib`](https://github.com/typst/typst-dev-assets/blob/1dba4bea22e5e19597fbf5f321b047ff7626e2d0/files/bib/works.bib).Both are licensed from Typst's authors under the Apache License 2.0.
Parts of the Rust plugin that interfaces with the [Hayagriva](https://github.com/typst/hayagriva) citation library are based on [typst-library's `bibliography.rs`](https://github.com/typst/typst/blob/26e65bfef5b1da7f6c72e1409237cf03fb5d6069/crates/typst-library/src/model/bibliography.rs). Likewise, the example bibliographies are taken or adapted from [typst-dev-assets' `works.bib`](https://github.com/typst/typst-dev-assets/blob/1dba4bea22e5e19597fbf5f321b047ff7626e2d0/files/bib/works.bib). Both are licensed from Typst's authors under the Apache License 2.0.

The example CSL style [`ieee.csl`](https://github.com/citation-style-language/styles/blob/fd6cb3e81762055d107839c3c288c359985f81c8/ieee.csl) is taken from the [CSL project](https://citationstyles.org/) who provide it under the [Creative Commons Attribution-ShareAlike 3.0 Unported license](https://creativecommons.org/licenses/by-sa/3.0/).
12 changes: 8 additions & 4 deletions plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ fn read_libraries(sources: &[Source]) -> Result<IndexMap<String, hayagriva::Entr
fn read_impl(config: Config) -> Result<Bibliography, String> {
let entries = read_libraries(&config.sources)?;

let style =
ArchivedStyle::by_name(&config.style).ok_or(format!("Unknown style: {}", config.style))?;
let citationberg::Style::Independent(style) = style.get() else {
let style = match config.style {
Style::BuiltIn(name) => ArchivedStyle::by_name(&name)
.ok_or(format!("Unknown style: {}", name))?
.get(),
Style::Custom(source) => citationberg::Style::from_xml(&source).map_err_to_string()?,
};
let citationberg::Style::Independent(style) = style else {
return Err("style is not an IndependentStyle".to_string());
};

Expand Down Expand Up @@ -213,7 +217,7 @@ mod tests {
content: bib.to_string(),
}],
full: true,
style: "ieee".to_string(),
style: Style::BuiltIn("ieee".to_string()),
locale: hayagriva::citationberg::LocaleCode::en_us(),
citations: vec![],
})
Expand Down
9 changes: 8 additions & 1 deletion plugin/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{
pub struct Config {
pub sources: Vec<Source>,
pub full: bool,
pub style: String,
pub style: Style,
pub locale: hayagriva::citationberg::LocaleCode,
pub citations: Vec<Citation>,
}
Expand All @@ -21,6 +21,13 @@ pub struct Source {
pub content: String,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum Style {
BuiltIn(String),
Custom(String),
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Citation {
Expand Down
Binary file modified src/alexandria.wasm
Binary file not shown.
9 changes: 8 additions & 1 deletion src/lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,18 @@
assert.ne(prefix, none, message: "when using multiple custom bibliographies, you must specify the prefix for each")
}

let style = csl-to-string(style)
if style in hayagriva.names {
style = (built-in: style)
} else {
style = (custom: read(style))
}

let locale = locale()
set-bibliography(prefix, citations => hayagriva.read(
path.map(path => (path: path, content: read(path))),
full,
csl-to-string(style),
style,
locale,
citations,
))
Expand Down
5 changes: 5 additions & 0 deletions tests/custom-ieee/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# generated by tytanic, do not edit

diff/**
out/**
ref/**
9 changes: 9 additions & 0 deletions tests/custom-ieee/ref.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import "../test-utils.typ": *

#test-citations

#bib(
// title: "Bibliography",
full: true,
style: "../ieee.csl",
)
11 changes: 11 additions & 0 deletions tests/custom-ieee/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "../test-utils.typ": *

#show: x-alexandria

#x-test-citations

#x-bib(
title: "Bibliography",
full: true,
style: "ieee.csl",
)
Loading

0 comments on commit 454d857

Please sign in to comment.