my fork of dmp
Diffstat (limited to 'src/dmp.rs')
| -rw-r--r-- | src/dmp.rs | 182 |
1 files changed, 99 insertions, 83 deletions
@@ -2615,31 +2615,31 @@ impl DiffMatchPatch { } /// Takes a diff array and returns a pretty HTML sequence. This function is mainly intended as an example from which to write ones own display functions. - /// + /// /// # Example /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Efficient, HtmlConfig}; /// # fn main() -> Result<(), Error> { /// let dmp = DiffMatchPatch::new(); - /// + /// /// let diffs = dmp.diff_main::<Efficient>("The old man and the new house?", "The old man and the old dog!")?; /// let htmlcfg = HtmlConfig::new(); - /// + /// /// let pretty = dmp.diff_pretty_html(&diffs, &htmlcfg)?; /// // Should print: "<span>The old man and the </span><del>new house?</del><ins>old dog!</ins>" /// println!("{pretty}"); - /// + /// /// # Ok(()) /// # } /// ``` - /// + /// /// Check out [`HtmlConfig`] options for ways to control the generated html. - /// + /// /// [`HtmlConfig`]: html/struct.HtmlConfig.html pub fn diff_pretty_html<T: DType>( &self, diffs: &[Diff<T>], - html_cfg: &HtmlConfig + html_cfg: &HtmlConfig, ) -> Result<String, crate::errors::Error> { let mut diffs = diffs.to_vec(); DiffMatchPatch::cleanup_semantic(&mut diffs); @@ -2653,14 +2653,14 @@ impl DiffMatchPatch { let txt = match T::to_string(diff.data()) { Ok(txt) => { let mut txt = txt - .replace("&", "&") - .replace("<", "<") - .replace(">", ">"); + .replace("&", "&") + .replace("<", "<") + .replace(">", ">"); if html_cfg.nltobr() { txt = txt.replace('\n', "<br>") } - + txt } Err(e) => { @@ -2675,33 +2675,51 @@ impl DiffMatchPatch { } match diff.op() { - Ops::Insert => Some( - format!( - "<{}{}{}>{txt}</{}>", - html_cfg.insert_tag(), - if let Some(cl) = html_cfg.insert_class() { format!(" class=\"{cl}\"") } else { String::new() }, - if let Some(st) = html_cfg.insert_style() { format!(" style=\"{st}\"") } else { String::new() }, - html_cfg.insert_tag() - ) - ), - Ops::Delete => Some( - format!( - "<{}{}{}>{txt}</{}>", - html_cfg.delete_tag(), - if let Some(cl) = html_cfg.delete_class() { format!(" class=\"{cl}\"") } else { String::new() }, - if let Some(st) = html_cfg.delete_style() { format!(" style=\"{st}\"") } else { String::new() }, - html_cfg.delete_tag() - ) - ), - Ops::Equal => Some( - format!( - "<{}{}{}>{txt}</{}>", - html_cfg.equality_tag(), - if let Some(cl) = html_cfg.equality_class() { format!(" class=\"{cl}\"") } else { String::new() }, - if let Some(st) = html_cfg.equality_style() { format!(" style=\"{st}\"") } else { String::new() }, - html_cfg.equality_tag() - ) - ), + Ops::Insert => Some(format!( + "<{}{}{}>{txt}</{}>", + html_cfg.insert_tag(), + if let Some(cl) = html_cfg.insert_class() { + format!(" class=\"{cl}\"") + } else { + String::new() + }, + if let Some(st) = html_cfg.insert_style() { + format!(" style=\"{st}\"") + } else { + String::new() + }, + html_cfg.insert_tag() + )), + Ops::Delete => Some(format!( + "<{}{}{}>{txt}</{}>", + html_cfg.delete_tag(), + if let Some(cl) = html_cfg.delete_class() { + format!(" class=\"{cl}\"") + } else { + String::new() + }, + if let Some(st) = html_cfg.delete_style() { + format!(" style=\"{st}\"") + } else { + String::new() + }, + html_cfg.delete_tag() + )), + Ops::Equal => Some(format!( + "<{}{}{}>{txt}</{}>", + html_cfg.equality_tag(), + if let Some(cl) = html_cfg.equality_class() { + format!(" class=\"{cl}\"") + } else { + String::new() + }, + if let Some(st) = html_cfg.equality_style() { + format!(" style=\"{st}\"") + } else { + String::new() + }, + html_cfg.equality_tag() + )), } }) .collect::<Vec<_>>() @@ -2738,26 +2756,26 @@ impl DiffMatchPatch { } /// Given two texts, or an already computed list of differences (`diffs`), return an array of patch objects. - /// + /// /// # Example /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Efficient, PatchInput}; - /// + /// /// # fn main() -> Result<(), Error> { /// let dmp = DiffMatchPatch::new(); - /// + /// /// // You can also make patches from the old and new string directly /// let patches = dmp.patch_make::<Efficient>(PatchInput::new_text_text("Apples are a fruit.", "Bananas are also fruit"))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; /// assert_eq!("Bananas are also fruit", new_from_old); - /// + /// /// // Or, create some diffs in `Efficient` or `Compact` mode /// let diffs = dmp.diff_main::<Efficient>("Apples are a fruit.", "Bananas are also fruit")?; /// // Now, lets convert the diffs to a bunch of patches - you can use an existing set of diffs to create patches /// let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; /// assert_eq!("Bananas are also fruit", new_from_old); - /// + /// /// // Or, from the source texts and diffs /// let patches = dmp.patch_make(PatchInput::new_text_diffs("Apples are a fruit.", &diffs))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; @@ -2765,10 +2783,10 @@ impl DiffMatchPatch { /// # Ok(()) /// # } /// ``` - /// + /// /// The [`PatchInput::new_text_diffs`] method is preferred, use it if you happen to have that data available, otherwise this function will compute the missing pieces. - /// - /// + /// + /// pub fn patch_make<T: DType>( &self, input: PatchInput<T>, @@ -2801,32 +2819,32 @@ impl DiffMatchPatch { } /// Reduces an array of patch objects to a block of text which looks extremely similar to the standard GNU diff/patch format. This text may be stored or transmitted. - /// + /// /// # Example - /// + /// /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Compat, PatchInput}; - /// + /// /// # fn main() -> Result<(), Error> { /// let dmp = DiffMatchPatch::new(); - /// + /// /// // Making patches from source and edited text - both in `Efficient` and `Compat` mode /// let patches = dmp.patch_make::<Compat>(PatchInput::new_text_text("Apples are fruit!", "Bananas are also fruit!"))?; /// let patch_to_text = dmp.patch_to_text(&patches); - /// + /// /// // Prints patches in GNU diff/ patch format /// // You can use this format for transmission and/ or storage. /// println!("{patch_to_text}"); - /// + /// /// # Ok(()) /// # } /// ``` - /// + /// /// Check out the [`diff_to_delta`] and [`diff_from_delta`] methods for a more compact way of representing diffs. - /// + /// /// [`diff_to_delta`]: ./method.diff_to_delta /// [`diff_from_delta`]: ./method.diff_from_delta - /// + /// pub fn patch_to_text<T: DType>(&self, patches: &Patches<T>) -> String { patches.iter().map(|p| p.to_string()).collect::<String>() } @@ -2834,26 +2852,26 @@ impl DiffMatchPatch { /// Parses a block of text (which was presumably created by the [`patch_to_text`] method) and returns an array of patch objects. /// /// # Example - /// + /// /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Compat, PatchInput}; - /// + /// /// # fn main() -> Result<(), Error> { /// let dmp = DiffMatchPatch::new(); - /// + /// /// // Making patches from source and edited text - both in `Efficient` and `Compat` mode /// let patches = dmp.patch_make::<Compat>(PatchInput::new_text_text("Apples are fruit!", "Bananas are also fruit!"))?; /// let patch_to_text = dmp.patch_to_text(&patches); - /// + /// /// // let's create patches back from text /// let patches_recreated = dmp.patch_from_text::<Compat>(&patch_to_text)?; - /// + /// /// // Now you can `patch_apply` the `patches_recreated` to your source text - /// + /// /// # Ok(()) /// # } /// ``` - /// + /// /// [`patch_to_text`]: ./method.patch_to_text pub fn patch_from_text<T: DType>(&self, text: &str) -> Result<Patches<T>, Error> { if text.is_empty() { @@ -2943,19 +2961,18 @@ impl DiffMatchPatch { Ok(patches) } - /// Crush the diff into an encoded string which describes the operations required to transform text_old into text_new. /// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. /// Operations are tab-separated. Inserted text is escaped using %xx notation. - /// + /// /// # Example - /// + /// /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Compat, PatchInput}; /// # fn main() -> Result<(), Error> { - /// + /// /// let dmp = DiffMatchPatch::new(); - /// + /// /// // let's create some diffs /// let diffs = dmp.diff_main::<Compat>("The old house and the new dog!", "The old man and the new dog!")?; /// // now, you can create a `delta` string which can be used to re-create the diffs @@ -2963,10 +2980,10 @@ impl DiffMatchPatch { /// println!("{delta:?}"); /// // You should see something like the following /// // "=8\t-5\t+man\t=17" - /// + /// /// // now you can use the `diff_from_delta()` to recover the diffs /// let diffs_later = dmp.diff_from_delta::<Compat>("The old house and the new dog!", &delta); - /// + /// /// # Ok(()) /// # } /// ``` @@ -2999,18 +3016,17 @@ impl DiffMatchPatch { T::to_string(&data) } - /// Given the original text `old`, and an encoded string which describes the /// operations required to transform text1 into text2, compute the full diff. - /// + /// /// # Example - /// + /// /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Compat, PatchInput}; /// # fn main() -> Result<(), Error> { - /// + /// /// let dmp = DiffMatchPatch::new(); - /// + /// /// // let's create some diffs /// let diffs = dmp.diff_main::<Compat>("The old house and the new dog!", "The old man and the new dog!")?; /// // now, you can create a `delta` string which can be used to re-create the diffs @@ -3018,16 +3034,16 @@ impl DiffMatchPatch { /// println!("{delta:?}"); /// // You should see something like the following /// // "=8\t-5\t+man\t=17" - /// + /// /// // now you can use the `diff_from_delta()` to recover the diffs /// let diffs_later = dmp.diff_from_delta::<Compat>("The old house and the new dog!", &delta); /// // Now, you can use these diffs to apply patches to the source text /// let patches = dmp.patch_make(PatchInput::new_text_diffs("The old house and the new dog!", &diffs))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "The old house and the new dog!")?; - /// - /// + /// + /// /// assert_eq!("The old man and the new dog!", &new_from_old); - /// + /// /// # Ok(()) /// # } /// ``` @@ -3099,27 +3115,27 @@ impl DiffMatchPatch { /// If `delete_threshold` is closer to 0, then the deleted text must match the expected text more closely. /// If `delete_threshold` is closer to 1, then the deleted text may contain anything. /// In most use cases `delete_threshold` should just be set to the same value as `match_threshold`. Both values default to `0.5` - /// + /// /// # Example /// # Example /// ``` /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Efficient, PatchInput}; - /// + /// /// # fn main() -> Result<(), Error> { /// let dmp = DiffMatchPatch::new(); - /// + /// /// // You can also make patches from the old and new string directly /// let patches = dmp.patch_make::<Efficient>(PatchInput::new_text_text("Apples are a fruit.", "Bananas are also fruit"))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; /// assert_eq!("Bananas are also fruit", new_from_old); - /// + /// /// // Or, create some diffs in `Efficient` or `Compact` mode /// let diffs = dmp.diff_main::<Efficient>("Apples are a fruit.", "Bananas are also fruit")?; /// // Now, lets convert the diffs to a bunch of patches - you can use an existing set of diffs to create patches /// let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; /// assert_eq!("Bananas are also fruit", new_from_old); - /// + /// /// // Or, from the source texts and diffs /// let patches = dmp.patch_make(PatchInput::new_text_diffs("Apples are a fruit.", &diffs))?; /// let (new_from_old, _) = dmp.patch_apply(&patches, "Apples are a fruit.")?; |