Repository navigation
Expand file tree
/
Copy pathparser_inline.go
More file actions
79 lines (72 loc) · 2.85 KB
/
Copy pathparser_inline.go
File metadata and controls
79 lines (72 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"strings"
)
func quotesHandler() lineHandler {
return replaceEachAndRecurse(reQuotes, func(_ *parseContext, marker string) string {
switch marker {
case `%"`:
return laquo
case `"%`:
return raquo
case `%'`:
return lsaquo
case `'%`:
return rsaquo
default:
return marker
}
})
}
// linkHandler renders inline links [text](url) and [text](url "title") as
// external hyperlinks. Internal fragment links [text](#id) are handled
// earlier by anchorLinkHandler; images  by imageHandler. Matches
// inside backtick code spans are left untouched.
func linkHandler() lineHandler {
return lineHandler{
match: func(line string, _ bool) bool { return matchOutsideBackticks(line, reLink) },
handle: func(ctx *parseContext, line string, insideBlock bool) (string, bool) {
out := replaceOutsideBackticks(line, reLink, func(sub []string) string {
if sub[3] != "" {
return fmt.Sprintf(`<a href="%s" title="%s">%s</a>`, sub[2], sub[3], sub[1])
}
return fmt.Sprintf(`<a href="%s">%s</a>`, sub[2], sub[1])
})
return parseLine(ctx, out, insideBlock), true
},
}
}
func boldHandler() lineHandler {
return replaceEachAndRecurse(reBold, func(ctx *parseContext, inner string) string {
return "<b>" + parseLine(ctx, inner, true) + "</b>"
})
}
func italicHandler() lineHandler {
return replaceEachAndRecurse(reItalic, func(ctx *parseContext, inner string) string {
return "<i>" + parseLine(ctx, inner, true) + "</i>"
})
}
func codeSpanHandler() lineHandler {
return replaceEachAndRecurse(reCode, func(_ *parseContext, inner string) string {
// Encode spell-specific trigger characters so that downstream handlers
// cannot match content that was written as inline code. The backslash
// is encoded first (it is a backslash-escape trigger, and encoding it
// keeps escapes literal inside code); its replacement introduces no
// further trigger characters.
inner = strings.ReplaceAll(inner, "\\", "\")
inner = strings.ReplaceAll(inner, "%", "%")
inner = strings.ReplaceAll(inner, "{", "{")
inner = strings.ReplaceAll(inner, "}", "}")
inner = strings.ReplaceAll(inner, "[", "[")
inner = strings.ReplaceAll(inner, "]", "]")
return `<span class="code">` + inner + "</span>"
})
}
// Numeric character references, not named HTML entities: an EPUB3 XHTML document
// is parsed as XML, where only < > & " ' are predefined, so
// and friends are undeclared-entity errors under epubcheck.
func commentHandler() lineHandler { return replaceAndRecurse(reComment, "$1") }
func emDashHandler() lineHandler { return replaceAndRecurse(reLongDash, " — ") }
func enDashHandler() lineHandler { return replaceAndRecurse(reMidDash, " – ") }
func ellipsisHandler() lineHandler { return replaceAndRecurse(reThreeDots, "…") }