about summary refs log tree commit diff stats
path: root/internal/builder/template.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/builder/template.go')
-rw-r--r--internal/builder/template.go42
1 files changed, 36 insertions, 6 deletions
diff --git a/internal/builder/template.go b/internal/builder/template.go
index ab36c85..bc31ad1 100644
--- a/internal/builder/template.go
+++ b/internal/builder/template.go
@@ -6,6 +6,7 @@ import (
 	"io"
 	"net/url"
 	"os"
+	"path/filepath"
 	"strings"
 	"sync"
 	"text/template"
@@ -29,6 +30,11 @@ var (
 	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 @@ func renderFeed(
 	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 @@ func renderFeedStyles() (io.Reader, error) {
 			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")
@@ -424,6 +425,35 @@ func renderFeedStyles() (io.Reader, error) {
 	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 {
 	r, w := io.Pipe()