├── .cargo-ok ├── .gitignore ├── .gitmodules ├── .rustfmt.toml ├── .travis.yml ├── Cargo.toml ├── LICENSE_APACHE.md ├── LICENSE_MIT.md ├── README.md ├── benches └── benchmarks.rs └── src ├── lib.rs ├── matchers ├── and.rs ├── falling_edge.rs ├── heading.rs ├── mod.rs ├── one_shot.rs └── start_of_next_line.rs └── rewriters ├── mod.rs ├── rewritten.rs └── writer.rs /.cargo-ok: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Michael-F-Bryan/markedit/b1cbec0bcfa06dc24635d2601d21926327685be2/.cargo-ok -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | Cargo.lock 3 | 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "benches/adventures.michaelfbryan.com"] 2 | path = benches/adventures.michaelfbryan.com 3 | url = https://github.com/Michael-F-Bryan/adventures.michaelfbryan.com.git 4 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 80 2 | tab_spaces = 4 3 | fn_single_line = true 4 | match_block_trailing_comma = true 5 | normalize_comments = true 6 | wrap_comments = true 7 | merge_imports = true 8 | reorder_impl_items = true 9 | use_field_init_shorthand = true 10 | use_try_shorthand = true 11 | normalize_doc_attributes = true 12 | report_todo = "Always" 13 | report_fixme = "Always" 14 | edition = "2018" 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | sudo: false 3 | cache: cargo 4 | 5 | rust: 6 | - 1.34.0 7 | - nightly 8 | 9 | script: 10 | - cargo build --verbose 11 | - cargo test --verbose 12 | - cargo doc --verbose 13 | 14 | before_deploy: 15 | - cargo doc --verbose 16 | - echo ' ' > target/doc/index.html 17 | - cp -r target/doc public 18 | 19 | deploy: 20 | - provider: pages 21 | edge: true 22 | github_token: "$GITHUB_TOKEN" 23 | local_dir: public 24 | on: 25 | branch: master 26 | rust: nightly 27 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "markedit" 3 | version = "0.3.1-alpha.0" 4 | authors = ["Michael Bryan "] 5 | license = "MIT OR Apache-2.0" 6 | edition = "2018" 7 | description = "An ergonomic library for manipulating unstructured Markdown documents." 8 | repository = "https://github.com/Michael-F-Bryan/markedit" 9 | keywords = ["markdown", "streaming", "edit"] 10 | categories = ["text-processing"] 11 | exclude = ["benches/adventures.michaelfbryan.com"] 12 | 13 | [badges] 14 | travis-ci = { repository = "Michael-F-Bryan/markedit", branch = "master" } 15 | 16 | [dependencies] 17 | pulldown-cmark = "0.7" 18 | 19 | [dev-dependencies] 20 | criterion = "0.3" 21 | glob = "0.3.0" 22 | 23 | [[bench]] 24 | name = "benchmarks" 25 | harness = false 26 | -------------------------------------------------------------------------------- /LICENSE_APACHE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE_MIT.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Michael Bryan 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # markedit 2 | 3 | [![Build Status](https://travis-ci.com/Michael-F-Bryan/markedit.svg?branch=master)](https://travis-ci.com/Michael-F-Bryan/markedit) 4 | [![Docs.rs Badge](https://docs.rs/markedit/badge.svg)](https://docs.rs/markedit) 5 | [![Crates.io](https://img.shields.io/crates/v/markedit)](https://crates.io/crates/markedit) 6 | ![Crates.io](https://img.shields.io/crates/l/markedit) 7 | 8 | ([API Docs]) 9 | 10 | An ergonomic library for manipulating unstructured Markdown documents. 11 | 12 | You may want to check out [the associated blog post][blog] to get a 13 | high-level understanding of this library and how it can be used. 14 | 15 | ## License 16 | 17 | This project is licensed under either of 18 | 19 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE.md) or 20 | http://www.apache.org/licenses/LICENSE-2.0) 21 | * MIT license ([LICENSE-MIT](LICENSE-MIT.md) or 22 | http://opensource.org/licenses/MIT) 23 | 24 | at your option. 25 | 26 | It is recommended to always use [cargo-crev][crev] to verify the 27 | trustworthiness of each of your dependencies, including this one. 28 | 29 | ### Contribution 30 | 31 | Unless you explicitly state otherwise, any contribution intentionally 32 | submitted for inclusion in the work by you, as defined in the Apache-2.0 33 | license, shall be dual licensed as above, without any additional terms or 34 | conditions. 35 | 36 | The intent of this crate is to be free of soundness bugs. The developers will 37 | do their best to avoid them, and welcome help in analyzing and fixing them. 38 | 39 | [API Docs]: https://michael-f-bryan.github.io/markedit 40 | [crev]: https://github.com/crev-dev/cargo-crev 41 | [blog]: http://adventures.michaelfbryan.com/posts/markedit/ -------------------------------------------------------------------------------- /benches/benchmarks.rs: -------------------------------------------------------------------------------- 1 | use criterion::{ 2 | criterion_group, criterion_main, BenchmarkId, Criterion, Throughput, 3 | }; 4 | use markedit::{Heading, Matcher, Rewriter, Writer}; 5 | use pulldown_cmark::Event; 6 | use std::path::{Path, PathBuf}; 7 | 8 | fn known_markdown_files() -> impl Iterator { 9 | let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); 10 | let markedit_files = vec![ 11 | manifest_dir.join("README.md"), 12 | manifest_dir.join("LICENSE_APACHE.md"), 13 | ]; 14 | 15 | let blog_posts_pattern = concat!( 16 | env!("CARGO_MANIFEST_DIR"), 17 | "/benches/adventures.michaelfbryan.com/content/posts/**/*.md" 18 | ); 19 | let blog_posts = glob::glob(blog_posts_pattern) 20 | .unwrap() 21 | .map(|entry| entry.unwrap()); 22 | 23 | blog_posts.chain(markedit_files) 24 | } 25 | 26 | fn canonical_name(path: &Path) -> &str { 27 | let stem = path.file_stem().unwrap().to_str().unwrap(); 28 | 29 | if stem == "index" { 30 | canonical_name(path.parent().unwrap()) 31 | } else { 32 | stem 33 | } 34 | } 35 | 36 | pub fn rewriting(c: &mut Criterion) { 37 | let mut group = c.benchmark_group("Rewriting"); 38 | 39 | for filename in known_markdown_files() { 40 | let src = std::fs::read_to_string(&filename).unwrap(); 41 | let name = canonical_name(&filename); 42 | 43 | group 44 | .throughput(Throughput::Bytes(src.len() as u64)) 45 | .bench_with_input( 46 | BenchmarkId::new("baseline parse", name), 47 | &src, 48 | |b, src| b.iter(|| markedit::parse(src).count()), 49 | ) 50 | .bench_with_input( 51 | BenchmarkId::new("add text after each heading", name), 52 | &src, 53 | |b, src| { 54 | b.iter(|| { 55 | markedit::insert_markdown_before( 56 | "## Sub-Heading", 57 | Heading::with_level(2).falling_edge(), 58 | ) 59 | .rewrite(markedit::parse(src)) 60 | .count() 61 | }) 62 | }, 63 | ) 64 | .bench_with_input( 65 | BenchmarkId::new("lowercase all text", name), 66 | &src, 67 | |b, src| { 68 | b.iter(|| { 69 | markedit::change_text( 70 | |_| true, 71 | |text| text.to_uppercase(), 72 | ) 73 | .rewrite(markedit::parse(src)) 74 | .count() 75 | }) 76 | }, 77 | ) 78 | .bench_with_input( 79 | BenchmarkId::new("uppercase level 2 headings", name), 80 | &src, 81 | |b, src| { 82 | b.iter(|| { 83 | upper_case_header_text(2) 84 | .rewrite(markedit::parse(src)) 85 | .count() 86 | }) 87 | }, 88 | ); 89 | } 90 | } 91 | 92 | fn upper_case_header_text<'src>(level: u32) -> impl Rewriter<'src> { 93 | let mut matcher = Heading::with_level(level); 94 | 95 | move |ev: Event<'src>, writer: &mut Writer<'src>| { 96 | if matcher.matches_event(&ev) { 97 | if let Event::Text(text) = ev { 98 | writer.push(Event::Text( 99 | text.into_string().to_uppercase().into(), 100 | )); 101 | return; 102 | } 103 | } 104 | 105 | writer.push(ev); 106 | } 107 | } 108 | 109 | criterion_group!(benches, rewriting); 110 | criterion_main!(benches); 111 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! An ergonomic library for manipulating markdown documents. 2 | //! 3 | //! There are two fundamental concepts in `markedit`, 4 | //! 5 | //! - `Matcher` - something which can match an [`Event`] from pulldown-cmark 6 | //! (typically implemented using state machines or simple functions) 7 | //! - `Rewriter` - something which can rewrite part of a stream of [`Event`]s 8 | //! (typically just a function) 9 | //! 10 | //! Together we can use these to transform a stream of [`Event`]s on the fly 11 | //! with minimal overhead. 12 | //! 13 | //! # Examples 14 | //! 15 | //! The use case which prompted this entire library was to insert arbitrary 16 | //! markdown after a heading. 17 | //! 18 | //! ```rust 19 | //! use pulldown_cmark::{Event, Tag}; 20 | //! use markedit::{Matcher, Heading}; 21 | //! 22 | //! let src = "# Heading\n Some text\n some more text \n\n # Another Heading"; 23 | //! 24 | //! // first we need to construct our predicate 25 | //! let matcher = Heading::with_level(1).falling_edge(); 26 | //! 27 | //! // we also need a rewriting rule 28 | //! let rule = markedit::insert_markdown_before("## Sub-Heading", matcher); 29 | //! 30 | //! // create our stream of events 31 | //! let events = markedit::parse(src); 32 | //! // then mutate them and collect them into a vector so we can inspect the 33 | //! // results 34 | //! let mutated: Vec<_> = markedit::rewrite(events, rule).collect(); 35 | //! 36 | //! // the heading before we want to insert 37 | //! assert_eq!(mutated[1], Event::Text("Heading".into())); 38 | //! // our inserted tags 39 | //! assert_eq!(mutated[3], Event::Start(Tag::Heading(2))); 40 | //! assert_eq!(mutated[4], Event::Text("Sub-Heading".into())); 41 | //! assert_eq!(mutated[5], Event::End(Tag::Heading(2))); 42 | //! // "Some text" is the line after 43 | //! assert_eq!(mutated[7], Event::Text("Some text".into())); 44 | //! ``` 45 | //! 46 | //! You can also use [`change_text()`] to upper-case text based on a predicate 47 | //! (e.g. the text contains a certain keyword). 48 | //! 49 | //! ```rust 50 | //! use pulldown_cmark::Event; 51 | //! 52 | //! let src = "# Heading\n Some text \n some more text \n\n # Another Heading"; 53 | //! 54 | //! // first we construct the rewriting rule 55 | //! let rule = markedit::change_text( 56 | //! |text| text.contains("Heading"), 57 | //! |text| text.to_uppercase(), 58 | //! ); 59 | //! 60 | //! // let's parse the input text into Events 61 | //! let events_before: Vec<_> = markedit::parse(src).collect(); 62 | //! 63 | //! // some sanity checks on the initial input 64 | //! assert_eq!(events_before[1], Event::Text("Heading".into())); 65 | //! assert_eq!(events_before[9], Event::Text("Another Heading".into())); 66 | //! 67 | //! // now rewrite the events using our rewriter rule 68 | //! let events_after: Vec<_> = markedit::rewrite(events_before, rule) 69 | //! .collect(); 70 | //! 71 | //! // and check the results 72 | //! println!("{:?}", events_after); 73 | //! assert_eq!(events_after[1], Event::Text("HEADING".into())); 74 | //! assert_eq!(events_after[9], Event::Text("ANOTHER HEADING".into())); 75 | //! ``` 76 | //! 77 | //! Note that everything works with streaming iterators, we only needed to 78 | //! `collect()` the events into a `Vec` for demonstration purposes. 79 | 80 | #![forbid(unsafe_code)] 81 | #![deny(missing_docs, missing_debug_implementations, rust_2018_idioms)] 82 | 83 | pub use pulldown_cmark; 84 | 85 | mod matchers; 86 | mod rewriters; 87 | 88 | pub use matchers::*; 89 | pub use rewriters::*; 90 | 91 | use pulldown_cmark::{Event, Parser}; 92 | 93 | /// A convenience function for parsing some text into [`Event`]s without 94 | /// needing to add [`pulldown_cmark`] as an explicit dependency. 95 | pub fn parse(text: &str) -> impl Iterator> + '_ { 96 | Parser::new(text) 97 | } 98 | -------------------------------------------------------------------------------- /src/matchers/and.rs: -------------------------------------------------------------------------------- 1 | use crate::Matcher; 2 | use pulldown_cmark::Event; 3 | 4 | /// A [`Matcher`] which only returns `true` when both inner [`Matcher`]s do. 5 | #[derive(Debug, Clone, PartialEq)] 6 | pub struct And { 7 | left: L, 8 | right: R, 9 | } 10 | 11 | impl And { 12 | /// Create a new [`And`] matcher. 13 | pub const fn new(left: L, right: R) -> Self { And { left, right } } 14 | } 15 | 16 | impl Matcher for And { 17 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 18 | // Note: We explicitly *don't* want to use short-circuiting logic here 19 | // because each inner matcher needs to see the entire event stream 20 | let left = self.left.matches_event(event); 21 | let right = self.right.matches_event(event); 22 | 23 | left && right 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/matchers/falling_edge.rs: -------------------------------------------------------------------------------- 1 | use crate::matchers::Matcher; 2 | use pulldown_cmark::Event; 3 | 4 | /// A [`Matcher`] which will detect the falling edge of another. 5 | /// 6 | /// # Examples 7 | /// 8 | /// ```rust 9 | /// # use markedit::{FallingEdge, Matcher, pulldown_cmark::{Tag, Event}}; 10 | /// 11 | /// let matches_something = markedit::exact_text("Something"); 12 | /// let mut matcher = FallingEdge::new(matches_something); 13 | /// 14 | /// // enter the paragraph 15 | /// let got = matcher.matches_event(&Event::Start(Tag::Paragraph)); 16 | /// assert_eq!(got, false); 17 | /// // then encounter some text. matches_something should have gone from false -> true 18 | /// let got = matcher.matches_event(&Event::Text("Something".into())); 19 | /// assert_eq!(got, false); 20 | /// // then leave the paragraph. `matches_something` should go from true -> false 21 | /// let got = matcher.matches_event(&Event::End(Tag::Paragraph)); 22 | /// assert_eq!(got, true, "We've entered a paragraph"); 23 | /// ``` 24 | #[derive(Debug, Clone, PartialEq)] 25 | pub struct FallingEdge { 26 | inner: M, 27 | previous_was_matched: bool, 28 | } 29 | 30 | impl FallingEdge { 31 | /// Create a new [`FallingEdge`] matcher. 32 | pub const fn new(inner: M) -> Self { 33 | FallingEdge { 34 | inner, 35 | previous_was_matched: false, 36 | } 37 | } 38 | } 39 | 40 | impl Matcher for FallingEdge { 41 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 42 | let current_is_matched = self.inner.matches_event(event); 43 | let is_falling_edge = self.previous_was_matched && !current_is_matched; 44 | self.previous_was_matched = current_is_matched; 45 | is_falling_edge 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/matchers/heading.rs: -------------------------------------------------------------------------------- 1 | use crate::matchers::Matcher; 2 | use pulldown_cmark::{Event, Tag}; 3 | 4 | /// Matches the items inside a heading tag, including the start and end tags. 5 | #[derive(Debug, Clone, PartialEq)] 6 | pub struct Heading { 7 | inside_heading: bool, 8 | level: Option, 9 | } 10 | 11 | impl Heading { 12 | /// Create a new [`Heading`]. 13 | const fn new(level: Option) -> Self { 14 | Heading { 15 | level, 16 | inside_heading: false, 17 | } 18 | } 19 | 20 | /// Matches any heading. 21 | pub const fn any_level() -> Self { Heading::new(None) } 22 | 23 | /// Matches only headings with the desired level. 24 | pub const fn with_level(level: u32) -> Self { Heading::new(Some(level)) } 25 | 26 | fn matches_level(&self, level: u32) -> bool { 27 | match self.level { 28 | Some(expected) => level == expected, 29 | None => true, 30 | } 31 | } 32 | } 33 | 34 | impl Matcher for Heading { 35 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 36 | match event { 37 | Event::Start(Tag::Heading(level)) if self.matches_level(*level) => { 38 | self.inside_heading = true; 39 | }, 40 | Event::End(Tag::Heading(level)) if self.matches_level(*level) => { 41 | self.inside_heading = false; 42 | // make sure the end tag is also matched 43 | return true; 44 | }, 45 | _ => {}, 46 | } 47 | 48 | self.inside_heading 49 | } 50 | } 51 | 52 | #[cfg(test)] 53 | mod tests { 54 | use super::*; 55 | use pulldown_cmark::LinkType; 56 | 57 | #[test] 58 | fn match_everything_inside_a_header() { 59 | // The original text for these events was: 60 | // 61 | // This is some text. 62 | // 63 | // ## Then a *header* 64 | // 65 | // [And a link](https://example.com) 66 | let inputs = vec![ 67 | (Event::Start(Tag::Paragraph), false), 68 | (Event::Text("This is some text.".into()), false), 69 | (Event::End(Tag::Paragraph), false), 70 | (Event::Start(Tag::Heading(2)), true), 71 | (Event::Text("Then a ".into()), true), 72 | (Event::Start(Tag::Emphasis), true), 73 | (Event::Text("header".into()), true), 74 | (Event::End(Tag::Emphasis), true), 75 | (Event::End(Tag::Heading(2)), true), 76 | (Event::Start(Tag::Paragraph), false), 77 | ( 78 | Event::Start(Tag::Link( 79 | LinkType::Inline, 80 | "https://example.com".into(), 81 | "".into(), 82 | )), 83 | false, 84 | ), 85 | (Event::Text("And a link".into()), false), 86 | ( 87 | Event::End(Tag::Link( 88 | LinkType::Inline, 89 | "https://example.com".into(), 90 | "".into(), 91 | )), 92 | false, 93 | ), 94 | (Event::End(Tag::Paragraph), false), 95 | ]; 96 | 97 | let mut matcher = Heading::any_level(); 98 | 99 | for (tag, should_be) in inputs { 100 | let got = matcher.matches_event(&tag); 101 | assert_eq!(got, should_be, "{:?}", tag); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/matchers/mod.rs: -------------------------------------------------------------------------------- 1 | mod and; 2 | mod falling_edge; 3 | mod heading; 4 | mod one_shot; 5 | mod start_of_next_line; 6 | 7 | pub use and::And; 8 | pub use falling_edge::FallingEdge; 9 | pub use heading::Heading; 10 | pub use one_shot::OneShot; 11 | pub use start_of_next_line::StartOfNextLine; 12 | 13 | use pulldown_cmark::{Event, Tag}; 14 | use std::borrow::Borrow; 15 | 16 | /// A predicate which can be fed a stream of [`Event`]s and tell you whether 17 | /// they match a desired condition. 18 | /// 19 | /// Individual [`Matcher`]s may choose to return `true` more than once. 20 | /// 21 | /// Any function which accepts a [`Event`] reference and returns a `bool` can be 22 | /// used as a [`Matcher`]. 23 | /// 24 | /// ```rust 25 | /// # use markedit::Matcher; 26 | /// # use pulldown_cmark::Event; 27 | /// fn assert_is_matcher(_: impl Matcher) {} 28 | /// 29 | /// assert_is_matcher(|ev: &Event<'_>| true); 30 | /// ``` 31 | pub trait Matcher { 32 | /// Evaluate this predicate against an event from an [`Event`] stream. 33 | fn matches_event(&mut self, event: &Event<'_>) -> bool; 34 | 35 | /// Find the index of the first [`Event`] which is matched by this 36 | /// predicate. 37 | fn first_match<'src, I, E>(mut self, events: I) -> Option 38 | where 39 | Self: Sized, 40 | I: IntoIterator + 'src, 41 | E: Borrow> + 'src, 42 | { 43 | events 44 | .into_iter() 45 | .position(|ev| self.matches_event(ev.borrow())) 46 | } 47 | 48 | /// Checks whether this [`Matcher`] would match anything in a stream of 49 | /// [`Event`]s. 50 | /// 51 | /// # Examples 52 | /// 53 | /// ```rust 54 | /// # use markedit::Matcher; 55 | /// let src = "# Heading\nsome text"; 56 | /// let matcher = markedit::exact_text("some text"); 57 | /// 58 | /// assert!(matcher.is_in(markedit::parse(src))); 59 | /// ``` 60 | fn is_in<'src, I, E>(mut self, events: I) -> bool 61 | where 62 | I: IntoIterator + 'src, 63 | E: Borrow>, 64 | Self: Sized, 65 | { 66 | events.into_iter().any(|ev| self.matches_event(ev.borrow())) 67 | } 68 | 69 | /// Returns a [`Matcher`] which will wait until `self` matches, then return 70 | /// `true` at the start of the next top-level element. 71 | fn then_start_of_next_line(self) -> StartOfNextLine 72 | where 73 | Self: Sized, 74 | { 75 | StartOfNextLine::new(self) 76 | } 77 | 78 | /// Wraps `self` in a [`Matcher`] which will only ever return `true` once. 79 | fn fuse(self) -> OneShot 80 | where 81 | Self: Sized, 82 | { 83 | OneShot::new(self) 84 | } 85 | 86 | /// Get a [`Matcher`] which returns `true` when `self` goes from `true` to 87 | /// `false`. 88 | fn falling_edge(self) -> FallingEdge 89 | where 90 | Self: Sized, 91 | { 92 | FallingEdge::new(self) 93 | } 94 | 95 | /// Get a [`Matcher`] which matches when `self` and `other` both match. 96 | fn and(self, other: M) -> And 97 | where 98 | Self: Sized, 99 | M: Matcher, 100 | { 101 | And::new(self, other) 102 | } 103 | 104 | /// Borrows the [`Matcher`] , rather than consuming it. 105 | /// 106 | /// This allows you to apply [`Matcher`] adaptors while retaining ownership 107 | /// of the original [`Matcher`]. 108 | fn by_ref(&mut self) -> Ref<'_, Self> 109 | where 110 | Self: Sized, 111 | { 112 | Ref(self) 113 | } 114 | } 115 | 116 | impl Matcher for F 117 | where 118 | F: FnMut(&Event<'_>) -> bool, 119 | { 120 | fn matches_event(&mut self, event: &Event<'_>) -> bool { self(event) } 121 | } 122 | 123 | /// A [`Matcher`] which matches everything. 124 | #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)] 125 | pub struct Always; 126 | 127 | impl Matcher for Always { 128 | fn matches_event(&mut self, _event: &Event<'_>) -> bool { true } 129 | } 130 | 131 | /// Get an iterator over the indices of matching events. 132 | /// 133 | /// # Examples 134 | /// 135 | /// ```rust 136 | /// use pulldown_cmark::Event; 137 | /// 138 | /// let matcher = markedit::exact_text("Header"); 139 | /// let src = "# Header\nsome text\n# Header"; 140 | /// let events: Vec<_> = markedit::parse(src).collect(); 141 | /// 142 | /// let indices: Vec<_> = markedit::match_indices(matcher, &events).collect(); 143 | /// 144 | /// assert_eq!(indices.len(), 2); 145 | /// 146 | /// for ix in indices { 147 | /// assert_eq!(events[ix], Event::Text("Header".into())); 148 | /// } 149 | /// ``` 150 | pub fn match_indices<'ev, M, I>( 151 | mut matcher: M, 152 | events: I, 153 | ) -> impl Iterator + 'ev 154 | where 155 | M: Matcher + 'ev, 156 | I: IntoIterator + 'ev, 157 | I::Item: Borrow> + 'ev, 158 | { 159 | events 160 | .into_iter() 161 | .enumerate() 162 | .filter_map(move |(i, event)| { 163 | if matcher.matches_event(event.borrow()) { 164 | Some(i) 165 | } else { 166 | None 167 | } 168 | }) 169 | } 170 | 171 | /// Gets all [`Event`]s between (inclusive) two matchers. 172 | /// 173 | /// # Examples 174 | /// 175 | /// ```rust 176 | /// use pulldown_cmark::Event; 177 | /// 178 | /// let src = "# Header\nnormal text\n# End"; 179 | /// 180 | /// let events: Vec<_> = markedit::parse(src).collect(); 181 | /// let start = markedit::exact_text("Header"); 182 | /// let end = markedit::exact_text("End"); 183 | /// 184 | /// let got = markedit::between(start, end, &events).unwrap(); 185 | /// 186 | /// assert_eq!(got.first().unwrap(), &Event::Text("Header".into())); 187 | /// assert_eq!(got.last().unwrap(), &Event::Text("End".into())); 188 | /// assert_eq!(got.len(), 7); 189 | /// ``` 190 | pub fn between<'ev, S, E>( 191 | start: S, 192 | end: E, 193 | events: &'ev [Event<'ev>], 194 | ) -> Option<&'ev [Event<'ev>]> 195 | where 196 | S: Matcher, 197 | E: Matcher, 198 | { 199 | if let Some(start_ix) = match_indices(start, events).next() { 200 | let rest = &events[start_ix..]; 201 | 202 | return Some( 203 | end.first_match(rest) 204 | .map_or(rest, |end_ix| &rest[..=end_ix]), 205 | ); 206 | } 207 | 208 | None 209 | } 210 | 211 | /// Match an [`Event::Text`] event with this *exact* text. 212 | /// 213 | /// Not to be confused with [`text_containing()`]. 214 | /// 215 | /// ```rust 216 | /// use markedit::Matcher; 217 | /// use pulldown_cmark::Event; 218 | /// 219 | /// assert_eq!( 220 | /// markedit::exact_text("Something").is_in(markedit::parse("Something")), 221 | /// true, 222 | /// ); 223 | /// assert_eq!( 224 | /// markedit::exact_text("Something").is_in(markedit::parse("Something Else")), 225 | /// false, 226 | /// ); 227 | /// ``` 228 | pub fn exact_text>(needle: S) -> impl Matcher { 229 | text(move |text| AsRef::::as_ref(text) == needle.as_ref()) 230 | } 231 | 232 | /// Match an [`Event::Text`] event which *contains* the provided string. 233 | /// 234 | /// Not to be confused with [`exact_text()`]. 235 | /// 236 | /// ```rust 237 | /// use markedit::Matcher; 238 | /// use pulldown_cmark::Event; 239 | /// 240 | /// assert_eq!( 241 | /// markedit::text_containing("Something").is_in(markedit::parse("Something")), 242 | /// true, 243 | /// ); 244 | /// assert_eq!( 245 | /// markedit::text_containing("Something").is_in(markedit::parse("Something Else")), 246 | /// true, 247 | /// ); 248 | /// ``` 249 | pub fn text_containing>(needle: S) -> impl Matcher { 250 | text(move |text| text.contains(needle.as_ref())) 251 | } 252 | 253 | /// Match a [`Event::Text`] node using an arbitrary predicate. 254 | pub fn text

(mut predicate: P) -> impl Matcher 255 | where 256 | P: FnMut(&str) -> bool, 257 | { 258 | move |ev: &Event<'_>| match ev { 259 | Event::Text(text) => predicate(text.as_ref()), 260 | _ => false, 261 | } 262 | } 263 | 264 | /// Matches the start of a link who's URL contains a certain string. 265 | /// 266 | /// # Examples 267 | /// 268 | /// ```rust 269 | /// # use markedit::Matcher; 270 | /// use pulldown_cmark::{Event, Tag}; 271 | /// 272 | /// let src = "Some text containing [a link to google](https://google.com/)."; 273 | /// let mut matcher = markedit::link_with_url_containing("google.com"); 274 | /// 275 | /// let events: Vec<_> = markedit::parse(src).collect(); 276 | /// 277 | /// let ix = matcher.first_match(&events).unwrap(); 278 | /// 279 | /// match &events[ix] { 280 | /// Event::Start(Tag::Link(_, url, _)) => assert_eq!(url.as_ref(), "https://google.com/"), 281 | /// _ => unreachable!(), 282 | /// } 283 | /// ``` 284 | pub fn link_with_url_containing>(needle: S) -> impl Matcher { 285 | move |ev: &Event<'_>| match ev { 286 | Event::Start(Tag::Link(_, link, _)) => { 287 | link.as_ref().contains(needle.as_ref()) 288 | }, 289 | _ => false, 290 | } 291 | } 292 | 293 | /// A glorified `&mut Matcher`. 294 | /// 295 | /// This is the return value for [`Matcher::by_ref()`], you won't normally use 296 | /// it directly. 297 | #[derive(Debug)] 298 | pub struct Ref<'a, M>(&'a mut M); 299 | 300 | impl<'a, M> Matcher for Ref<'a, M> 301 | where 302 | M: Matcher, 303 | { 304 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 305 | self.0.matches_event(event) 306 | } 307 | } 308 | -------------------------------------------------------------------------------- /src/matchers/one_shot.rs: -------------------------------------------------------------------------------- 1 | use crate::matchers::Matcher; 2 | use pulldown_cmark::Event; 3 | 4 | /// A [`Matcher`] which will only ever return `true` once. 5 | #[derive(Debug, Clone, PartialEq)] 6 | pub struct OneShot { 7 | inner: M, 8 | already_triggered: bool, 9 | } 10 | 11 | impl OneShot { 12 | /// Create a [`OneShot`] matcher. 13 | pub const fn new(inner: M) -> Self { 14 | OneShot { 15 | inner, 16 | already_triggered: false, 17 | } 18 | } 19 | } 20 | 21 | impl Matcher for OneShot { 22 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 23 | if self.already_triggered { 24 | return false; 25 | } 26 | 27 | let got = self.inner.matches_event(event); 28 | 29 | if got { 30 | self.already_triggered = true; 31 | } 32 | 33 | got 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/matchers/start_of_next_line.rs: -------------------------------------------------------------------------------- 1 | use crate::matchers::Matcher; 2 | use pulldown_cmark::Event; 3 | 4 | /// A [`Matcher`] which will match the start of the next top-level element after 5 | /// some inner [`Matcher`] matches. 6 | #[derive(Debug, Clone, PartialEq)] 7 | pub struct StartOfNextLine { 8 | inner: M, 9 | state: State, 10 | current_nesting_level: usize, 11 | } 12 | 13 | impl StartOfNextLine { 14 | /// Create a new [`StartOfNextLine`] matcher. 15 | pub const fn new(inner: M) -> Self { 16 | StartOfNextLine { 17 | inner, 18 | state: State::WaitingForFirstMatch, 19 | current_nesting_level: 0, 20 | } 21 | } 22 | 23 | fn update_nesting(&mut self, event: &Event<'_>) { 24 | match event { 25 | Event::Start(_) => self.current_nesting_level += 1, 26 | Event::End(_) => self.current_nesting_level -= 1, 27 | _ => {}, 28 | } 29 | } 30 | } 31 | 32 | impl StartOfNextLine { 33 | fn process_with_inner(&mut self, event: &Event<'_>) { 34 | if self.inner.matches_event(event) { 35 | self.state = State::LookingForLastEndTag; 36 | } 37 | } 38 | } 39 | 40 | impl Matcher for StartOfNextLine { 41 | fn matches_event(&mut self, event: &Event<'_>) -> bool { 42 | self.update_nesting(event); 43 | 44 | match self.state { 45 | State::WaitingForFirstMatch => { 46 | self.process_with_inner(event); 47 | }, 48 | State::LookingForLastEndTag => { 49 | if self.current_nesting_level == 0 { 50 | self.state = State::FoundLastEndTag; 51 | } 52 | }, 53 | State::FoundLastEndTag => { 54 | self.state = State::WaitingForFirstMatch; 55 | return true; 56 | }, 57 | } 58 | 59 | false 60 | } 61 | } 62 | 63 | #[derive(Debug, Copy, Clone, PartialEq)] 64 | enum State { 65 | WaitingForFirstMatch, 66 | LookingForLastEndTag, 67 | FoundLastEndTag, 68 | } 69 | 70 | #[cfg(test)] 71 | mod tests { 72 | use super::*; 73 | use pulldown_cmark::Parser; 74 | 75 | #[test] 76 | fn match_start_of_line_after_heading() { 77 | let src = "# Heading \nSome Text"; 78 | let events: Vec<_> = Parser::new(src).collect(); 79 | let mut matcher = StartOfNextLine::new(crate::exact_text("Heading")); 80 | 81 | let got = matcher.by_ref().first_match(events).unwrap(); 82 | 83 | assert_eq!(got, 3); 84 | // we're on the first start tag, so at nesting level 1 85 | assert_eq!(matcher.current_nesting_level, 1); 86 | assert_eq!(matcher.state, State::WaitingForFirstMatch); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/rewriters/mod.rs: -------------------------------------------------------------------------------- 1 | mod rewritten; 2 | mod writer; 3 | 4 | pub use rewritten::{rewrite, Rewritten}; 5 | pub use writer::Writer; 6 | 7 | use crate::Matcher; 8 | use pulldown_cmark::{CodeBlockKind, CowStr, Event, Tag}; 9 | 10 | /// Something which can rewrite events. 11 | pub trait Rewriter<'src> { 12 | /// Process a single [`Event`]. 13 | /// 14 | /// This may mean ignoring it, mutating it, or adding new events to the 15 | /// [`Writer`]'s buffer. 16 | /// 17 | /// The [`Writer`] is used as a temporary buffer that will then be streamed 18 | /// to the user via [`rewrite()`]. 19 | fn rewrite_event(&mut self, event: Event<'src>, writer: &mut Writer<'src>); 20 | 21 | /// Use this [`Rewriter`] to rewrite a stream of [`Event`]s. 22 | fn rewrite(self, events: E) -> Rewritten<'src, E, Self> 23 | where 24 | Self: Sized, 25 | E: IntoIterator>, 26 | { 27 | Rewritten::new(events, self) 28 | } 29 | } 30 | 31 | impl<'src, F> Rewriter<'src> for F 32 | where 33 | F: FnMut(Event<'src>, &mut Writer<'src>), 34 | { 35 | fn rewrite_event(&mut self, event: Event<'src>, writer: &mut Writer<'src>) { 36 | self(event, writer); 37 | } 38 | } 39 | 40 | /// Inserts some markdown text before whatever is matched by the [`Matcher`]. 41 | /// 42 | /// # Examples 43 | /// 44 | /// ```rust 45 | /// use markedit::Matcher; 46 | /// let src = "# Heading\nsome text\n"; 47 | /// 48 | /// let first_line_after_heading = markedit::exact_text("Heading") 49 | /// .falling_edge(); 50 | /// let rewriter = markedit::insert_markdown_before( 51 | /// "## Second Heading", 52 | /// first_line_after_heading, 53 | /// ); 54 | /// 55 | /// let events = markedit::parse(src); 56 | /// let rewritten: Vec<_> = markedit::rewrite(events, rewriter).collect(); 57 | /// 58 | /// // if everything went to plan, the output should contain "Second Heading" 59 | /// assert!(markedit::exact_text("Second Heading").is_in(&rewritten)); 60 | /// ``` 61 | pub fn insert_markdown_before<'src, M, S>( 62 | markdown_text: S, 63 | matcher: M, 64 | ) -> impl Rewriter<'src> + 'src 65 | where 66 | M: Matcher + 'src, 67 | S: AsRef + 'src, 68 | { 69 | let events = crate::parse(markdown_text.as_ref()) 70 | .map(owned_event) 71 | .collect(); 72 | insert_before(events, matcher) 73 | } 74 | 75 | /// Splice some events into the resulting event stream before every match. 76 | pub fn insert_before<'src, M>( 77 | to_insert: Vec>, 78 | mut matcher: M, 79 | ) -> impl Rewriter<'src> + 'src 80 | where 81 | M: Matcher + 'src, 82 | { 83 | move |ev: Event<'src>, writer: &mut Writer<'src>| { 84 | if matcher.matches_event(&ev) { 85 | writer.extend(to_insert.iter().cloned()); 86 | } 87 | writer.push(ev); 88 | } 89 | } 90 | 91 | /// A [`Rewriter`] which lets you update a [`Event::Text`] node based on some 92 | /// predicate. 93 | pub fn change_text<'src, M, F, S>( 94 | mut predicate: M, 95 | mut mutator: F, 96 | ) -> impl Rewriter<'src> + 'src 97 | where 98 | M: FnMut(&str) -> bool + 'src, 99 | F: FnMut(CowStr<'src>) -> S + 'src, 100 | S: Into>, 101 | { 102 | move |ev: Event<'src>, writer: &mut Writer<'src>| match ev { 103 | Event::Text(text) => { 104 | let text = if predicate(text.as_ref()) { 105 | mutator(text).into() 106 | } else { 107 | text 108 | }; 109 | writer.push(Event::Text(text)); 110 | }, 111 | _ => writer.push(ev), 112 | } 113 | } 114 | 115 | fn owned_event(ev: Event<'_>) -> Event<'static> { 116 | match ev { 117 | Event::Start(tag) => Event::Start(owned_tag(tag)), 118 | Event::End(tag) => Event::End(owned_tag(tag)), 119 | Event::Text(s) => Event::Text(owned_cow_str(s)), 120 | Event::Code(s) => Event::Code(owned_cow_str(s)), 121 | Event::Html(s) => Event::Html(owned_cow_str(s)), 122 | Event::FootnoteReference(s) => { 123 | Event::FootnoteReference(owned_cow_str(s)) 124 | }, 125 | Event::SoftBreak => Event::SoftBreak, 126 | Event::HardBreak => Event::HardBreak, 127 | Event::Rule => Event::Rule, 128 | Event::TaskListMarker(t) => Event::TaskListMarker(t), 129 | } 130 | } 131 | 132 | fn owned_cow_str(s: CowStr<'_>) -> CowStr<'static> { 133 | match s { 134 | CowStr::Borrowed(_) => CowStr::from(s.into_string()), 135 | CowStr::Boxed(boxed) => CowStr::Boxed(boxed), 136 | CowStr::Inlined(inlined) => CowStr::Inlined(inlined), 137 | } 138 | } 139 | 140 | fn owned_tag(tag: Tag<'_>) -> Tag<'static> { 141 | match tag { 142 | Tag::Paragraph => Tag::Paragraph, 143 | Tag::Heading(h) => Tag::Heading(h), 144 | Tag::BlockQuote => Tag::BlockQuote, 145 | Tag::CodeBlock(CodeBlockKind::Indented) => { 146 | Tag::CodeBlock(CodeBlockKind::Indented) 147 | }, 148 | Tag::CodeBlock(CodeBlockKind::Fenced(s)) => { 149 | Tag::CodeBlock(CodeBlockKind::Fenced(owned_cow_str(s))) 150 | }, 151 | Tag::List(u) => Tag::List(u), 152 | Tag::Item => Tag::Item, 153 | Tag::FootnoteDefinition(s) => Tag::FootnoteDefinition(owned_cow_str(s)), 154 | Tag::Table(alignment) => Tag::Table(alignment), 155 | Tag::TableHead => Tag::TableHead, 156 | Tag::TableRow => Tag::TableRow, 157 | Tag::TableCell => Tag::TableCell, 158 | Tag::Emphasis => Tag::Emphasis, 159 | Tag::Strong => Tag::Strong, 160 | Tag::Strikethrough => Tag::Strikethrough, 161 | Tag::Link(t, url, title) => { 162 | Tag::Link(t, owned_cow_str(url), owned_cow_str(title)) 163 | }, 164 | Tag::Image(t, url, alt) => { 165 | Tag::Image(t, owned_cow_str(url), owned_cow_str(alt)) 166 | }, 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/rewriters/rewritten.rs: -------------------------------------------------------------------------------- 1 | use crate::{Rewriter, Writer}; 2 | use pulldown_cmark::Event; 3 | 4 | /// The whole point. 5 | /// 6 | /// This function takes a stream of [`Event`]s and a [`Rewriter`], and gives 7 | /// you a stream of rewritten [`Event`]s. 8 | pub fn rewrite<'src, E, R>( 9 | events: E, 10 | rewriter: R, 11 | ) -> impl Iterator> + 'src 12 | where 13 | E: IntoIterator>, 14 | E::IntoIter: 'src, 15 | R: Rewriter<'src> + 'src, 16 | { 17 | Rewritten::new(events.into_iter(), rewriter) 18 | } 19 | 20 | /// A stream of [`Event`]s that have been modified by a [`Rewriter`]. 21 | #[derive(Debug)] 22 | pub struct Rewritten<'src, E, R> { 23 | events: E, 24 | rewriter: R, 25 | writer: Writer<'src>, 26 | } 27 | 28 | impl<'src, E, R> Rewritten<'src, E, R> { 29 | /// Create a new [`Rewritten`] iterator. 30 | pub fn new(events: E, rewriter: R) -> Self { 31 | Rewritten { 32 | rewriter, 33 | events, 34 | writer: Writer::new(), 35 | } 36 | } 37 | } 38 | 39 | impl<'src, E, R> Iterator for Rewritten<'src, E, R> 40 | where 41 | E: Iterator>, 42 | R: Rewriter<'src>, 43 | { 44 | type Item = Event<'src>; 45 | 46 | fn next(&mut self) -> Option { 47 | loop { 48 | // we're still working through items buffered by the rewriter 49 | if let Some(ev) = self.writer.buffer.pop_front() { 50 | return Some(ev); 51 | } 52 | 53 | // we need to pop another event and process it 54 | let event = self.events.next()?; 55 | self.rewriter.rewrite_event(event, &mut self.writer); 56 | } 57 | } 58 | } 59 | 60 | #[cfg(test)] 61 | mod tests { 62 | use super::*; 63 | 64 | use pulldown_cmark::Tag; 65 | 66 | #[test] 67 | fn ignore_some_events() { 68 | let events = vec![ 69 | Event::Start(Tag::Paragraph), 70 | Event::Text("This is some text.".into()), 71 | Event::Start(Tag::Heading(2)), 72 | Event::Text("This is some more text.".into()), 73 | ]; 74 | 75 | let rewritten: Vec> = rewrite( 76 | events, 77 | |event: Event<'static>, writer: &mut Writer<'static>| { 78 | if let event @ Event::Text(_) = event { 79 | writer.push(event); 80 | } 81 | }, 82 | ) 83 | .collect(); 84 | 85 | assert_eq!( 86 | rewritten, 87 | vec![ 88 | Event::Text("This is some text.".into()), 89 | Event::Text("This is some more text.".into()), 90 | ] 91 | ); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/rewriters/writer.rs: -------------------------------------------------------------------------------- 1 | use pulldown_cmark::Event; 2 | use std::collections::VecDeque; 3 | 4 | #[allow(unused_imports)] // for rustdoc 5 | use crate::Rewriter; 6 | 7 | /// The output buffer given to [`Rewriter::rewrite_event()`]. 8 | #[derive(Debug)] 9 | pub struct Writer<'a> { 10 | pub(crate) buffer: VecDeque>, 11 | } 12 | 13 | impl<'a> Writer<'a> { 14 | pub(crate) fn new() -> Writer<'a> { 15 | Writer { 16 | buffer: VecDeque::new(), 17 | } 18 | } 19 | 20 | /// Queue an [`Event`] to be emitted. 21 | pub fn push(&mut self, event: Event<'a>) { self.buffer.push_back(event); } 22 | } 23 | 24 | impl<'a> Extend> for Writer<'a> { 25 | fn extend>>(&mut self, iter: I) { 26 | self.buffer.extend(iter); 27 | } 28 | } 29 | --------------------------------------------------------------------------------