microdata

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: Unlicense Imports: 7 Imported by: 0

README

microdata

A microdata parser in Go

See http://www.w3.org/TR/microdata/ for more information about Microdata

Build Status

Installation

Simply run

go get github.com/iand/microdata

Documentation is at http://godoc.org/github.com/iand/microdata

Usage

Example of parsing a string containing HTML:

package main

import (
    "github.com/iand/microdata"
    "net/url"
    "strings"
)

func main() {
    html := `<div itemscope>
        <p>My name is <span itemprop="name">Elizabeth</span>.</p>
    </div>`

    baseUrl, _ := url.Parse("http://example.com/")
    p := microdata.NewParser(strings.NewReader(html), baseUrl)

    data, err := p.Parse()
    if err != nil {
        panic(err)
    }

    println("Name: ", data.Items[0].Properties["name"][0].(string))
}

Extract microdata from a webpage and print the result as JSON

package main

import (
    "bytes"
    "io/ioutil"
    "net/http"
    "net/url"
    "os"

    "github.com/iand/microdata"
)

func main() {

    baseUrl, _ := url.Parse("http://www.designhive.com/blog/using-schemaorg-microdata")

    resp, _ := http.Get(baseUrl.String())
    defer resp.Body.Close()

    html, _ := ioutil.ReadAll(resp.Body)

    p := microdata.NewParser(bytes.NewReader(html), baseUrl)

    data, _ := p.Parse()

    json, _ := data.JSON()
    os.Stdout.Write(json)
}

Authors

Contributors

Contributing

  • Do submit your changes as a pull request
  • Do your best to adhere to the existing coding conventions and idioms.
  • Do run go fmt on the code before committing
  • Do feel free to add yourself to the CREDITS file and the corresponding Contributors list in the README.md. Alphabetical order applies.
  • Don't touch the AUTHORS file. An existing author will add you if your contributions are significant enough.
  • Do note that in order for any non-trivial changes to be merged (as a rule of thumb, additions larger than about 15 lines of code), an explicit Public Domain Dedication needs to be on record from you. Please include a copy of the statement found in the WAIVER file with your pull request

License

This is free and unencumbered software released into the public domain. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

Documentation

Overview

Package microdata provides types and functions for paring microdata from web pages. See http://www.w3.org/TR/microdata/ for more information about Microdata

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Properties propertyMap `json:"properties"`
	Types      []string    `json:"type,omitempty"`
	ID         string      `json:"id,omitempty"`
}

Item represents a microdata item

func NewItem

func NewItem() *Item

NewItem creates a new microdata item

func (*Item) AddItem

func (i *Item) AddItem(property string, value *Item)

AddItem adds an Item type item property value

func (*Item) AddString

func (i *Item) AddString(property string, value string)

AddString adds a string type item property value

func (*Item) AddType

func (i *Item) AddType(value string)

AddType adds a type to the item

type Microdata

type Microdata struct {
	Items []*Item `json:"items"`
}

Microdata represents a set of microdata items

func NewMicrodata

func NewMicrodata() *Microdata

NewMicrodata creates a new microdata set

func (*Microdata) AddItem

func (m *Microdata) AddItem(value *Item)

AddItem adds an item to the microdata set

func (*Microdata) JSON

func (m *Microdata) JSON() ([]byte, error)

JSON converts the microdata set to JSON

type Parser

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

Parser is an HTML parser that extracts microdata

func NewParser

func NewParser(r io.Reader, base *url.URL) *Parser

NewParser creates a new parser for extracting microdata r is a reader over an HTML document base is the base URL for resolving relative URLs

func (*Parser) Parse

func (p *Parser) Parse() (*Microdata, error)

Parse the document and return a Microdata set

Jump to

Keyboard shortcuts

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