-
Notifications
You must be signed in to change notification settings - Fork 8
/
site.go
176 lines (152 loc) · 5.25 KB
/
site.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
package docsite
import (
"bytes"
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"os"
pathpkg "path"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/sourcegraph/docsite/markdown"
)
// VersionedFileSystem represents multiple versions of an http.FileSystem.
type VersionedFileSystem interface {
OpenVersion(ctx context.Context, version string) (http.FileSystem, error)
}
// Site represents a documentation site, including all of its templates, assets, and content.
type Site struct {
// Content is the versioned file system containing the Markdown files and assets (e.g., images)
// embedded in them.
Content VersionedFileSystem
// ContentExcludePattern is a regexp matching file paths to exclude in the content file system.
ContentExcludePattern *regexp.Regexp
// Base is the base URL (typically including only the path, such as "/" or "/help/") where the
// site is available.
Base *url.URL
// Templates is the file system containing the Go html/template templates used to render site
// pages
Templates http.FileSystem
// Assets is the file system containing the site-wide static asset files (e.g., global styles
// and logo).
Assets http.FileSystem
// AssetsBase is the base URL (sometimes only including the path, such as "/assets/") where the
// assets are available.
AssetsBase *url.URL
// Redirects contains a mapping from URL path to redirect destination URL.
Redirects map[string]*url.URL
// CheckIgnoreURLPattern is a regexp matching URLs to ignore in the Check method.
CheckIgnoreURLPattern *regexp.Regexp
}
// newContentPage creates a new ContentPage in the site.
func (s *Site) newContentPage(ctx context.Context, filePath string, data []byte, contentVersion string) (*ContentPage, error) {
var urlPathPrefix string
if contentVersion != "" {
urlPathPrefix = "/@" + contentVersion + "/"
}
urlPathPrefix = pathpkg.Join(urlPathPrefix, strings.TrimPrefix(pathpkg.Dir(filePath)+"/", "/"))
if urlPathPrefix != "" {
urlPathPrefix += "/"
}
base := s.Base
if base == nil {
base = &url.URL{Path: "/"}
}
path := contentFilePathToPath(filePath)
doc, err := markdown.Run(ctx, data, markdown.Options{
Base: base.ResolveReference(&url.URL{Path: urlPathPrefix}),
ContentFilePathToLinkPath: contentFilePathToPath,
Funcs: createMarkdownFuncs(s),
FuncInfo: markdown.FuncInfo{Version: contentVersion},
})
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("run Markdown for %s", filePath))
}
return &ContentPage{
Path: path,
FilePath: filePath,
Data: data,
Doc: *doc,
Breadcrumbs: makeBreadcrumbEntries(path),
}, nil
}
// AllContentPages returns a list of all content pages in the site.
func (s *Site) AllContentPages(ctx context.Context, contentVersion string) ([]*ContentPage, error) {
content, err := s.Content.OpenVersion(ctx, contentVersion)
if err != nil {
return nil, err
}
filter := func(path string) bool {
if !isContentPage(path) {
return false
}
return s.checkIsValidPath(path) == nil
}
var pages []*ContentPage
err = WalkFileSystem(content, filter, func(path string) error {
data, err := ReadFile(content, path)
if err != nil {
return err
}
page, err := s.newContentPage(ctx, path, data, contentVersion)
if err != nil {
return err
}
pages = append(pages, page)
return nil
})
return pages, err
}
// ResolveContentPage looks up the content page at the given version and path (which generally comes
// from a URL). The path may omit the ".md" file extension and the "/index" or "/index.md" suffix.
//
// If the resulting ContentPage differs from the path argument, the caller should (if possible)
// communicate a redirect.
func (s *Site) ResolveContentPage(ctx context.Context, contentVersion, path string) (*ContentPage, error) {
if err := s.checkIsValidPath(path); err != nil {
return nil, err
}
content, err := s.Content.OpenVersion(ctx, contentVersion)
if err != nil {
return nil, err
}
filePath, data, err := resolveAndReadAll(content, path)
if err != nil {
return nil, err
}
return s.newContentPage(ctx, filePath, data, contentVersion)
}
func (s *Site) checkIsValidPath(path string) error {
if s.ContentExcludePattern == nil || !s.ContentExcludePattern.MatchString(path) {
return nil
}
return &os.PathError{Op: "open", Path: path, Err: errors.New("path is excluded")}
}
// PageData is the data available to the HTML template used to render a page.
type PageData struct {
ContentVersion string // content version string requested
ContentPagePath string // content page path requested
ContentVersionNotFoundError bool // whether the requested version was not found
ContentPageNotFoundError bool // whether the requested content page was not found
// Content is the content page, when it is found.
Content *ContentPage
}
// RenderContentPage renders a content page using the template.
func (s *Site) RenderContentPage(page *PageData) ([]byte, error) {
tmpl, err := s.getTemplate(s.Templates, documentTemplateName, template.FuncMap{
"markdown": func(page ContentPage) template.HTML {
return template.HTML(page.Doc.HTML)
},
})
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, page); err != nil {
return nil, err
}
return buf.Bytes(), nil
}