예제 #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 (t *thumbnail) Process(ctx goldsmith.Context, f goldsmith.File) error {
	defer ctx.DispatchFile(f)

	thumbPath, create := t.thumbName(f.Path())
	if !create {
		return nil
	}

	var (
		fn  goldsmith.File
		err error
	)

	if cached(ctx, f.Path(), thumbPath) {
		thumbPathDst := filepath.Join(ctx.DstDir(), thumbPath)
		fn, err = goldsmith.NewFileFromAsset(thumbPath, thumbPathDst)
		if err != nil {
			return err
		}
	} else {
		var err error
		fn, err = t.thumbnail(f, thumbPath)
		if err != nil {
			return err
		}
	}

	ctx.DispatchFile(fn)
	return nil
}
예제 #3
0
func (c *collection) Process(ctx goldsmith.Context, f goldsmith.File) error {
	c.mtx.Lock()
	defer func() {
		f.SetValue(c.groupsKey, c.groups)
		c.files = append(c.files, f)
		c.mtx.Unlock()
	}()

	coll, ok := f.Value(c.collKey)
	if !ok {
		return nil
	}

	var collStrs []string
	switch t := coll.(type) {
	case string:
		collStrs = append(collStrs, t)
	case []string:
		collStrs = append(collStrs, t...)
	}

	for _, collStr := range collStrs {
		files, _ := c.groups[collStr]
		files = append(files, f)
		c.groups[collStr] = files
	}

	return nil
}
예제 #4
0
func (t *breadcrumbs) Process(ctx goldsmith.Context, f goldsmith.File) error {
	var parentNameStr string
	if parentName, ok := f.Value(t.parentKey); ok {
		parentNameStr, _ = parentName.(string)
	}

	var nodeNameStr string
	if nodeName, ok := f.Value(t.nameKey); ok {
		nodeNameStr, _ = nodeName.(string)
	}

	t.mtx.Lock()
	defer t.mtx.Unlock()

	node := &bcNode{File: f, parentName: parentNameStr}
	t.allNodes = append(t.allNodes, node)

	if len(nodeNameStr) > 0 {
		if _, ok := t.namedNodes[nodeNameStr]; ok {
			return fmt.Errorf("duplicate node: %s", nodeNameStr)
		}

		t.namedNodes[nodeNameStr] = node
	}

	return nil
}
예제 #5
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
}
예제 #6
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
}
예제 #7
0
func (lay *layout) Process(ctx goldsmith.Context, f goldsmith.File) error {
	var buff bytes.Buffer
	if _, err := buff.ReadFrom(f); err != nil {
		return err
	}

	if _, ok := f.Value(lay.layoutKey); ok {
		f.SetValue(lay.contentKey, template.HTML(buff.Bytes()))

		lay.filesMtx.Lock()
		lay.files = append(lay.files, f)
		lay.filesMtx.Unlock()
	} else {
		ctx.DispatchFile(f)
	}

	return nil
}
예제 #8
0
func (idx *index) Process(ctx goldsmith.Context, f goldsmith.File) error {
	idx.dirsMtx.Lock()
	defer idx.dirsMtx.Unlock()

	curr := f.Path()
	leaf := true

	for {
		if handled, _ := idx.handled[curr]; handled {
			break
		}

		idx.handled[curr] = true

		dir := path.Dir(curr)
		base := path.Base(curr)

		summary, ok := idx.dirs[dir]
		if !ok {
			summary = new(dirSummary)
			idx.dirs[dir] = summary
		}

		if leaf {
			if base == idx.filename {
				summary.index = f
			} else {
				ctx.DispatchFile(f)
			}
		}

		entry := DirEntry{Name: base, Path: curr, IsDir: !leaf, File: f}
		summary.entries = append(summary.entries, entry)

		if dir == "." {
			break
		}

		curr = dir
		leaf = false
	}

	return nil
}
예제 #9
0
func (t *tags) Process(ctx goldsmith.Context, f goldsmith.File) error {
	tagState := &tagState{Info: t.info}

	t.mtx.Lock()
	defer func() {
		f.SetValue(t.stateKey, tagState)
		t.files = append(t.files, f)
		t.mtx.Unlock()
	}()

	tags, ok := f.Value(t.tagsKey)
	if !ok {
		return nil
	}

	tagsArr, ok := tags.([]interface{})
	if !ok {
		return nil
	}

	for _, tag := range tagsArr {
		tagStr, ok := tag.(string)
		if !ok {
			continue
		}

		tagState.Tags = append(tagState.Tags, tagStr)

		item, ok := t.info[tagStr]
		item.Files = append(item.Files, f)
		if !ok {
			item.SafeName = safeTag(tagStr)
			item.RawName = tagStr
			item.Path = t.tagPagePath(tagStr)
		}

		t.info[tagStr] = item
	}

	sort.Strings(tagState.Tags)
	return nil
}
예제 #10
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
}
예제 #11
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
}