Exemplo n.º 1
0
func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err error) {
	var baseURL, inURL *url.URL

	if baseURL, err = url.Parse(t.BaseURL); err != nil {
		return
	}

	replace := func(in string) string {
		if inURL, err = url.Parse(in); err != nil {
			return in + "?"
		}
		if fragmentOnly(inURL) {
			return in
		}
		return baseURL.ResolveReference(inURL).String()
	}

	for _, el := range selectors {
		if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil {
			return
		}
	}

	return
}
Exemplo n.º 2
0
func (t *AbsURL) Apply(w io.Writer, r io.Reader) (err error) {
	var tr *htmltran.Transformer

	if tr, err = htmltran.NewFromReader(r); err != nil {
		return
	}

	if err = t.absUrlify(tr, elattr{"a", "href"}, elattr{"script", "src"}); err != nil {
		return
	}

	return tr.Render(w)
}
Exemplo n.º 3
0
func (n *NavActive) Apply(w io.Writer, r io.Reader) (err error) {
	var tr *htmltran.Transformer

	if n.Section == "" {
		_, err = io.Copy(w, r)
		return
	}

	if tr, err = htmltran.NewFromReader(r); err != nil {
		return
	}

	if n.AttrName == "" {
		n.AttrName = "hugo-nav"
	}

	err = tr.Apply(htmltran.ModifyAttrib("class", "active"), fmt.Sprintf("li[%s=%s]", n.AttrName, n.Section))
	if err != nil {
		return
	}

	return tr.Render(w)
}