|
| 1 | +package email |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "html" |
| 6 | + "strings" |
| 7 | + "text/template" |
| 8 | +) |
| 9 | + |
| 10 | +// StripHTML returns a version of a string with no HTML tags. |
| 11 | +func StripHTML(s string) string { |
| 12 | + output := "" |
| 13 | + |
| 14 | + // if we have a full html page we only need the body |
| 15 | + startBody := strings.Index(s, "<body") |
| 16 | + if startBody > -1 { |
| 17 | + endBody := strings.Index(s, "</body>") |
| 18 | + // try to find the end of the <body tag |
| 19 | + for i := startBody; i < endBody; i++ { |
| 20 | + if s[i] == '>' { |
| 21 | + startBody = i |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if startBody < endBody { |
| 27 | + s = s[startBody:endBody] |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Shortcut strings with no tags in them |
| 32 | + if !strings.ContainsAny(s, "<>") { |
| 33 | + output = s |
| 34 | + } else { |
| 35 | + // Removing line feeds |
| 36 | + s = strings.Replace(s, "\n", "", -1) |
| 37 | + |
| 38 | + // Then replace line breaks with newlines, to preserve that formatting |
| 39 | + s = strings.Replace(s, "</h1>", "\n\n", -1) |
| 40 | + s = strings.Replace(s, "</h2>", "\n\n", -1) |
| 41 | + s = strings.Replace(s, "</h3>", "\n\n", -1) |
| 42 | + s = strings.Replace(s, "</h4>", "\n\n", -1) |
| 43 | + s = strings.Replace(s, "</h5>", "\n\n", -1) |
| 44 | + s = strings.Replace(s, "</h6>", "\n\n", -1) |
| 45 | + s = strings.Replace(s, "</p>", "\n", -1) |
| 46 | + s = strings.Replace(s, "<br>", "\n", -1) |
| 47 | + s = strings.Replace(s, "<br/>", "\n", -1) |
| 48 | + s = strings.Replace(s, "<br />", "\n", -1) |
| 49 | + |
| 50 | + // Walk through the string removing all tags |
| 51 | + b := bytes.NewBufferString("") |
| 52 | + inTag := false |
| 53 | + for _, r := range s { |
| 54 | + switch r { |
| 55 | + case '<': |
| 56 | + inTag = true |
| 57 | + case '>': |
| 58 | + inTag = false |
| 59 | + default: |
| 60 | + if !inTag { |
| 61 | + b.WriteRune(r) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + output = b.String() |
| 66 | + } |
| 67 | + |
| 68 | + // Remove a few common harmless entities, to arrive at something more like plain text |
| 69 | + output = strings.Replace(output, "‘", "'", -1) |
| 70 | + output = strings.Replace(output, "’", "'", -1) |
| 71 | + output = strings.Replace(output, "“", "\"", -1) |
| 72 | + output = strings.Replace(output, "”", "\"", -1) |
| 73 | + output = strings.Replace(output, " ", " ", -1) |
| 74 | + output = strings.Replace(output, """, "\"", -1) |
| 75 | + output = strings.Replace(output, "'", "'", -1) |
| 76 | + |
| 77 | + // Translate some entities into their plain text equivalent (for example accents, if encoded as entities) |
| 78 | + output = html.UnescapeString(output) |
| 79 | + |
| 80 | + // In case we have missed any tags above, escape the text - removes <, >, &, ' and ". |
| 81 | + output = template.HTMLEscapeString(output) |
| 82 | + |
| 83 | + // After processing, remove some harmless entities &, ' and " which are encoded by HTMLEscapeString |
| 84 | + output = strings.Replace(output, """, "\"", -1) |
| 85 | + output = strings.Replace(output, "'", "'", -1) |
| 86 | + output = strings.Replace(output, "& ", "& ", -1) // NB space after |
| 87 | + output = strings.Replace(output, "&amp; ", "& ", -1) // NB space after |
| 88 | + |
| 89 | + return output |
| 90 | +} |
0 commit comments