Skip to content

nikolaydubina/htmlyaml

Repository files navigation

🐹 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)

Related Work