Skip to content
Snippets Groups Projects
sync.go 7.71 KiB
package html

import (
	"fmt"
	"github.com/andybalholm/cascadia"
	"gitlab.schukai.com/oss/bob/types"
	"gitlab.schukai.com/oss/libraries/go/markup/html/engine"
	"golang.org/x/net/html"
	"gopkg.in/yaml.v3"
	"os"
	"path/filepath"
	"strings"
)

type Specification struct {
	Selector string
}

func readHTML(p string) (*html.Node, error) {
	htmlFile, err := os.Open(p)
	if err != nil {
		return nil, err
	}
	defer htmlFile.Close()

	return html.Parse(htmlFile)
}

type StringNodeMap map[string]*html.Node
type StringListNodeMap map[string][]*html.Node

func getSourceFileMap(specification types.SyncSpecification) (StringNodeMap, error) {
	sourceFiles := make(StringNodeMap)

	for _, r := range specification.Sync {

		source := r.Source

		absSource, err := filepath.Abs(source.Path)
		if err != nil {
			return nil, err
		}

		// if already read, skip
		if _, ok := sourceFiles[absSource]; ok {
			continue
		}

		if sourceFiles[absSource], err = readHTML(absSource); err != nil {
			return nil, err
		}

	}

	return sourceFiles, nil
}

func getDestinationFiles(specification types.SyncSpecification) (StringNodeMap, error) {

	fileMap := make(StringNodeMap)

	for _, r := range specification.Sync {

		for _, d := range r.Destination.Path {

			d, err := filepath.Abs(d)
			if err != nil {
				return nil, err
			}