-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_doc.go
215 lines (191 loc) · 5.4 KB
/
node_doc.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2017 Atelier Disko. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/russross/blackfriday"
"golang.org/x/net/html"
)
// A document file.
type NodeDoc struct {
// Absolute path to the document file.
path string
}
// An order number, as a hint for outside sorting mechanisms.
func (d NodeDoc) Order() uint64 {
return orderNumber(filepath.Base(d.path))
}
// The document's computed title with any ordering numbers and the
// extension stripped off, usually for display purposes.
func (d NodeDoc) Title() string {
base := filepath.Base(d.path)
return removeOrderNumber(strings.TrimSuffix(base, filepath.Ext(base)))
}
// HTML as parsed from the underlying file.
//
// The provided set of prefix and node URL will be used to resolve
// relative source URLs and node URLs inside the documents, to
// i.e. make them absolute.
func (d NodeDoc) HTML(treePrefix string, nodeURL string, nodeGet NodeGetter) ([]byte, error) {
contents, err := ioutil.ReadFile(d.path)
if err != nil {
return nil, err
}
switch filepath.Ext(d.path) {
case ".md", ".markdown":
parsed, err := d.parseMarkdown(contents)
if err != nil {
return parsed, err
}
return d.postprocessHTML(parsed, treePrefix, nodeURL, nodeGet)
case ".html", ".htm":
return d.postprocessHTML(contents, treePrefix, nodeURL, nodeGet)
}
return nil, fmt.Errorf("Document not in a supported format: %s", prettyPath(d.path))
}
// Raw content of the underlying file.
func (d NodeDoc) Raw() ([]byte, error) {
contents, err := ioutil.ReadFile(d.path)
if err != nil {
return nil, err
}
return contents, nil
}
// Parses markdown into HTML and makes relative links absolute, so
// they are more portable.
func (d NodeDoc) parseMarkdown(contents []byte) ([]byte, error) {
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags &^ blackfriday.UseXHTML,
})
extensions := blackfriday.CommonExtensions |
blackfriday.Strikethrough | blackfriday.NoEmptyLineBeforeBlock&^
blackfriday.HeadingIDs&^blackfriday.DefinitionLists
return blackfriday.Run(
contents,
blackfriday.WithRenderer(renderer),
blackfriday.WithExtensions(extensions),
), nil
}
// Post-processes given HTML after it has been processed by i.e. the
// file-type specific parser.
//
// - Makes the HTML more portable, by turning relative source links
// into absolute ones.
//
// Works around buggy AbsolutePrefix feature in blackfriday. We
// need to have all source URLs absolute as documents can be placed
// anywhere inside the frontend's URL structure. The workaround can
// possibly be removed once PR #231 or a change functionally equal
// to it has been implemented.
//
// https://github.com/russross/blackfriday/pull/231
// https://github.com/russross/blackfriday/commit/27ba4cebef7f37e0bb5685e23cb7213cd809f9e8
// https://github.com/russross/blackfriday/commit/5c12499aa1ddda74561fb899c394f01fd1e8e9e6
//
// - Adds a title atttribute to node links
func (d NodeDoc) postprocessHTML(contents []byte, treePrefix string, nodeURL string, nodeGet NodeGetter) ([]byte, error) {
var buf bytes.Buffer
// Append slash to ensure last path element isn't recognized as a file.
treeBase, err := url.Parse(path.Join(treePrefix, nodeURL) + "/")
if err != nil {
return buf.Bytes(), err
}
nodeBase, err := url.Parse(nodeURL + "/")
if err != nil {
return buf.Bytes(), err
}
z := html.NewTokenizer(bytes.NewReader(contents))
// Helper to get an attribute value from a token.
attr := func(t html.Token, name string) (bool, int, string) {
for key, a := range t.Attr {
if a.Key == name {
return true, key, a.Val
}
}
return false, 0, ""
}
maybeMakeAbsolute := func(t html.Token) (html.Token, error) {
ok, key, v := attr(t, "src")
if !ok {
// No source to change.
return t, nil
}
u, err := url.Parse(v)
if err != nil {
return t, err
}
if u.IsAbs() {
return t, nil
}
t.Attr[key].Val = treeBase.ResolveReference(u).String()
return t, nil
}
maybeAddTitle := func(t html.Token) (html.Token, error) {
ok, _, v := attr(t, "title")
if ok && v != "" {
// Do not overwrite existing title.
return t, nil
}
ok, _, v = attr(t, "href")
if !ok {
// No URL to check at all.
return t, nil
}
u, err := url.Parse(v)
if err != nil {
return t, err
}
if u.Scheme != "" || u.Host != "" {
// Doesn't look like a node URL, save the lookup.
return t, nil
}
u = nodeBase.ResolveReference(u)
v = strings.TrimLeft(u.Path, "/")
ok, n, err := nodeGet(v)
if err != nil {
return t, err
}
if !ok {
return t, nil
}
t.Attr = append(t.Attr, html.Attribute{Key: "title", Val: n.Title()})
t.Attr = append(t.Attr, html.Attribute{Key: "data-node", Val: n.URL()})
return t, nil
}
for {
tt := z.Next()
t := z.Token()
switch tt {
case html.ErrorToken:
err := z.Err()
if err == io.EOF {
return buf.Bytes(), nil
}
return buf.Bytes(), err
case html.StartTagToken, html.SelfClosingTagToken:
switch t.Data {
case "img", "video":
t, err = maybeMakeAbsolute(t)
if err != nil {
return buf.Bytes(), err
}
case "a":
t, err = maybeAddTitle(t)
if err != nil {
return buf.Bytes(), err
}
}
}
buf.WriteString(t.String())
}
return buf.Bytes(), nil
}