htmldiff

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2025 License: MIT Imports: 10 Imported by: 0

README

html-diff

Calculate difference between two HTML snippets and return those differences as a merged HTML snippet showing the changes.

Usage:

	var cfg = &htmldiff.Config{
		Granularity:  5,
		InsertedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: palegreen;"}},
		DeletedSpan:  []htmldiff.Attribute{{Key: "style", Val: "background-color: lightpink;"}},
		ReplacedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightskyblue;"}},
		CleanTags:    []string{""},
	}
	res, err := cfg.HTMLdiff([]string{previousHTML, latestHTML})
	mergedHTML := res[0]

see example_test.go

Only deals with body HTML, so no headers, only what is within the body element.

Requires Go1.5+, with vendoring support. Vendors "github.com/mb0/diff", "golang.org/x/net/html" and "golang.org/x/net/html/atom".

Running the tests will create output files in testout/*.html.

For fuzz-testing using https://github.com/dvyukov/go-fuzz , the Fuzz() function is in fuzz.go (as at Feb'16 you need to rename the vendor directory while you fuzz, and go get the dependencies - an issue with "go/importer", see https://github.com/golang/go/issues/13756).

Pull requests welcome.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	Namespace, Key, Val string
}

Attribute exists so that this package does not export html.Attribute, to allow vendoring of "golang.org/x/net/html".

type Config

type Config struct {
	Granularity                             int         // how many letters to put together for a change, if possible
	InsertedSpan, DeletedSpan, ReplacedSpan []Attribute // the attributes for the span tags wrapping changes
	CleanTags                               []string    // HTML tags to clean from the input
}

Config describes the way that HTMLdiff works.

func (*Config) HTMLdiff

func (c *Config) HTMLdiff(versions []string) ([]string, error)

HTMLdiff finds all the differences in the versions of HTML snippits, versions[0] is the original, all other versions are the edits to be compared. The resulting merged HTML snippits are as many as there are edits to compare.

Example
package main

import (
	"fmt"

	htmldiff "github.com/davidgraymi/html-diff"
)

func main() {
	previousHTML := `<p>Bullet list:</p><ul><li>first item</li><li>第二</li><li>3rd</li></ul>`
	latestHTML := `<p>Bullet <b>list:</b></p><ul><li>first item</li><li>number two</li><li>3rd</li></ul>`
	var cfg = &htmldiff.Config{
		Granularity:  5,
		InsertedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: palegreen;"}},
		DeletedSpan:  []htmldiff.Attribute{{Key: "style", Val: "background-color: lightpink;"}},
		ReplacedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightskyblue;"}},
		CleanTags:    []string{""},
	}

	res, err := cfg.HTMLdiff([]string{previousHTML, latestHTML})
	if err != nil {
		fmt.Println(err)
	}
	mergedHTML := res[0]

	fmt.Println(mergedHTML)
}
Output:

<p>Bullet <b><span style="background-color: lightskyblue;">list:</span></b></p><ul><li>first item</li><li><span style="background-color: lightpink;">第二</span><span style="background-color: palegreen;">number two</span></li><li>3rd</li></ul>

Directories

Path Synopsis
Package vizhtml provides a way to display html node trees for debug purposes.
Package vizhtml provides a way to display html node trees for debug purposes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL