senu 0.2.0

Declare a Rust function once and serve it as a web page, a JSON API and an MCP tool, with one permission check and one contract test suite
Documentation
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
//! The Markdown policy (spec 0003, §4 and §5).
//!
//! pulldown-cmark parses the article; every event then passes an explicit
//! allow-list before `pulldown_cmark::html::push_html` writes HTML, which
//! escapes text and attributes. Raw HTML is shown as text, the info string
//! of a code block is dropped, and links must point at an existing page of
//! the site or at an `https://` address. Anything the policy does not list
//! is an error, so new parser features fail closed.

use std::collections::BTreeSet;
use std::fmt;
use std::ops::Range;

use pulldown_cmark::{
    Alignment, CodeBlockKind, Event, HeadingLevel, LinkType, Options, Parser, Tag, TagEnd,
};

/// The only parser extension: tables. Every other one stays off.
pub const OPTIONS: Options = Options::ENABLE_TABLES;

/// The longest title, in characters.
pub const MAX_TITLE_CHARS: usize = 80;

/// HTML written by [`render`] from allowed events only. The field is
/// private, so no other code can construct one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TrustedHtml(String);

impl TrustedHtml {
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// One `<script type="application/ld+json">` element that holds `data`
    /// (spec 0006, §6). `serde_json` writes the JSON, and every `<`, `>`
    /// and `&` in it becomes a `\u` escape, so no string in `data` can end
    /// the element or start markup. This and [`render`] are the only ways
    /// to build a `TrustedHtml`.
    pub fn json_ld(data: &serde_json::Value) -> Self {
        let json = data
            .to_string()
            .replace('<', "\\u003c")
            .replace('>', "\\u003e")
            .replace('&', "\\u0026");
        Self(format!(
            "<script type=\"application/ld+json\">{json}</script>"
        ))
    }
}

impl fmt::Display for TrustedHtml {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

// The single trusted-HTML boundary (spec 0003, SAFE-4): Askama inserts this
// type without escaping. Every other template value is escaped.
impl askama::filters::HtmlSafe for TrustedHtml {}

/// How words are counted for the reading time.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Counting {
    /// Runs of visible text separated by whitespace, line breaks or block
    /// boundaries, for English. Emphasis, links, inline code and raw HTML
    /// shown as text do not split a run.
    Words,
    /// Non-whitespace characters divided by two, for Japanese, which has
    /// no spaces between words.
    Characters,
}

/// A rendered article.
#[derive(Debug)]
pub struct Article {
    pub title: String,
    pub body: TrustedHtml,
    /// A rough word count of the body outside code blocks.
    pub words: usize,
}

/// The plain text that the page metadata is built from (spec 0006, §6).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Outline {
    /// The first paragraph after the title, as plain text; empty when the
    /// article has none.
    pub summary: String,
    /// Each level-2 heading with the first paragraph that follows it.
    pub sections: Vec<(String, String)>,
}

/// Reads the [`Outline`] of an article that [`render`] accepted. Only text
/// is collected, never markup: emphasis and links give their text, inline
/// code and raw HTML are literal, and code blocks, lists and tables are
/// skipped. Every consumer escapes the result.
pub fn outline(text: &str) -> Outline {
    enum Target {
        None,
        Heading,
        Paragraph,
    }
    let mut outline = Outline::default();
    let (mut target, mut buffer) = (Target::None, String::new());
    // Nesting depth inside blocks whose paragraphs do not count.
    let mut skipped = 0_usize;
    let mut seen_title = false;
    // A level-2 heading that still waits for its paragraph.
    let mut open_section = false;
    for event in Parser::new_ext(text, OPTIONS) {
        match event {
            Event::Start(
                Tag::CodeBlock(_)
                | Tag::HtmlBlock
                | Tag::List(_)
                | Tag::Table(_)
                | Tag::BlockQuote(_),
            ) => skipped += 1,
            Event::End(
                TagEnd::CodeBlock
                | TagEnd::HtmlBlock
                | TagEnd::List(_)
                | TagEnd::Table
                | TagEnd::BlockQuote(_),
            ) => skipped = skipped.saturating_sub(1),
            _ if skipped > 0 => {}
            Event::Start(Tag::Heading { level, .. }) => {
                buffer.clear();
                open_section = false;
                target = if level == HeadingLevel::H2 {
                    Target::Heading
                } else {
                    Target::None
                };
            }
            Event::End(TagEnd::Heading(level)) => {
                if level == HeadingLevel::H1 {
                    seen_title = true;
                }
                if matches!(target, Target::Heading) {
                    outline.sections.push((tidy(&buffer), String::new()));
                    open_section = true;
                }
                target = Target::None;
            }
            Event::Start(Tag::Paragraph) if seen_title => {
                buffer.clear();
                target = Target::Paragraph;
            }
            Event::End(TagEnd::Paragraph) if matches!(target, Target::Paragraph) => {
                let paragraph = tidy(&buffer);
                if outline.summary.is_empty() {
                    outline.summary = paragraph.clone();
                }
                if open_section && let Some(section) = outline.sections.last_mut() {
                    section.1 = paragraph;
                    open_section = false;
                }
                target = Target::None;
            }
            Event::Text(text) | Event::Code(text) | Event::InlineHtml(text) => {
                if !matches!(target, Target::None) {
                    buffer.push_str(&text);
                }
            }
            Event::SoftBreak | Event::HardBreak => buffer.push(' '),
            _ => {}
        }
    }
    outline
}

/// Collapses every run of whitespace into one space.
fn tidy(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// `text` in at most `limit` characters: whole when it fits, otherwise cut
/// at the last space near the end, or between characters when there is
/// none, as in Japanese, and closed with an ellipsis.
pub fn shorten(text: &str, limit: usize) -> String {
    if text.chars().count() <= limit {
        return text.to_owned();
    }
    let kept: String = text.chars().take(limit.saturating_sub(1)).collect();
    let cut = match kept.rfind(' ') {
        Some(space) if kept[space..].chars().count() <= 24 => &kept[..space],
        _ => kept.as_str(),
    };
    format!("{}…", cut.trim_end_matches([',', ';', ':', ' ', '、']))
}

/// Why an article was refused, at a byte offset of its text.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Problem {
    pub offset: usize,
    pub reason: String,
}

/// Renders an article whose first block is its level-1 title. `routes` are
/// the exact paths an internal link may point at.
pub fn render(
    text: &str,
    routes: &BTreeSet<String>,
    counting: Counting,
) -> Result<Article, Problem> {
    let mut events = Parser::new_ext(text, OPTIONS).into_offset_iter();
    let title = title(&mut events)?;
    let mut policy = Policy::new(routes, counting);
    let mut allowed = Vec::new();
    for (event, range) in events {
        let event = policy.event(event).map_err(|reason| Problem {
            offset: range.start,
            reason,
        })?;
        allowed.push(event);
    }
    let mut html = String::new();
    pulldown_cmark::html::push_html(&mut html, allowed.into_iter());
    Ok(Article {
        title,
        body: TrustedHtml(html),
        words: policy.words + policy.characters / 2,
    })
}

/// Reads the level-1 heading that must open the article. Inline code and
/// inline HTML in it become literal text.
fn title<'a>(
    events: &mut impl Iterator<Item = (Event<'a>, Range<usize>)>,
) -> Result<String, Problem> {
    let problem = |range: &Range<usize>, reason: &str| Problem {
        offset: range.start,
        reason: reason.to_owned(),
    };
    let heading = match events.next() {
        Some((
            Event::Start(Tag::Heading {
                level: HeadingLevel::H1,
                ..
            }),
            range,
        )) => range,
        other => {
            let range = other.map_or(0..0, |(_, range)| range);
            return Err(problem(
                &range,
                "the first block must be a level-1 heading, such as `# My note`",
            ));
        }
    };
    let mut title = String::new();
    for (event, range) in events.by_ref() {
        match event {
            Event::End(TagEnd::Heading(_)) => break,
            Event::Text(text) | Event::Code(text) | Event::InlineHtml(text) => {
                title.push_str(&text);
            }
            Event::SoftBreak | Event::HardBreak => title.push(' '),
            Event::Start(Tag::Emphasis | Tag::Strong)
            | Event::End(TagEnd::Emphasis | TagEnd::Strong) => {}
            Event::Start(Tag::Link { .. } | Tag::Image { .. }) => {
                return Err(problem(
                    &range,
                    "the title cannot contain a link or an image",
                ));
            }
            _ => {
                return Err(problem(
                    &range,
                    "the title can contain only text, emphasis and inline code",
                ));
            }
        }
    }
    let title = title.trim();
    match title.chars().count() {
        0 => Err(problem(&heading, "the title is empty")),
        n if n > MAX_TITLE_CHARS => {
            Err(problem(&heading, "the title is longer than 80 characters"))
        }
        _ => Ok(title.to_owned()),
    }
}

/// The event allow-list for the body, which also counts words.
struct Policy<'r> {
    routes: &'r BTreeSet<String>,
    counting: Counting,
    /// Inside a code block or an HTML block, which is shown as code.
    code_depth: usize,
    words: usize,
    characters: usize,
    /// The last counted text ended inside an English word.
    in_word: bool,
}

impl<'r> Policy<'r> {
    fn new(routes: &'r BTreeSet<String>, counting: Counting) -> Self {
        Self {
            routes,
            counting,
            code_depth: 0,
            words: 0,
            characters: 0,
            in_word: false,
        }
    }

    /// The event to write, or why the article is refused.
    fn event<'a>(&mut self, event: Event<'a>) -> Result<Event<'a>, String> {
        // Emphasis and links sit inside a word; every other tag and break
        // ends it.
        match &event {
            Event::Start(Tag::Emphasis | Tag::Strong | Tag::Link { .. })
            | Event::End(TagEnd::Emphasis | TagEnd::Strong | TagEnd::Link) => {}
            Event::Start(_) | Event::End(_) | Event::SoftBreak | Event::HardBreak | Event::Rule => {
                self.in_word = false;
            }
            _ => {}
        }
        Ok(match event {
            Event::Start(tag) => Event::Start(self.start(tag)?),
            Event::End(tag) => Event::End(self.end(tag)?),
            Event::Text(text) => {
                self.count(&text);
                Event::Text(text)
            }
            Event::Code(code) => {
                if self.counting == Counting::Characters || !self.in_word {
                    self.words += 1;
                }
                self.in_word = true;
                Event::Code(code)
            }
            // Raw HTML is shown as text, never passed through.
            Event::Html(html) | Event::InlineHtml(html) => {
                self.count(&html);
                Event::Text(html)
            }
            Event::SoftBreak => Event::SoftBreak,
            Event::HardBreak => Event::HardBreak,
            Event::Rule => Event::Rule,
            Event::InlineMath(_) | Event::DisplayMath(_) => return Err(unsupported("math")),
            Event::FootnoteReference(_) => return Err(unsupported("a footnote")),
            Event::TaskListMarker(_) => return Err(unsupported("a task list")),
        })
    }

    fn start<'a>(&mut self, tag: Tag<'a>) -> Result<Tag<'a>, String> {
        Ok(match tag {
            Tag::Paragraph => Tag::Paragraph,
            Tag::Heading {
                level: HeadingLevel::H1,
                ..
            } => {
                return Err(
                    "only the title can be a level-1 heading; use `## ` for a section".into(),
                );
            }
            Tag::Heading { level, .. } => Tag::Heading {
                level,
                id: None,
                classes: Vec::new(),
                attrs: Vec::new(),
            },
            Tag::BlockQuote(None) => Tag::BlockQuote(None),
            // The info string is dropped, and an HTML block becomes code.
            Tag::CodeBlock(_) | Tag::HtmlBlock => {
                self.code_depth += 1;
                Tag::CodeBlock(CodeBlockKind::Indented)
            }
            Tag::List(start) => Tag::List(start),
            Tag::Item => Tag::Item,
            // Column alignment would become a `style` attribute; it is dropped.
            Tag::Table(columns) => Tag::Table(vec![Alignment::None; columns.len()]),
            Tag::TableHead => Tag::TableHead,
            Tag::TableRow => Tag::TableRow,
            Tag::TableCell => Tag::TableCell,
            Tag::Emphasis => Tag::Emphasis,
            Tag::Strong => Tag::Strong,
            Tag::Link {
                link_type,
                dest_url,
                title,
                id,
            } => {
                // E-mail autolinks would become `mailto:`; wikilinks are off.
                let checked = matches!(
                    link_type,
                    LinkType::Inline
                        | LinkType::Reference
                        | LinkType::Collapsed
                        | LinkType::Shortcut
                        | LinkType::Autolink
                );
                if !(checked
                    && (self.routes.contains(dest_url.as_ref()) || is_https_address(&dest_url)))
                {
                    let shown = match link_type {
                        LinkType::Email => format!("mailto:{dest_url}"),
                        _ => dest_url.to_string(),
                    };
                    return Err(format!(
                        "the link {shown:?} is neither a page of this site, such as \"/\", nor an https:// address"
                    ));
                }
                Tag::Link {
                    link_type,
                    dest_url,
                    title,
                    id,
                }
            }
            Tag::Image { .. } => return Err(unsupported("an image")),
            Tag::BlockQuote(Some(_)) => return Err(unsupported("an alert block quote")),
            Tag::FootnoteDefinition(_) => return Err(unsupported("a footnote")),
            Tag::DefinitionList | Tag::DefinitionListTitle | Tag::DefinitionListDefinition => {
                return Err(unsupported("a definition list"));
            }
            Tag::Strikethrough => return Err(unsupported("strikethrough")),
            Tag::Superscript | Tag::Subscript => {
                return Err(unsupported("superscript or subscript"));
            }
            Tag::MetadataBlock(_) => return Err(unsupported("a metadata block")),
        })
    }

    /// Closes what [`Policy::start`] accepted.
    fn end(&mut self, tag: TagEnd) -> Result<TagEnd, String> {
        Ok(match tag {
            TagEnd::CodeBlock | TagEnd::HtmlBlock => {
                self.code_depth = self.code_depth.saturating_sub(1);
                TagEnd::CodeBlock
            }
            TagEnd::Paragraph
            | TagEnd::Heading(_)
            | TagEnd::BlockQuote(None)
            | TagEnd::List(_)
            | TagEnd::Item
            | TagEnd::Table
            | TagEnd::TableHead
            | TagEnd::TableRow
            | TagEnd::TableCell
            | TagEnd::Emphasis
            | TagEnd::Strong
            | TagEnd::Link => tag,
            TagEnd::BlockQuote(Some(_))
            | TagEnd::Image
            | TagEnd::FootnoteDefinition
            | TagEnd::DefinitionList
            | TagEnd::DefinitionListTitle
            | TagEnd::DefinitionListDefinition
            | TagEnd::Strikethrough
            | TagEnd::Superscript
            | TagEnd::Subscript
            | TagEnd::MetadataBlock(_) => return Err(unsupported("this Markdown")),
        })
    }

    fn count(&mut self, text: &str) {
        if self.code_depth > 0 {
            return;
        }
        match self.counting {
            Counting::Words => {
                for c in text.chars() {
                    if c.is_whitespace() {
                        self.in_word = false;
                    } else if !self.in_word {
                        self.words += 1;
                        self.in_word = true;
                    }
                }
            }
            Counting::Characters => {
                self.characters += text.chars().filter(|c| !c.is_whitespace()).count();
            }
        }
    }
}

fn unsupported(what: &str) -> String {
    format!("{what} is not supported in articles")
}

/// `https://`, a lowercase host name with a dot, then nothing or a tail
/// that starts with `/`, `?` or `#` and holds only visible ASCII without
/// `"<>\^`{|}`. A port, user information or a percent sign in the host is
/// therefore impossible.
pub fn is_https_address(dest: &str) -> bool {
    let Some(rest) = dest.strip_prefix("https://") else {
        return false;
    };
    let (host, tail) = rest.split_at(rest.find(['/', '?', '#']).unwrap_or(rest.len()));
    let host_ok = (1..=253).contains(&host.len())
        && host
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'.' || b == b'-')
        && host.contains('.')
        && !host.starts_with(['.', '-'])
        && !host.ends_with(['.', '-']);
    let tail_ok = tail
        .bytes()
        .all(|b| (b'!'..=b'~').contains(&b) && !b"\"<>\\^`{|}".contains(&b));
    host_ok && tail_ok
}

#[cfg(test)]
mod tests {
    use super::*;
    use pulldown_cmark::{BlockQuoteKind, CowStr};

    const ALLOWED_TAGS: [&str; 23] = [
        "p",
        "h2",
        "h3",
        "h4",
        "h5",
        "h6",
        "blockquote",
        "ul",
        "ol",
        "li",
        "table",
        "thead",
        "tbody",
        "tr",
        "th",
        "td",
        "em",
        "strong",
        "code",
        "pre",
        "a",
        "br",
        "hr",
    ];

    fn routes() -> BTreeSet<String> {
        ["/", "/ja/", "/first-note", "/ja/first-note"]
            .into_iter()
            .map(str::to_owned)
            .collect()
    }

    fn rendered(text: &str) -> Result<Article, Problem> {
        render(text, &routes(), Counting::Words)
    }

    /// The body HTML of an article titled `T`.
    fn body(markdown: &str) -> String {
        let text = format!("# T\n\n{markdown}");
        match rendered(&text) {
            Ok(article) => article.body.as_str().to_owned(),
            Err(problem) => panic!("{markdown:?} was refused: {problem:?}"),
        }
    }

    /// The 1-based line and reason of a refused article.
    fn refusal(text: &str) -> (usize, String) {
        let problem = rendered(text).expect_err("refused");
        let line = text[..problem.offset].matches('\n').count() + 1;
        (line, problem.reason)
    }

    /// Tags and attribute names in rendered HTML. Every `<` in the output
    /// starts a tag, because text is escaped.
    fn tags(html: &str) -> Vec<(String, Vec<String>)> {
        let mut found = Vec::new();
        let mut rest = html;
        while let Some(start) = rest.find('<') {
            let end = start + rest[start..].find('>').expect("a closed tag");
            let inner = rest[start + 1..end]
                .trim_start_matches('/')
                .trim_end_matches('/')
                .trim_end();
            let name_end = inner.find(char::is_whitespace).unwrap_or(inner.len());
            let mut attributes = Vec::new();
            let mut attrs = inner[name_end..].trim_start();
            while !attrs.is_empty() {
                let name_end = attrs.find(['=', ' ']).unwrap_or(attrs.len());
                attributes.push(attrs[..name_end].to_owned());
                attrs = &attrs[name_end..];
                if let Some(value) = attrs.strip_prefix("=\"") {
                    let close = value.find('"').expect("a quoted value");
                    attrs = &value[close + 1..];
                }
                attrs = attrs.trim_start();
            }
            found.push((inner[..name_end].to_owned(), attributes));
            rest = &rest[end + 1..];
        }
        found
    }

    fn assert_only_allowed_markup(html: &str) {
        for (tag, attributes) in tags(html) {
            assert!(ALLOWED_TAGS.contains(&tag.as_str()), "<{tag}> in {html}");
            for attribute in attributes {
                assert!(
                    ["href", "title", "start"].contains(&attribute.as_str()),
                    "{attribute} on <{tag}> in {html}"
                );
            }
        }
    }

    #[test]
    fn options_enable_tables_and_nothing_else() {
        assert_eq!(OPTIONS, Options::ENABLE_TABLES);
    }

    #[test]
    fn ordinary_markdown_becomes_standard_html() {
        let html = body(
            "Some *emphasis*, **strong** and `code`.\nNext line.  \nBreak.\n\n\
             ## Section\n\n###### Small\n\n> Quoted\n\n- one\n- two\n\n3. three\n4. four\n\n\
             | a | b |\n|:--|--:|\n| 1 | 2 |\n\n---\n",
        );
        assert_eq!(
            html,
            "<p>Some <em>emphasis</em>, <strong>strong</strong> and <code>code</code>.\nNext line.<br />\nBreak.</p>\n\
             <h2>Section</h2>\n<h6>Small</h6>\n<blockquote>\n<p>Quoted</p>\n</blockquote>\n\
             <ul>\n<li>one</li>\n<li>two</li>\n</ul>\n<ol start=\"3\">\n<li>three</li>\n<li>four</li>\n</ol>\n\
             <table><thead><tr><th>a</th><th>b</th></tr></thead><tbody>\n<tr><td>1</td><td>2</td></tr>\n</tbody></table>\n<hr />\n"
        );
    }

    #[test]
    fn markup_in_the_source_is_shown_as_text() {
        let cases = [
            "<script>alert(1)</script>",
            "<img src=x onerror=alert(1)>",
            "<div onclick=\"x()\">block</div>\n",
            "Inline <b onmouseover=x>bold</b> and <!-- comment -->",
            "`<script>in code</script>`",
            "```\n<script>fenced</script>\n```",
            "    <script>indented</script>",
        ];
        for markdown in cases {
            let html = body(markdown);
            assert_only_allowed_markup(&html);
            assert!(!html.contains("<script"), "{html}");
            assert!(html.contains("&lt;"), "{markdown:?} lost its text: {html}");
        }
        let html = body("<img src=x onerror=alert(1)>");
        assert_eq!(
            html,
            "<pre><code>&lt;img src=x onerror=alert(1)&gt;</code></pre>\n"
        );
    }

    #[test]
    fn the_info_string_of_a_code_block_is_dropped() {
        let html = body(
            "```\"><script>alert(1)</script>\nlet x = 1;\n```\n\n```rust file=src/x.rs\nfn f() {}\n```",
        );
        assert_eq!(
            html,
            "<pre><code>let x = 1;\n</code></pre>\n<pre><code>fn f() {}\n</code></pre>\n"
        );
    }

    #[test]
    fn images_and_a_second_title_are_refused() {
        assert_eq!(
            refusal("# T\n\n![alt](https://example.com/a.png)\n"),
            (3, "an image is not supported in articles".into())
        );
        let second = "only the title can be a level-1 heading; use `## ` for a section";
        assert_eq!(refusal("# T\n\ntext\n\n# Again\n"), (5, second.into()));
        assert_eq!(refusal("# T\n\nAgain\n=====\n"), (3, second.into()));
    }

    #[test]
    fn disabled_extensions_stay_literal_text() {
        let html = body(
            "Note[^1] and $x^2$ and ~~gone~~ and ^up^ and ~down~ and [[Wiki]].\n\n\
             [^1]: The footnote.\n\n- [ ] open\n- [x] done\n\n> [!NOTE]\n> Alert.\n\n\
             Term\n: Definition\n\n---\ntitle: x\n---\n",
        );
        assert_only_allowed_markup(&html);
        for absent in ["<input", "<sup", "<math", "<del", "<dl", "class="] {
            assert!(!html.contains(absent), "{absent} in {html}");
        }
        for literal in [
            "[^1]",
            "$x^2$",
            "~~gone~~",
            "[ ] open",
            "[x] done",
            "[!NOTE]",
            ": Definition",
        ] {
            assert!(html.contains(literal), "{literal} missing from {html}");
        }
    }

    #[test]
    fn unsupported_events_fail_closed() {
        let routes = routes();
        let events = [
            Event::FootnoteReference(CowStr::from("1")),
            Event::TaskListMarker(false),
            Event::InlineMath(CowStr::from("x")),
            Event::DisplayMath(CowStr::from("x")),
            Event::Start(Tag::FootnoteDefinition(CowStr::from("1"))),
            Event::Start(Tag::Strikethrough),
            Event::Start(Tag::Superscript),
            Event::Start(Tag::DefinitionList),
            Event::Start(Tag::BlockQuote(Some(BlockQuoteKind::Note))),
            Event::Start(Tag::Image {
                link_type: LinkType::Inline,
                dest_url: CowStr::from("https://example.com/a.png"),
                title: CowStr::from(""),
                id: CowStr::from(""),
            }),
            Event::Start(Tag::Link {
                link_type: LinkType::WikiLink { has_pothole: false },
                dest_url: CowStr::from("/first-note"),
                title: CowStr::from(""),
                id: CowStr::from(""),
            }),
            Event::End(TagEnd::Strikethrough),
        ];
        for event in events {
            let shown = format!("{event:?}");
            assert!(
                Policy::new(&routes, Counting::Words).event(event).is_err(),
                "{shown} was accepted"
            );
        }
    }

    #[test]
    fn link_titles_are_kept_escaped() {
        let html = body("[x](/first-note \"a \\\" onmouseover=alert(1) <b>\")");
        assert_eq!(
            html,
            "<p><a href=\"/first-note\" title=\"a &quot; onmouseover=alert(1) &lt;b&gt;\">x</a></p>\n"
        );
        assert_only_allowed_markup(&html);
    }

    #[test]
    fn allowed_link_destinations_pass() {
        for dest in [
            "/",
            "/ja/",
            "/first-note",
            "/ja/first-note",
            "https://example.com",
            "https://example.com/",
            "https://doc.rust-lang.org/book/ch01-01-installation.html",
            "https://a-b.example.org/x?y=1&z=%20#top",
            "https://example.com#top",
        ] {
            let html = body(&format!("[x](<{dest}>)"));
            assert!(html.contains("<a href=\""), "{dest}: {html}");
        }
        let html = body("<https://example.com/a>");
        assert_eq!(
            html,
            "<p><a href=\"https://example.com/a\">https://example.com/a</a></p>\n"
        );
    }

    #[test]
    fn other_link_destinations_are_refused_with_line_and_quoted_text() {
        let cases = [
            ("[x](javascript:alert(1))", "javascript:alert(1)"),
            ("[x](JavaScript:x)", "JavaScript:x"),
            ("[x](java&#115;cript:x)", "javascript:x"),
            ("[x](data:text/html,x)", "data:text/html,x"),
            ("[x](file:///etc/passwd)", "file:///etc/passwd"),
            ("[x](http://example.com)", "http://example.com"),
            ("[x](//example.com)", "//example.com"),
            ("[x](</\\example.com>)", "/\\example.com"),
            ("[x](https:example.com)", "https:example.com"),
            ("[x](https://user@example.com)", "https://user@example.com"),
            ("[x](https://example.com:8443)", "https://example.com:8443"),
            ("[x](https://%65xample.com)", "https://%65xample.com"),
            ("[x](/first%2Dnote)", "/first%2Dnote"),
            ("[x](/first-note?x=1)", "/first-note?x=1"),
            ("[x](/first-note#top)", "/first-note#top"),
            ("[x](#top)", "#top"),
            ("[x](/../first-note)", "/../first-note"),
            ("[x](/./first-note)", "/./first-note"),
            ("[x](/ja/../x)", "/ja/../x"),
            ("[x](first-note)", "first-note"),
            ("[x](./first-note)", "./first-note"),
            ("[x](/first-note/)", "/first-note/"),
            ("[x](/ja)", "/ja"),
            ("[x](/missing)", "/missing"),
            ("[x]()", ""),
            ("[x](<https://example.com/a b>)", "https://example.com/a b"),
            (
                "[x](<https://example.com/a\tb>)",
                "https://example.com/a\tb",
            ),
            (
                "[x](<https://example.com/\u{1}>)",
                "https://example.com/\u{1}",
            ),
            (
                "[x](<https://example.com/\u{1f}>)",
                "https://example.com/\u{1f}",
            ),
            (
                "[x](<https://example.com/\u{7f}>)",
                "https://example.com/\u{7f}",
            ),
            ("[x](<https://example.com/\\x>)", "https://example.com/\\x"),
            ("[x](<https://EXAMPLE.com>)", "https://EXAMPLE.com"),
            (
                "[x][ref]\n\n[ref]: mailto:a@example.com",
                "mailto:a@example.com",
            ),
            ("<a@example.com>", "mailto:a@example.com"),
        ];
        for (markdown, dest) in cases {
            let (line, reason) = refusal(&format!("# T\n\ntext\n{markdown}\n"));
            assert_eq!(line, 4, "{markdown:?}");
            let quoted = format!("the link {dest:?} is neither");
            assert!(reason.starts_with(&quoted), "{markdown:?}: {reason}");
            assert!(!reason.chars().any(char::is_control), "{reason:?}");
        }
    }

    #[test]
    fn destinations_the_parser_cannot_link_are_still_refused_by_the_rule() {
        for dest in [
            "https://example.com/\n",
            "https://example.com/\r",
            "https://example.com/\0",
            "https://exa_mple.com",
            "https://.example.com",
            "https://example.com.",
            "https://-example.com",
            "https://localhost",
            "https://example.com/{x}",
            "https://example.com/|",
            "https://example.com/^",
            "https://example.com/`",
            "https://example.com/\"",
            "https://example.com/<",
            "https://example.com/é",
            "",
        ] {
            assert!(!is_https_address(dest), "{dest:?}");
        }
        let longest = format!("https://{}.com", "a".repeat(249));
        assert!(is_https_address(&longest));
        assert!(!is_https_address(&format!(
            "https://{}.com",
            "a".repeat(250)
        )));
    }

    #[test]
    fn the_title_is_the_opening_level_1_heading() {
        let article = rendered("# Field *notes* with `code` and <b>tags</b>\n\nBody.\n").unwrap();
        assert_eq!(article.title, "Field notes with code and <b>tags</b>");
        assert_eq!(article.body.as_str(), "<p>Body.</p>\n");
        let setext = rendered("Setext title\n============\n").unwrap();
        assert_eq!(setext.title, "Setext title");
        assert_eq!(
            rendered(&format!("# {}\n", "é".repeat(80)))
                .unwrap()
                .title
                .chars()
                .count(),
            80
        );
    }

    #[test]
    fn title_rules_are_enforced_with_a_line() {
        let first = "the first block must be a level-1 heading, such as `# My note`";
        assert_eq!(refusal(""), (1, first.into()));
        assert_eq!(refusal("Text first.\n\n# Late\n"), (1, first.into()));
        assert_eq!(refusal("\n\n## Section\n\n# Late\n"), (3, first.into()));
        assert_eq!(refusal("#\n\nBody\n"), (1, "the title is empty".into()));
        assert_eq!(refusal("# \u{3000} \n"), (1, "the title is empty".into()));
        assert_eq!(
            refusal(&format!("\n# {}\n", "x".repeat(81))),
            (2, "the title is longer than 80 characters".into())
        );
        let link = "the title cannot contain a link or an image";
        assert_eq!(refusal("# A [link](/first-note)\n"), (1, link.into()));
        assert_eq!(
            refusal("# A ![picture](https://example.com/a.png)\n"),
            (1, link.into())
        );
        assert_eq!(refusal("# A <https://example.com>\n"), (1, link.into()));
    }

    #[test]
    fn english_counts_words_outside_code() {
        let article = rendered(
            "# Not counted in the title\n\nOne two  three\nfour `five six` seven.\n\n```\nnot counted at all\n```\n\n<p>\nhtml block\n</p>\n",
        )
        .unwrap();
        assert_eq!(article.words, 6);
    }

    #[test]
    fn formatting_inside_a_word_does_not_split_it() {
        let text = format!("# T\n\n{}\n", "foo**bar**baz ".repeat(100));
        let words = rendered(&text).unwrap().words;
        assert_eq!(words, 100);
        assert_eq!(crate::reading::reading_minutes(words), 1);
    }

    #[test]
    fn whitespace_breaks_and_blocks_separate_words_but_inline_markup_does_not() {
        let words = |markdown: &str| rendered(&format!("# T\n\n{markdown}")).unwrap().words;
        // Inline markup, links, inline code and raw HTML shown as text join.
        assert_eq!(words("*a*b [c](/)d e`f`g h<b>i</b>j\n"), 4);
        assert_eq!(words("[one two](/first-note), three\n"), 3);
        // Whitespace, line breaks and block boundaries separate.
        assert_eq!(words("a *b* [c](/) `d` <b>e</b>\n"), 5);
        assert_eq!(words("a\nb  \nc\n"), 3);
        assert_eq!(words("a\n\nb\n\n## c\n\n- d\n- e\n\n> f\n"), 6);
        assert_eq!(words("| a | b |\n|---|---|\n| c | d |\n"), 4);
        assert_eq!(words("a\n\n```\nnot counted\n```\nb\n"), 2);
    }

    #[test]
    fn japanese_counts_two_characters_as_a_word() {
        let text = "# 題名\n\nこれは 日本語の文です。\n\n`code` と、\n\n```\n数えない\n```\n";
        let article = render(text, &routes(), Counting::Characters).unwrap();
        // 11 characters in the first paragraph and 2 in the second, halved
        // and rounded down, plus one inline code span.
        assert_eq!(article.words, 13 / 2 + 1);
    }

    // Spec 0006, §6: the outline, the description and JSON-LD.

    #[test]
    fn the_outline_holds_the_first_paragraph_and_each_h2_with_its_paragraph() {
        let text = "# Title with `code`\n\n\
                    First *paragraph* with [a link](https://example.com/) and `code`\nover two lines.\n\n\
                    Second paragraph.\n\n\
                    ## What is it?\n\n```sh\nnot this\n```\n\n- nor this\n\n\
                    The **answer** <b>plain</b>.\n\nMore.\n\n\
                    ### Deeper\n\nSkipped for sections.\n\n\
                    ## No paragraph\n\n## Last one\n\nEnd.\n";
        let outline = outline(text);
        assert_eq!(
            outline.summary,
            "First paragraph with a link and code over two lines."
        );
        assert_eq!(
            outline.sections,
            vec![
                (
                    "What is it?".to_owned(),
                    "The answer <b>plain</b>.".to_owned()
                ),
                ("No paragraph".to_owned(), String::new()),
                ("Last one".to_owned(), "End.".to_owned()),
            ]
        );
        assert_eq!(super::outline("# Only a title\n"), Outline::default());
        assert_eq!(super::outline("# T\n\n```\ncode\n```\n").summary, "");
    }

    #[test]
    fn shorten_cuts_at_a_word_or_between_characters() {
        assert_eq!(shorten("short", 10), "short");
        assert_eq!(shorten("exactly ten", 11), "exactly ten");
        assert_eq!(shorten("one two three four", 12), "one two…");
        assert_eq!(shorten("one, two three four", 5), "one…");
        let japanese = "あ".repeat(200);
        let cut = shorten(&japanese, 160);
        assert_eq!(cut.chars().count(), 160);
        assert!(cut.ends_with('…'));
        let long_word = "a".repeat(300);
        assert_eq!(shorten(&long_word, 160).chars().count(), 160);
    }

    #[test]
    fn json_ld_cannot_end_its_script_element() {
        let data = serde_json::json!({
            "name": "</script><script>alert(1)</script>",
            "text": "a & b <!-- c --> \"quoted\" \u{2028}",
        });
        let element = TrustedHtml::json_ld(&data);
        let text = element.as_str();
        let inner = text
            .strip_prefix("<script type=\"application/ld+json\">")
            .and_then(|rest| rest.strip_suffix("</script>"))
            .expect("one script element");
        assert!(!inner.contains(['<', '>', '&']), "{inner}");
        let parsed: serde_json::Value = serde_json::from_str(inner).expect("valid JSON");
        assert_eq!(parsed, data);
    }
}