Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render multi-line doc properly #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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