|
1 | 1 | //! Rustdoc specific doc comment handling
|
2 | 2 |
|
| 3 | +use crate::documentation::Documentation; |
| 4 | + |
3 | 5 | // stripped down version of https://github.com/rust-lang/rust/blob/392ba2ba1a7d6c542d2459fb8133bebf62a4a423/src/librustdoc/html/markdown.rs#L810-L933
|
4 | 6 | pub fn is_rust_fence(s: &str) -> bool {
|
5 | 7 | let mut seen_rust_tags = false;
|
@@ -32,3 +34,170 @@ pub fn is_rust_fence(s: &str) -> bool {
|
32 | 34 |
|
33 | 35 | !seen_other_tags || seen_rust_tags
|
34 | 36 | }
|
| 37 | + |
| 38 | +const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"]; |
| 39 | + |
| 40 | +pub fn format_docs(src: &Documentation) -> String { |
| 41 | + format_docs_(src.as_str()) |
| 42 | +} |
| 43 | + |
| 44 | +fn format_docs_(src: &str) -> String { |
| 45 | + let mut processed_lines = Vec::new(); |
| 46 | + let mut in_code_block = false; |
| 47 | + let mut is_rust = false; |
| 48 | + |
| 49 | + for mut line in src.lines() { |
| 50 | + if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if let Some(header) = RUSTDOC_FENCES.into_iter().find_map(|fence| line.strip_prefix(fence)) |
| 55 | + { |
| 56 | + in_code_block ^= true; |
| 57 | + |
| 58 | + if in_code_block { |
| 59 | + is_rust = is_rust_fence(header); |
| 60 | + |
| 61 | + if is_rust { |
| 62 | + line = "```rust"; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if in_code_block { |
| 68 | + let trimmed = line.trim_start(); |
| 69 | + if is_rust && trimmed.starts_with("##") { |
| 70 | + line = &trimmed[1..]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + processed_lines.push(line); |
| 75 | + } |
| 76 | + processed_lines.join("\n") |
| 77 | +} |
| 78 | + |
| 79 | +fn code_line_ignored_by_rustdoc(line: &str) -> bool { |
| 80 | + let trimmed = line.trim(); |
| 81 | + trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t") |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_format_docs_adds_rust() { |
| 90 | + let comment = "```\nfn some_rust() {}\n```"; |
| 91 | + assert_eq!(format_docs_(comment), "```rust\nfn some_rust() {}\n```"); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_format_docs_handles_plain_text() { |
| 96 | + let comment = "```text\nthis is plain text\n```"; |
| 97 | + assert_eq!(format_docs_(comment), "```text\nthis is plain text\n```"); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_format_docs_handles_non_rust() { |
| 102 | + let comment = "```sh\nsupposedly shell code\n```"; |
| 103 | + assert_eq!(format_docs_(comment), "```sh\nsupposedly shell code\n```"); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_format_docs_handles_rust_alias() { |
| 108 | + let comment = "```ignore\nlet z = 55;\n```"; |
| 109 | + assert_eq!(format_docs_(comment), "```rust\nlet z = 55;\n```"); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_format_docs_handles_complex_code_block_attrs() { |
| 114 | + let comment = "```rust,no_run\nlet z = 55;\n```"; |
| 115 | + assert_eq!(format_docs_(comment), "```rust\nlet z = 55;\n```"); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_format_docs_handles_error_codes() { |
| 120 | + let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```"; |
| 121 | + assert_eq!(format_docs_(comment), "```rust\nlet b = 0 as *const _;\n```"); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_format_docs_skips_comments_in_rust_block() { |
| 126 | + let comment = |
| 127 | + "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```"; |
| 128 | + assert_eq!(format_docs_(comment), "```rust\n#stay1\nstay2\n```"); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_format_docs_does_not_skip_lines_if_plain_text() { |
| 133 | + let comment = |
| 134 | + "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```"; |
| 135 | + assert_eq!( |
| 136 | + format_docs_(comment), |
| 137 | + "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```", |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_format_docs_keeps_comments_outside_of_rust_block() { |
| 143 | + let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t"; |
| 144 | + assert_eq!(format_docs_(comment), comment); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_format_docs_preserves_newlines() { |
| 149 | + let comment = "this\nis\nmultiline"; |
| 150 | + assert_eq!(format_docs_(comment), comment); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_code_blocks_in_comments_marked_as_rust() { |
| 155 | + let comment = r#"```rust |
| 156 | +fn main(){} |
| 157 | +``` |
| 158 | +Some comment. |
| 159 | +``` |
| 160 | +let a = 1; |
| 161 | +```"#; |
| 162 | + |
| 163 | + assert_eq!( |
| 164 | + format_docs_(comment), |
| 165 | + "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```" |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn test_code_blocks_in_comments_marked_as_text() { |
| 171 | + let comment = r#"```text |
| 172 | +filler |
| 173 | +text |
| 174 | +``` |
| 175 | +Some comment. |
| 176 | +``` |
| 177 | +let a = 1; |
| 178 | +```"#; |
| 179 | + |
| 180 | + assert_eq!( |
| 181 | + format_docs_(comment), |
| 182 | + "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```" |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_format_docs_handles_escape_double_hashes() { |
| 188 | + let comment = r#"```rust |
| 189 | +let s = "foo |
| 190 | +## bar # baz"; |
| 191 | +```"#; |
| 192 | + |
| 193 | + assert_eq!(format_docs_(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```"); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn test_format_docs_handles_double_hashes_non_rust() { |
| 198 | + let comment = r#"```markdown |
| 199 | +## A second-level heading |
| 200 | +```"#; |
| 201 | + assert_eq!(format_docs_(comment), "```markdown\n## A second-level heading\n```"); |
| 202 | + } |
| 203 | +} |
0 commit comments