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
//! The output regions of the HTML output, and the page the backend's final
//! stage assembles from them.
//!
//! The entry artifact of the `html` output is a **page**: an HTML document a
//! person can open in a browser and a validator can check. A host that wants
//! the HTML of each document tree without a page around it reads the table
//! `flm.output.html` ([`HtmlFragments`]) instead; that table and the page's
//! table `flm.output.html.document` are written in the same run.
//!
//! The page is made in two steps:
//!
//! 1. [`regions`] makes the page's output regions. An output region is one
//! named part of an output's entry artifact, which the backend writes once
//! into the table `flm.output.<backend>.regions` ([`Regions`]). On this
//! backend the regions are the document's content (with every tree placed in
//! another tree written where it was placed), each piece the render places (a
//! [piece](crate::core::piece::Piece) is a document-level value a backend
//! places beside the content, such as the title), the stylesheets, the
//! scripts, the words of the page's title and its language. The stage `flm.html.regions`
//! ([`HtmlRegionsStage`](super::stages::HtmlRegionsStage)) calls it and
//! writes the result into the table `flm.output.html.regions`.
//! 2. [`assemble`] writes the page from those regions; the backend's final
//! stage ([`HtmlFinalStage`](super::stages::HtmlFinalStage)) calls it. A
//! host may assemble the same regions through a template of its own instead
//! (see [output regions and the final
//! stage](crate::guide::concepts_overview#output-regions-and-the-final-stage)).
//!
//! # The page's layout
//!
//! The final stage reads the regions alone and takes no template. The page
//! holds, in order: the document type line; `<html>`, with the `lang`
//! attribute when there is a [`LANG`] region; `<head>`, holding the character
//! encoding, the viewport line, the `<title>` holding the words of the
//! [`TITLE`] region, and the stylesheets; then `<body>`, holding
//! `<main id="flm-content" class="flm-content">` and, after it, the scripts.
//! Inside `<main>` come the pieces placed before the content, the content, and
//! the pieces placed after the content.
//!
//! The parts of a region are placed as block-level content
//! ([`HtmlSink::markup_block`](super::HtmlSink::markup_block)), so that the
//! line break this backend writes between two block-level tags is present
//! where a tree begins, between two trees, and before the closing tag of the
//! element they are written into. The bytes of each part are written
//! unchanged, indentation included: re-indenting finished HTML would require
//! reading output back, which this backend's sink does not allow.
//!
//! # Stylesheets and scripts
//!
//! The stylesheets and the scripts are the style and script definitions the
//! document requires, read from the definitions table
//! [`flm.html.definitions`](super::defs::DEFINITIONS_TABLE), in the order that
//! table gives. The [stage that writes that table](super::defs) has already
//! decided each definition's delivery (written into the page, loaded from an
//! address, or left to the host), and the regions stage reads nothing else
//! about definitions:
//!
//! - a text part becomes a `<style>` or a `<script>` holding it;
//! - an address part becomes a `<link rel="stylesheet">` or a
//! `<script src defer>`;
//! - a file part, which this library does not read, and a text part the page
//! cannot hold are left out and reported
//! ([`DefinitionNotInlinable`]);
//! - a definition the host provides itself writes nothing.
//!
//! Every element written for a definition carries the attribute
//! [`DEF_ATTRIBUTE`], so that a reader of the page can tell which definition it
//! came from.
//!
//! # Why this module writes unescaped bytes
//!
//! This module writes three kinds of bytes without escaping them, each through
//! an entry point of the [sink](super::HtmlSink) made for it:
//!
//! - **The HTML of each tree and each piece.** The [emitter](super::emit)
//! wrote those bytes through the same sink, with all document text escaped
//! where it was written. The page writes them unchanged as a
//! [`Markup`] value created by this library; escaping them again would show
//! the markup instead of rendering it.
//! - **The document type line** ([`DOCTYPE`]), written the same way, because it
//! is not a tag and the tag builder cannot write it.
//! - **The text of a style or script definition**, written into a `<style>` or
//! a `<script>` through
//! [`raw_text_element`](super::HtmlSink::raw_text_element). A browser reads
//! the content of those two elements character by character, so nothing in
//! it can be escaped. That text comes from a feature's author or from the
//! host's settings, never from a document. The only sequence it must not hold
//! is the element's own closing tag, which that entry point refuses; the
//! part is then left out and reported.
//!
//! An address a definition carries is written unchanged as an attribute value
//! (escaped like every attribute value). It comes from a feature's author or a
//! host, not from a document, so the [address policy](super::check_address), which
//! decides which addresses a link or an image may carry, does not apply to it,
//! and this library never fetches it.
use BTreeSet;
use crateArtifactId;
use cratePiecePlace;
use crate;
use crateDefContent;
use crateTreeId;
use crateTITLE as TITLE_PIECE;
use CONTENT_CLASS;
use ;
use ;
use ;
use Markup;
use HtmlSink;
use ;
/// The first line of every page, `<!DOCTYPE html>`: the document type
/// declaration that puts a browser in its standards mode.
///
/// It is not a tag, so the tag builder cannot write it; [`assemble`] writes it
/// as a [`Markup`] value created by this library. The line break after it is
/// the one the sink writes before the `<html>` element that follows.
pub const DOCTYPE: &str = "<!DOCTYPE html>";
/// The `id` of the `<main>` element holding the document's content,
/// `flm-content`: the same string as its class
/// ([`CONTENT_CLASS`]), so that a host can
/// select the content either way.
pub const CONTENT_ID: &str = "flm-content";
/// The value of the viewport line (`<meta name="viewport">`) every page
/// carries, which tells a small screen to lay the page out at its own width
/// rather than at a desktop's.
pub const VIEWPORT: &str = "width=device-width, initial-scale=1";
/// The attribute every element written for a style or script definition
/// carries, holding that definition's identifier: `data-flm-def`.
///
/// It is the page's own attribute, not one derived from a presentation
/// element. It lets a reader of the page (a person, or a tool that removes a
/// definition again) tell which definition each `<style>`, `<script>` or
/// `<link>` came from.
pub const DEF_ATTRIBUTE: &str = "data-flm-def";
/// The event that a script updating a page in place dispatches on a region's
/// element once that element's content has been replaced:
/// `flm:region-updated`.
///
/// It is one of the two conventions for a page updated in place, which the
/// rendering guide describes ([a page updated in
/// place](crate::guide::rendering#a-page-updated-in-place)); the other is
/// [`ORIGINAL_HTML_ATTRIBUTE`]. The event bubbles, so it reaches a listener on
/// the region's element and one on `document`. Its `detail` holds `region`,
/// the `id` of the region's element, and `artifacts`, the paths of the files
/// beside the page that changed with it. A script of the page that transforms
/// elements in the browser listens to it to transform the region's new
/// elements, as the formula renderer's configuration ([`super::mathjax`])
/// does.
pub const REGION_UPDATED_EVENT: &str = "flm:region-updated";
/// The attribute in which a script that transformed an element in the browser
/// records the HTML the element held before: `data-flm-original-html`.
///
/// It is the second convention for a page updated in place
/// ([`REGION_UPDATED_EVENT`] is the first). The script that updates the page
/// leaves an element carrying this attribute unchanged when the new HTML for
/// that element equals the recorded HTML, and otherwise replaces the element's
/// content, which removes the record; the script that transformed the element
/// then transforms it again when it receives the event. The formula
/// renderer's configuration writes this attribute on each formula just before
/// typesetting it.
pub const ORIGINAL_HTML_ATTRIBUTE: &str = "data-flm-original-html";
/// The event that a script of the page dispatches on the element holding a
/// copy of part of the page, once the copy is in the page:
/// `flm:content-copied`.
///
/// It extends the two conventions for a page updated in place
/// ([`REGION_UPDATED_EVENT`], [`ORIGINAL_HTML_ATTRIBUTE`]) to content that a
/// script copies within the page, such as a popup that previews the target of
/// a link; the rendering guide describes it ([a page updated in
/// place](crate::guide::rendering#a-page-updated-in-place)). The copy holds
/// none of the page's identifiers and keeps the record of each element it
/// copies. The event bubbles, and its `detail` holds nothing. A script of the
/// page that transforms elements in the browser listens to it to transform the
/// elements of the copy that are not transformed, as the formula renderer's
/// configuration ([`super::mathjax`]) does.
pub const CONTENT_COPIED_EVENT: &str = "flm:content-copied";
/// The result of [`regions`]: the output regions of the HTML output, and what
/// could not be written into them, for the regions stage to report.
/// Makes the output regions of an HTML page from the HTML of the trees, the
/// HTML of the pieces, the definitions table, the tree order and the render
/// options. The regions stage
/// ([`HtmlRegionsStage`](super::stages::HtmlRegionsStage)) calls it.
///
/// It makes these regions, in this order:
///
/// - [`CONTENT`], from `fragments`: the trees listed in `order`, in that order.
/// Each part of the region is one segment of HTML a tree's emission wrote,
/// so that the final stage places each on a line of its own. A tree that a
/// `flm.pres.subtree` node places in another tree is written where it was
/// placed, and not again at the top level, even when `order` lists it.
/// Trees that are placed only by one another, in a cycle, and by no other
/// tree, are written at the top level after the other trees, so that their
/// content still reaches the page.
/// - One region per piece of `pieces`, with the piece's place and words.
/// - [`STYLES`] and [`SCRIPTS`], from `definitions`: one part per element
/// written for a part of a definition (a `<style>`, a `<link>` or a
/// `<script>`).
/// - [`TITLE`]: the words of the title piece
/// ([`flm.metadata.title`](crate::stdlib::features::metadata::TITLE)) when `pieces`
/// holds it and its words are not empty, otherwise the title of `options`
/// ([`HtmlRenderOptions::title`]).
/// - [`LANG`], when `options` gives a language ([`HtmlRenderOptions::lang`]).
///
/// The placement that closes a cycle, a placed tree with no HTML, and a
/// definition part the page cannot hold write nothing and are recorded in the
/// result ([`HtmlRegions`]; a cycle in [`HtmlRegions::cycles`]). A tree with no HTML at the top level writes nothing and
/// is not recorded, because the failure that left it without HTML was reported
/// where it happened.
/// Returns the page the backend's final stage writes, assembled from
/// `regions`: the stylesheets in the head; in `<main>`, the pieces placed
/// before the content, the content and the pieces placed after it; the
/// scripts at the end of the body; the language and the title's words from
/// the regions [`LANG`] and [`TITLE`]. The [module
/// documentation](self#the-pages-layout) gives the full layout.
///
/// A region missing from `regions` contributes nothing. The page ends with a
/// line break.
/// Writes every part of `region` as block-level content, each on a line of
/// its own; writes nothing when `region` is `None`.
/// Returns the parts of every definition of kind `kind` in `definitions`, in
/// the table's order: one element per part, each written by a sink of its
/// own. A part that cannot be written is pushed onto `not_inlinable` instead.
/// Writes a text part as a `<style>` or a `<script>` holding it.
///
/// The content of those two elements cannot be escaped, because a browser
/// reads it character by character, so the sink refuses text holding the
/// element's own closing tag; the refusal is returned and nothing is written.
/// Writes an address part as a `<link rel="stylesheet">` or a
/// `<script src defer>`.
///
/// The address comes from a feature's author or a host, so it is written
/// unchanged (escaped as an attribute value); this library never fetches it.
/// A script is written with `defer`, which lets a browser finish reading the
/// page before the script runs and runs deferred scripts in the order they
/// were written.
/// Returns the trees that another tree's HTML places (a tree placing itself is
/// a cycle: it is written at the top level and reported).
/// The content region while it is being made.