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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! Reading the characters of content: the plain text of a run of nodes or of a
//! synthesized value ([`plain_text_of`], [`plain_text`]), the characters of an
//! argument that holds only characters, read by segments ([`read_chars`],
//! [`chars_text`]), and the whitespace of FLM text.
//!
//! # Plain text
//!
//! The **plain text** of content is the characters of its text nodes, in
//! document order. Everything else (comments, delimiters, the text of a macro or
//! environment command) is dropped, and nothing is put in its place. Content
//! that an element declares as not part of the text around it
//! ([`ApartFromText`](crate::model::aspects::ApartFromText)), such as a
//! footnote's words inside a heading's title, is dropped too. Use the plain
//! text where the words of some content are needed rather than a rendering of
//! it: the text of a reference point's content when a table is printed for
//! inspection (the [projection](crate::dump::project) of the table), or a
//! label compared with the text it refers to. [`plain_text`] reads the plain
//! text of a [synthesized
//! value](crate::guide::concepts_overview#synthesized-values), a small tree
//! held outside the document, which [`copy`](super::copy) builds.
//!
//! # Reading an argument as characters
//!
//! A label, a reference target and an address are written in arguments whose
//! content is characters: the characters-only group of `\label{…}` and
//! `\ref{…}`, verbatim content, or an ordinary argument that a command reads as
//! characters. [`read_chars`] is the function that reads such content. Where
//! [`plain_text_of`] skips everything that is not text, [`read_chars`] refuses it:
//! content holding a command (`\ref{\emph{x}}`, after a user definition put
//! `\emph{x}` into the target) or mathematics has no characters to read, and
//! [`read_chars`] returns [`NotCharacters`].
//!
//! One kind of node is neither characters nor refused: the **placeholder** of a
//! [user definition](crate::lang::rewrites::userdefs::Definition)'s template
//! (`#1`, `#{name}`, `#[…]{ref}`). A template that is lowered unexpanded (the
//! template of a [kept definition](crate::stdlib::features::userdefs::kept),
//! which the [recomposed LaTeX
//! backend](crate::stdlib::backends::latex_recomposed) writes back as a LaTeX
//! definition) still holds its placeholders when a label or a target inside it
//! is read. [`read_chars`] therefore returns the content as
//! [segments](CharsSegment), in order: runs of characters and placeholder
//! nodes. Each caller decides what a placeholder means:
//!
//! - A placeholder stands for characters that are not known yet (each use of
//! the definition supplies them). A reader of facts about the document (a
//! label, the target of a reference) therefore reads no label and no target
//! where one is written; [`CharsContent::text`] and [`chars_text`] return
//! `None` then.
//! - A lowering rule, which needs the characters to write them, reads them
//! through [`chars_at_lowering`](crate::core::lowering::chars_at_lowering).
//! For a placeholder, that function returns an error
//! ([`LowerError::PlaceholderCharacters`](crate::core::lowering::LowerError::PlaceholderCharacters))
//! that the rule passes on to the rule that inserted the template: the
//! user-definitions feature then expands the use sites of the definition
//! whose template it was lowering, and the lists feature reports a number
//! format in a list's own label template.
//!
//! Only a template lowered as it stands holds placeholders (on the recomposed
//! LaTeX backend, the template of a kept definition or a list's own label
//! template); everywhere else no placeholder remains, and the content is all
//! characters.
//!
//! # Whitespace
//!
//! Whitespace in FLM text is the four ASCII whitespace characters: the space,
//! the tab, the carriage return and the line feed. Every reader of parsed FLM
//! content (a document, a template) counts whitespace this way: the
//! block-structure pass, the lowering helpers' blank test
//! ([`is_blank`](crate::stdlib::backends::lowering::is_blank)), and each
//! feature's reader where it trims what it reads, splits it at whitespace or
//! tests whether content is blank. Content is blank when it is text made of
//! whitespace alone, or a node whose element carries the
//! [`Whitespace`](crate::model::aspects::Whitespace) aspect, such as `~` and
//! `\ `. Every other character is a character of the text, the no-break space
//! U+00A0 typed directly in the text among them.
//!
//! The one exception is the key of a `key=value` entry. techy's key-value
//! reader ([`parse_keyval`](techy::extract::parse_keyval)), through which the
//! features read such entries, trims each key with [`str::trim`], which removes
//! every Unicode whitespace character. A no-break space U+00A0 typed at either
//! end of a key is therefore removed, and the key is read without it; the
//! entry's value is trimmed by the rule above
//! ([`key_value_pieces`](crate::lang::copy::key_value_pieces)).
use HashSet;
use fmt;
use ;
use crate;
use crate;
/// Returns the plain text of a run of nodes: the characters of every text node
/// among the nodes and their descendants, in document order.
///
/// Comments, delimiters and the text of commands contribute nothing, and
/// neither does the content an element declares as apart from the text around
/// it ([`apart_from_text_of`]), with its descendants. The nodes may belong to
/// different trees. See the [module documentation](self#plain-text).
/// Returns the text of a run of nodes as [`plain_text_of`] reads it, with
/// `whitespace_node` written for each node whose element is whitespace
/// ([`is_whitespace_of`]: `~`, `\ `) and that no element set apart.
pub
/// Returns the key by which [`plain_text_of`] identifies a node to skip: its
/// tree and its place there.
/// Returns the plain text of a synthesized value's tree: [`plain_text_of`] over
/// its root.
// --- whitespace -----------------------------------------------------------------------
/// Whether `ch` is whitespace in FLM text: the space, the tab, the carriage
/// return or the line feed (see the [module documentation](self#whitespace)).
/// Every other character is content, the no-break space U+00A0 among them.
pub
/// Whether the byte `byte` of UTF-8 text is whitespace ([`is_space`]); a byte
/// of a character beyond ASCII never is.
pub
/// Whether `text` is blank: empty, or whitespace ([`is_space`]) alone.
pub
/// Whether `node` is blank: text that is blank ([`is_blank_text`]), or a node
/// that is whitespace, such as `~` and `\ ` (its element carries the
/// [`Whitespace`](crate::model::aspects::Whitespace) aspect,
/// [`is_whitespace_of`]). A comment is not blank: a reader that looks past
/// comments tests for them itself.
pub
/// Returns `text` with every run of whitespace ([`is_space`]) made one space
/// and the whitespace at both ends removed: the rule by which two written
/// texts are the same however their whitespace is laid out.
/// [`spelling_identity`](crate::stdlib::backends::pres::spelling_identity)
/// reads a citation key and a term's identity with it, and
/// [`Label::parse`](crate::model::aspects::Label::parse) each part of a label.
pub
// --- reading an argument as characters ------------------------------------------------
/// One segment of content read as characters by [`read_chars`]: a run of
/// characters, or a placeholder node of a user definition's template.
/// Content read as characters by [`read_chars`]: its [segments](CharsSegment),
/// in order. Adjacent characters form one segment, so two text segments never
/// follow each other; empty content has no segment at all.
///
/// Read the characters with [`text`](Self::text) when a placeholder makes the
/// content unusable, or go through the [segments](Self::segments) to handle
/// placeholders.
/// The error [`read_chars`] returns for content it cannot read as characters:
/// the content holds a callable node that is not a placeholder (a command, an
/// environment, a special character sequence) or a mathematics group, none of
/// which has characters to read.
/// Reads a run of nodes as characters and returns them by segments: text nodes
/// give their characters, comments nothing, groups other than mathematics their
/// children's characters without the delimiters, and a placeholder node of a
/// user definition's template becomes a segment of its own (see the [module
/// documentation](self#reading-an-argument-as-characters)).
///
/// Whitespace is kept as written; the caller trims what it needs to. The input
/// is any run of sibling nodes, typically an argument's content
/// ([`NodeRef::argument_content_nodes`]). A caller that has no use for
/// placeholders can call [`chars_text`] instead.
///
/// # Errors
///
/// [`NotCharacters`] when the content holds, at any depth, a callable node that
/// is not a placeholder, or a mathematics group.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use flm_core::core::registry::{PackageEntry, RegistryBuilder, RegistryInputs};
/// use flm_core::lang::text::read_chars;
/// use flm_core::lang::{parse_fragment, FlmOrigin, FlmSource, ParseOptions};
/// use flm_core::model::doc::FragmentLayout;
/// use flm_core::stdlib::{self, StdlibConfig};
///
/// let mut builder = RegistryBuilder::new();
/// builder.use_package(stdlib::add_features(PackageEntry::new("std"), StdlibConfig::new()));
/// builder.use_package(flm_core::math_passthrough::package());
/// let registry = builder.build(&RegistryInputs::new())?;
/// let source = FlmSource::new(r"See \ref{sec:{intro}}.");
/// let fragment = parse_fragment(
/// registry.lang(),
/// Arc::new(source.with_origin(FlmOrigin::none())),
/// FragmentLayout::default(),
/// &ParseOptions::default(),
/// )?;
/// let reference = fragment.tree().root().descendants()
/// .find(|node| node.name() == Some("ref"))
/// .expect("the reference");
/// let target = reference.argument_content_nodes_named("target")?.expect("written");
/// let content = read_chars(target.iter())?;
/// assert_eq!(content.text().as_deref(), Some("sec:intro"));
/// assert!(!content.has_placeholder());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// Returns the characters of a run of nodes when it holds only characters, and
/// `None` when it holds a command, mathematics or a placeholder. This is
/// [`read_chars`] followed by [`CharsContent::text`].
///
/// Use it when a placeholder makes the content unusable: a label, a target or
/// an address holding a placeholder is treated as absent.