Skip to content

Commit

Permalink
support nestable detached mods
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas committed Aug 1, 2024
1 parent 21d0640 commit 544b67b
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/stage_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub enum NorgAST {
modifier_type: NestableDetachedModifier,
level: u16,
extensions: Vec<DetachedModifierExtension>,
content: Box<NorgASTFlat>,
text: Box<NorgASTFlat>,
content: Vec<Self>,
},
RangeableDetachedModifier {
modifier_type: RangeableDetachedModifier,
Expand Down Expand Up @@ -52,7 +53,7 @@ fn convert(flat: NorgASTFlat) -> NorgAST {
match flat {
NorgASTFlat::Paragraph(tokens) => NorgAST::Paragraph(tokens),
NorgASTFlat::NestableDetachedModifier { modifier_type, level, extensions, content } =>
NorgAST::NestableDetachedModifier { modifier_type, level, extensions, content },
NorgAST::NestableDetachedModifier { modifier_type, level, extensions, text: content, content: vec![] },
NorgASTFlat::RangeableDetachedModifier { modifier_type, title, extensions, content } =>
NorgAST::RangeableDetachedModifier { modifier_type, title, extensions, content },
NorgASTFlat::Heading { level, title, extensions } =>
Expand Down Expand Up @@ -91,6 +92,37 @@ pub fn stage_4(flat: Vec<NorgASTFlat>) -> Vec<NorgAST> {
}

ast.push(NorgAST::Heading { level: *start_level, title: title.to_vec(), extensions: extensions.to_vec(), content })
} else if let NorgASTFlat::NestableDetachedModifier { level: start_level, modifier_type, extensions, content: text } = item {
let mut content = vec![];
let mut seen = false;
for j in (i+1)..flat.len() {
if let NorgASTFlat::NestableDetachedModifier { level, .. } = &flat[j] {
if level <= start_level {
// stop.
content = stage_4(flat[(i + 1)..j].to_vec());
i = j - 1;
seen = true;
break
}

} else {
// stop immediately if we see something that's not a NestableDetachedModifier
// of lesser level
break;
}
}
if !seen {
content = stage_4(flat[i+1..].to_vec());
i = flat.len();
}

ast.push(NorgAST::NestableDetachedModifier {
modifier_type: modifier_type.clone(),
level: *start_level,
extensions: extensions.to_vec(),
text: text.clone(),
content,
});
} else {
ast.push(convert(item.clone()));
}
Expand Down

0 comments on commit 544b67b

Please sign in to comment.