1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
use super::TextNode;
use crate::prelude::*;
/// A text space.
#[derive(Debug, Hash)]
pub struct SpaceNode;
#[node(Unlabellable, Behave)]
impl SpaceNode {
fn construct(_: &Vm, _: &mut Args) -> SourceResult<Content> {
Ok(Self.pack())
}
}
impl Unlabellable for SpaceNode {}
impl Behave for SpaceNode {
fn behaviour(&self) -> Behaviour {
Behaviour::Weak(2)
}
}
/// A line break.
#[derive(Debug, Hash)]
pub struct LinebreakNode {
pub justify: bool,
}
#[node(Behave)]
impl LinebreakNode {
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
let justify = args.named("justify")?.unwrap_or(false);
Ok(Self { justify }.pack())
}
}
impl Behave for LinebreakNode {
fn behaviour(&self) -> Behaviour {
Behaviour::Destructive
}
}
/// Strongly emphasizes content by increasing the font weight.
#[derive(Debug, Hash)]
pub struct StrongNode(pub Content);
#[node(Show)]
impl StrongNode {
/// The delta to apply on the font weight.
pub const DELTA: i64 = 300;
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
Ok(Self(args.expect("body")?).pack())
}
fn field(&self, name: &str) -> Option<Value> {
match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
}
}
impl Show for StrongNode {
fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> Content {
self.0.clone().styled(TextNode::DELTA, Delta(styles.get(Self::DELTA)))
}
}
/// A delta that is summed up when folded.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Delta(pub i64);
castable! {
Delta,
Expected: "integer",
Value::Int(delta) => Self(delta),
}
impl Fold for Delta {
type Output = i64;
fn fold(self, outer: Self::Output) -> Self::Output {
outer + self.0
}
}
/// Emphasizes content by flipping the italicness.
#[derive(Debug, Hash)]
pub struct EmphNode(pub Content);
#[node(Show)]
impl EmphNode {
fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
Ok(Self(args.expect("body")?).pack())
}
fn field(&self, name: &str) -> Option<Value> {
match name {
"body" => Some(Value::Content(self.0.clone())),
_ => None,
}
}
}
impl Show for EmphNode {
fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content {
self.0.clone().styled(TextNode::EMPH, Toggle)
}
}
/// A toggle that turns on and off alternatingly if folded.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Toggle;
impl Fold for Toggle {
type Output = bool;
fn fold(self, outer: Self::Output) -> Self::Output {
!outer
}
}
/// Convert a string or content to lowercase.
pub fn lower(_: &Vm, args: &mut Args) -> SourceResult<Value> {
case(Case::Lower, args)
}
/// Convert a string or content to uppercase.
pub fn upper(_: &Vm, args: &mut Args) -> SourceResult<Value> {
case(Case::Upper, args)
}
/// Change the case of text.
fn case(case: Case, args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("string or content")?;
Ok(match v {
Value::Str(v) => Value::Str(case.apply(&v).into()),
Value::Content(v) => Value::Content(v.styled(TextNode::CASE, Some(case))),
v => bail!(span, "expected string or content, found {}", v.type_name()),
})
}
/// A case transformation on text.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Case {
/// Everything is lowercased.
Lower,
/// Everything is uppercased.
Upper,
}
impl Case {
/// Apply the case to a string.
pub fn apply(self, text: &str) -> String {
match self {
Self::Lower => text.to_lowercase(),
Self::Upper => text.to_uppercase(),
}
}
}
/// Display text in small capitals.
pub fn smallcaps(_: &Vm, args: &mut Args) -> SourceResult<Value> {
let body: Content = args.expect("content")?;
Ok(Value::Content(body.styled(TextNode::SMALLCAPS, true)))
}
|