htmlyaml

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 9 Imported by: 1

README ΒΆ

🐹 htmlyaml: rich rendering of YAML as HTML in Go

codecov Go Report Card Go Reference Mentioned in Awesome Go OpenSSF Scorecard

  • pure Go
  • no Javascript
  • no dependencies
  • no reflect
  • no fmt
  • no text/template, html/template
  • 300 LOC
  • customizable rendering
  • JSON Path for elements access

// YAML has to be any
var v any
yaml.Unmarshal(exampleYAML, &v)

htmlPage := htmlyaml.DefaultPageMarshaler.Marshal(v)
// YAML has to be any
var v any
yaml.Unmarshal(exampleYAML, &v)

// customize how to render HTML elements
s := htmlyaml.DefaultMarshaler
s.Number = func(k string, v float64, s string) string {
    if k == "$.cakes.strawberry-cake.size" {
        return `<div class="yaml-value yaml-number" style="color:red;">` + s + `</div>`
    }
    if v > 10 {
        return `<div class="yaml-value yaml-number" style="color:blue;">` + s + `</div>`
    }
    return `<div class="yaml-value yaml-number">` + s + `</div>`
}

m := htmlyaml.DefaultPageMarshaler
m.Marshaler = &s

// write HTML page
htmlPage := m.Marshal(v)

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var (
	DefaultArrayDash    = `<div class="yaml-lang">-&nbsp;</div>`
	DefaultArrayEmpty   = `<div class="yaml-lang">&nbsp;[]</div>`
	DefaultMapColon     = `<div class="yaml-lang">:&nbsp;</div>`
	DefaultMapEmpty     = `<div class="yaml-lang">&nbsp;{}</div>`
	DefaultPaddingSpace = `<span class="yaml-padding-space">&nbsp;</span>`
)
View Source
var DefaultMarshaler = Marshaler{
	Null:       DefaultNull,
	Bool:       DefaultBool,
	String:     DefaultString,
	Number:     DefaultNumber,
	MapKey:     DefaultMapKey,
	ArrayDash:  DefaultArrayDash,
	ArrayEmpty: DefaultArrayEmpty,
	MapColon:   DefaultMapColon,
	MapEmpty:   DefaultMapEmpty,
	Row:        DefaultRow{Padding: 2, Space: DefaultPaddingSpace}.Marshal,
}

DefaultMarshaler adds basic HTML div classes for further styling.

View Source
var DefaultPageMarshaler = PageMarshaler{
	Title:            "htmlyaml",
	Template:         defaultPageTemplate,
	TemplateTitleKey: `{{.Title}}`,
	TemplateYAMLKey:  `{{.HTMLYAML}}`,
	Marshaler:        &DefaultMarshaler,
}

Functions ΒΆ

func DefaultBool ΒΆ

func DefaultBool(k string, v bool) string

func DefaultMapKey ΒΆ

func DefaultMapKey(key string, v string) string

func DefaultNull ΒΆ

func DefaultNull(k string) string

func DefaultNumber ΒΆ

func DefaultNumber(k string, v float64, s string) string

func DefaultString ΒΆ

func DefaultString(k string, v string) string

Types ΒΆ

type DefaultRow ΒΆ

type DefaultRow struct {
	Padding int
	Space   string
}

func (DefaultRow) Marshal ΒΆ

func (s DefaultRow) Marshal(v string, depth int) string

type JSONPathCollector ΒΆ

type JSONPathCollector struct {
	Keys map[string]any
}

JSONPathCollector provides HTML renderers as its methods and collects which JSON path keys have been passed to it and which values. This is useful for testing.

func NewJSONPathCollector ΒΆ

func NewJSONPathCollector() *JSONPathCollector

func (*JSONPathCollector) Bool ΒΆ

func (c *JSONPathCollector) Bool(k string, v bool) string

func (*JSONPathCollector) MapKey ΒΆ

func (c *JSONPathCollector) MapKey(k string, v string) string

func (*JSONPathCollector) Null ΒΆ

func (c *JSONPathCollector) Null(k string) string

func (*JSONPathCollector) Number ΒΆ

func (c *JSONPathCollector) Number(k string, v float64, s string) string

func (*JSONPathCollector) String ΒΆ

func (c *JSONPathCollector) String(k string, v string) string

type Marshaler ΒΆ

type Marshaler struct {
	Null       func(key string) string
	Bool       func(key string, v bool) string
	String     func(key string, v string) string
	Number     func(key string, v float64, s string) string
	ArrayDash  string
	ArrayEmpty string
	MapColon   string
	MapEmpty   string
	MapKey     func(key string, v string) string
	Row        func(s string, padding int) string
	// contains filtered or unexported fields
}

Marshaler converts YAML stored as Go `any` object represented into HTML. Rendering is customized by providing renderers for specific YAML elements. This facilitates CSS styling, CSS animations, and JavaScript events. YAML element renderers receive JSON Path and value of element. Should be used only for types: bool, float64, string, []any, map[string]any, nil. You can get allowed input easily with yaml.Unmarshal or json.Unmarshal to any. Since HTML automatically removes whitespace, to make indentation YAML conformant, spaces are wrapped in their own div element. Safe for repeated use. Not safe for concurrent use.

func (*Marshaler) Marshal ΒΆ

func (s *Marshaler) Marshal(v any) []byte

Marshaler converts YAML stored as Go `any` object represented into HTML.

func (*Marshaler) MarshalTo ΒΆ

func (s *Marshaler) MarshalTo(w io.Writer, v any) error

MarshalTo converts YAML stored as Go `any` object represented into HTML.

type PageMarshaler ΒΆ

type PageMarshaler struct {
	Title            string
	Template         []byte
	TemplateTitleKey string
	TemplateYAMLKey  string

	Marshaler interface {
		MarshalTo(w io.Writer, v any) error
	}
	// contains filtered or unexported fields
}

PageMarshaler encodes YAML via marshaller into HTML page by placing Title and content appropriately.

func (*PageMarshaler) Marshal ΒΆ

func (m *PageMarshaler) Marshal(v any) []byte

func (*PageMarshaler) MarshalTo ΒΆ

func (m *PageMarshaler) MarshalTo(w io.Writer, v any) error

Jump to

Keyboard shortcuts

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