Skip to content

Commit 9bd183d

Browse files
committed
render multi-line doc properly
1 parent 305bd6e commit 9bd183d

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/main.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,12 @@ fn extract_peripheral(args: ExtractPeripheral) -> Result<()> {
191191

192192
// Descriptions in SVD's contain a lot of noise and weird formatting. Clean them up.
193193
let description_cleanups = [
194-
// Fix weird newline spam in descriptions.
195-
(Regex::new("[ \n]+").unwrap(), " "),
196-
// Fix weird tab and cr spam in descriptions.
197-
(Regex::new("[\r\t]+").unwrap(), " "),
198-
// Replace double-space (end of sentence) with period.
199-
(
200-
Regex::new(r"(?<first_sentence>.*?)[\s]{2}(?<next_sentence>.*)").unwrap(),
201-
"$first_sentence. $next_sentence",
202-
),
194+
// Replace consecutive LF and whitespace with one newline/whitespace.
195+
(Regex::new("([ \n]){2,}").unwrap(), "$1"),
196+
// Replace (consecutive) CR with (one) LF.
197+
(Regex::new("\r+").unwrap(), "\n"),
198+
// Replace (consecutive) TAB with (one) whitespace
199+
(Regex::new("\t+").unwrap(), " "),
203200
// Make sure every description ends with a period.
204201
(
205202
Regex::new(r"(?<full_description>.*)(?<last_character>[\s'[^\.\s']])$").unwrap(),

src/util.rs

+20-4
Original file line numberDiff line numberDiff 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).
313315
pub 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
}

0 commit comments

Comments
 (0)