2019-06-08 14:16:05 +03:00
|
|
|
pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
|
2019-01-29 21:39:09 -05:00
|
|
|
let mut processed_lines = Vec::new();
|
|
|
|
let mut in_code_block = false;
|
2019-06-08 14:16:05 +03:00
|
|
|
for line in src.lines() {
|
2019-01-29 21:39:09 -05:00
|
|
|
if line.starts_with("```") {
|
2019-06-08 14:16:05 +03:00
|
|
|
in_code_block ^= true
|
2019-01-29 21:39:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
|
2019-06-08 14:16:05 +03:00
|
|
|
"```rust"
|
2019-01-29 21:39:09 -05:00
|
|
|
} else {
|
2019-06-08 14:16:05 +03:00
|
|
|
line
|
2019-01-29 21:39:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
processed_lines.push(line);
|
|
|
|
}
|
2019-06-08 14:16:05 +03:00
|
|
|
processed_lines.join("\n")
|
2019-01-29 21:39:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_codeblock_adds_rust() {
|
|
|
|
let comment = "```\nfn some_rust() {}\n```";
|
2019-06-08 14:16:05 +03:00
|
|
|
assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```");
|
2019-01-29 21:39:09 -05:00
|
|
|
}
|
|
|
|
}
|