summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-17 12:55:34 +0200
committerLaurenz <laurmaedje@gmail.com>2019-10-17 12:55:34 +0200
commit1987e5861cf2c033e3a540a5ef7c0f7106016929 (patch)
treedcbf2d32c88d394e63b60e7473b2f6ba79fc83e3 /src/library
parentf22f9513aea21408ebf6febd01912e630e9ad5e6 (diff)
Create basic box and line-break functions 📦
Diffstat (limited to 'src/library')
-rw-r--r--src/library/boxed.rs29
-rw-r--r--src/library/breaks.rs23
-rw-r--r--src/library/mod.rs13
3 files changed, 61 insertions, 4 deletions
diff --git a/src/library/boxed.rs b/src/library/boxed.rs
new file mode 100644
index 00000000..975888f4
--- /dev/null
+++ b/src/library/boxed.rs
@@ -0,0 +1,29 @@
+use super::prelude::*;
+
+/// Wraps content into a box.
+#[derive(Debug, PartialEq)]
+pub struct BoxFunc {
+ body: SyntaxTree
+}
+
+impl Function for BoxFunc {
+ fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) -> ParseResult<Self>
+ where Self: Sized {
+ if has_arguments(header) {
+ return err("pagebreak: expected no arguments");
+ }
+
+ if let Some(body) = body {
+ Ok(BoxFunc {
+ body: parse(body, ctx)?
+ })
+ } else {
+ err("box: expected body")
+ }
+ }
+
+ fn layout(&self, ctx: LayoutContext) -> LayoutResult<CommandList> {
+ let layout = layout_tree(&self.body, ctx)?;
+ Ok(commands![Command::AddMany(layout)])
+ }
+}
diff --git a/src/library/breaks.rs b/src/library/breaks.rs
index 22d572f0..a622350f 100644
--- a/src/library/breaks.rs
+++ b/src/library/breaks.rs
@@ -1,5 +1,28 @@
use super::prelude::*;
+/// Ends the current line.
+#[derive(Debug, PartialEq)]
+pub struct LinebreakFunc;
+
+impl Function for LinebreakFunc {
+ fn parse(header: &FuncHeader, body: Option<&str>, _: ParseContext) -> ParseResult<Self>
+ where Self: Sized {
+ if has_arguments(header) {
+ return err("linebreak: expected no arguments");
+ }
+
+ if body.is_some() {
+ return err("linebreak: expected no body");
+ }
+
+ Ok(LinebreakFunc)
+ }
+
+ fn layout(&self, _: LayoutContext) -> LayoutResult<CommandList> {
+ Ok(commands![Command::FinishFlexRun])
+ }
+}
+
/// Ends the current page.
#[derive(Debug, PartialEq)]
pub struct PagebreakFunc;
diff --git a/src/library/mod.rs b/src/library/mod.rs
index 7c54a9f6..d0c987a4 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -3,8 +3,9 @@
use crate::func::Scope;
mod align;
-mod styles;
+mod boxed;
mod breaks;
+mod styles;
/// Useful imports for creating your own functions.
pub mod prelude {
@@ -17,17 +18,21 @@ pub mod prelude {
}
pub use align::AlignFunc;
-pub use breaks::PagebreakFunc;
+pub use boxed::BoxFunc;
+pub use breaks::{LinebreakFunc, PagebreakFunc};
pub use styles::{BoldFunc, ItalicFunc, MonospaceFunc};
/// Create a scope with all standard functions.
pub fn std() -> Scope {
let mut std = Scope::new();
+ std.add::<AlignFunc>("align");
+ std.add::<BoxFunc>("box");
+ std.add::<LinebreakFunc>("linebreak");
+ std.add::<LinebreakFunc>("n");
+ std.add::<PagebreakFunc>("pagebreak");
std.add::<BoldFunc>("bold");
std.add::<ItalicFunc>("italic");
std.add::<MonospaceFunc>("mono");
- std.add::<AlignFunc>("align");
- std.add::<PagebreakFunc>("pagebreak");
std
}