diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-09 00:37:13 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-09 00:37:13 +0200 |
| commit | 5afb42ad89abb518a01a09051f0f9b6f75bd383e (patch) | |
| tree | b12368a287f22de711df8d759c20ee742ed5b4c2 /src/library | |
| parent | d69dfa84ec957ac4037f60a3335416a9f73b97c8 (diff) | |
Lists with indent-based parsing
- Unordered lists with indent-based parsing and basic layout using stacks
- Headings are now also indent based
- Removes syntax functions since they will be superseded by select & transform
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/markup.rs | 170 | ||||
| -rw-r--r-- | src/library/mod.rs | 12 | ||||
| -rw-r--r-- | src/library/pad.rs | 2 | ||||
| -rw-r--r-- | src/library/shapes.rs | 4 | ||||
| -rw-r--r-- | src/library/stack.rs | 2 |
5 files changed, 5 insertions, 185 deletions
diff --git a/src/library/markup.rs b/src/library/markup.rs deleted file mode 100644 index 0a80fe74..00000000 --- a/src/library/markup.rs +++ /dev/null @@ -1,170 +0,0 @@ -use super::*; -use crate::syntax::{HeadingNode, RawNode}; - -/// `linebreak`: Start a new line. -/// -/// # Syntax -/// This function has dedicated syntax: -/// ```typst -/// This line ends here, \ -/// And a new one begins. -/// ``` -/// -/// # Return value -/// A template that inserts a line break. -pub fn linebreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value { - Value::template(Node::LINEBREAK, move |ctx| { - ctx.linebreak(); - }) -} - -/// `parbreak`: Start a new paragraph. -/// -/// # Return value -/// A template that inserts a paragraph break. -pub fn parbreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value { - Value::template(Node::PARBREAK, move |ctx| { - ctx.parbreak(); - }) -} - -/// `strong`: Strong text. -/// -/// # Syntax -/// This function has dedicated syntax. -/// ```typst -/// This is *important*! -/// ``` -/// -/// # Positional parameters -/// - Body: optional, of type `template`. -/// -/// # Return value -/// A template that flips the boldness of text. The effect is scoped to the -/// body if present. -pub fn strong(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let body = args.eat::<TemplateValue>(ctx); - Value::template(Node::STRONG, move |ctx| { - let snapshot = ctx.state.clone(); - ctx.state.font.strong ^= true; - - if let Some(body) = &body { - body.exec(ctx); - ctx.state = snapshot; - } - }) -} - -/// `emph`: Emphasized text. -/// -/// # Syntax -/// This function has dedicated syntax. -/// ```typst -/// I would have _never_ thought so! -/// ``` -/// -/// # Positional parameters -/// - Body: optional, of type `template`. -/// -/// # Return value -/// A template that flips whether text is set in italics. The effect is scoped -/// to the body if present. -pub fn emph(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let body = args.eat::<TemplateValue>(ctx); - Value::template(Node::EMPH, move |ctx| { - let snapshot = ctx.state.clone(); - ctx.state.font.emph ^= true; - - if let Some(body) = &body { - body.exec(ctx); - ctx.state = snapshot; - } - }) -} - -/// `heading`: A section heading. -/// -/// # Syntax -/// This function has dedicated syntax. -/// ```typst -/// = Section -/// ... -/// -/// == Subsection -/// ... -/// ``` -/// -/// # Positional parameters -/// - Body, of type `template`. -/// -/// # Named parameters -/// - Section depth: `level`, of type `integer` between 1 and 6. -/// -/// # Return value -/// A template that sets the body as a section heading, that is, large and in -/// bold. -pub fn heading(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let level = args.eat_named(ctx, HeadingNode::LEVEL).unwrap_or(1); - let body = args - .eat_expect::<TemplateValue>(ctx, HeadingNode::BODY) - .unwrap_or_default(); - - Value::template(Node::HEADING, move |ctx| { - let snapshot = ctx.state.clone(); - let upscale = 1.6 - 0.1 * level as f64; - ctx.state.font.scale *= upscale; - ctx.state.font.strong = true; - - body.exec(ctx); - ctx.state = snapshot; - - ctx.parbreak(); - }) -} - -/// `raw`: Raw text. -/// -/// # Syntax -/// This function has dedicated syntax: -/// - For inline-level raw text: -/// ```typst -/// `...` -/// ``` -/// - For block-level raw text: -/// ````typst -/// ```rust -/// println!("Hello World!"); -/// ``` -/// ```` -/// -/// # Positional parameters -/// - Text, of type `string`. -/// -/// # Named parameters -/// - Language for syntax highlighting: `lang`, of type `string`. -/// - Whether the item is block level (split in its own paragraph): `block`, of -/// type `boolean`. -/// -/// # Return value -/// A template that sets the text raw, that is, in monospace and optionally with -/// syntax highlighting. -pub fn raw(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let text = args.eat_expect::<String>(ctx, RawNode::TEXT).unwrap_or_default(); - let _lang = args.eat_named::<String>(ctx, RawNode::LANG); - let block = args.eat_named(ctx, RawNode::BLOCK).unwrap_or(false); - - Value::template(Node::RAW, move |ctx| { - if block { - ctx.parbreak(); - } - - let snapshot = ctx.state.clone(); - ctx.set_monospace(); - ctx.push_text(&text); - ctx.state = snapshot; - - if block { - ctx.parbreak(); - } - }) -} diff --git a/src/library/mod.rs b/src/library/mod.rs index 6a46314c..f9e4f68a 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -8,7 +8,6 @@ mod basic; mod font; mod image; mod lang; -mod markup; mod math; mod pad; mod page; @@ -22,7 +21,6 @@ pub use align::*; pub use basic::*; pub use font::*; pub use lang::*; -pub use markup::*; pub use math::*; pub use pad::*; pub use page::*; @@ -38,20 +36,12 @@ use crate::eval::{EvalContext, FuncArgs, Scope, TemplateValue, Value}; use crate::exec::{Exec, FontFamily}; use crate::font::{FontStyle, FontWeight, VerticalFontMetric}; use crate::geom::*; -use crate::syntax::{Node, Spanned}; +use crate::syntax::Spanned; /// Construct a scope containing all standard library definitions. pub fn new() -> Scope { let mut std = Scope::new(); - // Syntax functions. - std.def_func(Node::LINEBREAK, linebreak); - std.def_func(Node::PARBREAK, parbreak); - std.def_func(Node::STRONG, strong); - std.def_func(Node::EMPH, emph); - std.def_func(Node::HEADING, heading); - std.def_func(Node::RAW, raw); - // Library functions. std.def_func("align", align); std.def_func("circle", circle); diff --git a/src/library/pad.rs b/src/library/pad.rs index 4b6236d0..6b944c9c 100644 --- a/src/library/pad.rs +++ b/src/library/pad.rs @@ -31,7 +31,7 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { ); Value::template("pad", move |ctx| { - let child = ctx.exec_template(&body).into(); + let child = ctx.exec_template_stack(&body).into(); ctx.push(PadNode { padding, child }); }) } diff --git a/src/library/shapes.rs b/src/library/shapes.rs index a47203c4..287b1c10 100644 --- a/src/library/shapes.rs +++ b/src/library/shapes.rs @@ -61,7 +61,7 @@ fn rect_impl( body: TemplateValue, ) -> Value { Value::template(name, move |ctx| { - let mut stack = ctx.exec_template(&body); + let mut stack = ctx.exec_template_stack(&body); stack.aspect = aspect; let fixed = FixedNode { width, height, child: stack.into() }; @@ -137,7 +137,7 @@ fn ellipse_impl( // perfectly into the ellipse. const PAD: f64 = 0.5 - SQRT_2 / 4.0; - let mut stack = ctx.exec_template(&body); + let mut stack = ctx.exec_template_stack(&body); stack.aspect = aspect; let fixed = FixedNode { diff --git a/src/library/stack.rs b/src/library/stack.rs index b6f92e16..03bdc6a1 100644 --- a/src/library/stack.rs +++ b/src/library/stack.rs @@ -26,7 +26,7 @@ pub fn stack(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let children = children .iter() .map(|child| { - let child = ctx.exec_template(child).into(); + let child = ctx.exec_template_stack(child).into(); StackChild::Any(child, ctx.state.aligns) }) .collect(); |
