// ReadPosts read posts files in srcDir/post func ReadPosts(ctx *Context) ([]*model.Post, error) { srcDir := ctx.SrcPostDir() if !com.IsDir(srcDir) { return nil, fmt.Errorf("posts directory '%s' is missing", srcDir) } log15.Info("Read|Posts|%s", srcDir) // try load post.toml or post.ini to read total meta file var ( err error postMeta = make(map[string]*model.Post) ) for t, f := range model.ShouldPostMetaFiles() { file := filepath.Join(ctx.SrcDir(), f) if !com.IsFile(file) { continue } postMeta, err = model.NewPostsFrontMatter(file, t) if err != nil { return nil, err } log15.Debug("Read|PostMeta|%s", file) break } var posts []*model.Post err = filepath.Walk(srcDir, func(p string, fi os.FileInfo, err error) error { if err != nil { return err } if fi.IsDir() { return nil } p = filepath.ToSlash(p) if filepath.Ext(p) == ".md" { metaKey := strings.TrimPrefix(p, filepath.ToSlash(srcDir+"/")) log15.Debug("Read|%s|%v", p, postMeta[metaKey] != nil) post, err := model.NewPostOfMarkdown(p, postMeta[metaKey]) if err != nil { log15.Warn("Read|Post|%s|%v", p, err) return nil } else if post != nil && !post.Draft { posts = append(posts, post) } if post.Draft == true { log15.Warn("Draft|%s", p) } } return nil }) sort.Sort(model.Posts(posts)) return posts, err }
// Sync copy assets to destination directory func Sync(ctx *Context) { if ctx.Err = ctx.Sync.SyncDir(ctx.Theme.StaticDir(), nil); ctx.Err != nil { return } opt := &sync.DirOption{ Filter: func(p string) bool { return path.Ext(p) != ".md" }, } var ignoreFiles []string opt.Prefix, _ = filepath.Rel(ctx.SrcDir(), ctx.SrcPostDir()) files := model.ShouldPostMetaFiles() for _, f := range files { ignoreFiles = append(ignoreFiles, f) } opt.Ignore = ignoreFiles if ctx.Err = ctx.Sync.SyncDir(ctx.SrcPostDir(), opt); ctx.Err != nil { return } ignoreFiles = []string{} files = model.ShouldPageMetaFiles() for _, f := range files { ignoreFiles = append(ignoreFiles, f) } opt.Ignore = ignoreFiles opt.Prefix = "" if ctx.Err = ctx.Sync.SyncDir(ctx.SrcPageDir(), opt); ctx.Err != nil { return } opt.Prefix, _ = filepath.Rel(ctx.SrcDir(), ctx.SrcMediaDir()) if ctx.Err = ctx.Sync.SyncDir(ctx.SrcMediaDir(), opt); ctx.Err != nil { return } opt.Ignore = []string{".git"} if ctx.Err = ctx.Sync.Clear(opt); ctx.Err != nil { return } }