From 88e072c139da72db4903c33eebcb843e99b66ff8 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 24 Sep 2021 06:49:47 +0000 Subject: [PATCH] initial commit --- go.mod | 5 +++++ go.sum | 9 +++++++++ internal/merge.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/merge.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d0845a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.tilde.town/magical/mergehtml + +go 1.14 + +require golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..445d19a --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4= +golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/merge.go b/internal/merge.go new file mode 100644 index 0000000..904a878 --- /dev/null +++ b/internal/merge.go @@ -0,0 +1,50 @@ +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. +// +// +// - 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()) + } +}