|
1 | 1 | /// This module defines the `Module` struct, its builder struct, and methods on both structs. |
2 | 2 | use std::path::{Path, PathBuf}; |
3 | 3 |
|
| 4 | +use regex::{Regex, RegexBuilder}; |
| 5 | + |
4 | 6 | use crate::Options; |
5 | 7 |
|
6 | 8 | /// All possible types of the AsciiDoc module |
@@ -259,16 +261,29 @@ impl Input { |
259 | 261 | } |
260 | 262 |
|
261 | 263 | // If comments are disabled via an option, delete comment lines from the content |
262 | | - // TODO: This doesn't handle AsciiDoc comment blocks at all |
263 | 264 | if !self.options.comments { |
264 | | - // Filter out comment lines in an iterator |
265 | | - let lines = template_with_replacements |
266 | | - .lines() |
267 | | - .filter(|line| !line.starts_with("//")); |
268 | | - // Connect the iterator back into a String, connecting with newlines |
269 | | - template_with_replacements = lines.collect::<Vec<&str>>().join("\n"); |
270 | | - // Add a final newline at the end of the file |
271 | | - template_with_replacements.push('\n'); |
| 265 | + // Delete multi-line (block) comments |
| 266 | + let multi_comments: Regex = RegexBuilder::new(r"^////[\s\S\n]*^////[\s]*\n") |
| 267 | + .multi_line(true) |
| 268 | + .swap_greed(true) |
| 269 | + .build() |
| 270 | + .unwrap(); |
| 271 | + template_with_replacements = multi_comments.replace(&template_with_replacements, "").to_string(); |
| 272 | + |
| 273 | + // Delete single-line comments |
| 274 | + let single_comments: Regex = RegexBuilder::new(r"^//.*\n") |
| 275 | + .multi_line(true) |
| 276 | + .swap_greed(true) |
| 277 | + .build() |
| 278 | + .unwrap(); |
| 279 | + template_with_replacements = single_comments.replace_all(&template_with_replacements, "").to_string(); |
| 280 | + |
| 281 | + // Delete leading white space left over by the deleted comments |
| 282 | + let leading_whitespace: Regex = RegexBuilder::new(r"^[\s\n]*") |
| 283 | + .multi_line(true) |
| 284 | + .build() |
| 285 | + .unwrap(); |
| 286 | + template_with_replacements = leading_whitespace.replace(&template_with_replacements, "").to_string(); |
272 | 287 | } |
273 | 288 |
|
274 | 289 | template_with_replacements |
|
0 commit comments