all repos — homestead @ 143c1d889fb83fe3db5642e2f3ac6e15587929ab

Code for my website

internal/builder/template.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package builder

import (
	"bytes"
	"encoding/xml"
	"io"
	"strings"
	"text/template"

	"go.alanpearce.eu/website/internal/atom"
	"go.alanpearce.eu/website/internal/config"
	"go.alanpearce.eu/website/internal/content"
	"go.alanpearce.eu/website/templates"

	"github.com/PuerkitoBio/goquery"
	"github.com/antchfx/xmlquery"
	"github.com/antchfx/xpath"
	"gitlab.com/tozd/go/errors"
)

var (
	nsMap = map[string]string{
		"xsl":   "http://www.w3.org/1999/XSL/Transform",
		"atom":  "http://www.w3.org/2005/Atom",
		"xhtml": "http://www.w3.org/1999/xhtml",
	}
)

type QuerySelection struct {
	*goquery.Selection
}

type QueryDocument struct {
	*goquery.Document
}

func NewDocumentFromReader(r io.Reader) (*QueryDocument, error) {
	doc, err := goquery.NewDocumentFromReader(r)

	return &QueryDocument{doc}, errors.WithMessage(err, "could not create query document")
}

func (q *QueryDocument) Find(selector string) *QuerySelection {
	return &QuerySelection{q.Document.Find(selector)}
}

func renderRobotsTXT(config *config.Config) (io.Reader, error) {
	r, w := io.Pipe()
	tpl, err := template.ParseFS(templates.Files, "robots.tmpl")
	if err != nil {
		return nil, err
	}
	go func() {
		err = tpl.Execute(w, map[string]interface{}{
			"BaseURL": config.BaseURL,
		})
		if err != nil {
			w.CloseWithError(err)
		}
		w.Close()
	}()

	return r, nil
}

func renderFeed(
	title string,
	config *config.Config,
	posts []content.Post,
	specific string,
) (io.WriterTo, error) {
	buf := &bytes.Buffer{}
	datetime := posts[0].Date.UTC()

	buf.WriteString(xml.Header)
	err := atom.LinkXSL(buf, "/feed-styles.xsl")
	if err != nil {
		return nil, err
	}
	feed := &atom.Feed{
		Title:   title,
		Link:    atom.MakeLink(config.BaseURL.URL),
		ID:      atom.MakeTagURI(config, specific),
		Updated: datetime,
		Entries: make([]*atom.FeedEntry, len(posts)),
	}

	for i, post := range posts {
		feed.Entries[i] = &atom.FeedEntry{
			Title:   post.Title,
			Link:    atom.MakeLink(config.BaseURL.JoinPath(post.URL)),
			ID:      atom.MakeTagURI(config, post.Basename),
			Updated: post.Date.UTC(),
			Summary: post.Description,
			Author:  config.Title,
			Content: atom.FeedContent{
				Content: post.Content,
				Type:    "html",
			},
		}
	}
	enc := xml.NewEncoder(buf)
	err = enc.Encode(feed)
	if err != nil {
		return nil, err
	}

	return buf, nil
}

func renderFeedStyles() (*strings.Reader, error) {
	tpl, err := template.ParseFS(templates.Files, "feed-styles.xsl")
	if err != nil {
		return nil, err
	}

	esc := &strings.Builder{}
	err = xml.EscapeText(esc, []byte(templates.CSS))

	if err != nil {
		return nil, err
	}

	w := &strings.Builder{}
	err = tpl.Execute(w, map[string]interface{}{
		"css": esc.String(),
	})
	if err != nil {
		return nil, err
	}

	return strings.NewReader(w.String()), nil
}

func getFeedStylesHash(r io.Reader) (string, error) {
	doc, err := xmlquery.Parse(r)
	if err != nil {
		return "", err
	}
	expr, err := xpath.CompileWithNS("//xhtml:style", nsMap)
	if err != nil {
		return "", errors.WithMessage(err, "could not parse XPath")
	}
	style := xmlquery.QuerySelector(doc, expr)

	return hash(style.InnerText()), nil
}

func getHTMLStyleHash(r io.Reader) (string, error) {
	doc, err := NewDocumentFromReader(r)
	if err != nil {
		return "", err
	}
	html := doc.Find("head > style").Text()

	return hash(html), nil
}