diff --git a/README.md b/README.md index 1d354054708c87e229d10a39776c2790f0d8770d..fee68d4c42252d32e2d3de3f56ce37b5813e31f3 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Furthermore, it will parse the templates for images, anchors, and text. This will generate HTML files from the prepared templates. ```bash -bob template generate --input ./output/ --output ./output/ +bob html generate --input ./output/ --output ./output/ ``` ##### Sync @@ -53,7 +53,7 @@ bob template generate --input ./output/ --output ./output/ This will sync HTML nodes from a source to a destination. ```bash -bob template sync --specification ./specification.yaml +bob html sync --specification ./specification.yaml ``` The structure of the specification file is as follows: @@ -62,17 +62,19 @@ The structure of the specification file is as follows: sync: - source: path: './source.html' - selector: '#mainscript' + selector: 'head' destination: path: './' exclude: - ./source.html + keep: + - title ``` -The `source` is the source file and the `selector` is the selector to find the node to sync. -The `destination` is the destination directory and the `exclude` is a list of files to exclude. +With the above specification, the `head` node from `./source.html` will be synced to all files in `./` except `./source.html`. +Furthermore, the `title` node will be kept. -Relative paths are relative to the specification file. +Relative paths are relative to the specification file. Absolute paths are absolute, obviously. #### Cut diff --git a/application/source/html/sync.go b/application/source/html/sync.go index f4486e38bee3001e1c525b1925f821ac965d18de..9a2a84027d8fe3643813357579dcc9692976356e 100644 --- a/application/source/html/sync.go +++ b/application/source/html/sync.go @@ -44,7 +44,7 @@ func SyncHtml(p string) error { sourceFiles := make(map[string]*html.Node) destinationFiles := make(map[string]*html.Node) - sourceDestination := make(map[string][]string) + destinationFile := make(map[string][]string) for _, r := range specification.Sync { @@ -107,7 +107,7 @@ func SyncHtml(p string) error { } destinationFiles[pp] = dd - sourceDestination[absSource] = append(sourceDestination[absSource], pp) + destinationFile[absSource] = append(destinationFile[absSource], pp) } @@ -133,7 +133,7 @@ func SyncHtml(p string) error { } destinationFiles[d] = dd - sourceDestination[absSource] = append(sourceDestination[absSource], d) + destinationFile[absSource] = append(destinationFile[absSource], d) } @@ -154,13 +154,32 @@ func SyncHtml(p string) error { sourceNode := query.MatchFirst(sourceFiles[absSource]) - dp := sourceDestination[absSource] + dp := destinationFile[absSource] for _, d := range dp { destinationSelector := r.Destination.Selector if destinationSelector == "" { destinationSelector = sourceSelector } + keepMap := make(map[string][]*html.Node) + + for _, n := range r.Destination.Keep { + q, err := cascadia.Compile(n) + if err != nil { + return err + } + + kNode := q.MatchAll(destinationFiles[d]) + if kNode == nil { + return fmt.Errorf("keep node not found: %s", n) + } + + for _, k := range kNode { + keepMap[n] = append(keepMap[n], engine.CloneNode(k)) + } + + } + query, err := cascadia.Compile(destinationSelector) if err != nil { return err @@ -174,6 +193,34 @@ func SyncHtml(p string) error { n := engine.CloneNode(sourceNode) destinationData.Parent.InsertBefore(n, destinationData) destinationData.Parent.RemoveChild(destinationData) + + for sel, k := range keepMap { + cas, err := cascadia.Compile(sel) + if err != nil { + return err + } + x := cas.MatchAll(destinationFiles[d]) + if x == nil { + return fmt.Errorf("could not find selector %s in %s", sel, d) + } + + for _, n1 := range x { + + for _, kk := range k { + + // node already removed, for example, by a previous keep + if n1.Parent == nil { + continue + } + + n1.Parent.InsertBefore(kk, n1) + n1.Parent.RemoveChild(n1) + } + + } + + } + } } diff --git a/application/source/types/sync-specification.go b/application/source/types/sync-specification.go index bafd4b8cbd7199022e658a6170fc41cbd5d3038b..b6047c1d16b244670e97d83655414bda7bef75e3 100644 --- a/application/source/types/sync-specification.go +++ b/application/source/types/sync-specification.go @@ -4,9 +4,6 @@ import "gopkg.in/yaml.v3" type PathOrList []string -// func (p PathOrList) MarshalYAML() (interface{}, error) { -// return yaml.Marshal(p) -// } func (p *PathOrList) UnmarshalYAML(value *yaml.Node) error { var multi []string @@ -24,15 +21,6 @@ func (p *PathOrList) UnmarshalYAML(value *yaml.Node) error { return nil } -//func (p *PathOrList) UnmarshalYAML(b interface{}) error { -// var s string -// if err := yaml.Unmarshal(b, &s); err != nil { -// return err -// } -// -// return nil -//} - type Sync struct { Source struct { Selector string `yaml:"selector"` @@ -42,6 +30,7 @@ type Sync struct { Selector string `yaml:"selector"` Path PathOrList `yaml:"path"` Exclude []string `yaml:"exclude"` + Keep []string `yaml:"keep"` } `yaml:"destination"` } diff --git a/development/examples/example1/config/spec1.yaml b/development/examples/example1/config/spec1.yaml index b55ca45c847df4ec2101aab84dd1a5066e1e1957..588884439418acb1b7eb1b7c852d7e331cd125df 100644 --- a/development/examples/example1/config/spec1.yaml +++ b/development/examples/example1/config/spec1.yaml @@ -2,19 +2,23 @@ sync: - source: path: '../original/test1.html' - selector: '#mainscript' + selector: head destination: path: '../original/' exclude: - ../original/test1.html + keep: + - title + - meta[name=description] + - meta[name=author] - - source: - path: '../original/test1.html' - selector: '.deco' - destination: - path: '../original/' - exclude: - - ../original/test1.html +# - source: +# path: '../original/test1.html' +# selector: '.deco' +# destination: +# path: '../original/' +# exclude: +# - ../original/test1.html diff --git a/development/examples/example1/original/test1.html b/development/examples/example1/original/test1.html index 5b7e9acad8157b903b19feb079248e71a1c0b3e2..c458fdd5642efc3082bdfd4dea9bcb5ea7db0293 100644 --- a/development/examples/example1/original/test1.html +++ b/development/examples/example1/original/test1.html @@ -9,7 +9,7 @@ <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> - <meta name="robots" content="noindex,noarchive"/> + <meta name="robots" content="noindex"/> <link rel="icon" type="image/x-icon" href="/asset/icon/favicon.svg"/> <meta name="theme-color" content="#c10000"/> @@ -17,10 +17,10 @@ <link rel="icon" type="image/png" sizes="32x32" href="/asset/icon/favicon-32x32.png"/> <link rel="icon" type="image/png" sizes="16x16" href="/asset/icon/favicon-16x16.png"/> <link rel="mask-icon" href="/asset/icon/safari-pinned-tab.svg" color="#fd1d1d"/> - <meta name="msapplication-TileColor" content="#fd1d1d"/> <meta name="msapplication-TileImage" content="/asset/icon/mstile-144x144.png"/> + <meta name="msapplication-TileColor" content="#fd1d1d"/> - <title>Bad Request ME</title> + <title>TESTx</title> <meta name="description" content="The request was malformed or invalid."/> <meta name="author" content="schukai GmbH"/> diff --git a/development/examples/example1/original/test2.html b/development/examples/example1/original/test2.html index 3a4f24aa88a66a038aec5489009b3c393cae39fe..a5071acbfd9ea76049bc444ebf3bb7b6eb73419c 100644 --- a/development/examples/example1/original/test2.html +++ b/development/examples/example1/original/test2.html @@ -9,7 +9,7 @@ <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> - <meta name="robots" content="noindex,noarchive"/> + <meta name="robots" content="noindex"/> <link rel="icon" type="image/x-icon" href="/asset/icon/favicon.svg"/> <meta name="theme-color" content="#c10000"/> @@ -17,12 +17,12 @@ <link rel="icon" type="image/png" sizes="32x32" href="/asset/icon/favicon-32x32.png"/> <link rel="icon" type="image/png" sizes="16x16" href="/asset/icon/favicon-16x16.png"/> <link rel="mask-icon" href="/asset/icon/safari-pinned-tab.svg" color="#fd1d1d"/> - <meta name="msapplication-TileColor" content="#fd1d1d"/> <meta name="msapplication-TileImage" content="/asset/icon/mstile-144x144.png"/> + <meta name="msapplication-TileColor" content="#fd1d1d"/> - <title>Bad Request ME</title> - <meta name="description" content="The request was malformed or invalid."/> - <meta name="author" content="schukai GmbH"/> + <title>TEST2</title> + <meta name="description" content="The 2request was malformed or invalid."/> + <meta name="author" content="sch2ukai GmbH"/> <link rel="icon" href="/favicon.ico"/> <link rel="icon" href="/favicon.svg" type="image/svg+xml"/> diff --git a/development/examples/example1/original/test3.html b/development/examples/example1/original/test3.html index 3a4f24aa88a66a038aec5489009b3c393cae39fe..3cf2308c7082435e6f1398448929c0364191b6e6 100644 --- a/development/examples/example1/original/test3.html +++ b/development/examples/example1/original/test3.html @@ -9,7 +9,7 @@ <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> - <meta name="robots" content="noindex,noarchive"/> + <meta name="robots" content="noindex"/> <link rel="icon" type="image/x-icon" href="/asset/icon/favicon.svg"/> <meta name="theme-color" content="#c10000"/> @@ -17,12 +17,12 @@ <link rel="icon" type="image/png" sizes="32x32" href="/asset/icon/favicon-32x32.png"/> <link rel="icon" type="image/png" sizes="16x16" href="/asset/icon/favicon-16x16.png"/> <link rel="mask-icon" href="/asset/icon/safari-pinned-tab.svg" color="#fd1d1d"/> - <meta name="msapplication-TileColor" content="#fd1d1d"/> <meta name="msapplication-TileImage" content="/asset/icon/mstile-144x144.png"/> + <meta name="msapplication-TileColor" content="#fd1d1d"/> - <title>Bad Request ME</title> - <meta name="description" content="The request was malformed or invalid."/> - <meta name="author" content="schukai GmbH"/> + <title>TEST3</title> + <meta name="description" content="The 3request was malformed or invalid."/> + <meta name="author" content="schukai3 GmbH"/> <link rel="icon" href="/favicon.ico"/> <link rel="icon" href="/favicon.svg" type="image/svg+xml"/>