mergehtml/internal/merge.go

51 lines
1.8 KiB
Go
Raw Normal View History

2021-09-24 06:49:47 +00:00
package internal
import (
"io"
"golang.org/x/net/html"
)
type Doc struct{}
type Assets struct {
Images []Asset
Styles []Asset
Scripts []Asset
}
// Merges an HTML document over top of a base template.
//
// <head>
// - <title> of the new doc overrides the base <title> unless the base
// contains {{content}}, in which case the new title is substituted
// - <style> elements from the new doc are placed after any base <style>
// - all other elements are appended to the base
//
// <body>
// - the <header> and <footer> and <main> elemnts are merged.
// in the base document, these elements may appear abitrarily nested inside
// wrapper elements; mergehtml will reproduce that structure in the merged document.
// if there are multiples, only the first will be used for merging.
// if they do not appear in the base doc, they will be appended to the body (header: prepended).
// in the source document, these elements will be removed from any
// containing elements before merging. if there are multiples their contents
// will be concatenated.
// html5 allows multiple <main> elements if all but one are hidden. mergehtml does not.
// - <script> elements are placed at the end of the body
// - all other elements are appended to <main> if it exists, or <body> if not.
//
// any classes attached to the <body>, <header>, <footer>, or <main> will be merged.
//
// all scripts and images will be collected and returned in the Assets object
//
func Merge(base, doc io.Reader) (merged string, assets *Assets, _ error)
func merge(base, doc *html.Node) (out *html.Node, assets *Assets) {
assets = new(Assets)
out, err = html.Parse("<!doctype html><html><head></head><body></body></html>")
if err != nil {
panic("mergehtml: internal error: " + err.Error())
}
}