///|
pub fn strip_html(input : String) -> String {
let mut out = ""
let mut in_tag = false
for ch in input {
if ch == '<' {
in_tag = true
} else if ch == '>' {
in_tag = false
out = out + " "
} else if !in_tag {
out = out + ch.to_string()
}
}
collapse_spaces(out)
}
///|
pub fn collapse_spaces(input : String) -> String {
let mut out = ""
let mut previous_space = false
for ch in input {
if is_space(ch) {
if !previous_space {
out = out + " "
}
previous_space = true
} else {
out = out + ch.to_string()
previous_space = false
}
}
trim_ascii(out)
}
///|
pub fn extract_urls(input : String) -> Array[String] {
let words = split_char(collapse_spaces(input), ' ')
let out : Array[String] = []
for word in words {
let cleaned = trim_url_punctuation(word)
if starts_with(cleaned, "http://") || starts_with(cleaned, "https://") {
out.push(cleaned)
}
}
out
}
///|
fn trim_url_punctuation(input : String) -> String {
let mut end = input.length()
while end > 0 {
let ch = input[end - 1]
if ch == '.' || ch == ',' || ch == ')' || ch == ']' || ch == '"' {
end = end - 1
} else {
return slice(input, 0, end)
}
}
""
}
///|
pub fn text_preview(input : String, limit : Int) -> String {
let text = collapse_spaces(input)
if text.length() <= limit {
text
} else {
slice(text, 0, limit) + "..."
}
}
///|
pub fn message_preview(message : MailMessage, limit : Int) -> String {
let plain = plain_text(message)
if plain != "" {
text_preview(plain, limit)
} else {
text_preview(strip_html(html_text(message)), limit)
}
}
///|
pub fn message_urls(message : MailMessage) -> Array[String] {
let out : Array[String] = []
for item in extract_urls(plain_text(message)) {
out.push(item)
}
for item in extract_urls(strip_html(html_text(message))) {
out.push(item)
}
out
}