Ejemplo n.º 1
0
func (pages *Pages) retrieve() error {
	if !pages.need {
		return nil
	}
	pages.need = false
	var pgs []*page.Page
	if len(pages.queries) > 0 {

		query := bleve.NewConjunctionQuery(pages.queries)
		total, uris, err := pages.ctx.app.pageIndexes.Query(pages.uri, query, pages.offset, pages.size)
		if err != nil {
			return err
		}

		for _, uri := range uris {
			pg := pages.ctx.get_page(uri)
			if pg != nil {
				pgs = append(pgs, pg)
			}
		}
		pages.total = total
	} else {
		datas := []interface{}{}
		for _, sfx := range pages.ctx.markdowns {
			vals, err := pages.ctx.app.pages.MultiGet(pages.uri, "."+sfx, pages.nested)
			if err != nil {
				return err
			}
			datas = append(datas, vals...)
		}

		for _, data := range datas {
			pg := page.NewPage("")
			if err := pg.SetData(data); err != nil {
				continue
			}
			pgs = append(pgs, pg)
		}
		pages.total = int64(len(pgs))
	}

	if pages.order != nil {
		sorter := page.PageSort{
			Field:  pages.order.Field,
			Ascend: pages.order.Ascend,
			Pages:  pgs,
		}
		sort.Sort(sorter)
	}

	pages.results = append(pages.results, pgs...)
	pages.ctx.Info("pages uri (%s) len(queries) (%d) total (%d) offset:size (%d:%d)", pages.uri, len(pages.queries), pages.total, pages.offset, pages.size)
	return nil
}
Ejemplo n.º 2
0
func (ctx *context) get_page(uri string) *page.Page {
	if pg, ok := ctx.app.cache.Get(uri); ok {
		defer ctx.app.cache.Set(uri, pg, ctx.cache_duration)
		return pg.(*page.Page)
	}

	pg := page.NewPage(uri)

	if val, err := ctx.app.pages.Get(uri, true); err == nil {
		if err := pg.SetData(val); err == nil {
			defer ctx.app.cache.Set(uri, pg, ctx.cache_duration)
			return pg
		}
	}

	if err := pg.Load(path.Join(ctx.app.Options.MarkdownRoot, uri)); err == nil {
		defer ctx.app.cache.Set(uri, pg, ctx.cache_duration)
		defer ctx.put_page(uri, pg)
		return pg
	}

	return nil
}
Ejemplo n.º 3
0
func (ctx *context) folder_pages(root, folder string, suffixes []string) error {
	stat, err := os.Stat(folder)
	if err != nil {
		return err
	}
	if !stat.IsDir() {
		return errors.New(folder + " is not folder")
	}
	if infos, err := ioutil.ReadDir(folder); err == nil {
		for _, info := range infos {
			if info.IsDir() {
				if err := ctx.folder_pages(path.Join(root, info.Name()),
					path.Join(folder, info.Name()), suffixes); err != nil {
					return err
				}
				continue
			}

			for _, suffix := range suffixes {
				if !util.HasSuffix(info.Name(), suffix) {
					continue
				}

				uri := path.Join(root, info.Name())
				fn := path.Join(folder, info.Name())
				pg := page.NewPage(uri)
				if err := pg.Load(fn); err != nil {
					return err
				}

				ctx.put_page(uri, pg)
			}
		}
	}
	return nil
}