diff_match_patch_rs/
html.rs

1/// A struct representing some properties to control the [`pretty_html`] generation.
2/// The `insert_tag`, `delete_tag` and `equality_tag` represents the `html` tag to wrap the part of the text being inserted, deleted or equality.
3///
4/// E.g. if `insert_tag` is set to `span`, the text to be inserted will be wrapped around with `<span>some text to insert</span>`.
5/// `insert_tag` defaults to the `ins` tag, `delete_tag` defaults to the `del` tag and `equality_tag` defaults to `span`.
6///
7/// `nltobr` switch enables or disables replacing of `\n` in the text with a `<br>` element. Defaults to `true`
8///
9/// `insert_class`, `delete_class` or `equality_class` if set will be added as a `class="your css class"`. Defaults to `None`
10///
11/// `insert_style`, `delete_style` and `equality_style` would add css style property to the output.
12/// E.g. if `insert_style: Some("background: yellow; color: purple")` is set the
13/// `insert` part of the pretty html would look like `<ins style="background: yellow; color: purple">insert text</ins>`
14///
15/// [`pretty_html`]: struct.DiffMatchPatch.html/method.diff_pretty_html
16pub struct HtmlConfig<'a> {
17    insert_tag: &'a str,
18    delete_tag: &'a str,
19    equality_tag: &'a str,
20    nltobr: bool,
21    insert_class: Option<&'a str>,
22    delete_class: Option<&'a str>,
23    equality_class: Option<&'a str>,
24    insert_style: Option<&'a str>,
25    delete_style: Option<&'a str>,
26    equality_style: Option<&'a str>,
27}
28
29impl Default for HtmlConfig<'_> {
30    fn default() -> Self {
31        Self {
32            insert_tag: "ins",
33            delete_tag: "del",
34            equality_tag: "span",
35            nltobr: true,
36            insert_class: None,
37            delete_class: None,
38            equality_class: None,
39            insert_style: None,
40            delete_style: None,
41            equality_style: None,
42        }
43    }
44}
45
46impl<'a> HtmlConfig<'a> {
47    /// Creates a new instance of the struct with some defaults.
48    ///
49    /// `insert_tag` defaults to "ins".
50    ///
51    /// `delete_tag` defaults to "del"
52    ///
53    /// `equality_tag` defaults to "span"
54    ///
55    /// `nltobr` defaults to `true`
56    ///
57    /// Other fields defaults to `None`
58    pub fn new() -> Self {
59        Self::default()
60    }
61
62    pub(crate) fn insert_tag(&self) -> &str {
63        self.insert_tag
64    }
65
66    /// Set the HTML tag to be used for text inserted
67    pub fn set_insert_tag(&mut self, tag: &'a str) {
68        self.insert_tag = tag;
69    }
70
71    pub(crate) fn delete_tag(&self) -> &str {
72        self.delete_tag
73    }
74
75    /// Set the HTML tag to be used for text deleted
76    pub fn set_delete_tag(&mut self, tag: &'a str) {
77        self.delete_tag = tag;
78    }
79
80    pub(crate) fn equality_tag(&self) -> &str {
81        self.equality_tag
82    }
83
84    /// Set the HTML tag to be used for text that has not changed
85    pub fn set_equality_tag(&mut self, tag: &'a str) {
86        self.equality_tag = tag;
87    }
88
89    pub(crate) fn nltobr(&self) -> bool {
90        self.nltobr
91    }
92
93    /// Switch to control if `\nl` should be replaced with `<br>`
94    pub fn set_nl_to_br(&mut self, nltobr: bool) {
95        self.nltobr = nltobr
96    }
97
98    pub(crate) fn insert_class(&self) -> Option<&'a str> {
99        self.insert_class
100    }
101
102    /// Set the css class for the text inserted
103    pub fn set_insert_class(&mut self, class: Option<&'a str>) {
104        self.insert_class = class;
105    }
106
107    pub(crate) fn delete_class(&self) -> Option<&'a str> {
108        self.delete_class
109    }
110
111    /// Set the delete class for text deleted
112    pub fn set_delete_class(&mut self, class: Option<&'a str>) {
113        self.delete_class = class;
114    }
115
116    pub(crate) fn equality_class(&self) -> Option<&'a str> {
117        self.equality_class
118    }
119
120    /// Set the css class for text that has not changed
121    pub fn set_equality_class(&mut self, class: Option<&'a str>) {
122        self.equality_class = class;
123    }
124
125    pub(crate) fn insert_style(&self) -> Option<&'a str> {
126        self.insert_style
127    }
128
129    /// Set the css style property for text inserted
130    pub fn set_insert_style(&mut self, style: Option<&'a str>) {
131        self.insert_style = style;
132    }
133
134    pub(crate) fn delete_style(&self) -> Option<&'a str> {
135        self.delete_style
136    }
137
138    /// Set the css style property for text deleted
139    pub fn set_delete_style(&mut self, style: Option<&'a str>) {
140        self.delete_style = style;
141    }
142
143    pub(crate) fn equality_style(&self) -> Option<&'a str> {
144        self.equality_style
145    }
146
147    /// Set the css style for text that has not changed
148    pub fn set_equality_style(&mut self, style: Option<&'a str>) {
149        self.equality_style = style;
150    }
151}