@@ -168,63 +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+ if curly_quotes {
178+ opts. insert ( Options :: ENABLE_SMART_PUNCTUATION ) ;
179+ }
177180 Parser :: new_ext ( text, opts)
178181}
179182
180183pub fn render_markdown_with_path ( text : & str , curly_quotes : bool , path : Option < & Path > ) -> String {
181184 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) ;
184186 let events = p
185187 . map ( clean_codeblock_headers)
186- . map ( |event| adjust_links ( event, path) )
187- . map ( |event| converter. convert ( event) ) ;
188+ . map ( |event| adjust_links ( event, path) ) ;
188189
189190 html:: push_html ( & mut s, events) ;
190191 s
191192}
192193
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-
228194fn clean_codeblock_headers ( event : Event < ' _ > ) -> Event < ' _ > {
229195 match event {
230196 Event :: Start ( Tag :: CodeBlock ( CodeBlockKind :: Fenced ( ref info) ) ) => {
@@ -243,38 +209,6 @@ fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> {
243209 }
244210}
245211
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-
278212/// Prints a "backtrace" of some `Error`.
279213pub fn log_backtrace ( e : & Error ) {
280214 error ! ( "Error: {}" , e) ;
@@ -450,23 +384,4 @@ more text with spaces
450384 assert_eq ! ( normalize_id( "" ) , "" ) ;
451385 }
452386 }
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- }
472387}
0 commit comments