2019-07-30 00:46:38 +00:00
|
|
|
pub(crate) fn format_docs(src: &str) -> String {
|
2019-01-30 02:39:09 +00:00
|
|
|
let mut processed_lines = Vec::new();
|
|
|
|
let mut in_code_block = false;
|
2019-06-08 11:16:05 +00:00
|
|
|
for line in src.lines() {
|
2019-07-30 00:46:38 +00:00
|
|
|
if in_code_block && line.trim_start().starts_with("# ") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-30 02:39:09 +00:00
|
|
|
if line.starts_with("```") {
|
2019-06-08 11:16:05 +00:00
|
|
|
in_code_block ^= true
|
2019-01-30 02:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
|
2019-06-08 11:16:05 +00:00
|
|
|
"```rust"
|
2019-01-30 02:39:09 +00:00
|
|
|
} else {
|
2019-06-08 11:16:05 +00:00
|
|
|
line
|
2019-01-30 02:39:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
processed_lines.push(line);
|
|
|
|
}
|
2019-06-08 11:16:05 +00:00
|
|
|
processed_lines.join("\n")
|
2019-01-30 02:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-07-30 00:46:38 +00:00
|
|
|
fn test_format_docs_adds_rust() {
|
2019-01-30 02:39:09 +00:00
|
|
|
let comment = "```\nfn some_rust() {}\n```";
|
2019-07-30 00:46:38 +00:00
|
|
|
assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_skips_comments_in_rust_block() {
|
|
|
|
let comment = "```rust\n # skip1\n# skip2\n#stay1\nstay2\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_keeps_comments_outside_of_rust_block() {
|
|
|
|
let comment = " # stay1\n# stay2\n#stay3\nstay4";
|
|
|
|
assert_eq!(format_docs(comment), comment);
|
2019-01-30 02:39:09 +00:00
|
|
|
}
|
|
|
|
}
|