-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_renderer.go
174 lines (148 loc) · 3.92 KB
/
page_renderer.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"fmt"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"gopkg.in/russross/blackfriday.v2"
"gopkg.in/src-d/go-git.v4/plumbing"
"io"
"net/url"
"path"
"regexp"
"strings"
)
var (
headlineRegex = regexp.MustCompile(`^(?i)h[0-9]$`)
linkRegex = regexp.MustCompile(`\[\[(.+)\|(.+)\]\]|\[\[(.+)\]\]`)
)
func htmlNodeExtractString(n *html.Node) (str string) {
var (
b strings.Builder
f func(*html.Node)
)
f = func(n *html.Node) {
if n.Type == html.TextNode {
b.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(n)
return b.String()
}
func htmlNodeGetAttributeByKey(n *html.Node, key string) (val string, err error) {
for _, attr := range n.Attr {
if attr.Key == key {
val = attr.Val
return
}
}
err = fmt.Errorf("Unknown attribute %s", key)
return
}
func (b *Bilbo) RenderPage(page *Page, commit plumbing.Hash) (err error) {
// Render the page according to its extension
ext := path.Ext(page.Filepath)
switch ext {
case ".md", ".markdown":
extensions := blackfriday.NoIntraEmphasis | blackfriday.Tables | blackfriday.FencedCode | blackfriday.Strikethrough | blackfriday.SpaceHeadings | blackfriday.AutoHeadingIDs | blackfriday.HeadingIDs | blackfriday.BackslashLineBreak | blackfriday.DefinitionLists
page.Rendered = blackfriday.Run(page.Source, blackfriday.WithExtensions(extensions))
default:
page.Rendered = page.Source
}
doc, err := html.Parse(bytes.NewReader(page.Rendered))
if err != nil {
return
}
firstHeadlineFound := false
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.DataAtom == atom.Body {
// blackfriday wraps the rendered HTML with <html><body> etc, remove it
n.DataAtom = atom.Article
n.Data = "article"
doc = n
}
if n.Type == html.ElementNode && !firstHeadlineFound && n.DataAtom == atom.H1 {
// Extract text from first headline node
firstHeadlineFound = true
page.Title = htmlNodeExtractString(n)
}
if n.Type == html.ElementNode && headlineRegex.MatchString(n.Data) {
// Prepend anchor link to headlines
id, err := htmlNodeGetAttributeByKey(n, "id")
if err != nil {
return
}
n.Attr = append(n.Attr, html.Attribute{Key: "class", Val: "a-hl"})
linkNode := &html.Node{
Type: html.ElementNode,
DataAtom: atom.A,
Data: "a",
Attr: []html.Attribute{
{Key: "class", Val: "a-hl__anchor"},
{Key: "href", Val: "#" + id},
{Key: "title", Val: "Set anchor to \"" + htmlNodeExtractString(n) + "\""},
},
}
linkNode.AppendChild(&html.Node{
Type: html.TextNode,
Data: "🔗",
})
n.InsertBefore(linkNode, n.FirstChild)
}
if n.Type == html.TextNode {
// Gollum-Style link tags https://github.com/gollum/gollum/wiki#link-tag
match := linkRegex.FindAllStringSubmatch(n.Data, 3)
if match != nil {
hasTitle := match[0][3] == ""
rawTitle := match[0][1]
if !hasTitle {
rawTitle = match[0][3]
}
rawLink := match[0][2]
if !hasTitle {
rawLink = match[0][3]
}
link, err := url.Parse(rawLink)
if err != nil {
return
}
classes := "external"
if !link.IsAbs() {
classes = "internal"
link.Path = normalizePageLink(link.Path, true)
if _, err := b.getPageAtCommit(link.Path, false, commit); err != nil {
classes = "internal absent"
}
}
linkNode := &html.Node{
Type: html.ElementNode,
DataAtom: atom.A,
Data: "a",
Attr: []html.Attribute{
{Key: "class", Val: classes},
{Key: "href", Val: link.String()},
},
}
linkNode.AppendChild(&html.Node{
Type: html.TextNode,
Data: rawTitle,
})
n.Parent.InsertBefore(linkNode, n)
n.Parent.RemoveChild(n)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
var buf bytes.Buffer
w := io.Writer(&buf)
html.Render(w, doc)
page.Rendered = buf.Bytes()
return
}