diff --git a/Cargo.lock b/Cargo.lock index 5a2e079eb..e429f4676 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -334,6 +334,15 @@ dependencies = [ "safemem", ] +[[package]] +name = "lipsum" +version = "0.8.0" +source = "git+https://github.com/reknih/lipsum#c97ce95ba01ed2cce1d1b0b230b6b78295b0720b" +dependencies = [ + "rand", + "rand_chacha", +] + [[package]] name = "lock_api" version = "0.4.6" @@ -543,6 +552,12 @@ dependencies = [ "miniz_oxide 0.5.1", ] +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "proc-macro2" version = "1.0.36" @@ -561,6 +576,31 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" + [[package]] name = "rctree" version = "0.4.0" @@ -854,6 +894,7 @@ dependencies = [ "iai", "image", "kurbo", + "lipsum", "memmap2", "miniz_oxide 0.4.4", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 341a96d10..1dbe7450a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ typst-macros = { path = "./macros" } bytemuck = "1" either = "1" fxhash = "0.2" +lipsum = { git = "https://github.com/reknih/lipsum", default-features = false } once_cell = "1" serde = { version = "1", features = ["derive"] } typed-arena = "2" diff --git a/src/library/mod.rs b/src/library/mod.rs index d3ed98da5..c68915c8a 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -88,6 +88,7 @@ pub fn new() -> Scope { std.def_fn("letter", utility::letter); std.def_fn("roman", utility::roman); std.def_fn("symbol", utility::symbol); + std.def_fn("lipsum", utility::lipsum); // Predefined colors. std.def_const("black", Color::BLACK); diff --git a/src/library/utility/blind.rs b/src/library/utility/blind.rs new file mode 100644 index 000000000..a4cfec90b --- /dev/null +++ b/src/library/utility/blind.rs @@ -0,0 +1,9 @@ +use lipsum::lipsum_from_seed; + +use crate::library::prelude::*; + +/// Create blind text. +pub fn lipsum(_: &mut Context, args: &mut Args) -> TypResult { + let words: usize = args.expect("number of words")?; + Ok(Value::Str(lipsum_from_seed(words, 97).into())) +} diff --git a/src/library/utility/mod.rs b/src/library/utility/mod.rs index 13220242b..4244ccbf1 100644 --- a/src/library/utility/mod.rs +++ b/src/library/utility/mod.rs @@ -1,9 +1,11 @@ //! Computational utility functions. +mod blind; mod color; mod math; mod string; +pub use blind::*; pub use color::*; pub use math::*; pub use string::*; diff --git a/tests/ref/utility/blind.png b/tests/ref/utility/blind.png new file mode 100644 index 000000000..e972567e0 Binary files /dev/null and b/tests/ref/utility/blind.png differ diff --git a/tests/typ/utility/blind.typ b/tests/typ/utility/blind.typ new file mode 100644 index 000000000..7d1cb9694 --- /dev/null +++ b/tests/typ/utility/blind.typ @@ -0,0 +1,32 @@ +// Test blind text. + +--- +// Test basic call. +#lipsum(19) + +--- +// Test custom paragraphs with user code. +#set text(8pt) + +{ + let sentences = lipsum(59) + .split(".") + .filter(s => s != "") + .map(s => s + ".") + + let used = 0 + for s in sentences { + if used < 2 { + used += 1 + } else { + parbreak() + used = 0 + } + s.trim() + [ ] + } +} + +--- +// Error: 8-10 missing argument: number of words +#lipsum()