all repos — website @ f690e8cb7a820b0685b98f83a6761cfc169487e4

My website

hash style elements during build step

Alan Pearce
commit

f690e8cb7a820b0685b98f83a6761cfc169487e4

parent

6b1697144e6261c09f1d2c254cf33d39f02ce92e

1 file changed, 36 insertions(+), 6 deletions(-)

changed files
M internal/builder/template.gointernal/builder/template.go
@@ -6,6 +6,7 @@ "fmt"
"io" "net/url" "os" + "path/filepath" "strings" "sync" "text/template"
@@ -29,6 +30,11 @@ css string
countHTML *goquery.Document liveReloadHTML *goquery.Document templates = make(map[string]*os.File) + 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", + } ) func loadTemplate(path string) (file *os.File, err error) {
@@ -391,7 +397,7 @@
return strings.NewReader(doc.OutputXML(true)), nil } -func renderFeedStyles() (io.Reader, error) { +func renderFeedStyles() (*strings.Reader, error) { reader, err := loadTemplate("templates/feed-styles.xsl") if err != nil { return nil, err
@@ -402,11 +408,6 @@ if err != nil {
panic("could not reset reader: " + err.Error()) } }() - 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", - } doc, err := xmlquery.Parse(reader) if err != nil { return nil, errors.Wrap(err, "could not parse XML")
@@ -422,6 +423,35 @@ Data: css,
}) return strings.NewReader(doc.OutputXML(true)), nil +} + +func getFeedStylesHash(r *strings.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.Wrap(err, "could not parse XPath") + } + style := xmlquery.QuerySelector(doc, expr) + + return hash(style.InnerText()), nil +} + +func getHTMLStyleHash(filenames ...string) (string, error) { + fn := filepath.Join(filenames...) + f, err := os.Open(fn) + if err != nil { + return "", err + } + defer f.Close() + doc, err := NewDocumentFromReader(f) + if err != nil { + return "", err + } + html := doc.Find("head > style").Text() + return hash(html), nil } func renderHTML(doc *goquery.Document) io.Reader {