refactor: move link tagging code

This commit is contained in:
Tobias Schmitz 2025-06-26 20:44:04 +02:00
parent 6ebe85d678
commit 605681d435
No known key found for this signature in database

View File

@ -40,11 +40,11 @@ pub(crate) struct StackEntry {
pub(crate) enum StackEntryKind { pub(crate) enum StackEntryKind {
Standard(Tag), Standard(Tag),
Link(LinkId, Packed<LinkMarker>),
Outline(OutlineCtx), Outline(OutlineCtx),
OutlineEntry(Packed<OutlineEntry>), OutlineEntry(Packed<OutlineEntry>),
Table(TableCtx), Table(TableCtx),
TableCell(Packed<TableCell>), TableCell(Packed<TableCell>),
Link(LinkId, Packed<LinkMarker>),
} }
impl StackEntryKind { impl StackEntryKind {
@ -409,16 +409,16 @@ pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) {
} }
} else if let Some(_) = elem.to_packed::<FigureCaption>() { } else if let Some(_) = elem.to_packed::<FigureCaption>() {
TagKind::Caption.into() TagKind::Caption.into()
} else if let Some(link) = elem.to_packed::<LinkMarker>() {
let link_id = gc.tags.next_link_id();
push_stack(gc, loc, StackEntryKind::Link(link_id, link.clone()));
return;
} else if let Some(table) = elem.to_packed::<TableElem>() { } else if let Some(table) = elem.to_packed::<TableElem>() {
push_stack(gc, loc, StackEntryKind::Table(TableCtx::new(table.clone()))); push_stack(gc, loc, StackEntryKind::Table(TableCtx::new(table.clone())));
return; return;
} else if let Some(cell) = elem.to_packed::<TableCell>() { } else if let Some(cell) = elem.to_packed::<TableCell>() {
push_stack(gc, loc, StackEntryKind::TableCell(cell.clone())); push_stack(gc, loc, StackEntryKind::TableCell(cell.clone()));
return; return;
} else if let Some(link) = elem.to_packed::<LinkMarker>() {
let link_id = gc.tags.next_link_id();
push_stack(gc, loc, StackEntryKind::Link(link_id, link.clone()));
return;
} else { } else {
return; return;
}; };
@ -448,16 +448,6 @@ pub(crate) fn handle_end(gc: &mut GlobalContext, loc: Location) {
let node = match entry.kind { let node = match entry.kind {
StackEntryKind::Standard(tag) => TagNode::Group(tag, entry.nodes), StackEntryKind::Standard(tag) => TagNode::Group(tag, entry.nodes),
StackEntryKind::Link(_, link) => {
let alt = link.alt.as_ref().map(EcoString::to_string);
let tag = TagKind::Link.with_alt_text(alt);
let mut node = TagNode::Group(tag, entry.nodes);
// Wrap link in reference tag, if it's not a url.
if let Destination::Position(_) | Destination::Location(_) = link.dest {
node = TagNode::Group(TagKind::Reference.into(), vec![node]);
}
node
}
StackEntryKind::Outline(ctx) => { StackEntryKind::Outline(ctx) => {
let nodes = ctx.build_outline(entry.nodes); let nodes = ctx.build_outline(entry.nodes);
TagNode::Group(TagKind::TOC.into(), nodes) TagNode::Group(TagKind::TOC.into(), nodes)
@ -487,6 +477,16 @@ pub(crate) fn handle_end(gc: &mut GlobalContext, loc: Location) {
return; return;
} }
StackEntryKind::Link(_, link) => {
let alt = link.alt.as_ref().map(EcoString::to_string);
let tag = TagKind::Link.with_alt_text(alt);
let mut node = TagNode::Group(tag, entry.nodes);
// Wrap link in reference tag, if it's not a url.
if let Destination::Position(_) | Destination::Location(_) = link.dest {
node = TagNode::Group(TagKind::Reference.into(), vec![node]);
}
node
}
}; };
gc.tags.push(node); gc.tags.push(node);