Example #1
0
// 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
	}
}
Example #2
0
// ReadPages read pages files in srcDir/page
func ReadPages(ctx *Context) ([]*model.Page, error) {
	srcDir := ctx.SrcPageDir()
	if !com.IsDir(srcDir) {
		return nil, fmt.Errorf("pages directory '%s' is missing", srcDir)
	}
	log15.Info("Read|Pages|%s", srcDir)

	var (
		err      error
		pageMeta = make(map[string]*model.Page)
	)
	for t, f := range model.ShouldPageMetaFiles() {
		file := filepath.Join(ctx.SrcDir(), f)
		if !com.IsFile(file) {
			continue
		}
		pageMeta, err = model.NewPagesFrontMatter(file, t)
		if err != nil {
			return nil, err
		}
		log15.Debug("Read|PageMeta|%s", file)
		break
	}

	var pages []*model.Page
	for _, page := range pageMeta {
		if page.Node {
			pages = append(pages, page)
		}
	}
	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" {
			rel, _ := filepath.Rel(srcDir, p)
			rel = strings.TrimSuffix(rel, filepath.Ext(rel))
			metaKey := strings.TrimPrefix(p, filepath.ToSlash(srcDir+"/"))
			log15.Debug("Read|%s|%v", p, pageMeta[metaKey] != nil)
			page, err := model.NewPageOfMarkdown(p, filepath.ToSlash(rel), pageMeta[metaKey])
			if err != nil {
				log15.Warn("Read|Page|%s|%v", p, err)
				return nil
			} else if page != nil && !page.Draft {
				pages = append(pages, page)
			}
			if page.Draft == true {
				log15.Warn("Draft|%s", p)
			}
			if err = page.LoadJSON(ctx.SrcDir()); err != nil {
				log15.Warn("Read|Page|JSON|%s|%s", page.JSONFile, err.Error())
			} else {
				if page.JSONFile != "" {
					log15.Debug("Read|Page|JSON|%s", page.JSONFile)
				}
			}
		}
		return nil
	})
	return pages, err
}