cli: Strip html comments
This commit is contained in:
parent
a4be8f4fa9
commit
22c7945e7b
|
|
@ -45,6 +45,35 @@ pub fn did(did: &Did) -> Paint<String> {
|
||||||
Paint::new(format!("{}…{}", &nid[..7], &nid[nid.len() - 7..]))
|
Paint::new(format!("{}…{}", &nid[..7], &nid[nid.len() - 7..]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Remove html style comments from a string.
|
||||||
|
///
|
||||||
|
/// The html comments must start at the beginning of a line and stop at the end.
|
||||||
|
pub fn strip_comments(s: &str) -> String {
|
||||||
|
let ends_with_newline = s.ends_with('\n');
|
||||||
|
let mut is_comment = false;
|
||||||
|
let mut w = String::new();
|
||||||
|
|
||||||
|
for line in s.lines() {
|
||||||
|
if is_comment {
|
||||||
|
if line.ends_with("-->") {
|
||||||
|
is_comment = false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else if line.starts_with("<!--") {
|
||||||
|
is_comment = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
w.push_str(line);
|
||||||
|
w.push('\n');
|
||||||
|
}
|
||||||
|
if !ends_with_newline {
|
||||||
|
w.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
w.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
/// Format a timestamp.
|
/// Format a timestamp.
|
||||||
pub fn timestamp(time: &Timestamp) -> Paint<String> {
|
pub fn timestamp(time: &Timestamp) -> Paint<String> {
|
||||||
let fmt = timeago::Formatter::new();
|
let fmt = timeago::Formatter::new();
|
||||||
|
|
@ -105,3 +134,61 @@ impl<'a> fmt::Display for Identity<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_strip_comments() {
|
||||||
|
let test = "\
|
||||||
|
commit 2\n\
|
||||||
|
\n\
|
||||||
|
<!--\n\
|
||||||
|
Please enter a comment for your patch update. Leaving this\n\
|
||||||
|
blank is also okay.\n\
|
||||||
|
-->";
|
||||||
|
let exp = "\
|
||||||
|
commit 2\n\
|
||||||
|
";
|
||||||
|
|
||||||
|
let res = strip_comments(test);
|
||||||
|
assert_eq!(exp, res);
|
||||||
|
|
||||||
|
let test = "\
|
||||||
|
commit 2\n\
|
||||||
|
-->";
|
||||||
|
let exp = "\
|
||||||
|
commit 2\n\
|
||||||
|
-->";
|
||||||
|
|
||||||
|
let res = strip_comments(test);
|
||||||
|
assert_eq!(exp, res);
|
||||||
|
|
||||||
|
let test = "\
|
||||||
|
<!--\n\
|
||||||
|
commit 2\n\
|
||||||
|
";
|
||||||
|
let exp = "";
|
||||||
|
|
||||||
|
let res = strip_comments(test);
|
||||||
|
assert_eq!(exp, res);
|
||||||
|
|
||||||
|
let test = "\
|
||||||
|
commit 2\n\
|
||||||
|
\n\
|
||||||
|
<!--\n\
|
||||||
|
<!--\n\
|
||||||
|
Please enter a comment for your patch update. Leaving this\n\
|
||||||
|
blank is also okay.\n\
|
||||||
|
-->\n\
|
||||||
|
-->";
|
||||||
|
let exp = "\
|
||||||
|
commit 2\n\
|
||||||
|
\n\
|
||||||
|
-->";
|
||||||
|
|
||||||
|
let res = strip_comments(test);
|
||||||
|
assert_eq!(exp, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ impl Message {
|
||||||
Message::Text(c) => Some(c),
|
Message::Text(c) => Some(c),
|
||||||
};
|
};
|
||||||
let comment = comment.unwrap_or_default();
|
let comment = comment.unwrap_or_default();
|
||||||
|
let comment = term::format::strip_comments(&comment);
|
||||||
let comment = comment.trim();
|
let comment = comment.trim();
|
||||||
|
|
||||||
Ok(comment.to_owned())
|
Ok(comment.to_owned())
|
||||||
|
|
@ -87,7 +88,6 @@ pub fn get_message(
|
||||||
let display_msg = default_msg.trim_end();
|
let display_msg = default_msg.trim_end();
|
||||||
|
|
||||||
let message = message.get(&format!("{display_msg}\n{PATCH_MSG}"))?;
|
let message = message.get(&format!("{display_msg}\n{PATCH_MSG}"))?;
|
||||||
let message = message.replace(PATCH_MSG.trim(), ""); // Delete help message.
|
|
||||||
|
|
||||||
let (title, description) = message.split_once('\n').unwrap_or((&message, ""));
|
let (title, description) = message.split_once('\n').unwrap_or((&message, ""));
|
||||||
let (title, description) = (title.trim().to_string(), description.trim().to_string());
|
let (title, description) = (title.trim().to_string(), description.trim().to_string());
|
||||||
|
|
@ -105,7 +105,6 @@ pub fn get_message(
|
||||||
/// Get a patch update message.
|
/// Get a patch update message.
|
||||||
pub fn get_update_message(message: term::patch::Message) -> io::Result<String> {
|
pub fn get_update_message(message: term::patch::Message) -> io::Result<String> {
|
||||||
let message = message.get(REVISION_MSG)?;
|
let message = message.get(REVISION_MSG)?;
|
||||||
let message = message.replace(REVISION_MSG.trim(), "");
|
|
||||||
let message = message.trim();
|
let message = message.trim();
|
||||||
|
|
||||||
Ok(message.to_owned())
|
Ok(message.to_owned())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue