h5p

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 9 Imported by: 0

README

H5P Go SDK

Build Status Lint Status Go Report Card Docs License

A Go library for creating, manipulating, and validating H5P (HTML5 Package) content with support for the official H5P file format and schemas.

✨ Features

  • 📦 Full H5P Package Support - Create and extract .h5p ZIP files
  • 🔒 Type-Safe Schema Implementation - Official H5P content type schemas
  • 🏗️ Question Set Builder - Fluent API for building interactive content
  • Validation - Built-in H5P compliance validation
  • 🎯 Multiple Question Types - Support for various H5P content types
  • 🔄 JSON Serialization - Complete marshaling/unmarshaling support

🚀 Quick Start

Installation
go get github.com/grokify/h5p-go
Create Your First Quiz
package main

import (
    "fmt"
    "log"
    "github.com/grokify/h5p-go"
)

func main() {
    // Create answers
    answers := []h5p.Answer{
        h5p.CreateAnswer("Paris", true),
        h5p.CreateAnswer("London", false),
        h5p.CreateAnswer("Berlin", false),
    }
    
    // Build question set
    questionSet, err := h5p.NewQuestionSetBuilder().
        SetTitle("Geography Quiz").
        SetProgressType("textual").
        SetPassPercentage(60).
        AddMultipleChoiceQuestion("What is the capital of France?", answers).
        Build()
    
    if err != nil {
        log.Fatal(err)
    }
    
    // Export to JSON
    jsonData, _ := questionSet.ToJSON()
    fmt.Printf("Generated H5P content:\n%s\n", string(jsonData))
}
Using Type-Safe Schemas
import "github.com/grokify/h5p-go/schemas"

// Create strongly-typed content
params := &schemas.MultiChoiceParams{
    Question: "What is 2 + 2?",
    Answers: []schemas.AnswerOption{
        {Text: "4", Correct: true},
        {Text: "5", Correct: false},
    },
    Behaviour: &schemas.Behaviour{
        Type: "single",
        EnableRetry: true,
    },
}

question := h5p.NewMultiChoiceQuestion(params)

📚 Documentation

Full Documentation →

Key Topics

🏗️ Architecture

h5p-go/
├── schemas/          # Official H5P content type schemas
├── semantics/        # Universal H5P semantics format  
├── builder.go        # Fluent API for content creation
├── questionset.go    # Core question set functionality
└── h5p_package.go    # Complete H5P package management

🧪 Testing

go test ./...

Run specific tests:

go test -v -run TestQuestionSet    # Question set tests
go test -v -run TestTyped          # Typed schema tests
go test -v -run TestH5PPackage     # Package management tests

📏 Standards Compliance

This library implements the official H5P specifications:

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

See our Contributing Guide for detailed information.

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • H5P Group for the H5P framework and specifications
  • The Go community for excellent tooling and libraries

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer

type Answer struct {
	Text     string `json:"text"`
	Correct  bool   `json:"correct"`
	Feedback string `json:"feedback,omitempty"`
}

Legacy types - use schemas.MultiChoiceParams instead Kept for backward compatibility, will be deprecated

func CreateAnswer

func CreateAnswer(text string, correct bool) Answer

func CreateAnswerWithFeedback

func CreateAnswerWithFeedback(text string, correct bool, feedback string) Answer

type BackgroundImage

type BackgroundImage struct {
	Path      string     `json:"path"`
	Mime      string     `json:"mime"`
	Copyright *Copyright `json:"copyright,omitempty"`
}

type Content

type Content struct {
	QuestionSet *QuestionSet `json:"questionSet,omitempty"`
	Params      interface{}  `json:",omitempty"`
}
type Copyright struct {
	Title   string `json:"title,omitempty"`
	Author  string `json:"author,omitempty"`
	License string `json:"license,omitempty"`
	Version string `json:"version,omitempty"`
	Source  string `json:"source,omitempty"`
}

type FeedbackRange

type FeedbackRange struct {
	From int    `json:"from"`
	To   int    `json:"to"`
	Text string `json:"text"`
}

func CreateFeedbackRange

func CreateFeedbackRange(from, to int, text string) FeedbackRange

type FileReference

type FileReference struct {
	Path string `json:"path"`
}

type H5PPackage

type H5PPackage struct {
	PackageDefinition *PackageDefinition `json:"-"`
	Content           *Content           `json:"-"`
	Libraries         []*Library         `json:"-"`
}

func LoadH5PPackage

func LoadH5PPackage(filePath string) (*H5PPackage, error)

func NewH5PPackage

func NewH5PPackage() *H5PPackage

func (*H5PPackage) AddLibrary

func (pkg *H5PPackage) AddLibrary(lib *Library)

func (*H5PPackage) CreateZipFile

func (pkg *H5PPackage) CreateZipFile(outputPath string) error

func (*H5PPackage) SetContent

func (pkg *H5PPackage) SetContent(content *Content)

func (*H5PPackage) SetPackageDefinition

func (pkg *H5PPackage) SetPackageDefinition(def *PackageDefinition)

type Library

type Library struct {
	Definition  *LibraryDefinition `json:"-"`
	Semantics   interface{}        `json:"-"`
	MachineName string             `json:"-"`
	Files       map[string][]byte  `json:"-"`
}

type LibraryDefinition

type LibraryDefinition struct {
	Title          string              `json:"title"`
	MachineName    string              `json:"machineName"`
	MajorVersion   int                 `json:"majorVersion"`
	MinorVersion   int                 `json:"minorVersion"`
	PatchVersion   int                 `json:"patchVersion"`
	Runnable       bool                `json:"runnable"`
	Author         string              `json:"author,omitempty"`
	License        string              `json:"license,omitempty"`
	Description    string              `json:"description,omitempty"`
	PreloadedJs    []FileReference     `json:"preloadedJs,omitempty"`
	PreloadedCss   []FileReference     `json:"preloadedCss,omitempty"`
	DropLibraryCss []FileReference     `json:"dropLibraryCss,omitempty"`
	Dependencies   []LibraryDependency `json:"preloadedDependencies,omitempty"`
}

type LibraryDependency

type LibraryDependency struct {
	MachineName  string `json:"machineName"`
	MajorVersion int    `json:"majorVersion"`
	MinorVersion int    `json:"minorVersion"`
}

type MultiChoiceQuestion

type MultiChoiceQuestion struct {
	Library string                     `json:"library"`
	Params  *schemas.MultiChoiceParams `json:"params"`
}

MultiChoiceQuestion represents a typed H5P MultiChoice question

func NewMultiChoiceQuestion

func NewMultiChoiceQuestion(params *schemas.MultiChoiceParams) *MultiChoiceQuestion

NewMultiChoiceQuestion creates a new typed MultiChoice question

func (*MultiChoiceQuestion) ToQuestion

func (mcq *MultiChoiceQuestion) ToQuestion() *Question

ToQuestion converts a MultiChoiceQuestion to a generic Question

type PackageDefinition

type PackageDefinition struct {
	Title                 string              `json:"title"`
	Language              string              `json:"language"`
	MainLibrary           string              `json:"mainLibrary"`
	EmbedTypes            []string            `json:"embedTypes"`
	License               string              `json:"license,omitempty"`
	DefaultLanguage       string              `json:"defaultLanguage,omitempty"`
	Author                string              `json:"author,omitempty"`
	PreloadedDependencies []LibraryDependency `json:"preloadedDependencies"`
	EditorDependencies    []LibraryDependency `json:"editorDependencies,omitempty"`
}

type Question

type Question struct {
	Library string      `json:"library"`
	Params  interface{} `json:"params"`
}

type QuestionSet

type QuestionSet struct {
	ProgressType       string           `json:"progressType,omitempty"`
	PassPercentage     int              `json:"passPercentage,omitempty"`
	BackgroundImage    *BackgroundImage `json:"backgroundImage,omitempty"`
	Questions          []Question       `json:"questions"`
	ShowIntroPage      bool             `json:"showIntroPage,omitempty"`
	StartButtonText    string           `json:"startButtonText,omitempty"`
	Introduction       string           `json:"introduction,omitempty"`
	Title              string           `json:"title,omitempty"`
	ShowResultPage     bool             `json:"showResultPage,omitempty"`
	Message            string           `json:"message,omitempty"`
	SolutionButtonText string           `json:"solutionButtonText,omitempty"`
	OverallFeedback    []FeedbackRange  `json:"overallFeedback,omitempty"`
}

func FromJSON

func FromJSON(data []byte) (*QuestionSet, error)

func (*QuestionSet) ToJSON

func (qs *QuestionSet) ToJSON() ([]byte, error)

func (*QuestionSet) Validate

func (qs *QuestionSet) Validate() error

type QuestionSetBuilder

type QuestionSetBuilder struct {
	// contains filtered or unexported fields
}
Example
builder := NewQuestionSetBuilder()

answers := []Answer{
	CreateAnswer("Paris", true),
	CreateAnswer("London", false),
	CreateAnswer("Berlin", false),
	CreateAnswer("Madrid", false),
}

feedbackRanges := []FeedbackRange{
	CreateFeedbackRange(0, 50, "You need more practice!"),
	CreateFeedbackRange(51, 80, "Good job!"),
	CreateFeedbackRange(81, 100, "Excellent work!"),
}

questionSet, err := builder.
	SetTitle("Geography Quiz").
	SetProgressType("textual").
	SetPassPercentage(60).
	SetIntroduction("Welcome to our geography quiz!").
	SetStartButtonText("Start Quiz").
	AddMultipleChoiceQuestion("What is the capital of France?", answers).
	AddOverallFeedback(feedbackRanges).
	Build()

if err != nil {
	log.Fatal(err)
}

jsonData, err := questionSet.ToJSON()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Generated H5P Question Set:\n%s\n", string(jsonData))

loadedQuestionSet, err := FromJSON(jsonData)
if err != nil {
	log.Fatal(err)
}

err = loadedQuestionSet.Validate()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Question set validated successfully!\n")
fmt.Printf("Number of questions: %d\n", len(loadedQuestionSet.Questions))
fmt.Printf("Pass percentage: %d%%\n", loadedQuestionSet.PassPercentage)

func NewQuestionSetBuilder

func NewQuestionSetBuilder() *QuestionSetBuilder

func (*QuestionSetBuilder) AddMultipleChoiceQuestion

func (b *QuestionSetBuilder) AddMultipleChoiceQuestion(question string, answers []Answer) *QuestionSetBuilder

func (*QuestionSetBuilder) AddOverallFeedback

func (b *QuestionSetBuilder) AddOverallFeedback(ranges []FeedbackRange) *QuestionSetBuilder

func (*QuestionSetBuilder) Build

func (b *QuestionSetBuilder) Build() (*QuestionSet, error)

func (*QuestionSetBuilder) SetBackgroundImage

func (b *QuestionSetBuilder) SetBackgroundImage(path, mime string) *QuestionSetBuilder

func (*QuestionSetBuilder) SetIntroduction

func (b *QuestionSetBuilder) SetIntroduction(introduction string) *QuestionSetBuilder

func (*QuestionSetBuilder) SetPassPercentage

func (b *QuestionSetBuilder) SetPassPercentage(percentage int) *QuestionSetBuilder

func (*QuestionSetBuilder) SetProgressType

func (b *QuestionSetBuilder) SetProgressType(progressType string) *QuestionSetBuilder

func (*QuestionSetBuilder) SetStartButtonText

func (b *QuestionSetBuilder) SetStartButtonText(text string) *QuestionSetBuilder

func (*QuestionSetBuilder) SetTitle

func (b *QuestionSetBuilder) SetTitle(title string) *QuestionSetBuilder

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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