ctb_formats_html/
text_decorator.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! HTML parsing, plain text rendering, and HTML table extraction utilities.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29/// A plain-text decorator for html2text that renders links and text without
30/// Markdown-isms such as bracketed links.
31#[derive(Clone, Debug, Default)]
32pub struct PlainTextDecorator;
33
34impl PlainTextDecorator {
35    /// Create a new `PlainTextDecorator`.
36    #[must_use]
37    pub fn new() -> Self {
38        Self
39    }
40}
41
42impl html2text::render::TextDecorator for PlainTextDecorator {
43    type Annotation = ();
44
45    fn decorate_link_start(
46        &mut self,
47        _url: &str,
48    ) -> (String, Self::Annotation) {
49        (String::new(), ())
50    }
51
52    fn decorate_link_end(&mut self) -> String {
53        String::new()
54    }
55
56    fn decorate_em_start(&self) -> (String, Self::Annotation) {
57        (String::new(), ())
58    }
59
60    fn decorate_em_end(&self) -> String {
61        String::new()
62    }
63
64    fn decorate_strong_start(&self) -> (String, Self::Annotation) {
65        (String::new(), ())
66    }
67
68    fn decorate_strong_end(&self) -> String {
69        String::new()
70    }
71
72    fn decorate_strikeout_start(&self) -> (String, Self::Annotation) {
73        (String::new(), ())
74    }
75
76    fn decorate_strikeout_end(&self) -> String {
77        String::new()
78    }
79
80    fn decorate_code_start(&self) -> (String, Self::Annotation) {
81        (String::new(), ())
82    }
83
84    fn decorate_code_end(&self) -> String {
85        String::new()
86    }
87
88    fn decorate_preformat_first(&self) -> Self::Annotation {}
89
90    fn decorate_preformat_cont(&self) -> Self::Annotation {}
91
92    fn decorate_image(
93        &mut self,
94        _src: &str,
95        title: &str,
96    ) -> (String, Self::Annotation) {
97        (title.to_string(), ())
98    }
99
100    fn header_prefix(&self, level: usize) -> String {
101        let mut s = String::new();
102        for _ in 0..level {
103            s.push('#');
104        }
105        s.push(' ');
106        s
107    }
108
109    fn quote_prefix(&self) -> String {
110        "> ".to_string()
111    }
112
113    fn unordered_item_prefix(&self) -> String {
114        "* ".to_string()
115    }
116
117    fn ordered_item_prefix(&self, i: i64) -> String {
118        format!("{i}. ")
119    }
120
121    fn make_subblock_decorator(&self) -> Self {
122        self.clone()
123    }
124}