2013-09-29 06:05:16 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
htmltran "code.google.com/p/go-html-transform/html/transform"
|
|
|
|
"fmt"
|
2013-10-01 21:26:21 +00:00
|
|
|
"io"
|
2013-09-29 06:05:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NavActive struct {
|
2013-10-01 21:26:21 +00:00
|
|
|
Section string
|
2013-10-01 19:51:00 +00:00
|
|
|
AttrName string
|
2013-09-29 06:05:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 21:42:08 +00:00
|
|
|
func (n *NavActive) Apply(w io.Writer, r io.Reader) (err error) {
|
2013-09-29 06:05:16 +00:00
|
|
|
var tr *htmltran.Transformer
|
|
|
|
|
|
|
|
if n.Section == "" {
|
|
|
|
_, err = io.Copy(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr, err = htmltran.NewFromReader(r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-01 19:51:00 +00:00
|
|
|
if n.AttrName == "" {
|
|
|
|
n.AttrName = "hugo-nav"
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:26:21 +00:00
|
|
|
err = tr.Apply(htmltran.ModifyAttrib("class", "active"), fmt.Sprintf("li[%s=%s]", n.AttrName, n.Section))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-09-29 06:05:16 +00:00
|
|
|
|
|
|
|
return tr.Render(w)
|
|
|
|
}
|