2025-09-11 20:50:00 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2025-10-05 14:27:48 +02:00
|
|
|
"strings"
|
2025-09-11 20:50:00 +02:00
|
|
|
)
|
|
|
|
|
|
2025-10-05 20:06:39 +02:00
|
|
|
const defaultPWidth = 112
|
2025-09-11 20:50:00 +02:00
|
|
|
|
|
|
|
|
type renderGuide struct {
|
2025-10-05 14:27:48 +02:00
|
|
|
inline bool
|
|
|
|
|
inlineChildren bool
|
|
|
|
|
void bool
|
|
|
|
|
script bool
|
|
|
|
|
verbatim bool
|
2025-09-11 20:50:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type renderer struct {
|
|
|
|
|
out io.Writer
|
2025-10-05 14:27:48 +02:00
|
|
|
originalOut io.Writer
|
|
|
|
|
indent Indentation
|
2025-09-11 20:50:00 +02:00
|
|
|
pwidth int
|
|
|
|
|
currentIndent string
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mergeRenderingGuides(rgs []renderGuide) renderGuide {
|
|
|
|
|
var rg renderGuide
|
|
|
|
|
for _, rgi := range rgs {
|
|
|
|
|
rg.inline = rg.inline || rgi.inline
|
2025-10-05 14:27:48 +02:00
|
|
|
rg.inlineChildren = rg.inlineChildren || rgi.inlineChildren
|
2025-09-11 20:50:00 +02:00
|
|
|
rg.void = rg.void || rgi.void
|
|
|
|
|
rg.script = rg.script || rgi.script
|
|
|
|
|
rg.verbatim = rg.verbatim || rgi.verbatim
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rg
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
func indentLines(indent string, s string) string {
|
|
|
|
|
l := strings.Split(s, "\n")
|
|
|
|
|
for i := range l {
|
|
|
|
|
l[i] = fmt.Sprintf("%s%s", indent, l[i])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.Join(l, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) getPrintf(tagName string) func(f string, a ...any) {
|
|
|
|
|
return func(f string, a ...any) {
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, r.err = fmt.Fprintf(r.out, f, a...)
|
|
|
|
|
if r.err != nil {
|
|
|
|
|
r.err = fmt.Errorf("tag %s: %w", tagName, r.err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) renderAttributes(tagName string, a []Attributes) {
|
|
|
|
|
printf := r.getPrintf(tagName)
|
|
|
|
|
for _, ai := range a {
|
|
|
|
|
for name, value := range ai {
|
2025-10-05 20:06:39 +02:00
|
|
|
if isTrue, _ := value.(bool); isTrue {
|
|
|
|
|
printf(" &s", name)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf(" %s=\"%s\"", name, attributeEscape(fmt.Sprint(value)))
|
2025-10-05 14:27:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) renderUnindented(name string, rg renderGuide, a []Attributes, children []any) {
|
|
|
|
|
printf := r.getPrintf(name)
|
|
|
|
|
printf("<%s", name)
|
|
|
|
|
r.renderAttributes(name, a)
|
|
|
|
|
printf(">")
|
|
|
|
|
if rg.void {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range children {
|
|
|
|
|
if ct, ok := c.(Tag); ok {
|
|
|
|
|
ct(r)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 20:06:39 +02:00
|
|
|
if rg.verbatim || rg.script {
|
|
|
|
|
printf(s)
|
2025-10-05 14:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-05 20:06:39 +02:00
|
|
|
if r.err == nil {
|
|
|
|
|
ew := newEscapeWriter(r.out)
|
|
|
|
|
ew.Write([]byte(s))
|
|
|
|
|
ew.Flush()
|
|
|
|
|
r.err = ew.err
|
|
|
|
|
}
|
2025-10-05 14:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("</%s>", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) ensureWrapper() bool {
|
|
|
|
|
if _, ok := r.out.(*wrapper); ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.originalOut = r.out
|
|
|
|
|
r.out = newWrapper(r.originalOut, r.pwidth, r.currentIndent)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) clearWrapper() {
|
|
|
|
|
w, ok := r.out.(*wrapper)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := w.Flush(); err != nil {
|
|
|
|
|
r.err = err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.out = r.originalOut
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) renderInline(name string, rg renderGuide, a []Attributes, children []any) {
|
|
|
|
|
newWrapper := r.ensureWrapper()
|
|
|
|
|
printf := r.getPrintf(name)
|
|
|
|
|
printf("<%s", name)
|
|
|
|
|
r.renderAttributes(name, a)
|
|
|
|
|
printf(">")
|
|
|
|
|
if rg.void {
|
|
|
|
|
if newWrapper {
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastBlock bool
|
|
|
|
|
for _, c := range children {
|
|
|
|
|
ct, isTag := c.(Tag)
|
|
|
|
|
if !isTag && rg.verbatim {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
s = indentLines(r.currentIndent+r.indent.Indent, s)
|
|
|
|
|
printf("\n%s", s)
|
|
|
|
|
lastBlock = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !isTag && rg.script {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
printf("\n%s", s)
|
|
|
|
|
lastBlock = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !isTag {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lastBlock {
|
|
|
|
|
printf("\n%s", r.currentIndent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.ensureWrapper() {
|
|
|
|
|
newWrapper = true
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 20:06:39 +02:00
|
|
|
if r.err == nil {
|
|
|
|
|
ew := newEscapeWriter(r.out)
|
|
|
|
|
ew.Write([]byte(s))
|
|
|
|
|
ew.Flush()
|
|
|
|
|
r.err = ew.err
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
lastBlock = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rgq renderGuidesQuery
|
|
|
|
|
ct(&rgq)
|
|
|
|
|
crg := mergeRenderingGuides(rgq.value)
|
|
|
|
|
if crg.inline {
|
|
|
|
|
if lastBlock {
|
|
|
|
|
printf("\n%s", r.currentIndent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.ensureWrapper() {
|
|
|
|
|
newWrapper = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ct(r)
|
|
|
|
|
lastBlock = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
cr := new(renderer)
|
|
|
|
|
*cr = *r
|
|
|
|
|
cr.currentIndent += cr.indent.Indent
|
|
|
|
|
cr.pwidth -= len([]rune(cr.indent.Indent))
|
|
|
|
|
if cr.pwidth < cr.indent.MinPWidth {
|
|
|
|
|
cr.pwidth = cr.indent.MinPWidth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\n%s", cr.currentIndent)
|
|
|
|
|
ct(cr)
|
|
|
|
|
if cr.err != nil {
|
|
|
|
|
r.err = cr.err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastBlock = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lastBlock {
|
|
|
|
|
printf("\n%s", r.currentIndent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("</%s>", name)
|
|
|
|
|
if newWrapper {
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) renderBlock(name string, rg renderGuide, a []Attributes, children []any) {
|
|
|
|
|
printf := r.getPrintf(name)
|
|
|
|
|
printf("<%s", name)
|
|
|
|
|
r.renderAttributes(name, a)
|
|
|
|
|
printf(">")
|
|
|
|
|
if rg.void {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(children) == 0 {
|
|
|
|
|
printf("</%s>", name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastBlock := true
|
|
|
|
|
originalIndent, originalWidth := r.currentIndent, r.pwidth
|
|
|
|
|
r.currentIndent += r.indent.Indent
|
|
|
|
|
r.pwidth -= len([]rune(r.indent.Indent))
|
|
|
|
|
if r.pwidth < r.indent.MinPWidth {
|
|
|
|
|
r.pwidth = r.indent.MinPWidth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range children {
|
|
|
|
|
ct, isTag := c.(Tag)
|
|
|
|
|
if !isTag && rg.verbatim {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
s = indentLines(r.currentIndent, s)
|
|
|
|
|
printf("\n%s", s)
|
|
|
|
|
lastBlock = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !isTag && rg.script {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
printf("\n%s", s)
|
|
|
|
|
lastBlock = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !isTag {
|
2025-10-05 20:06:39 +02:00
|
|
|
if c == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
s := fmt.Sprint(c)
|
|
|
|
|
if s == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lastBlock {
|
|
|
|
|
printf("\n%s", r.currentIndent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.ensureWrapper()
|
2025-10-05 20:06:39 +02:00
|
|
|
if r.err == nil {
|
|
|
|
|
ew := newEscapeWriter(r.out)
|
|
|
|
|
ew.Write([]byte(s))
|
|
|
|
|
ew.Flush()
|
|
|
|
|
r.err = ew.err
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
lastBlock = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rgq renderGuidesQuery
|
|
|
|
|
ct(&rgq)
|
|
|
|
|
crg := mergeRenderingGuides(rgq.value)
|
|
|
|
|
if crg.inline {
|
|
|
|
|
if lastBlock {
|
|
|
|
|
printf("\n%s", r.currentIndent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.ensureWrapper()
|
|
|
|
|
ct(r)
|
|
|
|
|
lastBlock = false
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
cr := new(renderer)
|
|
|
|
|
*cr = *r
|
|
|
|
|
printf("\n%s", cr.currentIndent)
|
|
|
|
|
ct(cr)
|
|
|
|
|
if cr.err != nil {
|
|
|
|
|
r.err = cr.err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastBlock = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.clearWrapper()
|
|
|
|
|
r.currentIndent, r.pwidth = originalIndent, originalWidth
|
|
|
|
|
printf("\n%s</%s>", r.currentIndent, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *renderer) render(name string, children []any) {
|
2025-09-11 20:50:00 +02:00
|
|
|
if r.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:27:48 +02:00
|
|
|
a, c, rgs := groupChildren(children)
|
|
|
|
|
rg := mergeRenderingGuides(rgs)
|
|
|
|
|
if r.indent.Indent == "" && r.indent.PWidth <= 0 {
|
|
|
|
|
r.renderUnindented(name, rg, a, c)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rg.inline || rg.inlineChildren {
|
|
|
|
|
r.renderInline(name, rg, a, c)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.renderBlock(name, rg, a, c)
|
|
|
|
|
}
|