Skip to content

Commit

Permalink
Merge pull request #108 from rodneylab/ci__add_jsr_meta
Browse files Browse the repository at this point in the history
ci: 🐝 add JSR metadata
  • Loading branch information
rodneylab authored Nov 28, 2024
2 parents f4e6083 + e441f66 commit 0731da0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
5 changes: 4 additions & 1 deletion deno.json
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/"]
},
Expand All @@ -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" }
}
66 changes: 66 additions & 0 deletions src/text/mod.rs
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
}
}
43 changes: 43 additions & 0 deletions src/text/tests.rs
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.");
}

0 comments on commit 0731da0

Please sign in to comment.