// List returns the articles found in the given directory in // the fs argument, recursively. func List(fs vfs.VFS, dir string) ([]*article.Article, error) { files, err := fs.ReadDir(dir) if err != nil { return nil, err } var articles []*article.Article for _, v := range files { p := path.Join(dir, v.Name()) if v.IsDir() { dirArticles, err := List(fs, p) if err != nil { return nil, err } articles = append(articles, dirArticles...) continue } if !extensions[strings.ToLower(path.Ext(p))] { // Not a recognized extension continue } article, err := article.Open(fs, p) if err != nil { return nil, err } articles = append(articles, article) } sortArticles(articles) return articles, nil }
// Setup configures a *build.Context to use the given VFS // as its filesystem. func Setup(ctx *build.Context, fs vfs.VFS) { ctx.JoinPath = path.Join ctx.SplitPathList = filepath.SplitList ctx.IsAbsPath = func(p string) bool { return p != "" && p[0] == '/' } ctx.IsDir = func(p string) bool { stat, err := fs.Stat(p) return err == nil && stat.IsDir() } ctx.HasSubdir = func(root, dir string) (string, bool) { root = path.Clean(root) if !strings.HasSuffix(root, separator) { root += separator } dir = path.Clean(dir) if !strings.HasPrefix(dir, root) { return "", false } return dir[len(root):], true } ctx.ReadDir = fs.ReadDir ctx.OpenFile = func(p string) (io.ReadCloser, error) { return fs.Open(p) } }
func vfsCreateWrite(fs vfs.VFS, filePath string) (vfs.WFile, error) { if err := vfs.MkdirAll(fs, path.Dir(filePath), 0700); err != nil { return nil, err } return fs.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) }
// Open returns a new Article loading it from the given filename in the given fs. func Open(fs vfs.VFS, filename string) (*Article, error) { f, err := fs.Open(filename) if err != nil { return nil, err } defer f.Close() article, err := New(f) if err != nil { return nil, err } article.Filename = filename return article, nil }