2019-12-21 14:27:38 -06:00
|
|
|
//! Transforms markdown
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-08-25 20:02:55 -05:00
|
|
|
const RUSTDOC_FENCE: &str = "```";
|
2021-01-01 10:31:32 -06:00
|
|
|
const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC: &[&str] = &[
|
|
|
|
"",
|
|
|
|
"rust",
|
|
|
|
"should_panic",
|
|
|
|
"ignore",
|
|
|
|
"no_run",
|
|
|
|
"compile_fail",
|
2021-04-19 04:41:45 -05:00
|
|
|
"allow_fail",
|
|
|
|
"test_harness",
|
2021-01-01 10:31:32 -06:00
|
|
|
"edition2015",
|
|
|
|
"edition2018",
|
|
|
|
"edition2021",
|
|
|
|
];
|
2020-08-25 20:02:55 -05:00
|
|
|
|
2019-07-29 19:46:38 -05:00
|
|
|
pub(crate) fn format_docs(src: &str) -> String {
|
2019-01-29 20:39:09 -06:00
|
|
|
let mut processed_lines = Vec::new();
|
|
|
|
let mut in_code_block = false;
|
2020-08-25 20:02:55 -05:00
|
|
|
let mut is_rust = false;
|
|
|
|
|
|
|
|
for mut line in src.lines() {
|
|
|
|
if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
|
2019-07-29 19:46:38 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:02:55 -05:00
|
|
|
if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
|
|
|
|
in_code_block ^= true;
|
2019-01-29 20:39:09 -06:00
|
|
|
|
2020-08-25 20:02:55 -05:00
|
|
|
if in_code_block {
|
2021-04-17 21:09:20 -05:00
|
|
|
is_rust =
|
2021-04-19 04:41:45 -05:00
|
|
|
header.split(',').any(|sub| is_rust_specific_code_block_attribute(sub.trim()));
|
2020-08-25 20:02:55 -05:00
|
|
|
|
|
|
|
if is_rust {
|
|
|
|
line = "```rust";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 20:39:09 -06:00
|
|
|
|
|
|
|
processed_lines.push(line);
|
|
|
|
}
|
2019-06-08 06:16:05 -05:00
|
|
|
processed_lines.join("\n")
|
2019-01-29 20:39:09 -06:00
|
|
|
}
|
|
|
|
|
2021-04-17 21:09:20 -05:00
|
|
|
fn is_rust_specific_code_block_attribute(attr: &str) -> bool {
|
|
|
|
if RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&attr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
attr.starts_with('E') && attr.len() == 5 && attr[1..].parse::<u32>().is_ok()
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:25:51 -05:00
|
|
|
fn code_line_ignored_by_rustdoc(line: &str) -> bool {
|
|
|
|
let trimmed = line.trim();
|
|
|
|
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
|
|
|
|
}
|
|
|
|
|
2019-01-29 20:39:09 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-07-29 19:46:38 -05:00
|
|
|
fn test_format_docs_adds_rust() {
|
2019-01-29 20:39:09 -06:00
|
|
|
let comment = "```\nfn some_rust() {}\n```";
|
2019-07-29 19:46:38 -05:00
|
|
|
assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:02:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_format_docs_handles_plain_text() {
|
|
|
|
let comment = "```text\nthis is plain text\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_handles_non_rust() {
|
|
|
|
let comment = "```sh\nsupposedly shell code\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_handles_rust_alias() {
|
|
|
|
let comment = "```ignore\nlet z = 55;\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_handles_complex_code_block_attrs() {
|
|
|
|
let comment = "```rust,no_run\nlet z = 55;\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
|
|
|
|
}
|
|
|
|
|
2021-04-17 21:09:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_format_docs_handles_error_codes() {
|
|
|
|
let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
|
|
|
|
assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:46:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_format_docs_skips_comments_in_rust_block() {
|
2019-07-30 13:25:51 -05:00
|
|
|
let comment =
|
|
|
|
"```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
|
2019-07-29 19:46:38 -05:00
|
|
|
assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:02:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_format_docs_does_not_skip_lines_if_plain_text() {
|
|
|
|
let comment =
|
|
|
|
"```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
|
|
|
|
assert_eq!(
|
|
|
|
format_docs(comment),
|
|
|
|
"```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:46:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_format_docs_keeps_comments_outside_of_rust_block() {
|
2019-07-30 13:25:51 -05:00
|
|
|
let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
|
2019-07-29 19:46:38 -05:00
|
|
|
assert_eq!(format_docs(comment), comment);
|
2019-01-29 20:39:09 -06:00
|
|
|
}
|
2019-07-31 09:46:15 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_docs_preserves_newlines() {
|
2021-01-08 08:41:08 -06:00
|
|
|
let comment = "this\nis\nmultiline";
|
2019-07-31 09:46:15 -05:00
|
|
|
assert_eq!(format_docs(comment), comment);
|
|
|
|
}
|
2019-08-03 17:01:48 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_code_blocks_in_comments_marked_as_rust() {
|
|
|
|
let comment = r#"```rust
|
|
|
|
fn main(){}
|
|
|
|
```
|
|
|
|
Some comment.
|
|
|
|
```
|
|
|
|
let a = 1;
|
|
|
|
```"#;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
format_docs(comment),
|
|
|
|
"```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
|
|
|
|
);
|
|
|
|
}
|
2020-08-25 20:02:55 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_code_blocks_in_comments_marked_as_text() {
|
|
|
|
let comment = r#"```text
|
|
|
|
filler
|
|
|
|
text
|
|
|
|
```
|
|
|
|
Some comment.
|
|
|
|
```
|
|
|
|
let a = 1;
|
|
|
|
```"#;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
format_docs(comment),
|
|
|
|
"```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
|
|
|
|
);
|
|
|
|
}
|
2019-01-29 20:39:09 -06:00
|
|
|
}
|