mirror of
https://github.com/typst/typst
synced 2025-05-19 11:35:27 +08:00
Documentation improvements (#2492)
This commit is contained in:
parent
c3114fa380
commit
27ab2bb9a2
@ -169,7 +169,7 @@ pub fn exp(
|
|||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the square root of a number.
|
/// Calculates the square root of a number.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
/// #calc.sqrt(16) \
|
/// #calc.sqrt(16) \
|
||||||
@ -486,7 +486,7 @@ fn fact_impl(start: u64, end: u64) -> Option<i64> {
|
|||||||
|
|
||||||
/// Calculates a binomial coefficient.
|
/// Calculates a binomial coefficient.
|
||||||
///
|
///
|
||||||
/// Returns the `k`-combination `n`, or the number of ways to choose `k`
|
/// Returns the `k`-combination of `n`, or the number of ways to choose `k`
|
||||||
/// items from a set of `n` without regard to order.
|
/// items from a set of `n` without regard to order.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -615,7 +615,7 @@ pub fn ceil(
|
|||||||
/// ```example
|
/// ```example
|
||||||
/// #assert(calc.trunc(3) == 3)
|
/// #assert(calc.trunc(3) == 3)
|
||||||
/// #assert(calc.trunc(-3.7) == -3)
|
/// #assert(calc.trunc(-3.7) == -3)
|
||||||
/// #assert(calc.trunc(15.9) == 15)
|
/// #calc.trunc(15.9)
|
||||||
/// ```
|
/// ```
|
||||||
#[func(title = "Truncate")]
|
#[func(title = "Truncate")]
|
||||||
pub fn trunc(
|
pub fn trunc(
|
||||||
|
@ -56,6 +56,9 @@ pub fn repr(
|
|||||||
|
|
||||||
/// Fails with an error.
|
/// Fails with an error.
|
||||||
///
|
///
|
||||||
|
/// Arguments are displayed to the user (not rendered in the document) as
|
||||||
|
/// strings, converting with `repr` if necessary.
|
||||||
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// The code below produces the error `panicked with: "this is wrong"`.
|
/// The code below produces the error `panicked with: "this is wrong"`.
|
||||||
/// ```typ
|
/// ```typ
|
||||||
@ -63,7 +66,7 @@ pub fn repr(
|
|||||||
/// ```
|
/// ```
|
||||||
#[func(keywords = ["error"])]
|
#[func(keywords = ["error"])]
|
||||||
pub fn panic(
|
pub fn panic(
|
||||||
/// The values to panic with.
|
/// The values to panic with and display to the user.
|
||||||
#[variadic]
|
#[variadic]
|
||||||
values: Vec<Value>,
|
values: Vec<Value>,
|
||||||
) -> StrResult<Never> {
|
) -> StrResult<Never> {
|
||||||
|
@ -451,15 +451,18 @@ impl Array {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Zips the array with other arrays. If the arrays are of unequal length,
|
/// Zips the array with other arrays.
|
||||||
/// it will only zip up until the last element of the shortest array and the
|
///
|
||||||
/// remaining elements will be ignored. The return value is an array where
|
/// Returns an array of arrays, where the `i`th inner array contains all the
|
||||||
/// each element is yet another array, the size of each of those is the
|
/// `i`th elements from each original array.
|
||||||
/// number of zipped arrays.
|
///
|
||||||
|
/// If the arrays to be zipped have different lengths, they are zipped up to
|
||||||
|
/// the last element of the shortest array and all remaining elements are
|
||||||
|
/// ignored.
|
||||||
///
|
///
|
||||||
/// This function is variadic, meaning that you can zip multiple arrays
|
/// This function is variadic, meaning that you can zip multiple arrays
|
||||||
/// together at once: `{(1, 2, 3).zip((3, 4, 5), (6, 7, 8))}` yields
|
/// together at once: `{(1, 2).zip(("A", "B"), (10, 20))}` yields
|
||||||
/// `{((1, 3, 6), (2, 4, 7), (3, 5, 8))}`.
|
/// `{((1, "A", 10), (2, "B", 20))}`.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn zip(
|
pub fn zip(
|
||||||
self,
|
self,
|
||||||
|
@ -345,7 +345,7 @@ impl Datetime {
|
|||||||
result.map(EcoString::from).map_err(format_time_format_error)
|
result.map(EcoString::from).map_err(format_time_format_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The year if it was specified or `{none}`, otherwise.
|
/// The year if it was specified, or `{none}` for times without a date.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn year(&self) -> Option<i32> {
|
pub fn year(&self) -> Option<i32> {
|
||||||
match self {
|
match self {
|
||||||
@ -355,7 +355,7 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The month if it was specified or `{none}`, otherwise.
|
/// The month if it was specified, or `{none}` for times without a date.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn month(&self) -> Option<u8> {
|
pub fn month(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
@ -365,7 +365,7 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The weekday if it was specified or `{none}`, otherwise.
|
/// The weekday (counting Monday as 1) or `{none}` for times without a date.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn weekday(&self) -> Option<u8> {
|
pub fn weekday(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
@ -375,7 +375,7 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The day if it was specified or `{none}`, otherwise.
|
/// The day if it was specified, or `{none}` for times without a date.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn day(&self) -> Option<u8> {
|
pub fn day(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
@ -385,7 +385,7 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The hour if it was specified or `{none}`, otherwise.
|
/// The hour if it was specified, or `{none}` for dates without a time.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn hour(&self) -> Option<u8> {
|
pub fn hour(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
@ -395,7 +395,7 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The minute if it was specified or `{none}`, otherwise.
|
/// The minute if it was specified, or `{none}` for dates without a time.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn minute(&self) -> Option<u8> {
|
pub fn minute(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
@ -405,17 +405,17 @@ impl Datetime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The second if it was specified or `{none}`, otherwise.
|
/// The second if it was specified, or `{none}` for dates without a time.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn second(&self) -> Option<u8> {
|
pub fn second(&self) -> Option<u8> {
|
||||||
match self {
|
match self {
|
||||||
Datetime::Date(_) => None,
|
Self::Date(_) => None,
|
||||||
Self::Time(time) => Some(time.second()),
|
Self::Time(time) => Some(time.second()),
|
||||||
Self::Datetime(datetime) => Some(datetime.second()),
|
Self::Datetime(datetime) => Some(datetime.second()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The ordinal (day of the year), if it exists.
|
/// The ordinal (day of the year), or `{none}` for times without a date.
|
||||||
#[func]
|
#[func]
|
||||||
pub fn ordinal(&self) -> Option<u16> {
|
pub fn ordinal(&self) -> Option<u16> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -71,10 +71,19 @@ pub use typst_macros::func;
|
|||||||
/// # Defining functions
|
/// # Defining functions
|
||||||
/// You can define your own function with a [let binding]($scripting/#bindings)
|
/// You can define your own function with a [let binding]($scripting/#bindings)
|
||||||
/// that has a parameter list after the binding's name. The parameter list can
|
/// that has a parameter list after the binding's name. The parameter list can
|
||||||
/// contain positional parameters, named parameters with default values and
|
/// contain mandatory positional parameters, named parameters with default
|
||||||
/// [argument sinks]($arguments). The right-hand side of the binding can be a
|
/// values and [argument sinks]($arguments).
|
||||||
/// block or any other expression. It defines the function's return value and
|
///
|
||||||
/// can depend on the parameters.
|
/// The right-hand side of a function binding is the function body, which can be
|
||||||
|
/// a block or any other expression. It defines the function's return value and
|
||||||
|
/// can depend on the parameters. If the function body is a [code
|
||||||
|
/// block]($scripting/#blocks), the return value is the result of joining the
|
||||||
|
/// values of each expression in the block.
|
||||||
|
///
|
||||||
|
/// Within a function body, the `return` keyword can be used to exit early and
|
||||||
|
/// optionally specify a return value. If no explicit return value is given, the
|
||||||
|
/// body evaluates to the result of joining all expressions preceding the
|
||||||
|
/// `return`.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
/// #let alert(body, fill: red) = {
|
/// #let alert(body, fill: red) = {
|
||||||
@ -110,7 +119,7 @@ pub use typst_macros::func;
|
|||||||
/// once?
|
/// once?
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Notable fact
|
/// # Note on function purity
|
||||||
/// In Typst, all functions are _pure._ This means that for the same
|
/// In Typst, all functions are _pure._ This means that for the same
|
||||||
/// arguments, they always return the same result. They cannot "remember" things to
|
/// arguments, they always return the same result. They cannot "remember" things to
|
||||||
/// produce another value when they are called a second time.
|
/// produce another value when they are called a second time.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user