Replace encode
with field
This commit is contained in:
parent
636bdb9e43
commit
671ce3dedd
@ -84,8 +84,8 @@ impl Show for MathNode {
|
||||
ShowNode::new(self.clone())
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
todo!()
|
||||
fn field(&self, _: &str) -> Option<Value> {
|
||||
None
|
||||
}
|
||||
|
||||
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
|
||||
|
@ -73,12 +73,11 @@ impl Show for HeadingNode {
|
||||
Self { body: self.body.unguard(sel), ..*self }.pack()
|
||||
}
|
||||
|
||||
fn encode(&self, styles: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"level" => Value::Int(self.level.get() as i64),
|
||||
"body" => Value::Content(self.body.clone()),
|
||||
"outlined" => Value::Bool(styles.get(Self::OUTLINED)),
|
||||
"numbered" => Value::Bool(styles.get(Self::NUMBERED)),
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"level" => Some(Value::Int(self.level.get() as i64)),
|
||||
"body" => Some(Value::Content(self.body.clone())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,16 +90,14 @@ impl<const L: ListKind> Show for ListNode<L> {
|
||||
.pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"tight" => Value::Bool(self.tight),
|
||||
"attached" => Value::Bool(self.attached),
|
||||
"items" => Value::Array(
|
||||
self.items
|
||||
.items()
|
||||
.map(|item| item.encode())
|
||||
.collect()
|
||||
),
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"tight" => Some(Value::Bool(self.tight)),
|
||||
"attached" => Some(Value::Bool(self.attached)),
|
||||
"items" => Some(Value::Array(
|
||||
self.items.items().map(|item| item.encode()).collect(),
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,10 @@ impl Show for RefNode {
|
||||
Self(self.0.clone()).pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"label" => Value::Str(self.0.clone().into()),
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"label" => Some(Value::Str(self.0.clone().into())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,14 +57,12 @@ impl Show for TableNode {
|
||||
.pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"cells" => Value::Array(
|
||||
self.cells
|
||||
.iter()
|
||||
.map(|cell| Value::Content(cell.clone()))
|
||||
.collect()
|
||||
),
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"cells" => Some(Value::Array(
|
||||
self.cells.iter().cloned().map(Value::Content).collect(),
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,11 @@ impl<const L: DecoLine> Show for DecoNode<L> {
|
||||
Self(self.0.unguard(sel)).pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! { "body" => Value::Content(self.0.clone()) }
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"body" => Some(Value::Content(self.0.clone())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn realize(
|
||||
|
@ -58,16 +58,17 @@ impl Show for LinkNode {
|
||||
.pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"url" => match &self.dest {
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"url" => Some(match &self.dest {
|
||||
Destination::Url(url) => Value::Str(url.clone().into()),
|
||||
Destination::Internal(loc) => Value::Dict(loc.encode()),
|
||||
},
|
||||
"body" => match &self.body {
|
||||
}),
|
||||
"body" => Some(match &self.body {
|
||||
Some(body) => Value::Content(body.clone()),
|
||||
None => Value::None,
|
||||
},
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -515,8 +515,11 @@ impl Show for StrongNode {
|
||||
Self(self.0.unguard(sel)).pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! { "body" => Value::Content(self.0.clone()) }
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"body" => Some(Value::Content(self.0.clone())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
|
||||
@ -540,8 +543,11 @@ impl Show for EmphNode {
|
||||
Self(self.0.unguard(sel)).pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! { "body" => Value::Content(self.0.clone()) }
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"body" => Some(Value::Content(self.0.clone())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
|
||||
|
@ -46,14 +46,11 @@ impl Show for RawNode {
|
||||
Self { text: self.text.clone(), ..*self }.pack()
|
||||
}
|
||||
|
||||
fn encode(&self, styles: StyleChain) -> Dict {
|
||||
dict! {
|
||||
"text" => Value::Str(self.text.clone().into()),
|
||||
"block" => Value::Bool(self.block),
|
||||
"lang" => match styles.get(Self::LANG) {
|
||||
Some(lang) => Value::Str(lang.clone().into()),
|
||||
None => Value::None,
|
||||
},
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"text" => Some(Value::Str(self.text.clone().into())),
|
||||
"block" => Some(Value::Bool(self.block)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,8 +38,11 @@ impl<const S: ScriptKind> Show for ShiftNode<S> {
|
||||
Self(self.0.clone()).pack()
|
||||
}
|
||||
|
||||
fn encode(&self, _: StyleChain) -> Dict {
|
||||
dict! { "body" => Value::Content(self.0.clone()) }
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
match name {
|
||||
"body" => Some(Value::Content(self.0.clone())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn realize(
|
||||
|
@ -7,7 +7,7 @@ use std::sync::Arc;
|
||||
use comemo::Tracked;
|
||||
|
||||
use super::{
|
||||
Builder, Dict, Key, Layout, LayoutNode, Property, Regions, Scratch, Selector, Show,
|
||||
Builder, Key, Layout, LayoutNode, Property, Regions, Scratch, Selector, Show,
|
||||
ShowNode, StyleChain, StyleEntry, StyleMap,
|
||||
};
|
||||
use crate::diag::{SourceResult, StrResult};
|
||||
@ -73,7 +73,7 @@ pub enum Content {
|
||||
Page(PageNode),
|
||||
/// A node that can be realized with styles, optionally with attached
|
||||
/// properties.
|
||||
Show(ShowNode, Option<Dict>),
|
||||
Show(ShowNode),
|
||||
/// Content with attached styles.
|
||||
Styled(Arc<(Self, StyleMap)>),
|
||||
/// A sequence of multiple nodes.
|
||||
@ -107,7 +107,7 @@ impl Content {
|
||||
where
|
||||
T: Show + Debug + Hash + Sync + Send + 'static,
|
||||
{
|
||||
Self::Show(node.pack(), None)
|
||||
Self::Show(node.pack())
|
||||
}
|
||||
|
||||
/// Create a new sequence node from multiples nodes.
|
||||
@ -242,7 +242,7 @@ impl Debug for Content {
|
||||
Self::Item(item) => item.fmt(f),
|
||||
Self::Pagebreak { weak } => write!(f, "Pagebreak({weak})"),
|
||||
Self::Page(page) => page.fmt(f),
|
||||
Self::Show(node, _) => node.fmt(f),
|
||||
Self::Show(node) => node.fmt(f),
|
||||
Self::Styled(styled) => {
|
||||
let (sub, map) = styled.as_ref();
|
||||
map.fmt(f)?;
|
||||
|
@ -8,7 +8,7 @@ use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::{
|
||||
methods, ops, Arg, Args, Array, CapturesVisitor, Closure, Content, Dict, Flow, Func,
|
||||
Pattern, Recipe, Scope, Scopes, StyleEntry, StyleMap, Value, Vm,
|
||||
Pattern, Recipe, Scope, Scopes, Show, StyleEntry, StyleMap, Value, Vm,
|
||||
};
|
||||
use crate::diag::{At, SourceResult, StrResult, Trace, Tracepoint};
|
||||
use crate::geom::{Abs, Angle, Em, Fr, Ratio};
|
||||
@ -706,9 +706,9 @@ impl Eval for ast::FieldAccess {
|
||||
Ok(match object {
|
||||
Value::Dict(dict) => dict.get(&field).at(span)?.clone(),
|
||||
|
||||
Value::Content(Content::Show(_, Some(dict))) => dict
|
||||
.get(&field)
|
||||
.map_err(|_| format!("unknown field {field:?}"))
|
||||
Value::Content(Content::Show(node)) => node
|
||||
.field(&field)
|
||||
.ok_or_else(|| format!("unknown field {field:?}"))
|
||||
.at(span)?
|
||||
.clone(),
|
||||
|
||||
|
@ -87,7 +87,7 @@ impl<'a> Builder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
Content::Show(node, _) => return self.show(node, styles),
|
||||
Content::Show(node) => return self.show(node, styles),
|
||||
Content::Styled(styled) => return self.styled(styled, styles),
|
||||
Content::Sequence(seq) => return self.sequence(seq, styles),
|
||||
|
||||
|
@ -3,8 +3,7 @@ use std::fmt::{self, Debug, Formatter};
|
||||
use comemo::Tracked;
|
||||
|
||||
use super::{
|
||||
Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleChain,
|
||||
StyleEntry, Value,
|
||||
Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleEntry, Value,
|
||||
};
|
||||
use crate::diag::SourceResult;
|
||||
use crate::library::structure::{DescNode, EnumNode, ListNode};
|
||||
@ -34,17 +33,13 @@ impl Recipe {
|
||||
pub fn apply(
|
||||
&self,
|
||||
world: Tracked<dyn World>,
|
||||
styles: StyleChain,
|
||||
sel: Selector,
|
||||
target: Target,
|
||||
) -> SourceResult<Option<Content>> {
|
||||
let content = match (target, &self.pattern) {
|
||||
(Target::Node(node), &Pattern::Node(id)) if node.id() == id => {
|
||||
let node = node.unguard(sel);
|
||||
self.call(world, || {
|
||||
let dict = node.encode(styles);
|
||||
Value::Content(Content::Show(node, Some(dict)))
|
||||
})?
|
||||
self.call(world, || Value::Content(Content::Show(node)))?
|
||||
}
|
||||
|
||||
(Target::Text(text), Pattern::Regex(regex)) => {
|
||||
|
@ -4,7 +4,7 @@ use std::sync::Arc;
|
||||
|
||||
use comemo::{Prehashed, Tracked};
|
||||
|
||||
use super::{Content, Dict, NodeId, Selector, StyleChain};
|
||||
use super::{Content, NodeId, Selector, StyleChain, Value};
|
||||
use crate::diag::SourceResult;
|
||||
use crate::World;
|
||||
|
||||
@ -13,8 +13,8 @@ pub trait Show: 'static {
|
||||
/// Unguard nested content against recursive show rules.
|
||||
fn unguard(&self, sel: Selector) -> ShowNode;
|
||||
|
||||
/// Encode this node into a dictionary.
|
||||
fn encode(&self, styles: StyleChain) -> Dict;
|
||||
/// Access a field on this node.
|
||||
fn field(&self, name: &str) -> Option<Value>;
|
||||
|
||||
/// The base recipe for this node that is executed if there is no
|
||||
/// user-defined show rule.
|
||||
@ -74,8 +74,8 @@ impl Show for ShowNode {
|
||||
self.0.unguard(sel)
|
||||
}
|
||||
|
||||
fn encode(&self, styles: StyleChain) -> Dict {
|
||||
self.0.encode(styles)
|
||||
fn field(&self, name: &str) -> Option<Value> {
|
||||
self.0.field(name)
|
||||
}
|
||||
|
||||
fn realize(
|
||||
|
@ -286,9 +286,7 @@ impl<'a> StyleChain<'a> {
|
||||
let sel = Selector::Nth(n);
|
||||
if self.guarded(sel) {
|
||||
guarded = true;
|
||||
} else if let Some(content) =
|
||||
recipe.apply(world, self, sel, target)?
|
||||
{
|
||||
} else if let Some(content) = recipe.apply(world, sel, target)? {
|
||||
realized = Some(content);
|
||||
break;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user