Skip to content

Commit 64232f0

Browse files
committed
Recognize block comments in AsciiDoc; Add a dependency on regex
1 parent 546fc79 commit 64232f0

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

Cargo.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ categories = ["command-line-utilities", "text-processing"]
1515
[dependencies]
1616
clap = "2"
1717
colored = "2"
18+
regex = "1"
1819

1920

2021
[package.metadata.rpm.cargo]

src/module.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/// This module defines the `Module` struct, its builder struct, and methods on both structs.
22
use std::path::{Path, PathBuf};
33

4+
use regex::{Regex, RegexBuilder};
5+
46
use crate::Options;
57

68
/// All possible types of the AsciiDoc module
@@ -259,16 +261,29 @@ impl Input {
259261
}
260262

261263
// If comments are disabled via an option, delete comment lines from the content
262-
// TODO: This doesn't handle AsciiDoc comment blocks at all
263264
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();
272287
}
273288

274289
template_with_replacements

0 commit comments

Comments
 (0)