{{- /* Format the displayed specimen only; the live demo keeps its original HTML. */ -}} {{- $tokens := findRE `(?s)|<(?:"[^"]*"|'[^']*'|[^'">])*>|[^<]+` (string .) -}} {{- $void := slice "area" "base" "br" "col" "embed" "hr" "img" "input" "link" "meta" "param" "source" "track" "wbr" -}} {{- $lines := slice -}} {{- $depth := 0 -}} {{- $skip := -1 -}} {{- range $i, $raw := $tokens -}} {{- if gt $i $skip -}} {{- $token := strings.TrimSpace $raw -}} {{- if $token -}} {{- $line := $token -}} {{- $open := gt (len (findRE `^<[A-Za-z]` $token)) 0 -}} {{- $leaf := false -}} {{- if hasPrefix $token "") -}} {{- $open = false -}} {{- else if lt (add $i 1) (len $tokens) -}} {{- $next := index $tokens (add $i 1) -}} {{- $close := printf "" $name -}} {{- if eq (lower (strings.TrimSpace $next)) $close -}} {{- $line = printf "%s%s" $token (strings.TrimSpace $next) -}} {{- $leaf = true -}} {{- $skip = add $i 1 -}} {{- else if and (not (hasPrefix (strings.TrimSpace $next) "<")) (lt (add $i 2) (len $tokens)) -}} {{- $end := strings.TrimSpace (index $tokens (add $i 2)) -}} {{- if eq (lower $end) $close -}} {{- $line = printf "%s%s%s" $token $next $end -}} {{- $leaf = true -}} {{- $skip = add $i 2 -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- $lines = $lines | append (printf "%*s%s" (mul $depth 2) "" $line) -}} {{- if and $open (not $leaf) }}{{ $depth = add $depth 1 }}{{ end -}} {{- end -}} {{- end -}} {{- end -}} {{- return (delimit $lines "\n") -}}