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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Pieces: document-level synthesized values that a backend places before or
//! after the document's content. This module holds the declaration of a piece
//! ([`PieceDecl`], [`PiecePlace`]) and the type of its content ([`Piece`]).
//!
//! A piece is a document-level synthesized value that a backend places before
//! or after the document's content (the title, the list of footnotes, the
//! bibliography), built once by its builder stage into a table named for the
//! piece and rendered by each backend with its own rules
//! ([pieces](crate::guide::concepts_overview#pieces)). A synthesized value is
//! a small tree that a stage builds while a run is under way, held outside the
//! document's trees ([synthesized
//! values](crate::guide::concepts_overview#synthesized-values)).
//!
//! Three things distinguish a piece from the document's content: the author
//! does not write it where it appears; a stage *builds* it from what the
//! document contains, once, over the whole document; and the backend *places*
//! it at the place its declaration gives (before the content or after it),
//! not at a position in a tree.
//!
//! A piece has three parts. A feature that offers a piece writes the first two
//! with the items of this module; the backends supply the third.
//!
//! 1. **Its content** is a [`Piece`]: a small tree built from document
//! elements (the elements the feature declares, text, references,
//! mathematics), held in a table named for the piece. The content is a
//! synthesized value: a consumer brings it up to date before reading it as
//! document content ([value
//! catch-up](crate::guide::concepts_overview#value-catch-up)). The stage
//! that writes the table is the piece's **builder**. Because the content is
//! a table, another stage that writes the same table replaces the builder
//! when a plan request prefers that stage.
//! 2. **Its declaration** is a [`PieceDecl`]: the piece's name, the table
//! holding its content, where it is placed, and whether a render places it
//! by default. The feature contributes it to the service slot `flm.pieces`
//! that the `pieces` feature owns
//! ([`stdlib::backends::pieces::SLOT`](crate::stdlib::backends::pieces::SLOT)).
//! 3. **Its rendering** belongs to each backend: a per-backend stage lowers
//! the content with the lowering rules the plan selected for that backend,
//! and the backend writes the result into its output, as an [output
//! region](crate::core::region) of its own on HTML and on the recomposed
//! LaTeX, inside the printed text on the text backend. That machinery is in
//! [`crate::stdlib::backends::pieces`].
//!
//! Because the content is built from document elements, and never a string or
//! a tree of one backend's own elements, one builder serves every backend, and
//! a stage that replaces the builder changes the piece on every output at
//! once.
//!
//! A piece in this sense is unrelated to a table piece, the part of one table
//! that one stage invocation produced ([`Table::absorb`], [`PieceClass`]).
//!
//! This module depends on no backend and no feature: it holds only the values
//! a feature declares a piece with.
use Arc;
use NodeTree;
use SerialValue;
use crate;
use crateFlm;
use crateName;
/// Where a backend places a piece: before or after the document's content.
/// The declaration of one piece: its name, the table that holds its content,
/// where a backend places it, and whether a render places it by default.
///
/// A feature that offers a piece does three things:
///
/// - it contributes this declaration to the service slot
/// [`flm.pieces`](crate::stdlib::backends::pieces::SLOT) as a peer
/// contribution
/// ([`FeatureBuilder::peer_slot`](crate::core::registry::FeatureBuilder::peer_slot)),
/// so that the feature also builds in a registry without the `pieces`
/// feature;
/// - it declares the table [`PieceDecl::table`], of value type [`Piece`]
/// ([`FeatureBuilder::table`](crate::core::registry::FeatureBuilder::table));
/// - it registers the builder stage that writes that table.
///
/// When two pieces have the same place, a backend places them in the order of
/// their contributions to the slot. Which pieces a render places is decided
/// by [`selected_pieces`](crate::stdlib::backends::pieces::selected_pieces).
///
/// # Examples
///
/// ```
/// use flm_core::core::piece::{PieceDecl, PiecePlace};
///
/// let decl = PieceDecl::new("my.notes", PiecePlace::AfterContent, "the notes");
/// assert_eq!(decl.table.name().as_str(), "my.notes");
/// assert!(decl.default_on);
/// assert!(!decl.default_on(false).default_on);
/// ```
/// The content of one piece: a synthesized value built from document
/// elements, or nothing to place.
///
/// This is the value type of every table that holds a piece's content, so
/// that a backend's pieces stage (the stage that lowers the pieces for that
/// backend, [`PiecesStage`](crate::stdlib::backends::pieces::PiecesStage))
/// reads any piece by its name alone. A content of `None` places nothing and
/// reports nothing: a list of notes in a document without notes is not an
/// error.
///
/// The content is a [synthesized
/// value](crate::guide::concepts_overview#synthesized-values), which a backend
/// brings up to date before lowering it
/// ([`StageCx::resolve_value`](crate::core::run::StageCx::resolve_value)). A
/// synthesized value must not state a fact about the document as a whole: a
/// labeled reference point inside it is refused when it is brought up to date,
/// the backend's pieces stage reports `flm.pieces.lowering-failed`
/// ([`PieceLoweringFailed`](crate::stdlib::backends::pieces::PieceLoweringFailed)),
/// and the piece is left out of that backend's output.
///
/// The piece's builder stage (the stage that builds its content) writes the
/// table once; a second value combined into the table replaces the first
/// ([`Table::absorb`]). The serialized form of the table
/// ([`Table::project`]) is a map whose key `content` holds the tree
/// ([`ProjectCx::tree`]), or an empty map when the piece holds nothing.