File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -310,12 +310,28 @@ pub fn relative_path(a: &str, b: &str) -> TokenStream {
310310 res
311311}
312312
313+ // If a document contains newline(either hardcode newline or escape sequence),
314+ // convert each newline into a valid markdown linebreak (but not the last line end).
313315pub fn doc ( doc : & Option < String > ) -> TokenStream {
314316 if let Some ( doc) = doc {
315- let doc = doc. replace ( "\\ n" , "\n " ) ;
316- let doc = respace ( & doc) ;
317- let doc = escape_brackets ( & doc) ;
318- quote ! ( #[ doc=#doc] )
317+ let mut doc = doc. replace ( "\\ r" , "\n " ) ;
318+ doc = doc. replace ( "\\ n" , "\n " ) ;
319+
320+ let doc_lines: Vec < _ > = doc. split ( '\n' ) . skip_while ( |v| v. is_empty ( ) ) . collect ( ) ;
321+
322+ let mut markdown_doc = String :: new ( ) ;
323+
324+ for ( index, line) in doc_lines. iter ( ) . enumerate ( ) {
325+ let mut line = respace ( line) ;
326+ line = escape_brackets ( & line) ;
327+ // save a lot of whitespace for one-line doc
328+ if index != doc_lines. len ( ) - 1 {
329+ line. push_str ( " \n " ) ;
330+ }
331+ markdown_doc. push_str ( & line) ;
332+ }
333+
334+ quote ! ( #[ doc=#markdown_doc] )
319335 } else {
320336 quote ! ( )
321337 }
You can’t perform that action at this time.
0 commit comments