-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{ | ||
"name": "@rodney/parsedown", | ||
"version": "0.4.2", | ||
"license": "BSD-3-Clause", | ||
"exports": "./mod.ts", | ||
"fmt": { | ||
"exclude": [".github/"] | ||
}, | ||
|
@@ -7,6 +11,5 @@ | |
"test": "deno test -A", | ||
"wasmbuild": "deno run -A jsr:@deno/[email protected] --project=parsedown" | ||
}, | ||
"exclude": ["lib/"], | ||
"imports": { "@std/assert": "jsr:@std/assert@^1.0.8" } | ||
} |
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,66 @@ | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
use std::borrow::Cow; | ||
|
||
pub fn format_line<'a, I: Into<Cow<'a, str>>>(line: I) -> Cow<'a, str> { | ||
let line = line.into(); | ||
fn is_replace_character(c: char) -> bool { | ||
c == '\'' || c == '"' | ||
} | ||
|
||
let first = line.find(is_replace_character); | ||
if let Some(value) = first { | ||
let (mut result, rest) = match value { | ||
0 => match &line[0..1] { | ||
"\"" => (String::from('\u{201c}'), line[1..].chars()), | ||
"'" => (String::from('\u{2018}'), line[1..].chars()), | ||
_ => (String::from(&line[0..value]), line[value..].chars()), | ||
}, | ||
_ => { | ||
if &line[(value - 1)..value] == " " { | ||
( | ||
String::from(&line[0..(value - 1)]), | ||
line[(value - 1)..].chars(), | ||
) | ||
} else { | ||
(String::from(&line[0..value]), line[value..].chars()) | ||
} | ||
} | ||
}; | ||
result.reserve(line.len() - value); | ||
|
||
let mut preceded_by_space = false; | ||
for c in rest { | ||
match c { | ||
'\'' => { | ||
if preceded_by_space { | ||
preceded_by_space = false; | ||
result.push('\u{2018}') | ||
} else { | ||
result.push('\u{2019}') | ||
} | ||
} | ||
'"' => { | ||
if preceded_by_space { | ||
preceded_by_space = false; | ||
result.push('\u{201c}') | ||
} else { | ||
result.push('\u{201d}') | ||
} | ||
} | ||
' ' => { | ||
preceded_by_space = true; | ||
result.push(c); | ||
} | ||
_ => { | ||
preceded_by_space = false; | ||
result.push(c); | ||
} | ||
} | ||
} | ||
Cow::Owned(result) | ||
} else { | ||
line | ||
} | ||
} |
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,43 @@ | ||
use crate::text::format_line; | ||
|
||
#[test] | ||
fn format_line_replaces_inner_apostrophe() { | ||
let line = "My apple's quite tasty."; | ||
let result = format_line(line); | ||
assert_eq!(result, "My apple’s quite tasty."); | ||
} | ||
|
||
#[test] | ||
fn test_line_replaces_outer_apostrophe() { | ||
let line = "My trees' apples are tasty."; | ||
let result = format_line(line); | ||
assert_eq!(result, "My trees’ apples are tasty."); | ||
} | ||
|
||
#[test] | ||
fn test_line_adds_double_smart_quotes() { | ||
let line = r#"The person said "My apple is quite tasty.""#; | ||
let result = format_line(line); | ||
assert_eq!(result, r#"The person said “My apple is quite tasty.”"#); | ||
} | ||
|
||
#[test] | ||
fn test_line_adds_single_smart_quotes() { | ||
let line = "My apple is quite 'tasty'."; | ||
let result = format_line(line); | ||
assert_eq!(result, "My apple is quite ‘tasty’."); | ||
} | ||
|
||
#[test] | ||
fn test_line_replaces_unmatched_single_double_quote_pairs() { | ||
let line = r#"My apple is quite 'tasty"."#; | ||
let result = format_line(line); | ||
assert_eq!(result, "My apple is quite ‘tasty”."); | ||
} | ||
|
||
#[test] | ||
fn test_line_does_nothing_when_line_has_no_quotes_or_apostrophes() { | ||
let line = "My apple is quite tasty."; | ||
let result = format_line(line); | ||
assert_eq!(result, "My apple is quite tasty."); | ||
} |