Replace encode with field

This commit is contained in:
Laurenz 2022-10-31 13:49:10 +01:00
parent 636bdb9e43
commit 671ce3dedd
16 changed files with 74 additions and 75 deletions

View File

@ -84,8 +84,8 @@ impl Show for MathNode {
ShowNode::new(self.clone()) ShowNode::new(self.clone())
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, _: &str) -> Option<Value> {
todo!() None
} }
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {

View File

@ -73,12 +73,11 @@ impl Show for HeadingNode {
Self { body: self.body.unguard(sel), ..*self }.pack() Self { body: self.body.unguard(sel), ..*self }.pack()
} }
fn encode(&self, styles: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"level" => Value::Int(self.level.get() as i64), "level" => Some(Value::Int(self.level.get() as i64)),
"body" => Value::Content(self.body.clone()), "body" => Some(Value::Content(self.body.clone())),
"outlined" => Value::Bool(styles.get(Self::OUTLINED)), _ => None,
"numbered" => Value::Bool(styles.get(Self::NUMBERED)),
} }
} }

View File

@ -90,16 +90,14 @@ impl<const L: ListKind> Show for ListNode<L> {
.pack() .pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"tight" => Value::Bool(self.tight), "tight" => Some(Value::Bool(self.tight)),
"attached" => Value::Bool(self.attached), "attached" => Some(Value::Bool(self.attached)),
"items" => Value::Array( "items" => Some(Value::Array(
self.items self.items.items().map(|item| item.encode()).collect(),
.items() )),
.map(|item| item.encode()) _ => None,
.collect()
),
} }
} }

View File

@ -16,9 +16,10 @@ impl Show for RefNode {
Self(self.0.clone()).pack() Self(self.0.clone()).pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"label" => Value::Str(self.0.clone().into()), "label" => Some(Value::Str(self.0.clone().into())),
_ => None,
} }
} }

View File

@ -57,14 +57,12 @@ impl Show for TableNode {
.pack() .pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"cells" => Value::Array( "cells" => Some(Value::Array(
self.cells self.cells.iter().cloned().map(Value::Content).collect(),
.iter() )),
.map(|cell| Value::Content(cell.clone())) _ => None,
.collect()
),
} }
} }

View File

@ -44,8 +44,11 @@ impl<const L: DecoLine> Show for DecoNode<L> {
Self(self.0.unguard(sel)).pack() Self(self.0.unguard(sel)).pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { "body" => Value::Content(self.0.clone()) } match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
} }
fn realize( fn realize(

View File

@ -58,16 +58,17 @@ impl Show for LinkNode {
.pack() .pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"url" => match &self.dest { "url" => Some(match &self.dest {
Destination::Url(url) => Value::Str(url.clone().into()), Destination::Url(url) => Value::Str(url.clone().into()),
Destination::Internal(loc) => Value::Dict(loc.encode()), Destination::Internal(loc) => Value::Dict(loc.encode()),
}, }),
"body" => match &self.body { "body" => Some(match &self.body {
Some(body) => Value::Content(body.clone()), Some(body) => Value::Content(body.clone()),
None => Value::None, None => Value::None,
}, }),
_ => None,
} }
} }

View File

@ -515,8 +515,11 @@ impl Show for StrongNode {
Self(self.0.unguard(sel)).pack() Self(self.0.unguard(sel)).pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { "body" => Value::Content(self.0.clone()) } match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
} }
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {
@ -540,8 +543,11 @@ impl Show for EmphNode {
Self(self.0.unguard(sel)).pack() Self(self.0.unguard(sel)).pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { "body" => Value::Content(self.0.clone()) } match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
} }
fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { fn realize(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> {

View File

@ -46,14 +46,11 @@ impl Show for RawNode {
Self { text: self.text.clone(), ..*self }.pack() Self { text: self.text.clone(), ..*self }.pack()
} }
fn encode(&self, styles: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { match name {
"text" => Value::Str(self.text.clone().into()), "text" => Some(Value::Str(self.text.clone().into())),
"block" => Value::Bool(self.block), "block" => Some(Value::Bool(self.block)),
"lang" => match styles.get(Self::LANG) { _ => None,
Some(lang) => Value::Str(lang.clone().into()),
None => Value::None,
},
} }
} }

View File

@ -38,8 +38,11 @@ impl<const S: ScriptKind> Show for ShiftNode<S> {
Self(self.0.clone()).pack() Self(self.0.clone()).pack()
} }
fn encode(&self, _: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
dict! { "body" => Value::Content(self.0.clone()) } match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
} }
fn realize( fn realize(

View File

@ -7,7 +7,7 @@ use std::sync::Arc;
use comemo::Tracked; use comemo::Tracked;
use super::{ 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, ShowNode, StyleChain, StyleEntry, StyleMap,
}; };
use crate::diag::{SourceResult, StrResult}; use crate::diag::{SourceResult, StrResult};
@ -73,7 +73,7 @@ pub enum Content {
Page(PageNode), Page(PageNode),
/// A node that can be realized with styles, optionally with attached /// A node that can be realized with styles, optionally with attached
/// properties. /// properties.
Show(ShowNode, Option<Dict>), Show(ShowNode),
/// Content with attached styles. /// Content with attached styles.
Styled(Arc<(Self, StyleMap)>), Styled(Arc<(Self, StyleMap)>),
/// A sequence of multiple nodes. /// A sequence of multiple nodes.
@ -107,7 +107,7 @@ impl Content {
where where
T: Show + Debug + Hash + Sync + Send + 'static, T: Show + Debug + Hash + Sync + Send + 'static,
{ {
Self::Show(node.pack(), None) Self::Show(node.pack())
} }
/// Create a new sequence node from multiples nodes. /// Create a new sequence node from multiples nodes.
@ -242,7 +242,7 @@ impl Debug for Content {
Self::Item(item) => item.fmt(f), Self::Item(item) => item.fmt(f),
Self::Pagebreak { weak } => write!(f, "Pagebreak({weak})"), Self::Pagebreak { weak } => write!(f, "Pagebreak({weak})"),
Self::Page(page) => page.fmt(f), Self::Page(page) => page.fmt(f),
Self::Show(node, _) => node.fmt(f), Self::Show(node) => node.fmt(f),
Self::Styled(styled) => { Self::Styled(styled) => {
let (sub, map) = styled.as_ref(); let (sub, map) = styled.as_ref();
map.fmt(f)?; map.fmt(f)?;

View File

@ -8,7 +8,7 @@ use unicode_segmentation::UnicodeSegmentation;
use super::{ use super::{
methods, ops, Arg, Args, Array, CapturesVisitor, Closure, Content, Dict, Flow, Func, 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::diag::{At, SourceResult, StrResult, Trace, Tracepoint};
use crate::geom::{Abs, Angle, Em, Fr, Ratio}; use crate::geom::{Abs, Angle, Em, Fr, Ratio};
@ -706,9 +706,9 @@ impl Eval for ast::FieldAccess {
Ok(match object { Ok(match object {
Value::Dict(dict) => dict.get(&field).at(span)?.clone(), Value::Dict(dict) => dict.get(&field).at(span)?.clone(),
Value::Content(Content::Show(_, Some(dict))) => dict Value::Content(Content::Show(node)) => node
.get(&field) .field(&field)
.map_err(|_| format!("unknown field {field:?}")) .ok_or_else(|| format!("unknown field {field:?}"))
.at(span)? .at(span)?
.clone(), .clone(),

View File

@ -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::Styled(styled) => return self.styled(styled, styles),
Content::Sequence(seq) => return self.sequence(seq, styles), Content::Sequence(seq) => return self.sequence(seq, styles),

View File

@ -3,8 +3,7 @@ use std::fmt::{self, Debug, Formatter};
use comemo::Tracked; use comemo::Tracked;
use super::{ use super::{
Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleChain, Args, Content, Func, Interruption, NodeId, Regex, Show, ShowNode, StyleEntry, Value,
StyleEntry, Value,
}; };
use crate::diag::SourceResult; use crate::diag::SourceResult;
use crate::library::structure::{DescNode, EnumNode, ListNode}; use crate::library::structure::{DescNode, EnumNode, ListNode};
@ -34,17 +33,13 @@ impl Recipe {
pub fn apply( pub fn apply(
&self, &self,
world: Tracked<dyn World>, world: Tracked<dyn World>,
styles: StyleChain,
sel: Selector, sel: Selector,
target: Target, target: Target,
) -> SourceResult<Option<Content>> { ) -> SourceResult<Option<Content>> {
let content = match (target, &self.pattern) { let content = match (target, &self.pattern) {
(Target::Node(node), &Pattern::Node(id)) if node.id() == id => { (Target::Node(node), &Pattern::Node(id)) if node.id() == id => {
let node = node.unguard(sel); let node = node.unguard(sel);
self.call(world, || { self.call(world, || Value::Content(Content::Show(node)))?
let dict = node.encode(styles);
Value::Content(Content::Show(node, Some(dict)))
})?
} }
(Target::Text(text), Pattern::Regex(regex)) => { (Target::Text(text), Pattern::Regex(regex)) => {

View File

@ -4,7 +4,7 @@ use std::sync::Arc;
use comemo::{Prehashed, Tracked}; use comemo::{Prehashed, Tracked};
use super::{Content, Dict, NodeId, Selector, StyleChain}; use super::{Content, NodeId, Selector, StyleChain, Value};
use crate::diag::SourceResult; use crate::diag::SourceResult;
use crate::World; use crate::World;
@ -13,8 +13,8 @@ pub trait Show: 'static {
/// Unguard nested content against recursive show rules. /// Unguard nested content against recursive show rules.
fn unguard(&self, sel: Selector) -> ShowNode; fn unguard(&self, sel: Selector) -> ShowNode;
/// Encode this node into a dictionary. /// Access a field on this node.
fn encode(&self, styles: StyleChain) -> Dict; fn field(&self, name: &str) -> Option<Value>;
/// The base recipe for this node that is executed if there is no /// The base recipe for this node that is executed if there is no
/// user-defined show rule. /// user-defined show rule.
@ -74,8 +74,8 @@ impl Show for ShowNode {
self.0.unguard(sel) self.0.unguard(sel)
} }
fn encode(&self, styles: StyleChain) -> Dict { fn field(&self, name: &str) -> Option<Value> {
self.0.encode(styles) self.0.field(name)
} }
fn realize( fn realize(

View File

@ -286,9 +286,7 @@ impl<'a> StyleChain<'a> {
let sel = Selector::Nth(n); let sel = Selector::Nth(n);
if self.guarded(sel) { if self.guarded(sel) {
guarded = true; guarded = true;
} else if let Some(content) = } else if let Some(content) = recipe.apply(world, sel, target)? {
recipe.apply(world, self, sel, target)?
{
realized = Some(content); realized = Some(content);
break; break;
} }