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 May 7, 2024
1 parent 305bd6e commit 9bd183d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ fn extract_peripheral(args: ExtractPeripheral) -> Result<()> {

// Descriptions in SVD's contain a lot of noise and weird formatting. Clean them up.
let description_cleanups = [
// Fix weird newline spam in descriptions.
(Regex::new("[ \n]+").unwrap(), " "),
// Fix weird tab and cr spam in descriptions.
(Regex::new("[\r\t]+").unwrap(), " "),
// Replace double-space (end of sentence) with period.
(
Regex::new(r"(?<first_sentence>.*?)[\s]{2}(?<next_sentence>.*)").unwrap(),
"$first_sentence. $next_sentence",
),
// Replace consecutive LF and whitespace with one newline/whitespace.
(Regex::new("([ \n]){2,}").unwrap(), "$1"),
// Replace (consecutive) CR with (one) LF.
(Regex::new("\r+").unwrap(), "\n"),
// Replace (consecutive) TAB with (one) whitespace
(Regex::new("\t+").unwrap(), " "),
// Make sure every description ends with a period.
(
Regex::new(r"(?<full_description>.*)(?<last_character>[\s'[^\.\s']])$").unwrap(),
Expand Down
24 changes: 20 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,28 @@ pub fn relative_path(a: &str, b: &str) -> TokenStream {
res
}

// If a document contains newline(either hardcode newline or escape sequence),
// convert each newline into a valid markdown linebreak (but not the last line end).
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 markdown_doc = String::new();

for (index, line) in doc_lines.iter().enumerate() {
let mut line = respace(line);
line = escape_brackets(&line);
// save a lot of whitespace for one-line doc
if index != doc_lines.len() - 1 {
line.push_str(" \n");
}
markdown_doc.push_str(&line);
}

quote!(#[doc=#markdown_doc])
} else {
quote!()
}
Expand Down

0 comments on commit 9bd183d

Please sign in to comment.