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
@@ -70,7 +70,7 @@ const PRELUDE: &str = r#"
|
||||
\def\tag{\texrratag}
|
||||
\def\cite#1{\texrracite{#1}}
|
||||
\def\citep#1{\texrracite{#1}}
|
||||
\def\citet#1{\texrracite{#1}}
|
||||
\def\citet#1{\texrratextualcite{#1}}
|
||||
\def\nocite#1{}
|
||||
\def\newblock{ }
|
||||
\def\caption#1{\texrracaptionstart#1\texrracaptionend}
|
||||
@@ -453,8 +453,8 @@ const PRELUDE: &str = r#"
|
||||
\def\And{\texrraauthorseparator}
|
||||
\def\AND{\texrraauthorseparator}
|
||||
\def\ifhmode{\iftrue}
|
||||
\def\paragraph#1{\par\texrraheadingstart{3}#1\texrraheadingend\par}
|
||||
\def\subparagraph#1{\par\texrraheadingstart{3}#1\texrraheadingend\par}
|
||||
\def\paragraph#1{\par\texrraunnumberedheadingstart{3}#1\texrraheadingend\par}
|
||||
\def\subparagraph#1{\par\texrraunnumberedheadingstart{3}#1\texrraheadingend\par}
|
||||
\def\@startsection#1#2#3#4#5#6#7{\par\texrraheadingstart{#2}#7\texrraheadingend\par}
|
||||
\def\setlength#1#2{#1=#2\relax}
|
||||
\def\addtolength#1#2{\advance#1#2\relax}
|
||||
|
||||
@@ -1787,10 +1787,17 @@ pub(super) fn build_positioned_pages(
|
||||
run.bold = true;
|
||||
}
|
||||
}
|
||||
let paragraph_alignment = runs.iter().find_map(|run| run.alignment);
|
||||
let quote = paragraph_alignment == Some(ParagraphAlignment::Quote);
|
||||
let paragraph_width_mm = if quote {
|
||||
(content_width_mm - 20.0).max(20.0)
|
||||
} else {
|
||||
content_width_mm
|
||||
};
|
||||
let lines = measure_positioned_lines(
|
||||
&styled_runs,
|
||||
font_size,
|
||||
content_width_mm,
|
||||
paragraph_width_mm,
|
||||
metrics,
|
||||
font_cache,
|
||||
cjk_family,
|
||||
@@ -1832,28 +1839,36 @@ pub(super) fn build_positioned_pages(
|
||||
max_depth,
|
||||
);
|
||||
|
||||
let paragraph_alignment = runs.iter().find_map(|run| run.alignment);
|
||||
let ragged = paragraph_alignment.is_some();
|
||||
let ragged = matches!(
|
||||
paragraph_alignment,
|
||||
Some(
|
||||
ParagraphAlignment::RaggedRight
|
||||
| ParagraphAlignment::RaggedLeft
|
||||
| ParagraphAlignment::Centered
|
||||
)
|
||||
);
|
||||
let quote_inset = if quote { 10.0 } else { 0.0 };
|
||||
let centered = paragraph_alignment == Some(ParagraphAlignment::Centered);
|
||||
let x = match (paragraph_alignment, &line.content) {
|
||||
(
|
||||
Some(ParagraphAlignment::RaggedLeft),
|
||||
PositionedLineContent::Text { line, font_size },
|
||||
) => {
|
||||
let text_line = text_layout::TextLine::new(
|
||||
line,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
*font_size,
|
||||
);
|
||||
millimeters_to_scaled(
|
||||
(content_width_mm - text_line.natural_width_mm(font_cache))
|
||||
.max(0.0),
|
||||
)
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
let x = millimeters_to_scaled(quote_inset)
|
||||
+ match (paragraph_alignment, &line.content) {
|
||||
(
|
||||
Some(ParagraphAlignment::RaggedLeft),
|
||||
PositionedLineContent::Text { line, font_size },
|
||||
) => {
|
||||
let text_line = text_layout::TextLine::new(
|
||||
line,
|
||||
cjk_family,
|
||||
mono_family,
|
||||
sans_family,
|
||||
*font_size,
|
||||
);
|
||||
millimeters_to_scaled(
|
||||
(paragraph_width_mm - text_line.natural_width_mm(font_cache))
|
||||
.max(0.0),
|
||||
)
|
||||
}
|
||||
_ => 0,
|
||||
};
|
||||
pages
|
||||
.last_mut()
|
||||
.expect("the page list starts with one page")
|
||||
@@ -2661,8 +2676,13 @@ pub(super) fn build_positioned_pages(
|
||||
cursor = cursor.saturating_add((normal_line_height as f64 * 0.25).round() as i64);
|
||||
let entry_gap = (normal_line_height as f64 * 0.2).round() as i64;
|
||||
for (index, (_key, runs)) in items.iter().enumerate() {
|
||||
let mut numbered = vec![Run::plain(format!("[{}] ", index + 1))];
|
||||
numbered.extend(runs.clone());
|
||||
let numbered = if model.author_year_citations {
|
||||
runs.clone()
|
||||
} else {
|
||||
let mut numbered = vec![Run::plain(format!("[{}] ", index + 1))];
|
||||
numbered.extend(runs.clone());
|
||||
numbered
|
||||
};
|
||||
for line in measure_positioned_lines(
|
||||
&numbered,
|
||||
10,
|
||||
|
||||
@@ -5973,6 +5973,31 @@ fn heading_start_primitive(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unnumbered_heading_start_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let value = read_scanned_token_list(input)?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>();
|
||||
let level = value.trim().parse::<u8>().map_err(|_| {
|
||||
input.fatal_error(error::SimpleTokenError::new(
|
||||
token,
|
||||
format!("heading level must be an integer, found `{value}`"),
|
||||
))
|
||||
})?;
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::HeadingStart {
|
||||
level,
|
||||
numbered: false,
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chapter_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
@@ -6075,6 +6100,90 @@ fn citation_primitive(
|
||||
argument_event_primitive(token, input, FrontendEventKind::Citation)
|
||||
}
|
||||
|
||||
fn textual_citation_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let keys = read_scanned_token_list(input)?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>();
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::Citation(format!("@text:{}", keys.trim())),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bibliography_metadata_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let key = read_unexpanded_argument(input, token, "expects a bibliography key")?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>();
|
||||
let author = read_unexpanded_argument(input, token, "expects an author")?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>();
|
||||
let year = read_unexpanded_argument(input, token, "expects a year")?
|
||||
.into_iter()
|
||||
.filter_map(|token| token.char())
|
||||
.collect::<String>();
|
||||
write_event(
|
||||
input,
|
||||
token,
|
||||
FrontendEventKind::BibliographyMetadata {
|
||||
key: key.trim().trim_matches(['{', '}']).to_string(),
|
||||
author: author.trim().trim_matches(['{', '}']).to_string(),
|
||||
year: year.trim().trim_matches(['{', '}']).to_string(),
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bib_metadata_author(fields: &std::collections::HashMap<String, String>) -> String {
|
||||
let raw = fields
|
||||
.get("author")
|
||||
.or_else(|| fields.get("editor"))
|
||||
.map(String::as_str)
|
||||
.unwrap_or("");
|
||||
let raw = raw.replace(['{', '}'], "");
|
||||
let surname = |author: &str| {
|
||||
author
|
||||
.split_once(',')
|
||||
.map(|(last, _)| last.trim().to_string())
|
||||
.unwrap_or_else(|| {
|
||||
author
|
||||
.split_whitespace()
|
||||
.last()
|
||||
.unwrap_or(author)
|
||||
.to_string()
|
||||
})
|
||||
};
|
||||
let authors: Vec<String> = raw
|
||||
.split(" and ")
|
||||
.map(str::trim)
|
||||
.filter(|author| !author.is_empty())
|
||||
.map(surname)
|
||||
.collect();
|
||||
match authors.as_slice() {
|
||||
[] => String::new(),
|
||||
[one] => one.clone(),
|
||||
[one, two] => format!("{one} and {two}"),
|
||||
[one, ..] => format!("{one} et al."),
|
||||
}
|
||||
}
|
||||
|
||||
fn bib_metadata_year(fields: &std::collections::HashMap<String, String>) -> String {
|
||||
fields
|
||||
.get("year")
|
||||
.map(|year| year.replace(['{', '}'], ""))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn bibliography_item_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
@@ -6108,7 +6217,13 @@ fn bibliography_primitive(
|
||||
|
||||
let mut source = String::from("\\begin{thebibliography}{9}");
|
||||
for entry in entries {
|
||||
source.push_str("\\bibitem{");
|
||||
source.push_str("\\texrrabibmeta{");
|
||||
source.push_str(&entry.key);
|
||||
source.push_str("}{");
|
||||
source.push_str(&bib_metadata_author(&entry.fields));
|
||||
source.push_str("}{");
|
||||
source.push_str(&bib_metadata_year(&entry.fields));
|
||||
source.push_str("}\\bibitem{");
|
||||
source.push_str(&entry.key);
|
||||
source.push('}');
|
||||
source.push_str(&entry.to_latex());
|
||||
@@ -7409,6 +7524,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
|
||||
"texrraheadingstart",
|
||||
texlang::command::BuiltIn::new_execution(heading_start_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"texrraunnumberedheadingstart",
|
||||
texlang::command::BuiltIn::new_execution(unnumbered_heading_start_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"texrraheadingend",
|
||||
texlang::command::BuiltIn::new_execution(heading_end_primitive),
|
||||
@@ -7429,6 +7548,14 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
|
||||
"texrracite",
|
||||
texlang::command::BuiltIn::new_execution(citation_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"texrratextualcite",
|
||||
texlang::command::BuiltIn::new_execution(textual_citation_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"texrrabibmeta",
|
||||
texlang::command::BuiltIn::new_execution(bibliography_metadata_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"bibitem",
|
||||
texlang::command::BuiltIn::new_execution(bibliography_item_primitive),
|
||||
|
||||
Reference in new issue
Block a user