Пример #1
0
func (*minify) Process(ctx goldsmith.Context, f goldsmith.File) error {
	var (
		buff bytes.Buffer
		err  error
	)

	switch m := min.New(); filepath.Ext(f.Path()) {
	case ".css":
		err = css.Minify(m, &buff, f, nil)
	case ".html", ".htm":
		err = html.Minify(m, &buff, f, nil)
	case ".js":
		err = js.Minify(m, &buff, f, nil)
	case ".json":
		err = json.Minify(m, &buff, f, nil)
	case ".svg":
		err = svg.Minify(m, &buff, f, nil)
	case ".xml":
		err = xml.Minify(m, &buff, f, nil)
	}

	if err != nil {
		return err
	}

	nf := goldsmith.NewFileFromData(f.Path(), buff.Bytes())
	nf.CopyValues(f)
	ctx.DispatchFile(nf)

	return nil
}
Пример #2
0
func (lay *layout) Finalize(ctx goldsmith.Context) error {
	for _, f := range lay.files {
		name, ok := f.Value(lay.layoutKey)
		if !ok {
			ctx.DispatchFile(f)
			continue
		}

		nameStr, ok := name.(string)
		if !ok {
			ctx.DispatchFile(f)
			continue
		}

		var buff bytes.Buffer
		if err := lay.tmpl.ExecuteTemplate(&buff, nameStr, f); err != nil {
			return err
		}

		nf := goldsmith.NewFileFromData(f.Path(), buff.Bytes())
		nf.CopyValues(f)
		ctx.DispatchFile(nf)
	}

	return nil
}
Пример #3
0
func (t *tags) buildPages(ctx goldsmith.Context, info map[string]tagInfo) (files []goldsmith.File) {
	for tag := range info {
		f := goldsmith.NewFileFromData(t.tagPagePath(tag), nil)
		f.SetValue(t.tagsKey, tagState{Index: tag, Info: t.info})
		for name, value := range t.indexMeta {
			f.SetValue(name, value)
		}

		files = append(files, f)
	}

	return
}
Пример #4
0
func (*frontmatter) Process(ctx goldsmith.Context, f goldsmith.File) error {
	meta, body, err := fm.Parse(f)
	if err != nil {
		return err
	}

	nf := goldsmith.NewFileFromData(f.Path(), body.Bytes())
	nf.CopyValues(f)
	for name, value := range meta {
		nf.SetValue(name, value)
	}
	ctx.DispatchFile(nf)

	return nil
}
Пример #5
0
func (l *livejs) Process(ctx goldsmith.Context, f goldsmith.File) error {
	doc, err := goquery.NewDocumentFromReader(f)
	if err != nil {
		return err
	}

	doc.Find("body").AppendHtml(l.js)

	html, err := doc.Html()
	if err != nil {
		return err
	}

	nf := goldsmith.NewFileFromData(f.Path(), []byte(html))
	ctx.DispatchFile(nf)

	return nil
}
Пример #6
0
func (idx *index) Finalize(ctx goldsmith.Context) error {
	for name, summary := range idx.dirs {
		sort.Sort(summary.entries)

		f := summary.index
		if f == nil {
			f = goldsmith.NewFileFromData(path.Join(name, idx.filename), make([]byte, 0))
			for name, value := range idx.meta {
				f.SetValue(name, value)
			}
		}

		f.SetValue(idx.filesKey, summary.entries)
		ctx.DispatchFile(f)
	}

	return nil
}
Пример #7
0
func (t *thumbnail) thumbnail(f goldsmith.File, thumbPath string) (goldsmith.File, error) {
	origImg, _, err := image.Decode(f)
	if err != nil {
		return nil, err
	}

	var thumbBuff bytes.Buffer
	thumbImg := resize.Thumbnail(t.dims, t.dims, origImg, resize.Bicubic)

	switch filepath.Ext(thumbPath) {
	case ".jpg", ".jpeg":
		err = jpeg.Encode(&thumbBuff, thumbImg, nil)
	case ".gif":
		err = gif.Encode(&thumbBuff, thumbImg, nil)
	case ".png":
		err = png.Encode(&thumbBuff, thumbImg)
	}

	return goldsmith.NewFileFromData(thumbPath, thumbBuff.Bytes()), nil
}
Пример #8
0
func (m *markdown) Process(ctx goldsmith.Context, f goldsmith.File) error {
	var buff bytes.Buffer
	if _, err := buff.ReadFrom(f); err != nil {
		return err
	}

	var data []byte
	switch m.mdType {
	case mdCommon:
		data = blackfriday.MarkdownCommon(buff.Bytes())
	case mdBasic:
		data = blackfriday.MarkdownBasic(buff.Bytes())
	}

	name := strings.TrimSuffix(f.Path(), path.Ext(f.Path())) + ".html"
	nf := goldsmith.NewFileFromData(name, data)
	nf.CopyValues(f)
	ctx.DispatchFile(nf)

	return nil
}
Пример #9
0
func (a *abs) Process(ctx goldsmith.Context, f goldsmith.File) error {
	doc, err := goquery.NewDocumentFromReader(f)
	if err != nil {
		return err
	}

	for _, attr := range a.attrs {
		path := fmt.Sprintf("*[%s]", attr)
		doc.Find(path).Each(func(index int, sel *goquery.Selection) {
			baseUrl, err := url.Parse(f.Path())
			val, _ := sel.Attr(attr)

			currUrl, err := url.Parse(val)
			if err != nil || currUrl.IsAbs() {
				return
			}

			currUrl = baseUrl.ResolveReference(currUrl)
			if a.baseUrl != nil {
				currUrl = a.baseUrl.ResolveReference(currUrl)
			}

			sel.SetAttr(attr, currUrl.String())
		})
	}

	html, err := doc.Html()
	if err != nil {
		return err
	}

	nf := goldsmith.NewFileFromData(f.Path(), []byte(html))
	nf.CopyValues(f)
	ctx.DispatchFile(nf)

	return nil
}