package builder import ( "encoding/xml" "io" "os" "path/filepath" "strings" "text/template" "website/internal/atom" "website/internal/config" "website/internal/content" "website/internal/log" "github.com/PuerkitoBio/goquery" "github.com/a-h/htmlformat" "github.com/antchfx/xmlquery" "github.com/antchfx/xpath" "github.com/pkg/errors" "golang.org/x/net/html" ) var ( css string templateFiles = 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 loadCSS() { bytes, err := os.ReadFile("templates/style.css") if err != nil { panic(err) } css = string(bytes) } func loadTemplate(path string) (file *os.File, err error) { if templateFiles[path] == nil { file, err = os.OpenFile(path, os.O_RDONLY, 0) if err != nil { return nil, errors.Wrapf(err, "could not load template at path %s", path) } templateFiles[path] = file } file = templateFiles[path] return } 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.Wrap(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.ParseFiles("templates/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.Reader, error) { reader, err := loadTemplate("templates/feed.xml") if err != nil { return nil, err } defer func() { _, err := reader.Seek(0, io.SeekStart) if err != nil { panic("could not reset reader: " + err.Error()) } }() doc, err := xmlquery.Parse(reader) if err != nil { return nil, errors.Wrap(err, "could not parse XML") } feed := doc.SelectElement("feed") feed.SelectElement("title").FirstChild.Data = title feed.SelectElement("link").SetAttr("href", config.BaseURL.String()) feed.SelectElement("id").FirstChild.Data = atom.MakeTagURI(config, specific) datetime, err := posts[0].Date.UTC().MarshalText() if err != nil { return nil, errors.Wrap(err, "could not convert post date to text") } feed.SelectElement("updated").FirstChild.Data = string(datetime) tpl := feed.SelectElement("entry") xmlquery.RemoveFromTree(tpl) for _, post := range posts { fullURL := config.BaseURL.JoinPath(post.URL).String() text, err := xml.MarshalIndent(&atom.FeedEntry{ Title: post.Title, Link: atom.MakeLink(fullURL), ID: atom.MakeTagURI(config, post.Basename), Updated: post.Date.UTC(), Summary: post.Description, Author: config.Title, Content: atom.FeedContent{ Content: post.Content, Type: "html", }, }, " ", " ") if err != nil { return nil, errors.Wrap(err, "could not marshal xml") } entry, err := xmlquery.ParseWithOptions( strings.NewReader(string(text)), xmlquery.ParserOptions{ Decoder: &xmlquery.DecoderOptions{ Strict: false, AutoClose: xml.HTMLAutoClose, Entity: xml.HTMLEntity, }, }, ) if err != nil { return nil, errors.Wrap(err, "could not parse XML") } xmlquery.AddChild(feed, entry.SelectElement("entry")) } return strings.NewReader(doc.OutputXML(true)), nil } func renderFeedStyles() (*strings.Reader, error) { reader, err := loadTemplate("templates/feed-styles.xsl") if err != nil { return nil, err } defer func() { _, err := reader.Seek(0, io.SeekStart) if err != nil { panic("could not reset reader: " + err.Error()) } }() doc, err := xmlquery.Parse(reader) if err != nil { return nil, errors.Wrap(err, "could not parse XML") } expr, err := xpath.CompileWithNS("//xhtml:style", nsMap) if err != nil { return nil, errors.Wrap(err, "could not parse XML") } style := xmlquery.QuerySelector(doc, expr) xmlquery.AddChild(style, &xmlquery.Node{ Type: xmlquery.TextNode, 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 { r, w := io.Pipe() go func() { _, err := w.Write([]byte("\n")) if err != nil { log.Error("error writing doctype", "error", err) w.CloseWithError(err) } err = htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) if err != nil { log.Error("error rendering html", "error", err) w.CloseWithError(err) return } defer w.Close() }() return r }