Documentation
¶
Index ¶
- Constants
- func RemoveSelections(doc []byte, selectors []string) ([]byte, error)
- type Document
- func (d *Document) Attribute(selector, attribute string) []byte
- func (d *Document) Canonical() []byte
- func (d *Document) Description() []byte
- func (d *Document) HTML() []byte
- func (d *Document) InnerHTML(selector string) []byte
- func (d *Document) OpenGraph() *OpenGraph
- func (d *Document) OuterHTML(selector string) []byte
- func (d *Document) RemoveSelection(selector string)
- func (d *Document) RemoveSelections(selectors []string)
- func (d *Document) Text(selector string) []byte
- func (d *Document) Title() []byte
- type OpenGraph
Examples ¶
Constants ¶
const ( TitleSelector = "html head title" DescriptionSelector = "meta[name=\"description\" i][content]" IconSelector = "link[rel=\"icon\" i], link[rel=\"shortcut icon\" i]" KeywordsSelector = "meta[name=\"keywords\" i][content]" RobotsSelector = "meta[name=\"robots\" i][content]" RatingSelector = "meta[name=\"rating\" i][content]" CanonicalSelector = "link[rel=\"canonical\" i][href]" AlternateSelector = "link[rel=\"alternate\" i]" )
const ( OpenGraphSelector = "meta[property*=\"og:\" i][content]" // Selector to get every OpenGraph element OpenGraphURLSelector = "meta[property=\"og:url\" i][content]" OpenGraphSiteNameSelector = "meta[property=\"og:site_name\" i][content]" OpenGraphTitleSelector = "meta[property=\"og:title\" i][content]" OpenGraphDescriptionSelector = "meta[property=\"og:description\" i][content]" OpenGraphLocaleSelector = "meta[property=\"og:locale\" i][content]" OpenGraphTypeSelector = "meta[property=\"og:type\" i][content]" OpenGraphImageSelector = "meta[property=\"og:image\" i][content]" )
Variables ¶
This section is empty.
Functions ¶
func RemoveSelections ¶
RemoveSelections removes elements from HTML document doc that matches any of the selectors.
Example ¶
package main
import (
"fmt"
"git.gorbe.io/go/html"
)
func main() {
doc := []byte(`
<!DOCTYPE html>
<html>
<head>
<title>Test Title</title>
</head>
<body>
<h1>Body</h1>
<h2>Sub Body</h2>
<p class="removeme">Remove Me</p>
<p class="deleteme">Delete Me</p>
</body>
</html>`)
newDoc, err := html.RemoveSelections(doc, []string{".removeme", ".deleteme"})
if err != nil {
// Handle error
}
fmt.Printf("%s\n", newDoc)
}
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
func ParseDocument ¶
func (*Document) Attribute ¶
Attribute returns the content of attribute from the first element that match selector.
If attribute not exist, returns nil.
Example ¶
package main
import (
"fmt"
"git.gorbe.io/go/html"
)
func main() {
doc := []byte(`
<!DOCTYPE html>
<html>
<head>
<title>Test Title</title>
<link rel="canonical" href="https://example.com">
</head>
<body>
<h1>Body</h1>
<h2>Sub Body</h2>
</body>
</html>`)
newDoc, err := html.ParseDocument(doc)
if err != nil {
// Handle error
return
}
href := newDoc.Attribute("link[rel=\"canonical\"]", "href")
fmt.Printf("%s", href)
}
Output: https://example.com
func (*Document) Description ¶
func (*Document) HTML ¶
HTML returns the HTML of the Document d.
If cant render the HTML, returns nil.
func (*Document) InnerHTML ¶
InnerHTML returns the inner HTML of the first element that match selector.
If not found or cant render the HTML, returns nil.
func (*Document) OuterHTML ¶
OuterHTML returns the outer HTML of the first element that match selector.
If not found or cant render the HTML, returns nil.
func (*Document) RemoveSelection ¶
RemoveSelection removes elements from d Document that matches the selector.
func (*Document) RemoveSelections ¶
RemoveSelections removes elements from d Document that matches any of the selectors.