ud

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 4 Imported by: 0

README

Build Status codecov

ud - Utility for replacing elements by id within html files.

Quick start

go get -u github.com/gregoryv/ud

Replace an element by id

echo "<em>new thing</em>" | ud -w -i "someid" -html index.html

which is same as

echo '<em id="someid">new thing</em>' | ud -w -html index.html

Note! when -i flag is not given -c has no effect, it will always replace the identified element.

Replace content of element by id use the -c flag

echo "<em>new thing</em>" | ud -w -c -i "someid" -html index.html

Primary usecase

The primary reason for this tool was to simply generate and update image maps within html when working with graphviz documents

dot -Tcmapx somegraph.dot | ud -w -html index.html

Documentation

Overview

Package ud provides rewriters for search and replace.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HtmlRewriter

type HtmlRewriter struct {
	// contains filtered or unexported fields
}

HtmlRewriter replaces html tags or their content by id.

func NewHtmlRewriter

func NewHtmlRewriter(id string, child bool, fragment []byte) *HtmlRewriter

NewHtmlRewriter returns a html rewriter. The rewriter can be used multiple times.

func (*HtmlRewriter) Rewrite

func (hr *HtmlRewriter) Rewrite(w io.Writer, r io.Reader) error

Rewrite the incoming stream on r and write it to w.

Example (Child)
r := strings.NewReader(`
<html>
 <body>
  <h1>Title</h1>
  <div id="ABC">Old content</div>
 </body>
</html>
`)
hr := NewHtmlRewriter("ABC", true, []byte(`new stuff`))
hr.Rewrite(os.Stdout, r)
Output:

<html>
 <body>
  <h1>Title</h1>
  <div id="ABC">new stuff</div>
 </body>
</html>
Example (Element)
r := strings.NewReader(`
<html>
 <body>
  <h1>Title</h1>
  <div id="ABC">Old content</div>
 </body>
</html>
`)
hr := NewHtmlRewriter("ABC", false, []byte(`new stuff`))
hr.Rewrite(os.Stdout, r)
Output:

<html>
 <body>
  <h1>Title</h1>
  new stuff
 </body>
</html>

Directories

Path Synopsis
cmd
ud

Jump to

Keyboard shortcuts

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