Debian Codegraph @

pkg/format/convert.go

raw symbols SCIP
1 package formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/
2
3 import (
4 "errorsscip-go gomod github.com/golang/go/src . errors/"
5 "fmtscip-go gomod github.com/golang/go/src . fmt/"
6 "htmlscip-go gomod github.com/golang/go/src . html/"
7 "regexpscip-go gomod github.com/golang/go/src . regexp/"
8 "strconvscip-go gomod github.com/golang/go/src . strconv/"
9 "stringsscip-go gomod github.com/golang/go/src . strings/"
10 "unicodescip-go gomod github.com/golang/go/src . unicode/"
11 )
12
13 // maxHeadingLevel is the maximum valid HTML heading level.
14 const maxHeadingLevelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/maxHeadingLevel. = 6
15
16 var (
17 markdownHeadingRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownHeadingRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`(?m)^#{1,6}\s+`)
18 markdownBoldRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownBoldRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`\*{1,2}(.+?)\*{1,2}`)
19 markdownItalicRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`_{1,2}(.+?)_{1,2}`)
20 markdownBoldHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownBoldHTMLRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`\*\*(.+?)\*\*`)
21 markdownItalicStarHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicStarHTMLRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`\*([^*].*?)\*`)
22 markdownItalicUnderHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicUnderHTMLRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`_([^_].*?)_`)
23 markdownStrongUnderHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownStrongUnderHTMLRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`__(.+?)__`)
24 markdownCodeRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownCodeRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().("`(.+?)`")
25 markdownLinkRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownLinkRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`\[(.+?)\]\(.+?\)`)
26 markdownImageRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownImageRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`!\[(.+?)\]\(.+?\)`)
27 markdownListRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownListRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`(?m)^\s*[-+*]\s+`)
28 markdownHRRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownHRRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`(?m)^\s*[-*_]{3,}\s*$`)
29 htmlTagRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlTagRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`<[^>]+>`)
30 mismatchedStrongEmRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/mismatchedStrongEmRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`<strong><em>(.+?)</strong></em>`)
31 mismatchedEmStrongRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/mismatchedEmStrongRegex. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().(`<em><strong>(.+?)</em></strong>`)
32 ErrUnsupportedFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrUnsupportedFormat. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("unsupported format conversion")
33 )
34
35 // ConvertFormat converts a message body from one format to another.
36 //
37 // Supported formats: "text", "markdown", "html".
38 // Conversions:
39 // - markdown → text: strips markdown formatting, returns plain text
40 // - html → text: strips HTML tags, returns plain text
41 // - text → markdown: escapes special characters for safe markdown inclusion
42 // - text → html: escapes HTML entities
43 // - markdown → html: converts basic markdown to HTML
44 // - html → markdown: converts basic HTML to markdown
45 // - any → same format: returns body unchanged
46 //
47 // Parameters:
48 // - body: the message body to convert.
49 // - from: the source format.
50 // - targetFormat: the desired output format.
51 //
52 // Returns:
53 // - string: the converted body.
54 // - error: an error if the conversion is unsupported.
55 func ConvertFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConvertFormat().(body, from, targetFormat string) (string, error) {
56 if from == targetFormat {
57 return body, nil
58 }
59
60 switch from {
61 case "markdown":
62 switch targetFormat {
63 case "text":
64 return markdownToTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownToText().(body), nil
65 case "html":
66 return markdownToHTMLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownToHTML().(body), nil
67 }
68 case "html":
69 switch targetFormat {
70 case "text":
71 return htmlToTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlToText().(body), nil
72 case "markdown":
73 return htmlToMarkdownscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlToMarkdown().(body), nil
74 }
75 case "text":
76 switch targetFormat {
77 case "markdown":
78 return textToMarkdownscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/textToMarkdown().(body), nil
79 case "html":
80 return textToHTMLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/textToHTML().(body), nil
81 }
82 }
83
84 return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %s → %s", ErrUnsupportedFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrUnsupportedFormat., from, targetFormat)
85 }
86
87 // markdownToText strips markdown formatting from the body and returns plain text.
88 //
89 // Parameters:
90 // - body: the markdown text.
91 //
92 // Returns:
93 // - string: the plain text.
94 func markdownToTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownToText().(body string) string {
95 const (
96 astPlaceholder = "\x00AST\x00"
97 undPlaceholder = "\x00UND\x00"
98 )
99
100 text := body
101 text = markdownHeadingRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownHeadingRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "")
102 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\*`, astPlaceholder)
103 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\_`, undPlaceholder)
104 text = markdownBoldRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownBoldRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "$1")
105 text = markdownItalicRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "$1")
106 text = markdownCodeRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownCodeRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "$1")
107 text = markdownImageRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownImageRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "$1")
108 text = markdownLinkRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownLinkRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "$1")
109 text = markdownListRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownListRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "")
110 text = markdownHRRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownHRRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "")
111 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, astPlaceholder, "*")
112 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, undPlaceholder, "_")
113 text = stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(text)
114
115 return text
116 }
117
118 // markdownToHTML converts basic markdown to HTML.
119 //
120 // Parameters:
121 // - body: the markdown text.
122 //
123 // Returns:
124 // - string: the HTML text.
125 func markdownToHTMLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownToHTML().(body string) string {
126 lines := stringsscip-go gomod github.com/golang/go/src . strings/.Splitscip-go gomod github.com/golang/go/src . strings/Split().(body, "\n")
127
128 var htmlBuilder stringsscip-go gomod github.com/golang/go/src . strings/.Builderscip-go gomod github.com/golang/go/src . strings/Builder#
129
130 for _, line := range lines {
131 trimmed := stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(line)
132 if trimmed == "" {
133 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("<br>")
134
135 continue
136 }
137
138 if stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(trimmed, "#") {
139 level := 0
140
141 for _, c := range trimmed {
142 if c == '#' {
143 level++
144 } else {
145 break
146 }
147 }
148
149 if level <= maxHeadingLevelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/maxHeadingLevel. && (level < len(trimmed) && unicodescip-go gomod github.com/golang/go/src . unicode/.IsSpacescip-go gomod github.com/golang/go/src . unicode/IsSpace().(rune(trimmed[level]))) {
150 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("<h")
151 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(strconvscip-go gomod github.com/golang/go/src . strconv/.Itoascip-go gomod github.com/golang/go/src . strconv/Itoa().(level))
152 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(">")
153
154 text := stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(trimmed[level:])
155 text = applyInlineFormattingscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/applyInlineFormatting().(text)
156 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(text)
157
158 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("</h")
159 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(strconvscip-go gomod github.com/golang/go/src . strconv/.Itoascip-go gomod github.com/golang/go/src . strconv/Itoa().(level))
160 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(">")
161
162 continue
163 }
164 }
165
166 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("<p>")
167
168 text := trimmed
169 text = applyInlineFormattingscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/applyInlineFormatting().(text)
170 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(text)
171 htmlBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("</p>")
172 }
173
174 return htmlBuilder.Stringscip-go gomod github.com/golang/go/src . strings/Builder#String().()
175 }
176
177 // htmlToText strips HTML tags from the body and returns plain text.
178 //
179 // Parameters:
180 // - body: the HTML text.
181 //
182 // Returns:
183 // - string: the plain text.
184 func htmlToTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlToText().(body string) string {
185 text := htmlTagRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlTagRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(body, "")
186 text = htmlscip-go gomod github.com/golang/go/src . html/.UnescapeStringscip-go gomod github.com/golang/go/src . html/UnescapeString().(text)
187 text = stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(text)
188
189 return text
190 }
191
192 // htmlToMarkdown converts basic HTML to markdown.
193 //
194 // Parameters:
195 // - body: the HTML text.
196 //
197 // Returns:
198 // - string: the markdown text.
199 func htmlToMarkdownscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlToMarkdown().(body string) string {
200 var lastHref string
201
202 text := htmlTagRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/htmlTagRegex..ReplaceAllStringFuncscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllStringFunc().(body, func(tag string) string {
203 lower := stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(tag)
204 switch {
205 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<br"):
206 return "\n"
207 case lower == "</strong>", lower == "</b>":
208 return "**"
209 case lower == "</em>", lower == "</i>":
210 return "_"
211 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h1"):
212 return "# "
213 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h2"):
214 return "## "
215 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h3"):
216 return "### "
217 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h4"):
218 return "#### "
219 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h5"):
220 return "##### "
221 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<h6"):
222 return "###### "
223 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<a") && (len(lower) > 2 && (lower[2] == ' ' || lower[2] == '>')):
224 if idx := stringsscip-go gomod github.com/golang/go/src . strings/.Indexscip-go gomod github.com/golang/go/src . strings/Index().(lower, `href="`); idx != -1 {
225 start := idx + len(`href="`)
226
227 end := stringsscip-go gomod github.com/golang/go/src . strings/.Indexscip-go gomod github.com/golang/go/src . strings/Index().(lower[start:], `"`)
228 if end != -1 {
229 lastHref = tag[start : start+end]
230 }
231 } else if idx := stringsscip-go gomod github.com/golang/go/src . strings/.Indexscip-go gomod github.com/golang/go/src . strings/Index().(lower, `href='`); idx != -1 {
232 start := idx + len(`href='`)
233
234 end := stringsscip-go gomod github.com/golang/go/src . strings/.Indexscip-go gomod github.com/golang/go/src . strings/Index().(lower[start:], `'`)
235 if end != -1 {
236 lastHref = tag[start : start+end]
237 }
238 }
239
240 return "["
241 case lower == "</a>":
242 href := lastHref
243 lastHref = ""
244
245 if href != "" {
246 return "](" + href + ")"
247 }
248
249 return "]"
250 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<strong") && (len(lower) > 7 && (lower[7] == '>' || lower[7] == ' ')),
251 stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<b") && (len(lower) > 2 && (lower[2] == '>' || lower[2] == ' ')):
252 return "**"
253 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<em") && (len(lower) > 3 && (lower[3] == '>' || lower[3] == ' ')),
254 stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<i") && (len(lower) > 2 && (lower[2] == '>' || lower[2] == ' ')):
255 return "_"
256 case lower == "</p>":
257 return "\n\n"
258 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h1"), stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h2"),
259 stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h3"), stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h4"),
260 stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h5"), stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</h6"):
261 return "\n"
262 case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "</"), stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "<!"):
263 return ""
264 default:
265 return ""
266 }
267 })
268
269 text = stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(text)
270 text = htmlscip-go gomod github.com/golang/go/src . html/.UnescapeStringscip-go gomod github.com/golang/go/src . html/UnescapeString().(text)
271
272 return text
273 }
274
275 // textToMarkdown escapes special characters in the body for safe markdown inclusion.
276 //
277 // Parameters:
278 // - body: the plain text.
279 //
280 // Returns:
281 // - string: the escaped markdown text.
282 func textToMarkdownscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/textToMarkdown().(body string) string {
283 text := stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(body, "_", "\\_")
284 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "*", "\\*")
285 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "`", "\\`")
286 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "[", "\\[")
287
288 return text
289 }
290
291 // textToHTML escapes HTML entities in the body.
292 //
293 // Parameters:
294 // - body: the plain text.
295 //
296 // Returns:
297 // - string: the HTML-escaped text.
298 func textToHTMLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/textToHTML().(body string) string {
299 return htmlscip-go gomod github.com/golang/go/src . html/.EscapeStringscip-go gomod github.com/golang/go/src . html/EscapeString().(body)
300 }
301
302 // applyInlineFormatting prepares text for emphasis conversion by protecting
303 // escaped delimiters, running emphasis conversion, then restoring literals.
304 //
305 // Parameters:
306 // - text: the markdown text.
307 //
308 // Returns:
309 // - string: the text with emphasis converted.
310 func applyInlineFormattingscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/applyInlineFormatting().(text string) string {
311 text = htmlscip-go gomod github.com/golang/go/src . html/.EscapeStringscip-go gomod github.com/golang/go/src . html/EscapeString().(text)
312 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\*`, "\x00AST\x00")
313 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\_`, "\x00UND\x00")
314 text = applyEmphasisscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/applyEmphasis().(text)
315 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "\x00AST\x00", "*")
316 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "\x00UND\x00", "_")
317
318 return text
319 }
320
321 // applyEmphasis converts markdown emphasis markers to HTML tags.
322 //
323 // It iteratively applies bold then italic regex passes until stable,
324 // then fixes the two possible mismatched-nesting patterns that can
325 // arise when emphasis markers straddle inserted tags.
326 //
327 // Parameters:
328 // - text: the markdown text.
329 //
330 // Returns:
331 // - string: the HTML text with emphasis tags.
332 func applyEmphasisscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/applyEmphasis().(text string) string {
333 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\*`, "\x00AST\x00")
334 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, `\_`, "\x00UND\x00")
335
336 changed := true
337 for changed {
338 changed = false
339
340 newText := markdownBoldHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownBoldHTMLRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "<strong>$1</strong>")
341 if newText != text {
342 changed = true
343 text = newText
344 }
345
346 newText = markdownItalicStarHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicStarHTMLRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "<em>$1</em>")
347 if newText != text {
348 changed = true
349 text = newText
350 }
351
352 newText = markdownStrongUnderHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownStrongUnderHTMLRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "<strong>$1</strong>")
353 if newText != text {
354 changed = true
355 text = newText
356 }
357
358 newText = markdownItalicUnderHTMLRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/markdownItalicUnderHTMLRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, "<em>$1</em>")
359 if newText != text {
360 changed = true
361 text = newText
362 }
363 }
364
365 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "\x00AST\x00", "*")
366 text = stringsscip-go gomod github.com/golang/go/src . strings/.ReplaceAllscip-go gomod github.com/golang/go/src . strings/ReplaceAll().(text, "\x00UND\x00", "_")
367
368 text = mismatchedStrongEmRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/mismatchedStrongEmRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, `<strong><em>$1</em></strong>`)
369 text = mismatchedEmStrongRegexscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/mismatchedEmStrongRegex..ReplaceAllStringscip-go gomod github.com/golang/go/src . regexp/Regexp#ReplaceAllString().(text, `<em><strong>$1</strong></em>`)
370
371 return text
372 }