-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Markdown length-limited summary implementation
This commit refactors the implementation of `markdown_summary_with_limit()`, separating the logic of determining when the limit has been reached from the actual rendering process. The main advantage of the new approach is that it guarantees that all HTML tags are closed, whereas the previous implementation could generate tags that were never closed. It also ensures that no empty tags are generated (e.g., `<em></em>`). The new implementation consists of a general-purpose struct `HtmlWithLimit` that manages the length-limiting logic and a function `markdown_summary_with_limit()` that renders Markdown to HTML using the struct.
- Loading branch information
Showing
4 changed files
with
125 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! See [`HtmlWithLimit`]. | ||
use std::fmt::Write; | ||
use std::ops::ControlFlow; | ||
|
||
use crate::html::escape::Escape; | ||
|
||
/// A buffer that allows generating HTML with a length limit. | ||
/// | ||
/// This buffer ensures that: | ||
/// | ||
/// * all tags are closed, | ||
/// * only the most recently opened tag is closed, | ||
/// * no tags are left empty (e.g., `<em></em>`) due to the length limit being reached, | ||
/// * all text is escaped. | ||
#[derive(Debug)] | ||
pub(super) struct HtmlWithLimit { | ||
buf: String, | ||
len: usize, | ||
limit: usize, | ||
/// A list of tags that have been requested to be opened via [`Self::open_tag()`] | ||
/// but have not actually been pushed to `buf` yet. This ensures that tags are not | ||
/// left empty (e.g., `<em></em>`) due to the length limit being reached. | ||
queued_tags: Vec<&'static str>, | ||
/// A list of all tags that have been opened but not yet closed. | ||
unclosed_tags: Vec<&'static str>, | ||
} | ||
|
||
impl HtmlWithLimit { | ||
/// Create a new buffer, with a limit of `length_limit`. | ||
pub(super) fn new(length_limit: usize) -> Self { | ||
Self { | ||
buf: String::new(), | ||
len: 0, | ||
limit: length_limit, | ||
unclosed_tags: Vec::new(), | ||
queued_tags: Vec::new(), | ||
} | ||
} | ||
|
||
/// Finish using the buffer and get the written output. | ||
/// This function will close all unclosed tags for you. | ||
pub(super) fn finish(mut self) -> String { | ||
self.close_all_tags(); | ||
self.buf | ||
} | ||
|
||
/// Write some plain text to the buffer, escaping as needed. | ||
/// | ||
/// This function skips writing the text if the length limit was reached | ||
/// and returns [`ControlFlow::Break`]. | ||
pub(super) fn push(&mut self, text: &str) -> ControlFlow<(), ()> { | ||
if self.len + text.len() > self.limit { | ||
return ControlFlow::BREAK; | ||
} | ||
|
||
self.flush_queue(); | ||
write!(self.buf, "{}", Escape(text)).unwrap(); | ||
self.len += text.len(); | ||
|
||
ControlFlow::CONTINUE | ||
} | ||
|
||
/// Open an HTML tag. | ||
pub(super) fn open_tag(&mut self, tag_name: &'static str) { | ||
self.queued_tags.push(tag_name); | ||
} | ||
|
||
/// Close the most recently opened HTML tag. | ||
pub(super) fn close_tag(&mut self) { | ||
let tag_name = self.unclosed_tags.pop().unwrap(); | ||
self.buf.push_str("</"); | ||
self.buf.push_str(tag_name); | ||
self.buf.push('>'); | ||
} | ||
|
||
/// Write all queued tags and add them to the `unclosed_tags` list. | ||
fn flush_queue(&mut self) { | ||
for tag_name in self.queued_tags.drain(..) { | ||
self.buf.push('<'); | ||
self.buf.push_str(tag_name); | ||
self.buf.push('>'); | ||
|
||
self.unclosed_tags.push(tag_name); | ||
} | ||
} | ||
|
||
/// Close all unclosed tags. | ||
fn close_all_tags(&mut self) { | ||
while !self.unclosed_tags.is_empty() { | ||
self.close_tag(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters