twittercard

package
v2.8.2 Latest Latest
Warning

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

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

Documentation

Overview

Package twittercard implements a way to represent Twitter Cards Markup data as typed Go structs, and render it as HTML meta tags, to customise how a page looks when shared on Twitter.

This is not a parser.

Example (App)
package main

import (
	"os"

	"github.com/tawesoft/golib/v2/html/meta/twittercard"
	"github.com/tawesoft/golib/v2/must"
)

func main() {
	app := twittercard.Card{
		Site:        twittercard.Account{Username: "tawesoft"},
		Title:       "Get the Kittens Game app!",
		Description: "My cat can even eat a whole watermelon.",
		Type:        twittercard.CardTypeApp,
		App: twittercard.CardApp{
			Country: "GB",
			Apps: []twittercard.App{
				{
					Store: twittercard.AppStoreIPad,
					Name:  "Kittens Game",
					ID:    "1198099725",
					Url:   "kittens-game://home",
				},
				{
					Store: twittercard.AppStoreGooglePlay,
					Name:  "Kittens Game",
					ID:    "com.nuclearunicorn.kittensgame",
				},
			},
		},
	}

	must.Result(os.Stdout.WriteString(`<!doctype html>
<html lang="en-gb">
    <head>
        <title>Twitter Card app example</title>
`))
	must.Check(app.Write(os.Stdout))
	must.Result(os.Stdout.WriteString(`</head><body>Test!</body></html>`))
}
Output:

Example (Player)
package main

import (
	"os"

	"github.com/tawesoft/golib/v2/html/meta/twittercard"
	"github.com/tawesoft/golib/v2/must"
)

func main() {
	video := twittercard.Video{
		Url:    "https://www.example.org/media/cat-photos/video-player.html",
		Width:  440,
		Height: 800,
		Streams: []twittercard.Media{
			{
				Url:  "https://www.example.org/media/cat-photos/video.mp4",
				Type: "video/mp4",
			},
		},
	}

	card := twittercard.Card{
		Site:        twittercard.Account{Username: "tawesoft"},
		Title:       "Top 10 reasons why I love my cat",
		Description: "My cat can even eat a whole watermelon.",
		Type:        twittercard.CardTypePlayer,
		Player: twittercard.CardPlayer{
			Video: video,
			Image: twittercard.Image{
				Url: "https://www.example.org/media/cat-photos/cat1.jpg",
				Alt: "A black and white cat (looking very cute) sitting on a blanket with a soft toy in mid-air.",
			},
		},
	}

	must.Result(os.Stdout.WriteString(`<!doctype html>
<html lang="en-gb">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Twitter Card video player example</title>
`))
	must.Check(card.Write(os.Stdout))
	must.Result(os.Stdout.WriteString(`</head><body>`))
	must.Check(video.Write(os.Stdout))
	must.Result(os.Stdout.WriteString(`</body></html>`))
}
Output:

Example (Summary)
package main

import (
	"os"

	"github.com/tawesoft/golib/v2/html/meta/twittercard"
	"github.com/tawesoft/golib/v2/must"
)

func main() {
	card := twittercard.Card{
		Site:        twittercard.Account{Username: "tawesoft"},
		Title:       "Top 10 reasons why I love my cat",
		Description: "My cat can even eat a whole watermelon.",
		Type:        twittercard.CardTypeSummaryLargeImage,
		SummaryLargeImage: twittercard.CardSummaryLargeImage{
			Image: twittercard.Image{
				Url: "https://www.example.org/media/cat-photos/cat1.jpg",
				Alt: "A black and white cat (looking very cute) sitting on a blanket with a soft toy in mid-air.",
			},
			Creator: twittercard.Account{ID: "8574052"}, // or @golightlyb
		},
	}

	must.Result(os.Stdout.WriteString(`<!doctype html>
<html lang="en-gb">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Twitter Card example</title>
`))
	must.Check(card.Write(os.Stdout))
	must.Result(os.Stdout.WriteString(`</head><body>Test!</body></html>`))
}
Output:

Index

Examples

Constants

View Source
const (
	CardTypeSummary           = "summary"
	CardTypeSummaryLargeImage = "summary_large_image"
	CardTypePlayer            = "player"
	CardTypeApp               = "app"
)
View Source
const (
	AppStoreIPad       = AppStore("ipad")
	AppStoreIPhone     = AppStore("iphone")
	AppStoreGooglePlay = AppStore("googleplay")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Username string // e.g. "@username"
	ID       string // e.g. "1234567890"
}

Account represents a Twitter account. Either Username or ID should be specified. Presumably, ID is constant even if a username may change.

type App

type App struct {
	Store AppStore // e.g. AppStoreIPad
	Name  string   // e.g. "My App"
	ID    string   // ID on App store e.g. "1234567890" or "org.example.myapp"
	Url   string   // Optional. Your app’s custom URL scheme for App "deep links".
}

App represents a specific app on a specific app store.

type AppStore

type AppStore string

type Card

type Card struct {
	Site        Account // the website that the card should be attributed to
	Title       string  // max 70 chars
	Description string  // max 200 chars
	Type        CardType

	// Discriminated by Type
	Summary           CardSummary
	SummaryLargeImage CardSummaryLargeImage
	Player            CardPlayer
	App               CardApp
}

func (Card) Write

func (c Card) Write(wr io.Writer) error

Write renders a Twitter Card as HTML. This should be done in the <head> of the document.

If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer.

type CardApp

type CardApp struct {
	// If your application is not available in the US App Store, you must set this
	// value to the two-letter country code for the App Store that contains your
	// application.
	Country string // e.g. "US".
	Apps    []App
}

type CardPlayer

type CardPlayer struct {
	Video Video
	Image Image // Fallback. Same aspect ratio as video. At least 68,600 pixels.
}

type CardSummary

type CardSummary struct {
	Image Image // Aspect ratio 1:1. From 144x144 to 4096x4096 pixels.
}

type CardSummaryLargeImage

type CardSummaryLargeImage struct {
	Image   Image   // Aspect ratio 2:1. From 300x157 to 4096x4096 pixels.
	Creator Account // the content creator, optional
}

type CardType

type CardType string

type Image

type Image struct {
	// URL to the image. May have aspect ratio and/or size requirements. Must be
	// less than 5MB in file size. Only JPG, PNG, WEBP and GIF formats are
	// supported. Only the first frame of an animated GIF will be used.
	Url string

	// A text description of the image conveying the essential nature of the
	// image to users who are visually impaired. Maximum 420 characters.
	Alt string
}

type Media

type Media struct {
	Type string // MIME type
	Url  string
}

type Video

type Video struct {
	// HTTPS URL of player frame (see https://github.com/twitterdev/cards-player-samples).
	// You can generate the body of this frame with the [Video.Write] method.
	Url string

	Width  int // of frame, in pixels
	Height int // of frame, in pixels

	Streams []Media // Optional. URL to raw video or audio streams.
}

func (Video) Write

func (c Video) Write(wr io.Writer) error

Write renders a video player frame as HTML. This should be done in the <body> of the document.

Note that this frame does not have to appear in the same document as the Twitter Card. It is the frame that appears at the [Video.Url] location.

If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer.

Jump to

Keyboard shortcuts

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