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
/// A struct representing some properties to control the [`pretty_html`] generation.
/// The `insert_tag`, `delete_tag` and `equality_tag` represents the `html` tag to wrap the part of the text being inserted, deleted or equality.
///
/// 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>`.
/// `insert_tag` defaults to the `ins` tag, `delete_tag` defaults to the `del` tag and `equality_tag` defaults to `span`.
///
/// `nltobr` switch enables or disables replacing of `\n` in the text with a `<br>` element. Defaults to `true`
///
/// `insert_class`, `delete_class` or `equality_class` if set will be added as a `class="your css class"`. Defaults to `None`
///
/// `insert_style`, `delete_style` and `equality_style` would add css style property to the output.
/// E.g. if `insert_style: Some("background: yellow; color: purple")` is set the
/// `insert` part of the pretty html would look like `<ins style="background: yellow; color: purple">insert text</ins>`
///
/// [`pretty_html`]: struct.DiffMatchPatch.html/method.diff_pretty_html
pub struct HtmlConfig<'a> {
insert_tag: &'a str,
delete_tag: &'a str,
equality_tag: &'a str,
nltobr: bool,
insert_class: Option<&'a str>,
delete_class: Option<&'a str>,
equality_class: Option<&'a str>,
insert_style: Option<&'a str>,
delete_style: Option<&'a str>,
equality_style: Option<&'a str>,
}
impl<'a> Default for HtmlConfig<'a> {
fn default() -> Self {
Self {
insert_tag: "ins",
delete_tag: "del",
equality_tag: "span",
nltobr: true,
insert_class: None,
delete_class: None,
equality_class: None,
insert_style: None,
delete_style: None,
equality_style: None,
}
}
}
impl<'a> HtmlConfig<'a> {
/// Creates a new instance of the struct with some defaults.
///
/// `insert_tag` defaults to "ins".
///
/// `delete_tag` defaults to "del"
///
/// `equality_tag` defaults to "span"
///
/// `nltobr` defaults to `true`
///
/// Other fields defaults to `None`
pub fn new() -> Self {
Self::default()
}
pub(crate) fn insert_tag(&self) -> &str {
self.insert_tag
}
/// Set the HTML tag to be used for text inserted
pub fn set_insert_tag(&mut self, tag: &'a str) {
self.insert_tag = tag;
}
pub(crate) fn delete_tag(&self) -> &str {
self.delete_tag
}
/// Set the HTML tag to be used for text deleted
pub fn set_delete_tag(&mut self, tag: &'a str) {
self.delete_tag = tag;
}
pub(crate) fn equality_tag(&self) -> &str {
self.equality_tag
}
/// Set the HTML tag to be used for text that has not changed
pub fn set_equality_tag(&mut self, tag: &'a str) {
self.equality_tag = tag;
}
pub(crate) fn nltobr(&self) -> bool {
self.nltobr
}
/// Switch to control if `\nl` should be replaced with `<br>`
pub fn set_nl_to_br(&mut self, nltobr: bool) {
self.nltobr = nltobr
}
pub(crate) fn insert_class(&self) -> Option<&'a str> {
self.insert_class
}
/// Set the css class for the text inserted
pub fn set_insert_class(&mut self, class: Option<&'a str>) {
self.insert_class = class;
}
pub(crate) fn delete_class(&self) -> Option<&'a str> {
self.delete_class
}
/// Set the delete class for text deleted
pub fn set_delete_class(&mut self, class: Option<&'a str>) {
self.delete_class = class;
}
pub(crate) fn equality_class(&self) -> Option<&'a str> {
self.equality_class
}
/// Set the css class for text that has not changed
pub fn set_equality_class(&mut self, class: Option<&'a str>) {
self.equality_class = class;
}
pub(crate) fn insert_style(&self) -> Option<&'a str> {
self.insert_style
}
/// Set the css style property for text inserted
pub fn set_insert_style(&mut self, style: Option<&'a str>) {
self.insert_style = style;
}
pub(crate) fn delete_style(&self) -> Option<&'a str> {
self.delete_style
}
/// Set the css style property for text deleted
pub fn set_delete_style(&mut self, style: Option<&'a str>) {
self.delete_style = style;
}
pub(crate) fn equality_style(&self) -> Option<&'a str> {
self.equality_style
}
/// Set the css style for text that has not changed
pub fn set_equality_style(&mut self, style: Option<&'a str>) {
self.equality_style = style;
}
}