From 903469a45f9d63f80951470c06b01ca2079cef0f Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Wed, 13 Oct 2021 20:27:00 -0700 Subject: [PATCH 1/3] update pulldown-cmark to 0.8.0 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f6c5e12..f1958ad6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1267,9 +1267,9 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.7.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" +checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8" dependencies = [ "bitflags", "getopts", diff --git a/Cargo.toml b/Cargo.toml index dd0b17bd..46fc04fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ lazy_static = "1.0" log = "0.4" memchr = "2.0" opener = "0.5" -pulldown-cmark = "0.7.0" +pulldown-cmark = "0.8.0" regex = "1.0.0" serde = "1.0" serde_derive = "1.0" From 1be69af553a4e2466ad3c57a327c9febef10005b Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Fri, 19 Nov 2021 15:14:22 -0800 Subject: [PATCH 2/3] need to actually enable it --- src/utils/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2352517a..969b2cfb 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -174,6 +174,7 @@ pub fn new_cmark_parser(text: &str) -> Parser<'_> { opts.insert(Options::ENABLE_FOOTNOTES); opts.insert(Options::ENABLE_STRIKETHROUGH); opts.insert(Options::ENABLE_TASKLISTS); + opts.insert(Options::ENABLE_SMART_PUNCTUATION); Parser::new_ext(text, opts) } From 1c0983b811abe1c26c092911b614649aebec318c Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Fri, 19 Nov 2021 17:26:30 -0800 Subject: [PATCH 3/3] use pulldown cmarks curly quotes --- src/renderer/html_handlebars/search.rs | 2 +- src/utils/mod.rs | 98 ++------------------------ 2 files changed, 7 insertions(+), 93 deletions(-) diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index 5d9a5dc9..2dc717fb 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -85,7 +85,7 @@ fn render_item( .with_context(|| "Could not convert HTML path to str")?; let anchor_base = utils::fs::normalize_path(filepath); - let mut p = utils::new_cmark_parser(&chapter.content).peekable(); + let mut p = utils::new_cmark_parser(&chapter.content, false).peekable(); let mut in_heading = false; let max_section_depth = u32::from(search_config.heading_split_level); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 969b2cfb..31679ceb 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -168,64 +168,29 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String { render_markdown_with_path(text, curly_quotes, None) } -pub fn new_cmark_parser(text: &str) -> Parser<'_> { +pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_> { let mut opts = Options::empty(); opts.insert(Options::ENABLE_TABLES); opts.insert(Options::ENABLE_FOOTNOTES); opts.insert(Options::ENABLE_STRIKETHROUGH); opts.insert(Options::ENABLE_TASKLISTS); - opts.insert(Options::ENABLE_SMART_PUNCTUATION); + if curly_quotes { + opts.insert(Options::ENABLE_SMART_PUNCTUATION); + } Parser::new_ext(text, opts) } pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String { let mut s = String::with_capacity(text.len() * 3 / 2); - let p = new_cmark_parser(text); - let mut converter = EventQuoteConverter::new(curly_quotes); + let p = new_cmark_parser(text, curly_quotes); let events = p .map(clean_codeblock_headers) - .map(|event| adjust_links(event, path)) - .map(|event| converter.convert(event)); + .map(|event| adjust_links(event, path)); html::push_html(&mut s, events); s } -struct EventQuoteConverter { - enabled: bool, - convert_text: bool, -} - -impl EventQuoteConverter { - fn new(enabled: bool) -> Self { - EventQuoteConverter { - enabled, - convert_text: true, - } - } - - fn convert<'a>(&mut self, event: Event<'a>) -> Event<'a> { - if !self.enabled { - return event; - } - - match event { - Event::Start(Tag::CodeBlock(_)) => { - self.convert_text = false; - event - } - Event::End(Tag::CodeBlock(_)) => { - self.convert_text = true; - event - } - Event::Text(ref text) if self.convert_text => { - Event::Text(CowStr::from(convert_quotes_to_curly(text))) - } - _ => event, - } - } -} - fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> { match event { Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(ref info))) => { @@ -244,38 +209,6 @@ fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> { } } -fn convert_quotes_to_curly(original_text: &str) -> String { - // We'll consider the start to be "whitespace". - let mut preceded_by_whitespace = true; - - original_text - .chars() - .map(|original_char| { - let converted_char = match original_char { - '\'' => { - if preceded_by_whitespace { - '‘' - } else { - '’' - } - } - '"' => { - if preceded_by_whitespace { - '“' - } else { - '”' - } - } - _ => original_char, - }; - - preceded_by_whitespace = original_char.is_whitespace(); - - converted_char - }) - .collect() -} - /// Prints a "backtrace" of some `Error`. pub fn log_backtrace(e: &Error) { error!("Error: {}", e); @@ -451,23 +384,4 @@ more text with spaces assert_eq!(normalize_id(""), ""); } } - - mod convert_quotes_to_curly { - use super::super::convert_quotes_to_curly; - - #[test] - fn it_converts_single_quotes() { - assert_eq!(convert_quotes_to_curly("'one', 'two'"), "‘one’, ‘two’"); - } - - #[test] - fn it_converts_double_quotes() { - assert_eq!(convert_quotes_to_curly(r#""one", "two""#), "“one”, “two”"); - } - - #[test] - fn it_treats_tab_as_whitespace() { - assert_eq!(convert_quotes_to_curly("\t'one'"), "\t‘one’"); - } - } }