diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-11-17 15:16:37 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-11-17 15:16:37 +0100 |
| commit | f6cb4d725ee6e4fd09b92b5af7348d11ac951b10 (patch) | |
| tree | 7cf8bc7b0158a8a453fd9e2a2fe5a857ef036d5e /src/library/structure.rs | |
| parent | 4d0bdc4ca4cb5e8ca1a70b38a0fc0ec37d9e4857 (diff) | |
Update standard library functions 🎁
Diffstat (limited to 'src/library/structure.rs')
| -rw-r--r-- | src/library/structure.rs | 172 |
1 files changed, 0 insertions, 172 deletions
diff --git a/src/library/structure.rs b/src/library/structure.rs deleted file mode 100644 index 2dbf33ff..00000000 --- a/src/library/structure.rs +++ /dev/null @@ -1,172 +0,0 @@ -use crate::func::prelude::*; -use Command::*; - -/// ↩ `line.break`, `n`: Ends the current line. -#[derive(Debug, PartialEq)] -pub struct Linebreak; - -function! { - data: Linebreak, - parse: plain, - layout(_, _) { Ok(commands![BreakFlex]) } -} - -/// ↕ `paragraph.break`: Ends the current paragraph. -/// -/// This has the same effect as two subsequent newlines. -#[derive(Debug, PartialEq)] -pub struct Parbreak; - -function! { - data: Parbreak, - parse: plain, - layout(_, _) { Ok(commands![FinishFlex]) } -} - -/// 📜 `page.break`: Ends the current page. -#[derive(Debug, PartialEq)] -pub struct Pagebreak; - -function! { - data: Pagebreak, - parse: plain, - layout(_, _) { Ok(commands![BreakStack]) } -} - -/// 📐 `align`: Aligns content in different ways. -/// -/// **Positional arguments:** -/// - `left`, `right` or `center` _(required)_. -#[derive(Debug, PartialEq)] -pub struct Align { - body: Option<SyntaxTree>, - alignment: Alignment, -} - -function! { - data: Align, - - parse(args, body, ctx) { - let body = parse!(optional: body, ctx); - let arg = args.get_pos::<ArgIdent>()?; - let alignment = match arg.val { - "left" => Alignment::Left, - "right" => Alignment::Right, - "center" => Alignment::Center, - s => err!("invalid alignment specifier: {}", s), - }; - args.done()?; - - Ok(Align { - body, - alignment, - }) - } - - layout(this, ctx) { - Ok(commands![match &this.body { - Some(body) => { - AddMany(layout_tree(body, LayoutContext { - alignment: this.alignment, - .. ctx - })?) - } - None => SetAlignment(this.alignment) - }]) - } -} - -/// 📦 `box`: Layouts content into a box. -/// -/// **Positional arguments:** None. -/// -/// **Keyword arguments:** -/// - flow: either `horizontal` or `vertical` _(optional)_. -#[derive(Debug, PartialEq)] -pub struct Boxed { - body: SyntaxTree, - flow: Flow, -} - -function! { - data: Boxed, - - parse(args, body, ctx) { - let body = parse!(required: body, ctx); - - let mut flow = Flow::Vertical; - if let Some(ident) = args.get_key_opt::<ArgIdent>("flow")? { - flow = match ident.val { - "vertical" => Flow::Vertical, - "horizontal" => Flow::Horizontal, - f => err!("invalid flow specifier: {}", f), - }; - } - args.done()?; - - Ok(Boxed { - body, - flow, - }) - } - - layout(this, ctx) { - Ok(commands![ - AddMany(layout_tree(&this.body, LayoutContext { - flow: this.flow, - .. ctx - })?) - ]) - } -} - -macro_rules! spacefunc { - ($ident:ident, $doc:expr, $var:ident => $command:expr) => ( - #[doc = $doc] - /// - /// **Positional arguments:** - /// - Spacing as a size or number, which is interpreted as a multiple - /// of the font size _(required)_. - #[derive(Debug, PartialEq)] - pub struct $ident(Spacing); - - function! { - data: $ident, - - parse(args, body, _ctx) { - parse!(forbidden: body); - - let arg = args.get_pos::<ArgExpr>()?; - let spacing = match arg.val { - Expression::Size(s) => Spacing::Absolute(*s), - Expression::Num(f) => Spacing::Relative(*f as f32), - _ => err!("invalid spacing, expected size or number"), - }; - - Ok($ident(spacing)) - } - - layout(this, ctx) { - let $var = match this.0 { - Spacing::Absolute(s) => s, - Spacing::Relative(f) => f * ctx.style.font_size, - }; - - Ok(commands![$command]) - } - } - ); -} - -/// Absolute or font-relative spacing. -#[derive(Debug, PartialEq)] -enum Spacing { - Absolute(Size), - Relative(f32), -} - -spacefunc!(HorizontalSpace, "📖 `h`: Adds horizontal whitespace.", - space => AddFlex(Layout::empty(space, Size::zero()))); - -spacefunc!(VerticalSpace, "📑 `v`: Adds vertical whitespace.", - space => Add(Layout::empty(Size::zero(), space))); |
