Adjust outline indent option

This commit is contained in:
Laurenz 2023-06-09 14:50:29 +02:00
parent 903f440b28
commit 7ece53ae0c
2 changed files with 26 additions and 21 deletions

View File

@ -326,8 +326,8 @@ pub trait Outlinable: Refable {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum OutlineIndent { pub enum OutlineIndent {
Bool(bool), Bool(bool),
Length(Spacing), Rel(Rel<Length>),
Function(Func), Func(Func),
} }
impl OutlineIndent { impl OutlineIndent {
@ -338,7 +338,7 @@ impl OutlineIndent {
seq: &mut Vec<Content>, seq: &mut Vec<Content>,
span: Span, span: Span,
) -> SourceResult<()> { ) -> SourceResult<()> {
match &indent { match indent {
// 'none' | 'false' => no indenting // 'none' | 'false' => no indenting
None | Some(Smart::Custom(OutlineIndent::Bool(false))) => {} None | Some(Smart::Custom(OutlineIndent::Bool(false))) => {}
@ -366,23 +366,20 @@ impl OutlineIndent {
} }
// Length => indent with some fixed spacing per level // Length => indent with some fixed spacing per level
Some(Smart::Custom(OutlineIndent::Length(length))) => { Some(Smart::Custom(OutlineIndent::Rel(length))) => {
seq.push(HElem::new(*length).pack().repeat(ancestors.len())); seq.push(
HElem::new(Spacing::Rel(*length)).pack().repeat(ancestors.len()),
);
} }
// Function => call function with the current depth and take // Function => call function with the current depth and take
// the returned content // the returned content
Some(Smart::Custom(OutlineIndent::Function(func))) => { Some(Smart::Custom(OutlineIndent::Func(func))) => {
let depth = ancestors.len(); let depth = ancestors.len();
let returned = func.call_vt(vt, [depth])?; let LengthOrContent(content) =
let Ok(returned) = returned.cast::<Content>() else { func.call_vt(vt, [depth])?.cast().at(span)?;
bail!( if !content.is_empty() {
span, seq.push(content);
"indent function must return content"
);
};
if !returned.is_empty() {
seq.push(returned);
} }
} }
}; };
@ -395,10 +392,18 @@ cast! {
OutlineIndent, OutlineIndent,
self => match self { self => match self {
Self::Bool(v) => v.into_value(), Self::Bool(v) => v.into_value(),
Self::Length(v) => v.into_value(), Self::Rel(v) => v.into_value(),
Self::Function(v) => v.into_value() Self::Func(v) => v.into_value()
}, },
v: bool => OutlineIndent::Bool(v), v: bool => OutlineIndent::Bool(v),
v: Spacing => OutlineIndent::Length(v), v: Rel<Length> => OutlineIndent::Rel(v),
v: Func => OutlineIndent::Function(v), v: Func => OutlineIndent::Func(v),
}
struct LengthOrContent(Content);
cast! {
LengthOrContent,
v: Rel<Length> => Self(HElem::new(Spacing::Rel(v)).pack()),
v: Content => Self(v),
} }

View File

@ -35,7 +35,7 @@
#outline(indent: true) #outline(indent: true)
#outline(indent: none) #outline(indent: none)
#outline(indent: auto) #outline(indent: auto)
#outline(indent: 2em) #outline(indent: n => 2em * n)
#outline(indent: n => ([-], [], [==], [====]).at(n)) #outline(indent: n => ([-], [], [==], [====]).at(n))
#outline(indent: n => "!" * calc.pow(2, n)) #outline(indent: n => "!" * calc.pow(2, n))
@ -54,7 +54,7 @@
#lorem(10) #lorem(10)
--- ---
// Error: 2-35 indent function must return content // Error: 2-35 expected relative length or content, found dictionary
#outline(indent: n => (a: "dict")) #outline(indent: n => (a: "dict"))
= Heading = Heading