Streamline field names

This commit is contained in:
Laurenz 2023-03-08 10:54:04 +01:00
parent 1b2b53ecb9
commit e5eab73374
30 changed files with 102 additions and 74 deletions

View File

@ -5,7 +5,6 @@ authors = ["The Typst Project Developers"]
edition = "2021"
[lib]
test = false
doctest = false
bench = false

View File

@ -309,7 +309,10 @@ fn code_block(resolver: &dyn Resolver, lang: &str, text: &str) -> Html {
let world = DocWorld(source);
let mut frames = match typst::compile(&world, &world.0) {
Ok(doc) => doc.pages,
Err(err) => panic!("failed to compile {text}: {err:?}"),
Err(err) => {
let msg = &err[0].message;
panic!("while trying to compile {text}:\n\nerror: {msg}");
}
};
if let Some([x, y, w, h]) = zoom {

View File

@ -745,3 +745,29 @@ const TYPE_ORDER: &[&str] = &[
"selector",
"stroke",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_docs() {
provide(&TestResolver);
}
struct TestResolver;
impl Resolver for TestResolver {
fn link(&self, _: &str) -> Option<String> {
None
}
fn example(&self, _: Html, _: &[Frame]) -> Html {
Html::new(String::new())
}
fn image(&self, _: &str, _: &[u8]) -> String {
String::new()
}
}
}

View File

@ -85,8 +85,8 @@ fantasy encyclopedia.
#set heading(numbering: "(I)")
#show heading: it => block[
#set align(center)
\~ _#it.title;_
#set text(font: "Inria Serif")
\~ _#it.body;_
#it.numbers \~
]

View File

@ -359,7 +359,7 @@ a way to set any of that, we need to write our own heading show rule.
#show heading: it => block[
#set align(center)
#set text(12pt, weight: "regular")
#smallcaps(it.title)
#smallcaps(it.body)
]
<<< ...
@ -441,7 +441,7 @@ differentiate between section and subsection headings:
): it => block[
#set align(center)
#set text(12pt, weight: "regular")
#smallcaps(it.title)
#smallcaps(it.body)
]
#show heading.where(
@ -450,7 +450,7 @@ differentiate between section and subsection headings:
size: 11pt,
weight: "regular",
style: "italic",
it.title + [.],
it.body + [.],
)
>>>
>>> #align(center, text(

View File

@ -107,7 +107,7 @@ previous chapter.
>>> text(
>>> 13pt,
>>> weight: "regular",
>>> smallcaps(it.title),
>>> smallcaps(it.body),
>>> )
>>> ),
>>> )
@ -118,7 +118,7 @@ previous chapter.
>>> 11pt,
>>> weight: "regular",
>>> style: "italic",
>>> it.title + [.],
>>> it.body + [.],
>>> )
>>> )
@ -288,7 +288,7 @@ path of the file after the `{from}` keyword.
>>> text(
>>> 13pt,
>>> weight: "regular",
>>> smallcaps(it.title),
>>> smallcaps(it.body),
>>> )
>>> ),
>>> )
@ -299,7 +299,7 @@ path of the file after the `{from}` keyword.
>>> 11pt,
>>> weight: "regular",
>>> style: "italic",
>>> it.title + [.],
>>> it.body + [.],
>>> )
>>> )
>>>

View File

@ -20,11 +20,6 @@ use crate::prelude::*;
styles.set(Self::ALIGNMENT, aligns);
})]
pub struct AlignNode {
/// The content to align.
#[positional]
#[required]
pub body: Content,
/// The alignment along both axes.
///
/// Possible values for horizontal alignments are:
@ -62,6 +57,11 @@ pub struct AlignNode {
#[skip]
#[default(Axes::new(GenAlign::Start, GenAlign::Specific(Align::Top)))]
pub alignment: Axes<Option<GenAlign>>,
/// The content to align.
#[positional]
#[required]
pub body: Content,
}
impl Show for AlignNode {

View File

@ -26,7 +26,7 @@ pub struct BoxNode {
/// The contents of the box.
#[positional]
#[default]
pub body: Content,
pub body: Option<Content>,
/// The width of the box.
///
@ -133,16 +133,16 @@ impl Layout for BoxNode {
.unwrap_or(regions.base());
// Apply inset.
let mut child = self.body();
let mut body = self.body().unwrap_or_default();
let inset = styles.get(Self::INSET);
if inset.iter().any(|v| !v.is_zero()) {
child = child.padded(inset.map(|side| side.map(Length::from)));
body = body.padded(inset.map(|side| side.map(Length::from)));
}
// Select the appropriate base and expansion for the child depending
// on whether it is automatically or relatively sized.
let pod = Regions::one(size, expand);
let mut frame = child.layout(vt, styles, pod)?.into_frame();
let mut frame = body.layout(vt, styles, pod)?.into_frame();
// Apply baseline shift.
let shift = styles.get(Self::BASELINE).relative_to(frame.height());
@ -191,11 +191,11 @@ impl Layout for BoxNode {
/// Blocks are also useful to force elements that would otherwise be inline to
/// become block-level, especially when writing show rules.
/// ```example
/// #show heading: it => it.title
/// #show heading: it => it.body
/// = Blockless
/// More text.
///
/// #show heading: it => block(it.title)
/// #show heading: it => block(it.body)
/// = Blocky
/// More text.
/// ```
@ -236,7 +236,7 @@ pub struct BlockNode {
/// The contents of the block.
#[positional]
#[default]
pub body: Content,
pub body: Option<Content>,
/// The block's width.
///
@ -360,10 +360,10 @@ impl Layout for BlockNode {
regions: Regions,
) -> SourceResult<Fragment> {
// Apply inset.
let mut child = self.body();
let mut body = self.body().unwrap_or_default();
let inset = styles.get(Self::INSET);
if inset.iter().any(|v| !v.is_zero()) {
child = child.clone().padded(inset.map(|side| side.map(Length::from)));
body = body.clone().padded(inset.map(|side| side.map(Length::from)));
}
// Resolve the sizing to a concrete size.
@ -380,7 +380,7 @@ impl Layout for BlockNode {
// Measure to ensure frames for all regions have the same width.
if sizing.x == Smart::Auto {
let pod = Regions::one(size, Axes::splat(false));
let frame = child.layout(vt, styles, pod)?.into_frame();
let frame = body.layout(vt, styles, pod)?.into_frame();
size.x = frame.width();
expand.x = true;
}
@ -407,10 +407,10 @@ impl Layout for BlockNode {
pod.last = None;
}
child.layout(vt, styles, pod)?.into_frames()
body.layout(vt, styles, pod)?.into_frames()
} else {
let pod = Regions::one(size, expand);
child.layout(vt, styles, pod)?.into_frames()
body.layout(vt, styles, pod)?.into_frames()
};
// Prepare fill and stroke.

View File

@ -77,7 +77,7 @@ pub struct EnumNode {
/// ) [+ #phase]
/// ```
#[variadic]
pub items: Vec<EnumItem>,
pub children: Vec<EnumItem>,
/// If this is `{false}`, the items are spaced apart with
/// [enum spacing]($func/enum.spacing). If it is `{true}`, they use normal
@ -203,7 +203,7 @@ impl Layout for EnumNode {
let mut parents = styles.get(Self::PARENTS);
let full = styles.get(Self::FULL);
for item in self.items() {
for item in self.children() {
number = item.number().unwrap_or(number);
let resolved = if full {

View File

@ -34,7 +34,7 @@ impl Layout for FlowNode {
if let Some(node) = child.to::<StyledNode>() {
map = node.map();
styles = outer.chain(&map);
child = node.sub();
child = node.body();
}
if let Some(node) = child.to::<VNode>() {

View File

@ -73,7 +73,7 @@ pub struct GridNode {
///
/// The cells are populated in row-major order.
#[variadic]
pub cells: Vec<Content>,
pub children: Vec<Content>,
/// Defines the column sizes.
///
@ -114,7 +114,7 @@ impl Layout for GridNode {
regions: Regions,
) -> SourceResult<Fragment> {
// Prepare grid layout by unifying content and gutter tracks.
let cells = self.cells();
let cells = self.children();
let layouter = GridLayouter::new(
vt,
Axes::new(&self.columns().0, &self.rows().0),

View File

@ -49,7 +49,7 @@ pub struct ListNode {
/// ]
/// ```
#[variadic]
pub items: Vec<ListItem>,
pub children: Vec<ListItem>,
/// If this is `{false}`, the items are spaced apart with [list
/// spacing]($func/list.spacing). If it is `{true}`, they use normal
@ -141,7 +141,7 @@ impl Layout for ListNode {
let marker = styles.get(Self::MARKER).resolve(vt.world(), depth)?;
let mut cells = vec![];
for item in self.items() {
for item in self.children() {
cells.push(Content::empty());
cells.push(marker.clone());
cells.push(Content::empty());

View File

@ -307,7 +307,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> {
) -> SourceResult<()> {
let map = self.scratch.maps.alloc(styled.map());
let stored = self.scratch.styles.alloc(styles);
let content = self.scratch.content.alloc(styled.sub());
let content = self.scratch.content.alloc(styled.body());
let styles = stored.chain(map);
self.interrupt_style(&map, None)?;
self.accept(content, styles)?;

View File

@ -521,7 +521,7 @@ fn collect<'a>(
let outer = styles;
let mut styles = *styles;
if let Some(node) = child.to::<StyledNode>() {
child = Box::leak(Box::new(node.sub()));
child = Box::leak(Box::new(node.body()));
styles = outer.chain(Box::leak(Box::new(node.map())));
}

View File

@ -40,7 +40,7 @@ use crate::prelude::*;
pub struct TableNode {
/// The contents of the table cells.
#[variadic]
pub cells: Vec<Content>,
pub children: Vec<Content>,
/// Defines the column sizes.
/// See the [grid documentation]($func/grid) for more information on track
@ -135,7 +135,7 @@ impl Layout for TableNode {
let gutter = Axes::new(self.column_gutter().0, self.row_gutter().0);
let cols = tracks.x.len().max(1);
let cells: Vec<_> = self
.cells()
.children()
.into_iter()
.enumerate()
.map(|(i, child)| {

View File

@ -36,7 +36,7 @@ pub struct TermsNode {
/// ) [/ #product: Born in #year.]
/// ```
#[variadic]
pub items: Vec<TermItem>,
pub children: Vec<TermItem>,
/// If this is `{false}`, the items are spaced apart with [term list
/// spacing]($func/terms.spacing). If it is `{true}`, they use normal
@ -101,12 +101,12 @@ impl Layout for TermsNode {
};
let mut cells = vec![];
for item in self.items() {
for child in self.children() {
let body = Content::sequence(vec![
HNode::new((-body_indent).into()).pack(),
(item.term() + TextNode::packed(':')).strong(),
(child.term() + TextNode::packed(':')).strong(),
SpaceNode::new().pack(),
item.description(),
child.description(),
]);
cells.push(Content::empty());

View File

@ -79,12 +79,12 @@ pub struct ScriptsNode {
/// The base to attach the scripts to.
#[positional]
#[required]
pub base: Content,
pub body: Content,
}
impl LayoutMath for ScriptsNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
self.base().layout_math(ctx)
self.body().layout_math(ctx)
}
}
@ -102,12 +102,12 @@ pub struct LimitsNode {
/// The base to attach the limits to.
#[positional]
#[required]
pub base: Content,
pub body: Content,
}
impl LayoutMath for LimitsNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
self.base().layout_math(ctx)
self.body().layout_math(ctx)
}
}

View File

@ -20,7 +20,7 @@ const VERTICAL_PADDING: Ratio = Ratio::new(0.1);
pub struct VecNode {
/// The elements of the vector.
#[variadic]
pub elements: Vec<Content>,
pub children: Vec<Content>,
/// The delimiter to use.
///
@ -36,7 +36,7 @@ pub struct VecNode {
impl LayoutMath for VecNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
let delim = ctx.styles().get(Self::DELIM);
let frame = layout_vec_body(ctx, &self.elements(), Align::Center)?;
let frame = layout_vec_body(ctx, &self.children(), Align::Center)?;
layout_delimiters(ctx, frame, Some(delim.open()), Some(delim.close()))
}
}
@ -141,7 +141,7 @@ impl LayoutMath for MatNode {
pub struct CasesNode {
/// The branches of the case distinction.
#[variadic]
pub branches: Vec<Content>,
pub children: Vec<Content>,
/// The delimiter to use.
///
@ -157,7 +157,7 @@ pub struct CasesNode {
impl LayoutMath for CasesNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
let delim = ctx.styles().get(Self::DELIM);
let frame = layout_vec_body(ctx, &self.branches(), Align::Left)?;
let frame = layout_vec_body(ctx, &self.children(), Align::Left)?;
layout_delimiters(ctx, frame, Some(delim.open()), None)
}
}

View File

@ -242,7 +242,7 @@ impl LayoutMath for Content {
let prev_size = ctx.size;
ctx.map.apply(prev_map.clone());
ctx.size = ctx.styles().get(TextNode::SIZE);
styled.sub().layout_math(ctx)?;
styled.body().layout_math(ctx)?;
ctx.size = prev_size;
ctx.map = prev_map;
return Ok(());

View File

@ -44,7 +44,7 @@ impl LayoutRoot for DocumentNode {
if let Some(node) = child.to::<StyledNode>() {
map = node.map();
styles = outer.chain(&map);
child = node.sub();
child = node.body();
}
if let Some(page) = child.to::<PageNode>() {

View File

@ -45,7 +45,7 @@ pub struct HeadingNode {
/// The heading's title.
#[positional]
#[required]
pub title: Content,
pub body: Content,
/// The logical nesting depth of the heading, starting from one.
#[named]
@ -120,14 +120,14 @@ impl Prepare for HeadingNode {
impl Show for HeadingNode {
fn show(&self, _: &mut Vt, this: &Content, _: StyleChain) -> SourceResult<Content> {
let mut realized = self.title();
let mut realized = self.body();
let numbers = this.field("numbers").unwrap();
if *numbers != Value::None {
realized = numbers.clone().display()
+ HNode::new(Em::new(0.3).into()).with_weak(true).pack()
+ realized;
}
Ok(BlockNode::new().with_body(realized).pack())
Ok(BlockNode::new().with_body(Some(realized)).pack())
}
}

View File

@ -103,7 +103,7 @@ impl Show for OutlineNode {
) -> SourceResult<Content> {
let mut seq = vec![ParbreakNode::new().pack()];
if let Some(title) = styles.get(Self::TITLE) {
let body = title.clone().unwrap_or_else(|| {
let title = title.clone().unwrap_or_else(|| {
TextNode::packed(match styles.get(TextNode::LANG) {
Lang::GERMAN => "Inhaltsverzeichnis",
Lang::ENGLISH | _ => "Contents",
@ -111,7 +111,7 @@ impl Show for OutlineNode {
});
seq.push(
HeadingNode::new(body)
HeadingNode::new(title)
.pack()
.styled(HeadingNode::NUMBERING, None)
.styled(HeadingNode::OUTLINED, false),
@ -161,7 +161,7 @@ impl Show for OutlineNode {
}
// Format the numbering.
let mut start = heading.title();
let mut start = heading.body();
let numbers = node.field("numbers").unwrap();
if *numbers != Value::None {
start = numbers.clone().display() + SpaceNode::new().pack() + start;
@ -175,7 +175,7 @@ impl Show for OutlineNode {
seq.push(SpaceNode::new().pack());
seq.push(
BoxNode::new()
.with_body(filler.clone())
.with_body(Some(filler.clone()))
.with_width(Fr::one().into())
.pack(),
);

View File

@ -171,7 +171,7 @@ impl Show for RawNode {
};
if self.block() {
realized = BlockNode::new().with_body(realized).pack();
realized = BlockNode::new().with_body(Some(realized)).pack();
}
Ok(realized)

View File

@ -55,7 +55,7 @@ impl Content {
/// Attach a span to the content.
pub fn spanned(mut self, span: Span) -> Self {
if let Some(styled) = self.to::<StyledNode>() {
self = StyledNode::new(styled.sub().spanned(span), styled.map()).pack();
self = StyledNode::new(styled.body().spanned(span), styled.map()).pack();
}
self.span = Some(span);
self
@ -83,7 +83,7 @@ impl Content {
} else if let Some(styled) = self.to::<StyledNode>() {
let mut map = styled.map();
map.apply(styles);
StyledNode::new(styled.sub(), map).pack()
StyledNode::new(styled.body(), map).pack()
} else {
StyledNode::new(self, styles).pack()
}
@ -268,7 +268,7 @@ impl Debug for Content {
if let Some(styled) = self.to::<StyledNode>() {
styled.map().fmt(f)?;
styled.sub().fmt(f)
styled.body().fmt(f)
} else if let Some(seq) = self.to::<SequenceNode>() {
f.debug_list().entries(&seq.children()).finish()
} else if self.id.name() == "space" {
@ -334,7 +334,7 @@ pub struct StyledNode {
/// The styled content.
#[positional]
#[required]
pub sub: Content,
pub body: Content,
/// The styles.
#[positional]

View File

@ -165,7 +165,7 @@ pub enum Expr {
Let(LetBinding),
/// A set rule: `set text(...)`.
Set(SetRule),
/// A show rule: `show heading: it => [*{it.body}*]`.
/// A show rule: `show heading: it => emph(it.body)`.
Show(ShowRule),
/// An if-else conditional: `if x { y } else { z }`.
Conditional(Conditional),
@ -1591,7 +1591,7 @@ impl SetRule {
}
node! {
/// A show rule: `show heading: it => [*{it.body}*]`.
/// A show rule: `show heading: it => emph(it.body)`.
ShowRule
}

View File

@ -224,7 +224,7 @@ pub enum SyntaxKind {
LetBinding,
/// A set rule: `set text(...)`.
SetRule,
/// A show rule: `show heading: it => [*{it.body}*]`.
/// A show rule: `show heading: it => emph(it.body)`.
ShowRule,
/// An if-else conditional: `if x { y } else { z }`.
Conditional,

View File

@ -15,7 +15,7 @@
---
// Test field on node.
#show list: node => {
test(node.items.len(), 3)
test(node.children.len(), 3)
}
- A

View File

@ -2,7 +2,7 @@
---
// Override lists.
#show list: it => "(" + it.items.map(item => item.body).join(", ") + ")"
#show list: it => "(" + it.children.map(v => v.body).join(", ") + ")"
- A
- B
@ -31,9 +31,9 @@ my heading?
box(move(dy: -1pt)[📖])
h(5pt)
if it.level == 1 {
underline(text(1.25em, blue, it.title))
underline(text(1.25em, blue, it.body))
} else {
text(red, it.title)
text(red, it.body)
}
})
@ -51,7 +51,7 @@ Another text.
#show heading: it => {
set text(red)
show "ding": [🛎]
it.title
it.body
}
= Heading

View File

@ -35,7 +35,7 @@
#show terms: it => table(
columns: 2,
inset: 3pt,
..it.items.map(item => (emph(item.term), item.description)).flatten(),
..it.children.map(v => (emph(v.term), v.description)).flatten(),
)
/ A: One letter

View File

@ -38,7 +38,7 @@ multiline.
---
// Test styling.
#show heading.where(level: 5): it => block(
text(font: "Roboto", fill: eastern, it.title + [!])
text(font: "Roboto", fill: eastern, it.body + [!])
)
= Heading