Skip to content

Commit 44ba535

Browse files
authored
Merge pull request #36 from AdamIsrael/emdash
Convert double hyphens to em-dash
2 parents a3f43b8 + 55bea43 commit 44ba535

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/markdown.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@ fn strip_comments(mut content: String) -> String {
1919
content.trim().to_string()
2020
}
2121

22+
/// Convert hyphens to em-dashes
23+
fn convert_hyphens_to_em_dashes(mut content: String) -> String {
24+
let re = Regex::new(r"(\s+--\s+)").unwrap();
25+
content = Regex::replace_all(&re, content.as_str(), "—").to_string();
26+
content.trim().to_string()
27+
}
28+
2229
/// Convert the content of a Markdown into a collection of paragraphs.
2330
fn content_to_paragraphs(mut content: String) -> Vec<Paragraph> {
2431
// Pre-process the content
2532

2633
// Add support single and multi-line %% comment blocks %%
2734
content = strip_comments(content);
2835

36+
// Replace double-hyphens to em-dashes
37+
content = convert_hyphens_to_em_dashes(content);
38+
2939
let mut paragraphs: Vec<Paragraph> = vec![];
3040
let sep = Paragraph::new()
3141
.add_run(Run::new().add_text("#"))
@@ -262,6 +272,14 @@ mod tests {
262272
assert!(content.is_empty());
263273
}
264274

275+
#[test]
276+
fn test_convert_hyphens_to_em_dashes() {
277+
let content = convert_hyphens_to_em_dashes(
278+
"This is a test -- only a test -- he was told.".to_string(),
279+
);
280+
assert!(content == "This is a test—only a test—he was told.");
281+
}
282+
265283
#[test]
266284
fn test_trim_doublespace() {
267285
let s = "This is a test. This is only a test.\nIf this were an actual emergency, you would be instructed where to go and what to do.";

0 commit comments

Comments
 (0)