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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! The standard features of flm-core: the features and backends that make the library
//! a working FLM processor, and the function that adds them to a package.
//!
//! A *feature* ([`FlmFeature`]) is a Rust value that contributes a related bundle of
//! things to a registry (elements, stages, tables, lowering rules, backends, services,
//! settings) through the feature builder ([`FeatureBuilder`]), and a *package* is a
//! named bundle of features together with the packages it builds on
//! ([`PackageEntry`]). A program declares its own package and adds the standard
//! features to it with [`add_features`]. The standard features provide:
//!
//! - the commands and environments an author writes: inline formatting, literal
//! characters, headings, mathematics, labels and references, verbatim content, lists,
//! footnotes and the other xnotes, hyperlinks, defined terms, theorems, quotations,
//! annotations, images, floats, tables of cells, citations, included files and user
//! definitions;
//! - the stages that structure and check a document: block structure, the element
//! index, the table of reference points, numbering, and the checks every workflow runs
//! (see [Every workflow validates](#every-workflow-validates-the-collection-flmvalidated)
//! below);
//! - the document's metadata (title, authors, date), shown before the content, and the
//! table of contents;
//! - four output formats: plain text (`text`), HTML (`html`), rendered LaTeX
//! (`latex-rendered`) and recomposed LaTeX (`latex-recomposed`), the last two built
//! on `latex`, which every LaTeX backend shares, with the lowering stage, the
//! presentation elements and the pieces (document-level values such as the title,
//! placed before or after the content; [`Piece`](crate::core::piece::Piece)) they
//! share ([`backends`]);
//! - the `check` workflow, which validates a document and writes no output.
//!
//! The standard features process no mathematics: the `math` feature introduces
//! mathematics into the language, and another feature processes it, for example the
//! feature [`math-passthrough`](crate::math_passthrough), which carries every formula to
//! the output as written, or a feature of an extension that typesets formulas. A
//! registry that holds the standard features and no such feature builds and plans every
//! standard workflow, and a render reports each formula as an Error (the
//! [`math`](features::math) module documentation, *Processing mathematics*).
//!
//! The standard features have no special status: each registers through the same
//! feature builder a feature of another crate uses, so a program can add commands,
//! change how a standard element is rendered or add an output format without an
//! extension point made for the purpose (see [features and
//! packages](crate::guide::concepts_overview#features-and-packages), and [the extension
//! guide](crate::guide::extending) for writing a feature or a package).
//!
//! # Adding the standard features to a package
//!
//! A program written in Rust declares its package entry, adds the standard features to
//! it with [`add_features`] from a [`StdlibConfig`], and adds the entry to a registry
//! builder with
//! [`RegistryBuilder::use_package`](crate::core::registry::RegistryBuilder::use_package):
//!
//! ```
//! use flm_core::core::registry::{PackageEntry, RegistryBuilder, RegistryInputs};
//! use flm_core::stdlib::{self, StdlibConfig};
//!
//! let mut builder = RegistryBuilder::new();
//! builder.use_package(stdlib::add_features(PackageEntry::new("std"), StdlibConfig::new()));
//! let registry = builder.build(&RegistryInputs::new())?;
//!
//! // The standard features ship the `check` workflow: validation, no output.
//! assert!(registry.workflow(stdlib::CHECK_WORKFLOW).is_some());
//! # Ok::<(), flm_core::core::registry::RegistryBuildError>(())
//! ```
//!
//! The package's name is the program's choice, and so are the features it adds to the
//! package after the standard ones.
//!
//! [`StdlibConfig`] holds the configuration of each of the sixteen standard features
//! that take one; the other seventeen take none. A feature's configuration is the plain
//! Rust value the feature is constructed from; it is fixed when the registry is built
//! and cannot vary per document or per render (see [settings, options and
//! configuration](crate::guide::concepts_overview#settings-options-and-configuration)).
//!
//! A program that reads its settings as data, for example from a document's front
//! matter, uses [`add_features_from_settings`] instead, in the build function of its
//! package's [`PackageFactory`](crate::core::registry::PackageFactory): the function
//! reads each feature's configuration from the settings it is given and leaves out the
//! features those settings switch off.
//!
//! The pipeline guide ([`crate::guide::pipeline`]) explains what a run does with the
//! stages the features declare, and the embedding guide ([`crate::guide::embedding`])
//! shows a complete program.
//!
//! # The standard features and where each is documented
//!
//! [`add_features`] adds the thirty-three features in this order, each once, after the
//! features the package entry holds. Each is given with the module that documents it:
//!
//! 1. `textstyles` ([`features::textstyles`]): `\emph`, `\textbf`, `\textit`, `\texttt`.
//! 2. `literals` ([`features::literals`]): the commands and characters that stand for
//! one character of text, such as `\%` and `~`.
//! 3. `blocks` ([`features::blocks`]): paragraphs and blocks, typographic
//! substitutions, and the check of the tree contract.
//! 4. `index` ([`features::index`]): the table of the nodes whose elements carry each
//! aspect.
//! 5. `refpoints` ([`features::refpoints`]): the table of every reference point.
//! 6. `numbering` ([`features::numbering`]): counters, counter styles, and the number
//! of every numbered reference point.
//! 7. `math` ([`features::math`]): inline and display mathematics: the display-math
//! environments, their equation lines and numbers, the text commands of mathematics.
//! 8. `refs` ([`features::refs`]): `\label`, `\ref`, `\hyperref`, anchors, and the
//! label check.
//! 9. `headings` ([`features::headings`]): `\section` to `\subparagraph`, and heading
//! kinds a program declares.
//! 10. `verbatim` ([`features::verbatim`]): content taken exactly as written.
//! 11. `lists` ([`features::lists`]): `itemize`, `enumerate`, `description` and their
//! `\item`s.
//! 12. `xnotes` ([`features::xnotes`]): `\footnote` and the other kinds of xnote.
//! 13. `href` ([`features::href`]): `\href`, `\url` and `\email`.
//! 14. `defterm` ([`features::defterm`]): the `defterm` environment and `\term`.
//! 15. `theorems` ([`features::theorems`]): `theorem`, `lemma`, `proof` and the other
//! theorem kinds.
//! 16. `quote` ([`features::quote`]): `quote`, `blockquote` and `address`.
//! 17. `annotations` ([`features::annotations`]): a reviewer's highlights and
//! comments, one command per configured reviewer.
//! 18. `graphics` ([`features::graphics`]): `\includegraphics`.
//! 19. `floats` ([`features::floats`]): `figure`, `table` and other float types, with
//! their captions.
//! 20. `cells` ([`features::cells`]): tables of cells, the `cells` environment.
//! 21. `citations` ([`features::citations`]): `\cite` and the list of cited works.
//! 22. `input` ([`features::input`]): `\input`, the content of another file.
//! 23. `userdefs` ([`features::userdefs`]): macros and environments defined by
//! substitution.
//! 24. `lowering` ([`backends::lowering`]): the lowering stage, and the lowering rules
//! of the structural elements.
//! 25. `metadata` ([`features::metadata`]): the document's title, authors and date.
//! 26. `toc` ([`features::toc`]): the table of contents, a table of the listed
//! headings and a piece placed before the content.
//! 27. `pieces` ([`backends::pieces`]): what a backend places before or after the
//! document's content.
//! 28. `text` ([`backends::text`]): the plain-text backend and the `text` workflow.
//! 29. `latex` ([`backends::latex`]): what every LaTeX backend shares.
//! 30. `latex-recomposed` ([`backends::latex_recomposed`]): the recomposed-LaTeX
//! backend and the `latex-recomposed` workflow.
//! 31. `latex-rendered` ([`backends::latex_rendered`]): the rendered-LaTeX
//! backend and the `latex-rendered` workflow.
//! 32. `html` ([`backends::html`]): the HTML backend and the `html` workflow.
//! 33. `check` ([`CheckWorkflow`]): the collection `flm.validated` and the `check`
//! workflow.
//!
//! The order is the order in which the registry registers them. It breaks the tie between two
//! stages a plan could run in either order, it decides which feature is recorded as
//! the owner of an element value two features both declare (the one placed last), and, where two
//! features define one command name in the same parsing mode, it decides whose
//! definition applies: the later one, and the clash is reported unless the feature
//! order places that feature explicitly after the other, or that feature declares an
//! override. A weak declaration of the name applies only where no feature declares the
//! name ordinarily in that mode, whatever the order (see [`crate::lang::mint::packages`]).
//!
//! # Every workflow validates: the collection `flm.validated`
//!
//! A *collection* is a name for a set of capabilities that any feature may add to:
//! demanding the collection demands every member, and a collection can be demanded but
//! never provided ([`CollectionDef`]).
//!
//! The `check` feature ([`CheckWorkflow`]) declares the collection [`VALIDATED`]
//! (`flm.validated`) and the workflow [`CHECK_WORKFLOW`] (`check`), which demands the
//! collection and nothing else: a run of `check` validates the document and writes no
//! output. The workflows of the four standard backends, `text`, `html`,
//! `latex-rendered` and `latex-recomposed`, demand the collection beside their output. A render therefore
//! reports the problems `check` reports: a label written twice or a cited key with no
//! entry is reported alike by a render and by `check`.
//!
//! The members the standard features add are:
//!
//! - `flm.blocks.contract-checked` ([`blocks`](features::blocks)): every tree
//! satisfies the [tree contract](crate::lang::rewrites::blocks::check), or its
//! violations are reported.
//! - `flm.refs.check-labels` ([`refs`]): no label is written twice,
//! every label's type is one its construct accepts, and every reference that
//! resolves to nothing is reported.
//! - `flm.theorems.check-proofs` ([`theorems`](features::theorems)): every statement
//! that expects a proof and has none is reported, and so is every malformed
//! back-reference of a proof.
//! - `flm.floats.check-handlers` ([`floats`](features::floats)): every content handler
//! that a float type used in the document requires and no feature registers is
//! reported.
//! - `flm.citations.check` ([`citations`](features::citations)): every cited key with
//! no entry is reported.
//! - `flm.metadata.read` ([`metadata`]): the document's metadata is parsed, and
//! the problems the parse finds are reported.
//! - `flm.input.spliced` ([`input`]): the content of every included
//! file is in place in the document's trees.
//!
//! The feature that processes mathematics adds one more member, the check of the
//! formulas. Its check stage provides `flm.math.content`, which the
//! [`math`](features::math) feature declares (a stage has checked the content of every
//! formula), and which no standard feature provides or adds. The feature
//! [`math-passthrough`](crate::math_passthrough) adds `flm.math.content` itself; its
//! stage `flm.math-passthrough.check` accepts every formula and reports every equation
//! line that holds no mathematics. An extension adds to a collection
//! only a capability named under its own stem, so the `math-mathjax` extension adds
//! `math-mathjax.check`, which its check provides beside `flm.math.content`. Where two
//! such features are registered, the plan request prefers one stage, because a
//! capability with two providers and nothing to choose between them makes the plan
//! build fail ([`PlanError::Contested`](crate::core::plan::PlanError::Contested)).
//!
//! A feature adds its member from its own registration
//! ([`FeatureBuilder::collection_member`]), in any order of registration. In a
//! registry that does not hold the `check` feature, the build drops the member without
//! a diagnostic, so a feature that adds a check still registers on its own. A registry
//! that holds a standard backend feature but not the `check` feature fails to build,
//! because the backend's workflow demands the collection `flm.validated`, which no
//! feature then declares; a
//! program that wants the checks without the rest of the standard features registers
//! [`check_feature`] beside its own features.
//!
//! The checks read the document's trees as the plan's rewrites leave them. Where a
//! render of the recomposed LaTeX keeps the use sites of a user definition (to write
//! them back as calls), the checks see the call and not the definition's expansion;
//! LaTeX checks that content when it compiles the output.
//!
//! An Error a check reports does not stop a render. The stages of the standard
//! backends have no effect outside the run's store, so the run's error gate (see [the
//! error gate](crate::guide::embedding#the-error-gate)) does not stop them: the output
//! is computed, and the diagnostics are reported beside it.
//!
//! **An unresolved reference is the only problem whose severity depends on the
//! workflow.** The label check of the [`refs`] feature reads how the
//! references were resolved, so every plan that demands the collection runs the
//! resolution of references and reports the references that resolve to nothing. A plan
//! that renders nothing cannot know whether a label would resolve in a render, because
//! a render may install the external index of labels defined in other documents
//! ([`EXTERNAL_INDEX`](features::refs::EXTERNAL_INDEX)). An unresolved
//! reference is therefore a Warning under `check`, regardless of
//! [`RefsRenderOptions`](features::refs::RefsRenderOptions), and an Error under a
//! rendering workflow unless those options allow it. A label written twice and a label
//! of a type its construct does not accept are Errors under every workflow, because
//! each is a fact of the document alone.
use Arc;
use crate;
use cratePackageSettings;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crate;
use crateRefsConfig;
use crate;
use crate;
use crate;
use crate;
/// The registered name of the [`CheckWorkflow`] feature, `check`. The feature
/// and the workflow it declares ([`CHECK_WORKFLOW`]) have the same name.
pub const CHECK_FEATURE: &str = "check";
/// The name of the collection `flm.validated`, which every check of the standard
/// features joins: demanding it demands every member, so a plan that demands it
/// runs every check the registry holds.
///
/// The [`CheckWorkflow`] feature declares the collection. Every workflow the
/// standard features register demands it: [`CHECK_WORKFLOW`] demands it alone, and the
/// workflows of the `text`, `html` and `latex-recomposed` backends demand it beside
/// their output, so a document gets the same diagnostics under every workflow
/// (with one exception, an unresolved reference). The [module
/// documentation](self#every-workflow-validates-the-collection-flmvalidated) lists
/// the members.
pub const VALIDATED: &str = "flm.validated";
/// The name of the `check` workflow, `check`: it demands [`VALIDATED`] and
/// nothing else, so a run of it validates the document and writes no output.
///
/// A rendering workflow runs the same checks, because it demands [`VALIDATED`]
/// beside its output. The only diagnostic whose severity differs is an
/// unresolved reference: a Warning under `check`, an Error under a rendering
/// workflow unless the render options of the `refs` feature allow it (see the
/// [module documentation](self#every-workflow-validates-the-collection-flmvalidated)).
pub const CHECK_WORKFLOW: &str = "check";
/// The configuration of the standard features: one field for each of the sixteen
/// standard features that take a configuration. The other seventeen features take
/// none.
///
/// [`add_features`] constructs each of these features from its field. Each field's
/// documentation says what it holds and what its default is; [`StdlibConfig::new`]
/// is the default of every field. A feature's configuration is the plain Rust
/// value the feature is constructed from; it is fixed when the registry is built
/// and cannot vary per document or per render (see [settings, options and
/// configuration](crate::guide::concepts_overview#settings-options-and-configuration)).
/// The configurations are fixed then because the commands the language knows, the
/// trees a run freezes and the reference points that are numbered depend on them.
///
/// A program that reads the configurations as data uses
/// [`add_features_from_settings`], which reads each field from the settings of
/// its feature.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::StdlibConfig;
/// use flm_core::stdlib::features::blocks::BlocksConfig;
/// use flm_core::stdlib::features::headings::HeadingsConfig;
///
/// let config = StdlibConfig::new()
/// .blocks(BlocksConfig::no_typography())
/// .headings(HeadingsConfig::new().run_in_levels([4, 5]));
/// assert!(!config.blocks.auto_unicode_quotes);
/// assert!(config.headings.heading("paragraph").is_some_and(|kind| kind.run_in));
/// ```
/// Returns `entry` with the thirty-three standard features added after the
/// features it holds, each once, in the order the [module
/// documentation](self#the-standard-features-and-where-each-is-documented)
/// lists them, with the sixteen configurable features constructed from the
/// fields of `config`.
///
/// A program declares its package, adds the standard features to it, and adds
/// its own features after them; the module documentation has an example. A
/// program that reads its settings as data uses [`add_features_from_settings`]
/// instead.
/// Returns `entry` with the standard features added after the features it
/// holds, in the order of [`add_features`], each configured from the settings,
/// and without the features the settings switch off.
///
/// The function reads the configuration of each of the sixteen configurable
/// features from its settings, and takes the default where the settings give
/// none. A program that chooses its packages by name and reads their settings
/// as data calls it in the build function of its package's
/// [`PackageFactory`](crate::core::registry::PackageFactory): the factory then
/// lists each standard feature with the declaration of its settings when it
/// takes some ([`FlmFeature::settings`]), and the program reads the settings
/// against those declarations. The features
/// the entry already holds are kept, whatever the settings say about them.
///
/// The settings also hold option values (values of a feature's option types).
/// The package entry does not take them: the program sets them on the document
/// and on the render request (see [`crate::core::settings::FeatureSettings`]).
/// A program written in Rust that builds its configuration in code calls
/// [`add_features`].
///
/// # Examples
///
/// ```
/// use flm_core::core::registry::{PackageEntry, PackageFactory, RegistryBuilder, RegistryInputs};
/// use flm_core::core::settings::PackageSettings;
/// use flm_core::stdlib;
///
/// let factory = PackageFactory::new("std", |settings: &PackageSettings| {
/// Ok(stdlib::add_features_from_settings(PackageEntry::new("std"), settings))
/// })?;
/// let math = factory.settings("math").expect("math takes settings");
/// let data = serde_json::json!({ "numbered_equations": false });
///
/// let mut settings = PackageSettings::new();
/// settings.insert(math.read(data.as_object().unwrap()).unwrap());
/// settings.switch_off("annotations");
///
/// let entry = factory.build(&settings)?;
/// assert!(entry.features.iter().all(|feature| feature.info().name != "annotations"));
/// let mut builder = RegistryBuilder::new();
/// builder.use_package(entry);
/// assert!(builder.build(&RegistryInputs::new()).is_ok());
/// # Ok::<(), flm_core::core::registry::PackageError>(())
/// ```
/// The thirty-three standard features, in the order of [`add_features`], with
/// the configurable ones constructed from the fields of `config`.
/// The `check` feature: it declares the collection [`VALIDATED`]
/// (`flm.validated`) and the workflow [`CHECK_WORKFLOW`] (`check`), which
/// demands that collection, and nothing else.
///
/// The feature declares no element and no stage. A program that registers its
/// own features without the rest of the standard features can register this one
/// beside them ([`check_feature`]) to get the `check` workflow over the checks its
/// features add to the collection.
///
/// The workflow of every standard backend demands [`VALIDATED`] too, so a
/// registry that holds a standard backend feature must hold this feature: without
/// it, the registry build fails with an error that gives the backend's workflow
/// and the missing name `flm.validated`. See the [module
/// documentation](self#every-workflow-validates-the-collection-flmvalidated).
;
/// Returns the [`CheckWorkflow`] feature, ready to be listed in a package entry or
/// registered beside the features whose checks the collection `flm.validated`
/// gathers.