diff options
author | Alan Pearce | 2024-06-13 20:51:49 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-13 20:52:16 +0200 |
commit | f690e8cb7a820b0685b98f83a6761cfc169487e4 (patch) | |
tree | 3102375c3c3c9461776a79082f4db9898c1eee5e /internal/builder/template.go | |
parent | 6b1697144e6261c09f1d2c254cf33d39f02ce92e (diff) | |
download | website-f690e8cb7a820b0685b98f83a6761cfc169487e4.tar.lz website-f690e8cb7a820b0685b98f83a6761cfc169487e4.tar.zst website-f690e8cb7a820b0685b98f83a6761cfc169487e4.zip |
hash style elements during build step
Diffstat (limited to 'internal/builder/template.go')
-rw-r--r-- | internal/builder/template.go | 42 |
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() |