ogp

package module
v0.0.0-...-1041070 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 5 Imported by: 0

README

go-ogp

Go Reference

The Open Graph protocol parser for Go.

Example

Parse HTML
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/shota3506/go-ogp"
)

func main() {
	res, err := http.Get("https://ogp.me/")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	obj, err := ogp.Parse(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(obj.Title) // Open Graph protocol
}

Render HTML meta tags
package main

import (
	"os"

	"github.com/shota3506/go-ogp"
	"golang.org/x/net/html"
)

func main() {
	obj := &ogp.Object{
		Title: "Example",
		Type:  "website",
		Images: []*ogp.Image{
			{URL: "https://example.com/image.jpg"},
		},
		URL: "https://example.com",
	}

	head := &html.Node{Type: html.ElementNode, Data: "head"}
	for _, meta := range obj.HTML() {
		head.AppendChild(meta)
	}
	html.Render(os.Stdout, head)
}

Documentation

Overview

Package opg parses the Open Graph Protocol data from HTML documents.

The package follows the specification at [https://ogp.me/].

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Audio

type Audio struct {
	URL       string
	SecureURL string
	Type      string
}

An Audio is an audio file to accompany object.

type Image

type Image struct {
	URL       string
	SecureURL string
	Type      string
	Width     uint64
	Height    uint64
	Alt       string
}

An Image is an image which represents object within the graph.

type Locale

type Locale struct {
	Locale     string
	Alternates []string
}

type Metadata

type Metadata struct {
	Property string
	Content  string
}

type Object

type Object struct {
	Title       string
	Type        string
	Images      []*Image
	URL         string
	Audios      []*Audio
	Description string
	Determiner  string
	Locale      *Locale
	SiteName    string
	Videos      []*Video
}

Object represents the Open Graph protocol object.

func Parse

func Parse(r io.Reader) (*Object, error)

Parse parses the given io.Reader and returns the Open Graph protocol object.

Example
res, err := http.Get("https://ogp.me/")
if err != nil {
	log.Fatal(err)
}
defer res.Body.Close()

obj, err := ogp.Parse(res.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Println(obj.Title) // Open Graph protocol
Output:

func (*Object) HTML

func (o *Object) HTML() []*html.Node

HTML returns a slice of *html.Node that represents meta tags for the object.

type Video

type Video struct {
	URL       string
	SecureURL string
	Type      string
	Width     uint64
	Height    uint64
	Alt       string
}

A Video is a video that complements object.

Jump to

Keyboard shortcuts

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