mirror of
https://github.com/typst/typst
synced 2025-05-22 04:55:29 +08:00
Add fields
method to content (#1340)
This commit is contained in:
parent
b6b6666efd
commit
644bbf9914
@ -604,6 +604,13 @@ field does not exist or fails with an error if no default value was specified.
|
|||||||
A default value to return if the field does not exist.
|
A default value to return if the field does not exist.
|
||||||
- returns: any
|
- returns: any
|
||||||
|
|
||||||
|
### fields()
|
||||||
|
Return the fields of this content.
|
||||||
|
|
||||||
|
```example
|
||||||
|
#repr(rect(width: 10cm, height: 10cm).fields())
|
||||||
|
```
|
||||||
|
|
||||||
### location()
|
### location()
|
||||||
The location of the content. This is only available on content returned by
|
The location of the content. This is only available on content returned by
|
||||||
[query]($func/query), for other content it will fail with an error. The
|
[query]($func/query), for other content it will fail with an error. The
|
||||||
|
@ -81,6 +81,7 @@ pub fn call(
|
|||||||
"at" => content
|
"at" => content
|
||||||
.at(&args.expect::<EcoString>("field")?, args.named("default")?)
|
.at(&args.expect::<EcoString>("field")?, args.named("default")?)
|
||||||
.at(span)?,
|
.at(span)?,
|
||||||
|
"fields" => Value::Dict(content.dict()),
|
||||||
"location" => content
|
"location" => content
|
||||||
.location()
|
.location()
|
||||||
.ok_or("this method can only be called on content returned by query(..)")
|
.ok_or("this method can only be called on content returned by query(..)")
|
||||||
@ -332,7 +333,13 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] {
|
|||||||
("starts-with", true),
|
("starts-with", true),
|
||||||
("trim", true),
|
("trim", true),
|
||||||
],
|
],
|
||||||
"content" => &[("func", false), ("has", true), ("at", true), ("location", false)],
|
"content" => &[
|
||||||
|
("func", false),
|
||||||
|
("has", true),
|
||||||
|
("at", true),
|
||||||
|
("fields", false),
|
||||||
|
("location", false),
|
||||||
|
],
|
||||||
"array" => &[
|
"array" => &[
|
||||||
("all", true),
|
("all", true),
|
||||||
("any", true),
|
("any", true),
|
||||||
|
@ -12,7 +12,7 @@ use super::{
|
|||||||
};
|
};
|
||||||
use crate::diag::{SourceResult, StrResult};
|
use crate::diag::{SourceResult, StrResult};
|
||||||
use crate::doc::Meta;
|
use crate::doc::Meta;
|
||||||
use crate::eval::{Cast, Str, Value, Vm};
|
use crate::eval::{Cast, Dict, Str, Value, Vm};
|
||||||
use crate::syntax::Span;
|
use crate::syntax::Span;
|
||||||
use crate::util::pretty_array_like;
|
use crate::util::pretty_array_like;
|
||||||
|
|
||||||
@ -251,6 +251,13 @@ impl Content {
|
|||||||
.ok_or_else(|| missing_field_no_default(field))
|
.ok_or_else(|| missing_field_no_default(field))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the fields of the content as a dict.
|
||||||
|
pub fn dict(&self) -> Dict {
|
||||||
|
self.fields()
|
||||||
|
.map(|(key, value)| (key.to_owned().into(), value))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
/// The content's label.
|
/// The content's label.
|
||||||
pub fn label(&self) -> Option<&Label> {
|
pub fn label(&self) -> Option<&Label> {
|
||||||
match self.field_ref("label")? {
|
match self.field_ref("label")? {
|
||||||
|
@ -48,3 +48,8 @@
|
|||||||
---
|
---
|
||||||
// Error: 2-5 cannot mutate a constant: box
|
// Error: 2-5 cannot mutate a constant: box
|
||||||
#box.push(1)
|
#box.push(1)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test content fields method.
|
||||||
|
#test([a].fields(), (text: "a"))
|
||||||
|
#test([a *b*].fields(), (children: ([a], [ ], strong[b])))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user