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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//! The `latex-recomposed` feature ([`LatexRecomposed`]), which registers the
//! `latex-recomposed` backend: its native element set, its render options, its
//! two structural lowering rules, its stages and their tables, the
//! `latex-recomposed` output and the `latex-recomposed` workflow.
//!
//! A feature ([`FlmFeature`]) is a Rust value that contributes a related
//! bundle of things to a registry. This feature registers beside three others
//! that it relies on: the `lowering` feature
//! ([`LoweringFeature`](crate::stdlib::backends::lowering::LoweringFeature)),
//! which registers the shared presentation elements; the `latex` feature
//! ([`LatexFamily`](crate::stdlib::backends::latex::LatexFamily)), which
//! registers the LaTeX construct elements that this backend writes natively
//! and the service slots through which the regions stage
//! ([`LatexRecomposedRegionsStage`]) resolves package requests and definition
//! pieces; and the `check` feature
//! ([`check_feature`](crate::stdlib::check_feature)), which declares the
//! collection `flm.validated` that the `latex-recomposed` workflow demands
//! (see *What the workflow demands* below). A host normally obtains this
//! feature through [`feature()`].
//!
//! # The native element set
//!
//! The native set ([`native_elements`]) lists the elements that the backend's
//! emitter ([`emit_tree`](super::emit_tree)) writes as they are, without a
//! lowering rule. It is small: the backend
//! recomposes, so nearly everything reaches the emitter as a LaTeX construct
//! element that a feature's own lowering rule staged (built into the lowered
//! tree), and the emitter writes those construct elements and little else.
//!
//! - The four construct elements of the LaTeX family ([`construct::ALL`]): a
//! macro call, an environment, raw LaTeX text, and a definition. Every
//! lowering rule for this backend stages these.
//! - [`MATH_PASSTHROUGH`](crate::stdlib::backends::pres::MATH_PASSTHROUGH) and
//! [`VERBATIM`](crate::stdlib::backends::pres::VERBATIM): mathematics and
//! verbatim content are written to LaTeX byte for byte, so the shared
//! elements that carry them as source need no translation; the emitter
//! writes the delimiters and the source. Mathematics itself is written from
//! its nodes by the `math-passthrough` feature's rules on this backend, so
//! that a user definition used inside a formula is handled; a
//! `flm.pres.math-passthrough` node reaches the emitter only from that
//! feature's rule for an unknown command of math mode
//! ([`OPAQUE_RULE`](crate::math_passthrough::OPAQUE_RULE)), when such
//! a node is lowered on its own, outside a math group and outside a
//! display-math environment, for instance in a math-mode argument of a user
//! definition written as a call.
//! - [`UNKNOWN`](crate::stdlib::backends::pres::UNKNOWN): a macro or
//! environment that no feature declares is content the author wrote in
//! LaTeX syntax already; the emitter writes it as written, and the error
//! reported for it is among the run's diagnostics.
//! - [`UNSUPPORTED`](crate::stdlib::backends::pres::UNSUPPORTED): the node
//! that the lowering driver stages in place of an accepted unrenderable
//! element (an element that the plan request accepts as one the backend
//! need not render; see
//! [`PlanRequest::unsupported`](crate::core::plan::PlanRequest::unsupported)).
//! Every backend must write it natively; the emitter writes a comment line
//! that gives the element's name.
//! - [`DROPPED`](crate::stdlib::backends::pres::DROPPED): the node that a
//! lowering rule stages where it left out something it could not place
//! (stray content of a table of cells, the later of two overlapping cells);
//! the emitter writes a comment line that says what was left out.
//! - [`SUBTREE`](crate::stdlib::backends::pres::SUBTREE): the place of
//! another tree of the document, such as the tree of a file included with a
//! heading of its own; the recomposed LaTeX of that tree is written at that
//! place.
//! - Five structural elements
//! ([`StructuralElements`](crate::lang::structural::StructuralElements))
//! whose meaning LaTeX needs no translation for ([`FLOW_THROUGH`]):
//! `flm.text` (document text, which the emitter escapes; it must be native
//! rather than lowered because escaping happens where the bytes are
//! written), `flm.group` (a brace group), `flm.opaque-wrapper` (its
//! children, since it has no delimiters), `flm.comment` and
//! `flm.container`.
//!
//! The rest of the shared presentation elements (`par`, `span`,
//! `block-heading`, `runin-heading`, `link`, `list`, `lines`, `image`, the
//! decorations and their marks) are not in the set, because no rule of this
//! backend produces them (the features' recomposition rules stage construct
//! elements instead), and an element in the native set is an element the
//! emitter promises to write. `flm.parbreak` and `flm.paragraph` are not in
//! the set either: the two structural rules of [`mod@super::rules`] lower
//! them, and the paragraph-break rule is where the backend states that it
//! needs the raw tree. The set holds no element that a content feature owns.
//! Each content feature registers its own recomposition rule for this backend
//! ([`mod@super::rules`] explains how); this feature registers no lowering rule
//! but its own two.
//!
//! # What the workflow demands
//!
//! The `latex-recomposed` [workflow](crate::core::registry::WorkflowDef)
//! lists the `latex-recomposed` output and also *demands* one capability,
//! that is, asks the plan for the stages that provide it
//! ([`PlanRequest::demand`](crate::core::plan::PlanRequest::demand)). That
//! capability is [`flm.validated`](crate::stdlib::VALIDATED), a collection
//! ([`CollectionDef`](crate::core::registry::CollectionDef)): a name for a
//! set of capabilities that any feature may add to. Each check in the
//! registry adds its capability to it, and demanding the collection demands
//! all of them. A recomposition therefore runs the same
//! checks as every other workflow: a refused label type, a duplicate label or
//! an error in mathematics content is reported here exactly as by the
//! [`check` workflow](crate::stdlib::CHECK_WORKFLOW). The checks read the
//! document tree, in which a use site of a user definition that this backend
//! keeps is still a call of the definition; what the definition contains is
//! checked by LaTeX. The collection is declared by the `check` feature
//! ([`check_feature`](crate::stdlib::check_feature)), so a registry that
//! offers this backend must also hold that feature; a registry without it
//! fails to build, with an error that identifies this workflow's demands and
//! the missing name.
//!
//! Everything else that a run needs follows from the requirements of the
//! selected stages and lowering rules: the paragraph-break rule requires
//! `flm.blocks.raw`, and the reference rules read the table of safe labels,
//! so the stage that writes that table joins the plan.
use ;
use Arc;
use crateRegionsOutput;
use crateSettingsDecl;
use crateREGIONS_SLOT;
use crate;
use cratestructural;
use crateElementName;
use crateBind;
use crateVALIDATED;
use crateconstruct;
use crate;
use crate;
use crate;
use cratepres;
use rules;
use ;
/// The registered name of the [`LatexRecomposed`] feature, `latex-recomposed`.
/// It is also the middle part of the identifiers of the conditions this
/// backend reports, `flm.latex-recomposed.*`.
pub const FEATURE_NAME: &str = "latex-recomposed";
/// The declaration of the feature's settings: one option type,
/// [`LatexRecomposedRenderOptions`], bound to the render request, whose fields
/// are the settings keys. See [`crate::core::settings`].
/// The backend's registered name, `latex-recomposed`. A lowering rule marked
/// as the default on this backend refers to it
/// ([`DefaultMark::OnBackends`](crate::core::lowering::DefaultMark::OnBackends)),
/// and the names of the backend's lowering stage and lowered-trees table
/// (`flm.lowered.latex-recomposed`) are derived from it.
pub const BACKEND: &str = "latex-recomposed";
/// The default stem of the style file's name, `flmrecomposed`; see
/// [`LatexRecomposedRenderOptions::style_stem`].
pub const DEFAULT_STYLE_STEM: &str = "flmrecomposed";
/// The structural elements that this backend's emitter writes natively,
/// without a lowering rule: `flm.text`, `flm.group`, `flm.opaque-wrapper`,
/// `flm.comment` and `flm.container` (see the module documentation and
/// [structural elements](crate::guide::concepts_overview#structural-elements)).
pub const FLOW_THROUGH: = ;
/// The shared presentation elements ([`pres`]) that this backend's emitter
/// writes natively: `flm.pres.math-passthrough`, `flm.pres.verbatim`,
/// `flm.pres.unknown`, `flm.pres.unsupported`, `flm.pres.dropped`,
/// `flm.pres.subtree` and `flm.pres.span` (see the module documentation). The
/// span is here for the structural rule of `flm.math`
/// ([`MATH_RULE`](crate::stdlib::backends::lowering::rules::MATH_RULE)),
/// which every backend must be able to write; its content is written, its
/// role and kinds are not.
pub const SHARED_NATIVE: = ;
/// The backend's native element set, the elements its emitter writes without
/// a lowering rule: the four LaTeX construct elements ([`construct::ALL`]),
/// then [`SHARED_NATIVE`], then [`FLOW_THROUGH`], and nothing else. The
/// [`LatexRecomposed`] feature passes it to the backend's declaration
/// ([`BackendDef`]); see the module documentation for why each element is in
/// it.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::latex_recomposed::native_elements;
///
/// let native = native_elements();
/// assert_eq!(native.len(), 16);
/// assert!(native.iter().any(|element| element.as_str() == "flm.pres.latex.macro"));
/// assert!(native.iter().all(|element| element.as_str() != "flm.pres.par"));
/// ```
/// The render options of the `latex-recomposed` backend: how text is
/// escaped, which LaTeX packages and definitions the style file holds, how
/// user definitions, headings, labels, references and proofs are written, and
/// when the style file is written.
///
/// The option type is declared with [`Bind::Render`]: its value is set on the
/// render request
/// ([`PlanRequest::render_options`](crate::core::plan::PlanRequest::render_options())),
/// never on the document, so two runs that ask for different values use two
/// plans. The same fields are the keys of the feature's settings (see
/// [`crate::core::settings`]). Every field has a default, listed in its
/// documentation.
///
/// Several options are marked **You own this**: when you set them, the library
/// no longer guarantees that the recomposed LaTeX compiles or means what the
/// document says, and the documentation of the option states what you must
/// then provide yourself.
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::latex::UnicodeMode;
/// use flm_core::stdlib::backends::latex_recomposed::LatexRecomposedRenderOptions;
///
/// let options = LatexRecomposedRenderOptions::default();
/// assert_eq!(options.unicode, UnicodeMode::Declare);
/// assert!(!options.drop_comments);
/// assert_eq!(options.style_stem, "flmrecomposed");
/// assert!(!options.drop_math_labels_without_refs);
/// assert!(!options.resolved_proof_refs);
/// ```
/// When the style file is written: the value of the render option
/// [`style_file`](LatexRecomposedRenderOptions::style_file).
///
/// The style file ([`style`](crate::stdlib::backends::latex::style)) is the
/// `.sty` file written beside the recomposed LaTeX document, holding exactly
/// the package requests, character declarations and definitions that the
/// document needs to compile. A document that needs none of them (plain text
/// with no heading, label, reference, verbatim content or declared character)
/// needs no style file, unless its author asked for one through
/// [`extra_packages`](LatexRecomposedRenderOptions::extra_packages) or
/// [`extra_preamble`](LatexRecomposedRenderOptions::extra_preamble), whose
/// content is written only into the style file.
///
/// The regions stage ([`LatexRecomposedRegionsStage`]) reads this value.
/// The feature that registers the `latex-recomposed` backend (see the module
/// documentation).
///
/// Use [`feature()`] to obtain it as a value that a package entry can list.
/// When registered, it declares:
///
/// - the tables: the lowered trees that its lowering stage writes
/// (`flm.lowered.latex-recomposed`), the three tables that its emit stage
/// writes, the lowered pieces (document-level values such as the title,
/// placed beside the content; [`Piece`](crate::core::piece::Piece)), the
/// output regions, the style-file table,
/// the artifact table and the artifact set (see [`mod@super::stages`]);
/// - the settings, with the render options
/// ([`LatexRecomposedRenderOptions`]);
/// - the pieces stage ([`PiecesStage`]), which lowers only the pieces that
/// the render request asks to place, and the regions stage
/// ([`LatexRecomposedRegionsStage`]);
/// - its regions output ([`RegionsOutput`]), contributed to the service slot
/// `flm.output.regions` so that a host can assemble the output's regions
/// through a template of its own;
/// - the two structural lowering rules ([`super::rules()`]);
/// - the backend ([`BackendDef`]), with its native element set and its
/// lowering, emit and final stages ([`super::stages()`]);
/// - the `latex-recomposed` output ([`OutputDef`]) and the
/// `latex-recomposed` workflow ([`WorkflowDef`]).
;
/// Returns the [`LatexRecomposed`] feature as a shared value, ready to be
/// listed in a package entry.
///
/// A package that lists it must also list three other features, or the
/// registry build fails with an error that identifies the missing name:
///
/// - the `lowering` feature
/// ([`lowering::feature`](crate::stdlib::backends::lowering::feature())),
/// which registers the shared presentation elements;
/// - the `latex` feature
/// ([`latex::feature`](crate::stdlib::backends::latex::feature())), which
/// registers the LaTeX construct elements that this backend writes natively
/// and the service slots that its regions stage reads;
/// - the `check` feature ([`check_feature`](crate::stdlib::check_feature)),
/// which declares the [`flm.validated`](crate::stdlib::VALIDATED)
/// collection that this backend's workflow demands beside its output.
///
/// It depends on no content feature: each standard feature registers its own
/// recomposition rule for this backend, and an absent feature contributes
/// none. When the registry holds an element that a document may contain and
/// that no selected rule leads to the native set, building a plan for this
/// backend fails ([`PlanError::Incomplete`](crate::core::plan::PlanError::Incomplete)),
/// unless the plan request accepts that element as unrenderable
/// ([`PlanRequest::unsupported`](crate::core::plan::PlanRequest::unsupported)).
///
/// # Examples
///
/// ```
/// use flm_core::stdlib::backends::latex_recomposed;
///
/// assert_eq!(latex_recomposed::feature().info().name.as_str(), latex_recomposed::FEATURE_NAME);
/// ```