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

+34
View File
@@ -63,6 +63,25 @@ pub enum TitleStyle {
Article,
/// Preprint-style title block with rules, an undertitle, and author columns.
Preprint,
/// Journal-style title block: title, author/affiliation columns, abstract,
/// and keywords.
Journal,
}
/// One author block in a structured front matter.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct AuthorBlock {
pub runs: Vec<Run>,
/// One-based affiliation indices attached to this author.
pub affiliations: Vec<usize>,
pub email: Option<String>,
}
/// One numbered affiliation in a structured front matter.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Affiliation {
pub index: usize,
pub runs: Vec<Run>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
@@ -88,6 +107,16 @@ pub struct DocumentModel {
pub title_style: TitleStyle,
/// Optional line printed under the title rules, e.g. "A Preprint".
pub undertitle: Option<String>,
/// Short title from `\title[short]{long}`.
pub short_title: Option<String>,
/// Structured author blocks from `\author` / `\affil` / `\email`.
pub authors: Vec<AuthorBlock>,
/// Numbered affiliations from `\affil[n]{...}`.
pub affiliations: Vec<Affiliation>,
/// Body of `\abstract{...}` captured for journal title blocks.
pub abstract_runs: Vec<Run>,
/// Body of `\keywords{...}` captured for journal title blocks.
pub keywords: Vec<Run>,
pub page: PageSize,
pub parindent: common::Scaled,
/// Base font size selected by the document class (10, 11, or 12pt).
@@ -120,6 +149,11 @@ impl Default for DocumentModel {
date: String::new(),
title_style: TitleStyle::default(),
undertitle: None,
short_title: None,
authors: Vec::new(),
affiliations: Vec::new(),
abstract_runs: Vec::new(),
keywords: Vec::new(),
page: PageSize::default(),
// article's default \parindent for 10/11pt text.
parindent: common::Scaled(17 * 65_536),
+108 -1
View File
@@ -478,6 +478,12 @@ struct Parser {
pending_table_caption: Option<Vec<Run>>,
caption_capture: Option<Vec<Run>>,
metadata_capture: Option<(expanded::MetadataField, Vec<Run>)>,
author_affiliations: Vec<usize>,
pending_author_email: Option<String>,
email_capture: Option<Vec<Run>>,
affiliation_capture: Option<(usize, Vec<Run>)>,
abstract_capture: Option<Vec<Run>>,
keywords_capture: Option<Vec<Run>>,
framed_box: Option<Vec<Run>>,
box_frame: bool,
box_width: Option<common::Scaled>,
@@ -590,6 +596,12 @@ impl Parser {
pending_table_caption: None,
caption_capture: None,
metadata_capture: None,
author_affiliations: Vec::new(),
pending_author_email: None,
email_capture: None,
affiliation_capture: None,
abstract_capture: None,
keywords_capture: None,
framed_box: None,
box_frame: true,
box_width: None,
@@ -787,7 +799,16 @@ impl Parser {
self.document.title = value;
self.document.title_runs = runs;
}
expanded::MetadataField::Author => self.document.author = value,
expanded::MetadataField::Author => {
self.document.author = value;
let affiliations = std::mem::take(&mut self.author_affiliations);
let email = self.pending_author_email.take();
self.document.authors.push(crate::document::AuthorBlock {
runs,
affiliations,
email,
});
}
expanded::MetadataField::Date => self.document.date = value,
}
}
@@ -1006,6 +1027,9 @@ impl Parser {
self.finish_metadata(*field);
}
expanded::FrontendEventKind::PackageLoaded(package) => {
if package == "sn-jnl" {
self.document.title_style = TitleStyle::Journal;
}
if package == "arxiv" {
self.document.title_style = TitleStyle::Preprint;
self.document.author_year_citations = true;
@@ -1030,6 +1054,60 @@ impl Parser {
self.append_text("\u{F7001}");
}
}
expanded::FrontendEventKind::MetadataShortTitle(short) => {
self.document.short_title = Some(short.clone());
}
expanded::FrontendEventKind::AuthorAffiliations(indices) => {
self.author_affiliations = indices.clone();
}
expanded::FrontendEventKind::MetadataEmailStart => {
self.email_capture = Some(Vec::new());
}
expanded::FrontendEventKind::MetadataEmailEnd => {
if let Some(mut runs) = self.email_capture.take() {
trim_run_end(&mut runs);
let email = runs.iter().map(|run| run.text.as_str()).collect::<String>();
self.pending_author_email = Some(email);
}
}
expanded::FrontendEventKind::AffiliationStart(index) => {
if self.document.title_style == TitleStyle::Article {
self.document.title_style = TitleStyle::Journal;
}
self.affiliation_capture = Some((*index, Vec::new()));
}
expanded::FrontendEventKind::AffiliationEnd => {
if let Some((index, mut runs)) = self.affiliation_capture.take() {
trim_run_end(&mut runs);
self.document
.affiliations
.push(crate::document::Affiliation { index, runs });
}
}
expanded::FrontendEventKind::AbstractStart => {
if self.document.title_style == TitleStyle::Article {
self.document.title_style = TitleStyle::Journal;
}
self.abstract_capture = Some(Vec::new());
}
expanded::FrontendEventKind::AbstractEnd => {
if let Some(mut runs) = self.abstract_capture.take() {
trim_run_end(&mut runs);
self.document.abstract_runs = runs;
}
}
expanded::FrontendEventKind::KeywordsStart => {
if self.document.title_style == TitleStyle::Article {
self.document.title_style = TitleStyle::Journal;
}
self.keywords_capture = Some(Vec::new());
}
expanded::FrontendEventKind::KeywordsEnd => {
if let Some(mut runs) = self.keywords_capture.take() {
trim_run_end(&mut runs);
self.document.keywords = runs;
}
}
expanded::FrontendEventKind::Geometry(settings) => {
apply_class_font_size(&mut self.document, settings);
apply_geometry(&mut self.document, settings);
@@ -1749,6 +1827,31 @@ impl Parser {
.expect("page decoration capture was checked")
.2;
}
if self.email_capture.is_some() {
return self
.email_capture
.as_mut()
.expect("email capture was checked");
}
if self.affiliation_capture.is_some() {
return &mut self
.affiliation_capture
.as_mut()
.expect("affiliation capture was checked")
.1;
}
if self.abstract_capture.is_some() {
return self
.abstract_capture
.as_mut()
.expect("abstract capture was checked");
}
if self.keywords_capture.is_some() {
return self
.keywords_capture
.as_mut()
.expect("keywords capture was checked");
}
if self.footnote_capture.is_some() {
return self
.footnote_capture
@@ -1764,6 +1867,10 @@ impl Parser {
&& self.centered.is_none()
&& self.framed_box.is_none()
&& self.metadata_capture.is_none()
&& self.email_capture.is_none()
&& self.affiliation_capture.is_none()
&& self.abstract_capture.is_none()
&& self.keywords_capture.is_none()
&& self.page_decoration_capture.is_none()
&& self.caption_capture.is_none()
&& self.heading.is_none()
+16
View File
@@ -277,6 +277,22 @@ pub enum FrontendEventKind {
PackageLoaded(String),
/// A separator between author blocks (LaTeX's \And).
MetadataSeparator,
/// Short title from the optional argument of `\title[short]{long}`.
MetadataShortTitle(String),
/// Affiliation indices from the optional argument of `\author[1,2]{...}`.
AuthorAffiliations(Vec<usize>),
/// Start of an `\affil[n]{...}` body.
AffiliationStart(usize),
AffiliationEnd,
/// Start/end of an `\abstract{...}` body captured for the title block.
AbstractStart,
AbstractEnd,
/// Start/end of a `\keywords{...}` body.
KeywordsStart,
KeywordsEnd,
/// Start/end of the e-mail address inside an author block.
MetadataEmailStart,
MetadataEmailEnd,
ParagraphAlignment(crate::document::ParagraphAlignment),
TableOfContents,
ListOfFigures,