Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit 5daaa2a

Browse files
committed
blog: use vfs.FileSystem
1 parent 1278dfc commit 5daaa2a

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

blog/blog.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"time"
2222

2323
"golang.org/x/tools/blog/atom"
24+
"golang.org/x/tools/godoc/vfs"
25+
"golang.org/x/tools/godoc/vfs/httpfs"
2426
"golang.org/x/tools/present"
2527
)
2628

2729
var validJSONPFunc = regexp.MustCompile(`(?i)^[a-z_][a-z0-9_.]*$`)
2830

2931
// Config specifies Server configuration values.
3032
type Config struct {
33+
RootFS vfs.FileSystem
3134
ContentPath string // Relative or absolute location of article files and related content.
3235
TemplatePath string // Relative or absolute location of template files.
3336

@@ -73,36 +76,47 @@ type Server struct {
7376
func NewServer(cfg Config) (*Server, error) {
7477
present.PlayEnabled = cfg.PlayEnabled
7578

76-
root := filepath.Join(cfg.TemplatePath, "root.tmpl")
77-
parse := func(name string) (*template.Template, error) {
78-
t := template.New("").Funcs(funcMap)
79-
return t.ParseFiles(root, filepath.Join(cfg.TemplatePath, name))
79+
parse := func(fs vfs.FileSystem, t *template.Template, filenames ...string) (*template.Template, error) {
80+
if t == nil {
81+
t = template.New(filenames[0]).Funcs(funcMap)
82+
} else {
83+
t = t.Funcs(funcMap)
84+
}
85+
for _, name := range filenames {
86+
data, err := vfs.ReadFile(fs, filepath.Join(cfg.TemplatePath, name))
87+
if err != nil {
88+
return nil, err
89+
}
90+
if _, err := t.Parse(string(data)); err != nil {
91+
return nil, err
92+
}
93+
}
94+
return t, nil
8095
}
8196

8297
s := &Server{cfg: cfg}
8398

8499
// Parse templates.
85100
var err error
86-
s.template.home, err = parse("home.tmpl")
101+
s.template.home, err = parse(s.cfg.RootFS, nil, "root.tmpl", "home.tmpl")
87102
if err != nil {
88103
return nil, err
89104
}
90-
s.template.index, err = parse("index.tmpl")
105+
s.template.index, err = parse(s.cfg.RootFS, nil, "root.tmpl", "index.tmpl")
91106
if err != nil {
92107
return nil, err
93108
}
94-
s.template.article, err = parse("article.tmpl")
109+
s.template.article, err = parse(s.cfg.RootFS, nil, "root.tmpl", "article.tmpl")
95110
if err != nil {
96111
return nil, err
97112
}
98-
p := present.Template().Funcs(funcMap)
99-
s.template.doc, err = p.ParseFiles(filepath.Join(cfg.TemplatePath, "doc.tmpl"))
113+
s.template.doc, err = parse(s.cfg.RootFS, present.Template(), "doc.tmpl")
100114
if err != nil {
101115
return nil, err
102116
}
103117

104118
// Load content.
105-
err = s.loadDocs(filepath.Clean(cfg.ContentPath))
119+
err = s.loadDocs(s.cfg.ContentPath)
106120
if err != nil {
107121
return nil, err
108122
}
@@ -118,11 +132,23 @@ func NewServer(cfg Config) (*Server, error) {
118132
}
119133

120134
// Set up content file server.
121-
s.content = http.StripPrefix(s.cfg.BasePath, http.FileServer(http.Dir(cfg.ContentPath)))
135+
s.content = http.StripPrefix(s.cfg.BasePath, http.FileServer(
136+
httpfs.New(getNameSpace(s.cfg.RootFS, s.cfg.ContentPath)),
137+
))
122138

123139
return s, nil
124140
}
125141

142+
func getNameSpace(fs vfs.FileSystem, ns string) vfs.NameSpace {
143+
newns := make(vfs.NameSpace)
144+
if ns != "" {
145+
newns.Bind("/", fs, ns, vfs.BindReplace)
146+
} else {
147+
newns.Bind("/", fs, "/", vfs.BindReplace)
148+
}
149+
return newns
150+
}
151+
126152
var funcMap = template.FuncMap{
127153
"sectioned": sectioned,
128154
"authors": authors,
@@ -171,16 +197,21 @@ func authorName(a present.Author) string {
171197
func (s *Server) loadDocs(root string) error {
172198
// Read content into docs field.
173199
const ext = ".article"
174-
fn := func(p string, info os.FileInfo, err error) error {
200+
fn := func(fs vfs.FileSystem, p string, info os.FileInfo, err error) error {
175201
if filepath.Ext(p) != ext {
176202
return nil
177203
}
178-
f, err := os.Open(p)
204+
f, err := fs.Open(p)
179205
if err != nil {
180206
return err
181207
}
182208
defer f.Close()
183-
d, err := present.Parse(f, p, 0)
209+
ctx := &present.Context{
210+
ReadFile: func(filename string) ([]byte, error) {
211+
return vfs.ReadFile(s.cfg.RootFS, filename)
212+
},
213+
}
214+
d, err := ctx.Parse(f, p, 0)
184215
if err != nil {
185216
return err
186217
}
@@ -199,7 +230,7 @@ func (s *Server) loadDocs(root string) error {
199230
})
200231
return nil
201232
}
202-
err := filepath.Walk(root, fn)
233+
err := Walk(s.cfg.RootFS, root, fn)
203234
if err != nil {
204235
return err
205236
}

blog/local.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"runtime"
1414

1515
"golang.org/x/tools/godoc/static"
16+
"golang.org/x/tools/godoc/vfs"
1617
"golang.org/x/tools/godoc/vfs/httpfs"
1718
"golang.org/x/tools/godoc/vfs/mapfs"
1819

@@ -25,8 +26,9 @@ const (
2526

2627
var cfg = blog.Config{
2728
Hostname: hostname,
28-
ContentPath: path.Join(runtime.GOROOT(), `/translations/blog/zh_CN/content`),
29-
TemplatePath: path.Join(runtime.GOROOT(), `/translations/blog/zh_CN/template`),
29+
RootFS: vfs.OS(path.Join(runtime.GOROOT(), `/translations/blog/zh_CN`)),
30+
ContentPath: "content",
31+
TemplatePath: "template",
3032
BaseURL: "//" + hostname,
3133
GodocURL: "//golang.org",
3234
HomeArticles: 5, // articles to display on the home page

0 commit comments

Comments
 (0)