-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdjot.go
More file actions
105 lines (84 loc) · 2.63 KB
/
djot.go
File metadata and controls
105 lines (84 loc) · 2.63 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"html"
"regexp"
"strings"
"unicode"
"github.com/sivukhin/godjot/djot_parser"
"github.com/sivukhin/godjot/html_writer"
)
func normalizeDTag(input string) string {
input = strings.ToLower(input)
input = strings.Join(strings.Fields(input), "-")
var sb strings.Builder
prevDash := false
for _, r := range input {
if r == '-' {
if !prevDash {
sb.WriteRune(r)
}
prevDash = true
continue
}
if unicode.IsLetter(r) || unicode.IsDigit(r) || r > 127 {
sb.WriteRune(r)
prevDash = false
}
}
result := strings.Trim(sb.String(), "-")
return result
}
func processWikilinks(djotInput string) string {
lines := strings.Split(djotInput, "\n")
defPattern := regexp.MustCompile(`^\[([^\]]+)\]:\s*(.+)$`)
definedRefs := make(map[string]bool)
for _, line := range lines {
line = strings.TrimSpace(line)
match := defPattern.FindStringSubmatch(line)
if match != nil {
ref := strings.TrimSpace(match[1])
definedRefs[ref] = true
}
}
implicitPattern := regexp.MustCompile(`\[([^\]]+)\]\[\]`)
explicitPattern := regexp.MustCompile(`\[([^\]]+)\]\[([^\]]+)\]`)
djotInput = implicitPattern.ReplaceAllStringFunc(djotInput, func(match string) string {
submatch := implicitPattern.FindStringSubmatch(match)
display := strings.TrimSpace(submatch[1])
if definedRefs[display] {
return match
}
normalized := normalizeDTag(display)
return `<span class="wikilink" title="wikilink to ` + html.EscapeString(normalized) + `">` + display + `</span>`
})
djotInput = explicitPattern.ReplaceAllStringFunc(djotInput, func(match string) string {
submatch := explicitPattern.FindStringSubmatch(match)
display := strings.TrimSpace(submatch[1])
ref := strings.TrimSpace(submatch[2])
if ref == "" {
if definedRefs[display] {
return match
}
normalized := normalizeDTag(display)
return `<span class="wikilink" title="wikilink to ` + html.EscapeString(normalized) + `">` + display + `</span>`
}
if definedRefs[ref] {
return match
}
normalized := normalizeDTag(ref)
return `<span class="wikilink" title="wikilink to ` + html.EscapeString(normalized) + `">` + display + `</span>`
})
return djotInput
}
var nostrLinkMatcher = regexp.MustCompile(`href="nostr:((npub|note|nevent|nprofile|naddr)1[a-z0-9]+)"`)
func djotToHTML(djotInput string) string {
djotInput = processWikilinks(djotInput)
ast := djot_parser.BuildDjotAst([]byte(djotInput))
result := djot_parser.NewConversionContext(
"html",
djot_parser.DefaultConversionRegistry,
nil,
).ConvertDjotToHtml(&html_writer.HtmlWriter{}, ast...)
result = nostrLinkMatcher.ReplaceAllString(result, `href="/$1"`)
return result
}