Build structured journal front matter for sn-jnl

This commit is contained in:
gloamxun committed 2026-10-20 01:08:37 +08:00
1 parent 82542740e7
commit 466abd4ce6
10 files changed
+684 -7

No files matched your search

-2
View File
@@ -19,8 +19,6 @@ const PRELUDE: &str = r#"
\def\geometry#1{\texrrageometry{#1}}
\def\begin#1{\texrrabegin{#1}}
\def\end#1{\texrraend{#1}}
\def\title#1{\texrrametadatastart{title}#1\texrrametadataend{title}}
\def\author#1{\texrrametadatastart{author}#1\texrrametadataend{author}}
\def\date#1{\texrrametadatastart{date}#1\texrrametadataend{date}}
\def\@title{\texrrametadatatitle}
\def\@author{\texrrametadataauthor}
+257
View File
@@ -1794,6 +1794,263 @@ pub(super) fn build_positioned_pages(
}
}
}
TitleStyle::Journal => {
let title_runs: Vec<Run> = if model.title_runs.is_empty() {
if model.title.is_empty() {
Vec::new()
} else {
vec![Run::plain(model.title.clone())]
}
} else {
model.title_runs.clone()
};
let mut styled_title = title_runs;
for run in &mut styled_title {
run.bold = true;
}
for line in measure_positioned_lines(
&styled_title,
base_font_size + 5,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
0,
true,
);
}
let superscript = |number: usize| -> String {
number
.to_string()
.chars()
.map(|digit| match digit {
'0' => '⁰',
'1' => '¹',
'2' => '²',
'3' => '³',
'4' => '⁴',
'5' => '⁵',
'6' => '⁶',
'7' => '⁷',
'8' => '⁸',
'9' => '⁹',
other => other,
})
.collect()
};
let mut author_emails = Vec::new();
for author in &model.authors {
let mut runs = author.runs.clone();
if !author.affiliations.is_empty() {
let marks = author
.affiliations
.iter()
.map(|index| superscript(*index))
.collect::<String>();
runs.push(Run::plain(marks));
}
for line in measure_positioned_lines(
&runs,
base_font_size,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
0,
true,
);
}
if let Some(email) = &author.email {
author_emails.push(email.clone());
}
}
for affiliation in &model.affiliations {
let mut runs = vec![Run::plain(format!("{} ", superscript(affiliation.index)))];
runs.extend(affiliation.runs.clone());
for line in measure_positioned_lines(
&runs,
base_font_size.saturating_sub(1).max(1),
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
0,
true,
);
}
}
if !author_emails.is_empty() {
let email_runs =
vec![Run::plain(format!("E-mail: {}", author_emails.join("; ")))];
for line in measure_positioned_lines(
&email_runs,
base_font_size.saturating_sub(1).max(1),
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
0,
true,
);
}
}
if !model.abstract_runs.is_empty() {
let mut heading = Run::plain("Abstract".to_string());
heading.bold = true;
for line in measure_positioned_lines(
std::slice::from_ref(&heading),
base_font_size,
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
0,
true,
);
}
let inset_mm = 15.0_f64.min(content_width_mm / 4.0);
let abstract_width = (content_width_mm - 2.0 * inset_mm).max(20.0);
for line in measure_positioned_lines(
&model.abstract_runs,
base_font_size,
abstract_width,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
millimeters_to_scaled(inset_mm),
false,
);
}
}
if !model.keywords.is_empty() {
let mut runs = vec![Run::plain("Keywords: ".to_string())];
let mut keyword_run = Run::plain(String::new());
keyword_run.bold = true;
runs.push(keyword_run);
runs.extend(model.keywords.clone());
for line in measure_positioned_lines(
&runs,
base_font_size.saturating_sub(1).max(1),
content_width_mm,
metrics,
font_cache,
cjk_family,
mono_family,
sans_family,
math_metrics,
math_layout,
cache,
) {
push_title_line(
&mut pages,
&mut deferred,
&mut cursor,
page_breaks,
page_goal,
top_skip,
max_depth,
line,
millimeters_to_scaled(15.0_f64.min(content_width_mm / 4.0)),
false,
);
}
}
}
TitleStyle::Preprint => {
let rule_width = millimeters_to_scaled(content_width_mm);
let rule_height = i64::from(scaled_points(2.0).0).max(1);
+216
View File
@@ -2172,6 +2172,129 @@ fn make_title_primitive(
Ok(())
}
fn token_list_text(tokens: &[token::Token]) -> String {
tokens.iter().filter_map(|token| token.char()).collect()
}
fn parse_affiliation_indices(value: &str) -> Vec<usize> {
value
.split([',', ' '])
.filter_map(|part| part.trim().parse::<usize>().ok())
.collect()
}
fn push_frontmatter_content(
input: &mut vm::ExecutionInput<CompilerState>,
token: token::Token,
command: &'static str,
content: Vec<token::Token>,
) {
let trace_key = token.trace_key();
let mut expansion = strip_braced_tokens(&content).to_vec();
expansion.push(control_sequence_token(input, command, trace_key));
input
.as_mut()
.expansions_mut()
.extend(expansion.into_iter().rev());
}
fn title_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
if let Some(short) = read_optional_bracketed_argument(input, token, "short title")? {
write_event(
input,
token,
FrontendEventKind::MetadataShortTitle(token_list_text(&short).trim().to_string()),
);
}
let content = read_unexpanded_argument(input, token, "expects a title")?;
write_event(
input,
token,
FrontendEventKind::MetadataStart(MetadataField::Title),
);
push_frontmatter_content(input, token, "texrratitleend", content);
Ok(())
}
fn author_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
if let Some(next) = input.unexpanded().next()? {
if !matches!(next.value(), token::Value::Other('*')) {
input.unexpanded().back(next);
}
}
if let Some(indices) = read_optional_bracketed_argument(input, token, "author affiliation")? {
write_event(
input,
token,
FrontendEventKind::AuthorAffiliations(parse_affiliation_indices(&token_list_text(
&indices,
))),
);
}
let content = read_unexpanded_argument(input, token, "expects an author")?;
write_event(
input,
token,
FrontendEventKind::MetadataStart(MetadataField::Author),
);
push_frontmatter_content(input, token, "texrraauthorend", content);
Ok(())
}
fn affiliation_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
if let Some(next) = input.unexpanded().next()? {
if !matches!(next.value(), token::Value::Other('*')) {
input.unexpanded().back(next);
}
}
let index = read_optional_bracketed_argument(input, token, "affiliation number")?
.and_then(|tokens| token_list_text(&tokens).trim().parse::<usize>().ok())
.unwrap_or(1);
let content = read_unexpanded_argument(input, token, "expects an affiliation")?;
write_event(input, token, FrontendEventKind::AffiliationStart(index));
push_frontmatter_content(input, token, "texrraaffiliationend", content);
Ok(())
}
fn abstract_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects an abstract")?;
write_event(input, token, FrontendEventKind::AbstractStart);
push_frontmatter_content(input, token, "texrraabstractend", content);
Ok(())
}
fn keywords_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects keywords")?;
write_event(input, token, FrontendEventKind::KeywordsStart);
push_frontmatter_content(input, token, "texrrakeywordsend", content);
Ok(())
}
fn email_frontmatter_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let content = read_unexpanded_argument(input, token, "expects an e-mail address")?;
write_event(input, token, FrontendEventKind::MetadataEmailStart);
push_frontmatter_content(input, token, "texrraemailend", content);
Ok(())
}
fn metadata_field_argument(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -2869,6 +2992,12 @@ fn engine_managed_command(name: &EngineCommandName) -> bool {
matches!(
name.as_str(),
"maketitle"
| "title"
| "author"
| "affil"
| "abstract"
| "keywords"
| "email"
| "section"
| "subsection"
| "subsubsection"
@@ -4289,6 +4418,19 @@ fn define_tex_macro(
};
let macro_name = engine_command_name(name, input.vm().cs_name_interner());
if matches!(
&macro_name,
EngineCommandName::ControlSequence(name)
if matches!(
name.as_str(),
"title" | "author" | "affil" | "abstract" | "keywords" | "email"
)
) {
// The front matter is owned by the engine's semantic model. Document
// classes routinely replace these commands with box-level storage
// macros that the renderer cannot read, so keep the engine definitions.
return Ok(());
}
let global = matches!(
scope,
texcraft_stdext::collections::groupingmap::Scope::Global
@@ -7681,6 +7823,80 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"texrramaketitle",
texlang::command::BuiltIn::new_execution(make_title_primitive),
);
built_ins.insert(
"title",
texlang::command::BuiltIn::new_execution(title_frontmatter_primitive),
);
built_ins.insert(
"author",
texlang::command::BuiltIn::new_execution(author_frontmatter_primitive),
);
built_ins.insert(
"affil",
texlang::command::BuiltIn::new_execution(affiliation_frontmatter_primitive),
);
built_ins.insert(
"abstract",
texlang::command::BuiltIn::new_execution(abstract_frontmatter_primitive),
);
built_ins.insert(
"keywords",
texlang::command::BuiltIn::new_execution(keywords_frontmatter_primitive),
);
built_ins.insert(
"email",
texlang::command::BuiltIn::new_execution(email_frontmatter_primitive),
);
built_ins.insert(
"texrratitleend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(
input,
token,
FrontendEventKind::MetadataEnd(MetadataField::Title),
);
Ok(())
}),
);
built_ins.insert(
"texrraauthorend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(
input,
token,
FrontendEventKind::MetadataEnd(MetadataField::Author),
);
Ok(())
}),
);
built_ins.insert(
"texrraaffiliationend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(input, token, FrontendEventKind::AffiliationEnd);
Ok(())
}),
);
built_ins.insert(
"texrraabstractend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(input, token, FrontendEventKind::AbstractEnd);
Ok(())
}),
);
built_ins.insert(
"texrrakeywordsend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(input, token, FrontendEventKind::KeywordsEnd);
Ok(())
}),
);
built_ins.insert(
"texrraemailend",
texlang::command::BuiltIn::new_execution(|token, input| {
write_event(input, token, FrontendEventKind::MetadataEmailEnd);
Ok(())
}),
);
built_ins.insert(
"texrrametadatatitle",
texlang::command::BuiltIn::new_execution(|token, input| {