Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Strip html comments
Slack Coder committed 2 years ago
commit 22c7945e7be7de5f5dde45fc7a66bc49fd076f18
parent a4be8f4fa967f87ce8f724cf7449b69ee7200a5b
2 files changed +88 -2
modified radicle-cli/src/terminal/format.rs
@@ -45,6 +45,35 @@ pub fn did(did: &Did) -> Paint<String> {
    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.
pub fn timestamp(time: &Timestamp) -> Paint<String> {
    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);
+
    }
+
}
modified radicle-cli/src/terminal/patch.rs
@@ -31,6 +31,7 @@ impl Message {
            Message::Text(c) => Some(c),
        };
        let comment = comment.unwrap_or_default();
+
        let comment = term::format::strip_comments(&comment);
        let comment = comment.trim();

        Ok(comment.to_owned())
@@ -87,7 +88,6 @@ pub fn get_message(
    let display_msg = default_msg.trim_end();

    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) = (title.trim().to_string(), description.trim().to_string());
@@ -105,7 +105,6 @@ pub fn get_message(
/// Get a patch update message.
pub fn get_update_message(message: term::patch::Message) -> io::Result<String> {
    let message = message.get(REVISION_MSG)?;
-
    let message = message.replace(REVISION_MSG.trim(), "");
    let message = message.trim();

    Ok(message.to_owned())