fix the html link parsing
This commit is contained in:
parent
e6dfd840c9
commit
b710162ba8
2 changed files with 53 additions and 1 deletions
|
|
@ -193,8 +193,58 @@ else
|
|||
quotebuf = quotebuf $0
|
||||
next
|
||||
}
|
||||
function linkify(line, result, pos, url, email, before, after, linktext) {
|
||||
result = line
|
||||
# First: Handle email<mailto:email> pattern - keep only one email as link
|
||||
while (match(result, /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}<mailto:[^&]*>/)) {
|
||||
before = substr(result, 1, RSTART-1)
|
||||
# Extract just the email part (before <mailto:)
|
||||
pos = index(substr(result, RSTART), "<mailto:")
|
||||
email = substr(result, RSTART, pos-1)
|
||||
after = substr(result, RSTART+RLENGTH)
|
||||
result = before "<a href=\"mailto:" email "\">" email "</a>" after
|
||||
}
|
||||
# Second: Handle standalone <mailto:email>
|
||||
while (match(result, /<mailto:[^&]+>/)) {
|
||||
before = substr(result, 1, RSTART-1)
|
||||
email = substr(result, RSTART+11, RLENGTH-15)
|
||||
after = substr(result, RSTART+RLENGTH)
|
||||
result = before "<a href=\"mailto:" email "\">" email "</a>" after
|
||||
}
|
||||
# Third: Handle text<https://url> - text becomes link text
|
||||
while (match(result, /[A-Za-z0-9_-]+<https?:\/\/[^&]+>/)) {
|
||||
before = substr(result, 1, RSTART-1)
|
||||
full = substr(result, RSTART, RLENGTH)
|
||||
# Find where < starts
|
||||
pos = index(full, "<")
|
||||
linktext = substr(full, 1, pos-1)
|
||||
# Extract URL: after < (4 chars) until > (4 chars at end)
|
||||
url = substr(full, pos+4, length(full)-pos-7)
|
||||
after = substr(result, RSTART+RLENGTH)
|
||||
result = before "<a href=\"" url "\" target=\"_blank\">" linktext "</a>" after
|
||||
}
|
||||
# Fourth: Handle standalone <https://url>
|
||||
while (match(result, /<https?:\/\/[^&]+>/)) {
|
||||
before = substr(result, 1, RSTART-1)
|
||||
url = substr(result, RSTART+4, RLENGTH-8)
|
||||
after = substr(result, RSTART+RLENGTH)
|
||||
result = before "<a href=\"" url "\" target=\"_blank\">" url "</a>" after
|
||||
}
|
||||
# Fifth: Handle plain URLs (not already in href)
|
||||
while (match(result, /https?:\/\/[^ &<>"\n\t]+/)) {
|
||||
before = substr(result, 1, RSTART-1)
|
||||
if (before ~ /href="$/) break
|
||||
url = substr(result, RSTART, RLENGTH)
|
||||
after = substr(result, RSTART+RLENGTH)
|
||||
# Clean trailing punctuation
|
||||
sub(/[.,;:!?)]+$/, "", url)
|
||||
result = before "<a href=\"" url "\" target=\"_blank\">" url "</a>" after
|
||||
}
|
||||
return result
|
||||
}
|
||||
{
|
||||
flush_quote()
|
||||
# HTML escape first
|
||||
gsub(/</, "\\<")
|
||||
gsub(/>/, "\\>")
|
||||
if (!incode) {
|
||||
|
|
@ -205,6 +255,8 @@ else
|
|||
after = substr($0, RSTART+RLENGTH)
|
||||
$0 = before "<code>" code "</code>" after
|
||||
}
|
||||
# Linkify URLs and emails
|
||||
$0 = linkify($0)
|
||||
}
|
||||
print
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ bind editor ^T complete
|
|||
|
||||
# Pager
|
||||
bind index,pager V edit-raw-message
|
||||
macro index,pager H "<pipe-message>~/dotfiles/bin/view-email-html<enter>" "View email in browser"
|
||||
macro index,pager H "<pipe-message>~/dotfiles/bin/view-email-html<enter><clear-flag>N<sync-mailbox>" "View email in browser"
|
||||
bind pager c imap-fetch-mail
|
||||
bind pager j next-line
|
||||
bind pager k previous-line
|
||||
|
|
|
|||
Loading…
Reference in a new issue