@@ -168,63 +168,29 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
168
168
render_markdown_with_path ( text, curly_quotes, None )
169
169
}
170
170
171
- pub fn new_cmark_parser ( text : & str ) -> Parser < ' _ > {
171
+ pub fn new_cmark_parser ( text : & str , curly_quotes : bool ) -> Parser < ' _ > {
172
172
let mut opts = Options :: empty ( ) ;
173
173
opts. insert ( Options :: ENABLE_TABLES ) ;
174
174
opts. insert ( Options :: ENABLE_FOOTNOTES ) ;
175
175
opts. insert ( Options :: ENABLE_STRIKETHROUGH ) ;
176
176
opts. insert ( Options :: ENABLE_TASKLISTS ) ;
177
+ if curly_quotes {
178
+ opts. insert ( Options :: ENABLE_SMART_PUNCTUATION ) ;
179
+ }
177
180
Parser :: new_ext ( text, opts)
178
181
}
179
182
180
183
pub fn render_markdown_with_path ( text : & str , curly_quotes : bool , path : Option < & Path > ) -> String {
181
184
let mut s = String :: with_capacity ( text. len ( ) * 3 / 2 ) ;
182
- let p = new_cmark_parser ( text) ;
183
- let mut converter = EventQuoteConverter :: new ( curly_quotes) ;
185
+ let p = new_cmark_parser ( text, curly_quotes) ;
184
186
let events = p
185
187
. map ( clean_codeblock_headers)
186
- . map ( |event| adjust_links ( event, path) )
187
- . map ( |event| converter. convert ( event) ) ;
188
+ . map ( |event| adjust_links ( event, path) ) ;
188
189
189
190
html:: push_html ( & mut s, events) ;
190
191
s
191
192
}
192
193
193
- struct EventQuoteConverter {
194
- enabled : bool ,
195
- convert_text : bool ,
196
- }
197
-
198
- impl EventQuoteConverter {
199
- fn new ( enabled : bool ) -> Self {
200
- EventQuoteConverter {
201
- enabled,
202
- convert_text : true ,
203
- }
204
- }
205
-
206
- fn convert < ' a > ( & mut self , event : Event < ' a > ) -> Event < ' a > {
207
- if !self . enabled {
208
- return event;
209
- }
210
-
211
- match event {
212
- Event :: Start ( Tag :: CodeBlock ( _) ) => {
213
- self . convert_text = false ;
214
- event
215
- }
216
- Event :: End ( Tag :: CodeBlock ( _) ) => {
217
- self . convert_text = true ;
218
- event
219
- }
220
- Event :: Text ( ref text) if self . convert_text => {
221
- Event :: Text ( CowStr :: from ( convert_quotes_to_curly ( text) ) )
222
- }
223
- _ => event,
224
- }
225
- }
226
- }
227
-
228
194
fn clean_codeblock_headers ( event : Event < ' _ > ) -> Event < ' _ > {
229
195
match event {
230
196
Event :: Start ( Tag :: CodeBlock ( CodeBlockKind :: Fenced ( ref info) ) ) => {
@@ -243,38 +209,6 @@ fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> {
243
209
}
244
210
}
245
211
246
- fn convert_quotes_to_curly ( original_text : & str ) -> String {
247
- // We'll consider the start to be "whitespace".
248
- let mut preceded_by_whitespace = true ;
249
-
250
- original_text
251
- . chars ( )
252
- . map ( |original_char| {
253
- let converted_char = match original_char {
254
- '\'' => {
255
- if preceded_by_whitespace {
256
- '‘'
257
- } else {
258
- '’'
259
- }
260
- }
261
- '"' => {
262
- if preceded_by_whitespace {
263
- '“'
264
- } else {
265
- '”'
266
- }
267
- }
268
- _ => original_char,
269
- } ;
270
-
271
- preceded_by_whitespace = original_char. is_whitespace ( ) ;
272
-
273
- converted_char
274
- } )
275
- . collect ( )
276
- }
277
-
278
212
/// Prints a "backtrace" of some `Error`.
279
213
pub fn log_backtrace ( e : & Error ) {
280
214
error ! ( "Error: {}" , e) ;
@@ -450,23 +384,4 @@ more text with spaces
450
384
assert_eq ! ( normalize_id( "" ) , "" ) ;
451
385
}
452
386
}
453
-
454
- mod convert_quotes_to_curly {
455
- use super :: super :: convert_quotes_to_curly;
456
-
457
- #[ test]
458
- fn it_converts_single_quotes ( ) {
459
- assert_eq ! ( convert_quotes_to_curly( "'one', 'two'" ) , "‘one’, ‘two’" ) ;
460
- }
461
-
462
- #[ test]
463
- fn it_converts_double_quotes ( ) {
464
- assert_eq ! ( convert_quotes_to_curly( r#""one", "two""# ) , "“one”, “two”" ) ;
465
- }
466
-
467
- #[ test]
468
- fn it_treats_tab_as_whitespace ( ) {
469
- assert_eq ! ( convert_quotes_to_curly( "\t 'one'" ) , "\t ‘one’" ) ;
470
- }
471
- }
472
387
}
0 commit comments