Example #1
0
// 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
}
Example #2
0
// 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)
	}
}
Example #3
0
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)
}
Example #4
0
// 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
}