Add blind text function

This commit is contained in:
Martin Haug 2022-04-25 12:58:10 +02:00
parent 7163b4a6c5
commit 649c101f07
7 changed files with 86 additions and 0 deletions

41
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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);

View File

@ -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<Value> {
let words: usize = args.expect("number of words")?;
Ok(Value::Str(lipsum_from_seed(words, 97).into()))
}

View File

@ -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::*;

BIN
tests/ref/utility/blind.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -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()