-
Notifications
You must be signed in to change notification settings - Fork 0
Add ellipsis command-line option #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2fb09e7
Add test for long ellipsis sequences
leynos 1d5dfc2
Integrate ellipsis into processing
leynos 8fdc144
Expose ellipsis helper and expand tests
leynos 5dc856a
Fix README grammar and clarify API
leynos 1a33ac3
Clarify option usage in README
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,105 @@ | ||
| //! Replace sequences of three dots with the ellipsis character. | ||
| //! | ||
| //! Groups of three consecutive dots become a single Unicode ellipsis. Longer | ||
| //! runs are processed left-to-right so trailing dots that do not form a | ||
| //! complete triple remain. Fenced code blocks and inline code spans are left | ||
| //! untouched. | ||
|
|
||
| use regex::Regex; | ||
|
|
||
| use crate::wrap::{Token, tokenize_markdown}; | ||
|
|
||
| static DOT_RE: std::sync::LazyLock<Regex> = | ||
| std::sync::LazyLock::new(|| Regex::new(r"\.{3,}").unwrap()); | ||
|
|
||
| /// Replace `...` with `…` outside code spans and fences. | ||
| #[must_use] | ||
| pub fn replace_ellipsis(lines: &[String]) -> Vec<String> { | ||
| if lines.is_empty() { | ||
| return Vec::new(); | ||
| } | ||
| let joined = lines.join("\n"); | ||
| let mut out = String::new(); | ||
| for token in tokenize_markdown(&joined) { | ||
| match token { | ||
| Token::Text(t) => { | ||
| let replaced = DOT_RE.replace_all(t, |caps: ®ex::Captures<'_>| { | ||
| let len = caps[0].len(); | ||
| let ellipses = "…".repeat(len / 3); | ||
| let leftover = ".".repeat(len % 3); | ||
| format!("{ellipses}{leftover}") | ||
| }); | ||
| out.push_str(&replaced); | ||
| } | ||
| Token::Code(c) => { | ||
| out.push('`'); | ||
| out.push_str(c); | ||
| out.push('`'); | ||
| } | ||
| Token::Fence(f) => { | ||
| out.push_str(f); | ||
| } | ||
| Token::Newline => out.push('\n'), | ||
| } | ||
| } | ||
| out.split('\n').map(str::to_string).collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn replaces_simple_text() { | ||
| let input = vec!["wait...".to_string()]; | ||
| let expected = vec!["wait…".to_string()]; | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ignores_code_spans() { | ||
| let input = vec!["a `b...` c".to_string()]; | ||
| let expected = input.clone(); | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ignores_fenced_blocks() { | ||
| let input = vec!["```".to_string(), "...".to_string(), "```".to_string()]; | ||
| let expected = input.clone(); | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn replaces_long_sequences() { | ||
| let input = vec![".... ..... ...... .......".to_string()]; | ||
| let expected = vec!["…. ….. …… …….".to_string()]; | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn handles_empty_input() { | ||
| let input: Vec<String> = Vec::new(); | ||
| let expected: Vec<String> = Vec::new(); | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn handles_multiple_fenced_blocks() { | ||
| let input = vec![ | ||
| "text...".to_string(), | ||
| "```".to_string(), | ||
| "code...".to_string(), | ||
| "```".to_string(), | ||
| "more text...".to_string(), | ||
| ]; | ||
| let expected = vec![ | ||
| "text…".to_string(), | ||
| "```".to_string(), | ||
| "code...".to_string(), | ||
| "```".to_string(), | ||
| "more text…".to_string(), | ||
| ]; | ||
| assert_eq!(replace_ellipsis(&input), expected); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.