Skip to content

Commit

Permalink
render multi-line doc properly
Browse files Browse the repository at this point in the history
  • Loading branch information
eZioPan committed Apr 30, 2024
1 parent 1c198ae commit 481e9d5
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,27 @@ pub fn relative_path(a: &str, b: &str) -> TokenStream {
res
}

// If a document contains newline(either hardcode newline or escape sequence),
// convert it into multiple #[doc=""] sequence, and append 2 whitespace at end of each line (but not the last line),
// to make multiple valid markdown lines.
pub fn doc(doc: &Option<String>) -> TokenStream {
if let Some(doc) = doc {
let doc = doc.replace("\\n", "\n");
let doc = respace(&doc);
let doc = escape_brackets(&doc);
quote!(#[doc=#doc])
let mut doc = doc.replace("\\r", "\n");
doc = doc.replace("\\n", "\n");
let doc_lines: Vec<_> = doc.split('\n').skip_while(|v| v.is_empty()).collect();

let mut doc_token_stream = TokenStream::new();
for (index, content) in doc_lines.iter().enumerate() {
let mut content = respace(content);
content = escape_brackets(&content);
// save a lot of whitespace for one-line doc
if index != doc_lines.len() - 1 {
content.push_str(" ");
}
doc_token_stream.extend(quote!(#[doc=#content]));
}

doc_token_stream
} else {
quote!()
}
Expand Down

0 comments on commit 481e9d5

Please sign in to comment.