Skip to content

Commit 1c0983b

Browse files
committed
use pulldown cmarks curly quotes
1 parent 1be69af commit 1c0983b

File tree

2 files changed

+7
-93
lines changed

2 files changed

+7
-93
lines changed

src/renderer/html_handlebars/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn render_item(
8585
.with_context(|| "Could not convert HTML path to str")?;
8686
let anchor_base = utils::fs::normalize_path(filepath);
8787

88-
let mut p = utils::new_cmark_parser(&chapter.content).peekable();
88+
let mut p = utils::new_cmark_parser(&chapter.content, false).peekable();
8989

9090
let mut in_heading = false;
9191
let max_section_depth = u32::from(search_config.heading_split_level);

src/utils/mod.rs

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -168,64 +168,29 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
168168
render_markdown_with_path(text, curly_quotes, None)
169169
}
170170

171-
pub fn new_cmark_parser(text: &str) -> Parser<'_> {
171+
pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_> {
172172
let mut opts = Options::empty();
173173
opts.insert(Options::ENABLE_TABLES);
174174
opts.insert(Options::ENABLE_FOOTNOTES);
175175
opts.insert(Options::ENABLE_STRIKETHROUGH);
176176
opts.insert(Options::ENABLE_TASKLISTS);
177-
opts.insert(Options::ENABLE_SMART_PUNCTUATION);
177+
if curly_quotes {
178+
opts.insert(Options::ENABLE_SMART_PUNCTUATION);
179+
}
178180
Parser::new_ext(text, opts)
179181
}
180182

181183
pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String {
182184
let mut s = String::with_capacity(text.len() * 3 / 2);
183-
let p = new_cmark_parser(text);
184-
let mut converter = EventQuoteConverter::new(curly_quotes);
185+
let p = new_cmark_parser(text, curly_quotes);
185186
let events = p
186187
.map(clean_codeblock_headers)
187-
.map(|event| adjust_links(event, path))
188-
.map(|event| converter.convert(event));
188+
.map(|event| adjust_links(event, path));
189189

190190
html::push_html(&mut s, events);
191191
s
192192
}
193193

194-
struct EventQuoteConverter {
195-
enabled: bool,
196-
convert_text: bool,
197-
}
198-
199-
impl EventQuoteConverter {
200-
fn new(enabled: bool) -> Self {
201-
EventQuoteConverter {
202-
enabled,
203-
convert_text: true,
204-
}
205-
}
206-
207-
fn convert<'a>(&mut self, event: Event<'a>) -> Event<'a> {
208-
if !self.enabled {
209-
return event;
210-
}
211-
212-
match event {
213-
Event::Start(Tag::CodeBlock(_)) => {
214-
self.convert_text = false;
215-
event
216-
}
217-
Event::End(Tag::CodeBlock(_)) => {
218-
self.convert_text = true;
219-
event
220-
}
221-
Event::Text(ref text) if self.convert_text => {
222-
Event::Text(CowStr::from(convert_quotes_to_curly(text)))
223-
}
224-
_ => event,
225-
}
226-
}
227-
}
228-
229194
fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> {
230195
match event {
231196
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(ref info))) => {
@@ -244,38 +209,6 @@ fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> {
244209
}
245210
}
246211

247-
fn convert_quotes_to_curly(original_text: &str) -> String {
248-
// We'll consider the start to be "whitespace".
249-
let mut preceded_by_whitespace = true;
250-
251-
original_text
252-
.chars()
253-
.map(|original_char| {
254-
let converted_char = match original_char {
255-
'\'' => {
256-
if preceded_by_whitespace {
257-
'‘'
258-
} else {
259-
'’'
260-
}
261-
}
262-
'"' => {
263-
if preceded_by_whitespace {
264-
'“'
265-
} else {
266-
'”'
267-
}
268-
}
269-
_ => original_char,
270-
};
271-
272-
preceded_by_whitespace = original_char.is_whitespace();
273-
274-
converted_char
275-
})
276-
.collect()
277-
}
278-
279212
/// Prints a "backtrace" of some `Error`.
280213
pub fn log_backtrace(e: &Error) {
281214
error!("Error: {}", e);
@@ -451,23 +384,4 @@ more text with spaces
451384
assert_eq!(normalize_id(""), "");
452385
}
453386
}
454-
455-
mod convert_quotes_to_curly {
456-
use super::super::convert_quotes_to_curly;
457-
458-
#[test]
459-
fn it_converts_single_quotes() {
460-
assert_eq!(convert_quotes_to_curly("'one', 'two'"), "‘one’, ‘two’");
461-
}
462-
463-
#[test]
464-
fn it_converts_double_quotes() {
465-
assert_eq!(convert_quotes_to_curly(r#""one", "two""#), "“one”, “two”");
466-
}
467-
468-
#[test]
469-
fn it_treats_tab_as_whitespace() {
470-
assert_eq!(convert_quotes_to_curly("\t'one'"), "\t‘one’");
471-
}
472-
}
473387
}

0 commit comments

Comments
 (0)