Add abstract quotations, unnumbered paragraphs, and author-year citations
This commit is contained in:
1 parent
3406f84e86
commit
7f1f202de1
11 files changed
+366
-60
No files matched your search
@@ -65,6 +65,12 @@ pub enum TitleStyle {
|
||||
Preprint,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct CitationMetadata {
|
||||
pub author: String,
|
||||
pub year: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Footnote {
|
||||
pub number: u32,
|
||||
@@ -90,6 +96,8 @@ pub struct DocumentModel {
|
||||
pub block_sources: Vec<Option<SourceLocation>>,
|
||||
pub labels: HashMap<String, String>,
|
||||
pub citations: HashMap<String, usize>,
|
||||
pub citation_metadata: HashMap<String, CitationMetadata>,
|
||||
pub author_year_citations: bool,
|
||||
pub footnotes: Vec<Footnote>,
|
||||
pub page_style: PageStyle,
|
||||
pub page_style_overrides: Vec<PageStyleOverride>,
|
||||
@@ -113,6 +121,8 @@ impl Default for DocumentModel {
|
||||
block_sources: Vec::new(),
|
||||
labels: HashMap::new(),
|
||||
citations: HashMap::new(),
|
||||
citation_metadata: HashMap::new(),
|
||||
author_year_citations: false,
|
||||
footnotes: Vec::new(),
|
||||
page_style: PageStyle::default(),
|
||||
page_style_overrides: Vec::new(),
|
||||
@@ -292,6 +302,9 @@ pub struct ListItem {
|
||||
pub enum UnresolvedReference {
|
||||
Label(String),
|
||||
Citation(Vec<String>),
|
||||
/// A textual citation such as natbib's `\citet`, rendered as
|
||||
/// `Author (year)` instead of a parenthetical citation.
|
||||
TextualCitation(Vec<String>),
|
||||
/// A table-of-contents/list page number for the block with this index.
|
||||
/// The target is resolved to a page number during page layout.
|
||||
TocPage(usize),
|
||||
@@ -341,6 +354,7 @@ pub enum ParagraphAlignment {
|
||||
RaggedRight,
|
||||
RaggedLeft,
|
||||
Centered,
|
||||
Quote,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
|
||||
@@ -501,6 +501,7 @@ struct Parser {
|
||||
suppress_indent: bool,
|
||||
paragraph_alignment: ParagraphAlignment,
|
||||
alignment_saves: Vec<ParagraphAlignment>,
|
||||
in_abstract: bool,
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
@@ -551,6 +552,7 @@ impl Parser {
|
||||
suppress_indent: false,
|
||||
paragraph_alignment: ParagraphAlignment::Justified,
|
||||
alignment_saves: Vec::new(),
|
||||
in_abstract: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -945,6 +947,7 @@ impl Parser {
|
||||
expanded::FrontendEventKind::PackageLoaded(package) => {
|
||||
if package == "arxiv" {
|
||||
self.document.title_style = TitleStyle::Preprint;
|
||||
self.document.author_year_citations = true;
|
||||
if self.document.undertitle.is_none() {
|
||||
self.document.undertitle = Some("A Preprint".to_string());
|
||||
}
|
||||
@@ -1263,13 +1266,29 @@ impl Parser {
|
||||
self.append_reference(UnresolvedReference::Label(key.clone()))
|
||||
}
|
||||
expanded::FrontendEventKind::Citation(keys) => {
|
||||
self.append_reference(UnresolvedReference::Citation(
|
||||
keys.split(',').map(str::trim).map(str::to_string).collect(),
|
||||
))
|
||||
let (keys, textual) = match keys.strip_prefix("@text:") {
|
||||
Some(keys) => (keys, true),
|
||||
None => (keys.as_str(), false),
|
||||
};
|
||||
let keys = keys.split(',').map(str::trim).map(str::to_string).collect();
|
||||
self.append_reference(if textual {
|
||||
UnresolvedReference::TextualCitation(keys)
|
||||
} else {
|
||||
UnresolvedReference::Citation(keys)
|
||||
})
|
||||
}
|
||||
expanded::FrontendEventKind::BibliographyItem(key) => {
|
||||
self.start_bibliography_item(key.clone())
|
||||
}
|
||||
expanded::FrontendEventKind::BibliographyMetadata { key, author, year } => {
|
||||
self.document.citation_metadata.insert(
|
||||
key.clone(),
|
||||
crate::document::CitationMetadata {
|
||||
author: author.clone(),
|
||||
year: year.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
expanded::FrontendEventKind::IncludeGraphics { options, path } => {
|
||||
self.add_image(path.clone(), options.clone())
|
||||
}
|
||||
@@ -1864,6 +1883,11 @@ impl Parser {
|
||||
&& let Some(first) = runs.first_mut()
|
||||
{
|
||||
first.alignment = Some(self.paragraph_alignment);
|
||||
} else if self.in_abstract
|
||||
&& runs.first().is_some_and(|run| run.alignment.is_none())
|
||||
&& let Some(first) = runs.first_mut()
|
||||
{
|
||||
first.alignment = Some(ParagraphAlignment::Quote);
|
||||
}
|
||||
prepend_paragraph_indent(&mut runs, indent);
|
||||
let source = self.paragraph_source.take();
|
||||
@@ -1962,8 +1986,12 @@ impl Parser {
|
||||
self.push_block(Block::Centered {
|
||||
runs: vec![heading],
|
||||
});
|
||||
self.in_abstract = true;
|
||||
}
|
||||
(false, "abstract") => {
|
||||
self.flush_paragraph();
|
||||
self.in_abstract = false;
|
||||
}
|
||||
(false, "abstract") => self.flush_paragraph(),
|
||||
(true, "raggedright") => {
|
||||
self.flush_paragraph();
|
||||
self.alignment_saves.push(self.paragraph_alignment);
|
||||
|
||||
@@ -237,6 +237,11 @@ pub enum FrontendEventKind {
|
||||
Reference(String),
|
||||
Citation(String),
|
||||
BibliographyItem(String),
|
||||
BibliographyMetadata {
|
||||
key: String,
|
||||
author: String,
|
||||
year: String,
|
||||
},
|
||||
PageStyle(crate::document::PageStyleKind),
|
||||
ThisPageStyle(crate::document::PageStyleKind),
|
||||
PageDecorationStart {
|
||||
|
||||
@@ -1,14 +1,38 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::document::{Block, DocumentModel, ListBlock, ParsedDocument, Run, UnresolvedReference};
|
||||
use crate::document::{
|
||||
Block, CitationMetadata, DocumentModel, ListBlock, ParsedDocument, Run, UnresolvedReference,
|
||||
};
|
||||
|
||||
/// Resolves document references after parsing has collected all labels and citations.
|
||||
pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
|
||||
let mut document = parsed.0;
|
||||
fn citation_label(
|
||||
key: &str,
|
||||
citations: &HashMap<String, usize>,
|
||||
metadata: &HashMap<String, CitationMetadata>,
|
||||
author_year: bool,
|
||||
) -> String {
|
||||
if author_year && let Some(entry) = metadata.get(key) {
|
||||
match (entry.author.is_empty(), entry.year.is_empty()) {
|
||||
(false, false) => return format!("{}, {}", entry.author, entry.year),
|
||||
(false, true) => return entry.author.clone(),
|
||||
(true, false) => return entry.year.clone(),
|
||||
(true, true) => {}
|
||||
}
|
||||
}
|
||||
citations
|
||||
.get(key)
|
||||
.map(usize::to_string)
|
||||
.unwrap_or_else(|| key.to_string())
|
||||
}
|
||||
|
||||
fn resolve_runs(
|
||||
runs: &mut [Run],
|
||||
labels: &HashMap<String, String>,
|
||||
citations: &HashMap<String, usize>,
|
||||
metadata: &HashMap<String, CitationMetadata>,
|
||||
author_year: bool,
|
||||
) {
|
||||
for run in runs {
|
||||
let Some(reference) = run.unresolved_reference.take() else {
|
||||
@@ -24,17 +48,42 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "??".to_string()),
|
||||
UnresolvedReference::Citation(keys) => {
|
||||
let formatted = keys
|
||||
.iter()
|
||||
.map(|key| {
|
||||
citations
|
||||
.get(key)
|
||||
.map(usize::to_string)
|
||||
.unwrap_or_else(|| key.clone())
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("[{formatted}]")
|
||||
if author_year {
|
||||
let formatted = keys
|
||||
.iter()
|
||||
.map(|key| citation_label(key, citations, metadata, true))
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ");
|
||||
format!("({formatted})")
|
||||
} else {
|
||||
let formatted = keys
|
||||
.iter()
|
||||
.map(|key| citation_label(key, citations, metadata, false))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("[{formatted}]")
|
||||
}
|
||||
}
|
||||
UnresolvedReference::TextualCitation(keys) => {
|
||||
if author_year {
|
||||
keys.iter()
|
||||
.map(|key| {
|
||||
let label = citation_label(key, citations, metadata, true);
|
||||
match label.rsplit_once(", ") {
|
||||
Some((author, year)) => format!("{author} ({year})"),
|
||||
None => label,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ")
|
||||
} else {
|
||||
let formatted = keys
|
||||
.iter()
|
||||
.map(|key| citation_label(key, citations, metadata, false))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("[{formatted}]")
|
||||
}
|
||||
}
|
||||
};
|
||||
run.text = text;
|
||||
@@ -45,42 +94,46 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
|
||||
list: &mut ListBlock,
|
||||
labels: &HashMap<String, String>,
|
||||
citations: &HashMap<String, usize>,
|
||||
metadata: &HashMap<String, CitationMetadata>,
|
||||
author_year: bool,
|
||||
) {
|
||||
for item in &mut list.items {
|
||||
resolve_runs(&mut item.runs, labels, citations);
|
||||
resolve_runs(&mut item.runs, labels, citations, metadata, author_year);
|
||||
for nested in &mut item.nested {
|
||||
resolve_list(nested, labels, citations);
|
||||
resolve_list(nested, labels, citations, metadata, author_year);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let labels = &document.labels;
|
||||
let citations = &document.citations;
|
||||
let metadata = &document.citation_metadata;
|
||||
let author_year = document.author_year_citations;
|
||||
for block in &mut document.blocks {
|
||||
match block {
|
||||
Block::Paragraph(runs) | Block::Heading { runs, .. } => {
|
||||
resolve_runs(runs, labels, citations)
|
||||
resolve_runs(runs, labels, citations, metadata, author_year)
|
||||
}
|
||||
Block::List(list) => resolve_list(list, labels, citations),
|
||||
Block::List(list) => resolve_list(list, labels, citations, metadata, author_year),
|
||||
Block::Table { rows, caption, .. } => {
|
||||
for cell in rows.iter_mut().flatten() {
|
||||
resolve_runs(cell, labels, citations);
|
||||
resolve_runs(cell, labels, citations, metadata, author_year);
|
||||
}
|
||||
if let Some(caption) = caption {
|
||||
resolve_runs(caption, labels, citations);
|
||||
resolve_runs(caption, labels, citations, metadata, author_year);
|
||||
}
|
||||
}
|
||||
Block::Image { caption, .. } => {
|
||||
if let Some(caption) = caption {
|
||||
resolve_runs(caption, labels, citations);
|
||||
resolve_runs(caption, labels, citations, metadata, author_year);
|
||||
}
|
||||
}
|
||||
Block::FramedBox { runs, .. } | Block::Centered { runs } => {
|
||||
resolve_runs(runs, labels, citations)
|
||||
resolve_runs(runs, labels, citations, metadata, author_year)
|
||||
}
|
||||
Block::Bibliography(items) => {
|
||||
for (_, runs) in items {
|
||||
resolve_runs(runs, labels, citations);
|
||||
resolve_runs(runs, labels, citations, metadata, author_year);
|
||||
}
|
||||
}
|
||||
Block::HorizontalRule { .. }
|
||||
|
||||
Reference in new issue
Block a user