File: json.go

package info (click to toggle)
golang-github-alecthomas-chroma-v2 2.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,788 kB
  • sloc: xml: 41,690; javascript: 459; python: 420; makefile: 51; sh: 37
file content (39 lines) | stat: -rw-r--r-- 774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package formatters

import (
	"encoding/json"
	"fmt"
	"io"

	"github.com/alecthomas/chroma/v2"
)

// JSON formatter outputs the raw token structures as JSON.
var JSON = Register("json", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, it chroma.Iterator) error {
	if _, err := fmt.Fprintln(w, "["); err != nil {
		return err
	}
	i := 0
	for t := it(); t != chroma.EOF; t = it() {
		if i > 0 {
			if _, err := fmt.Fprintln(w, ","); err != nil {
				return err
			}
		}
		i++
		bytes, err := json.Marshal(t)
		if err != nil {
			return err
		}
		if _, err := fmt.Fprint(w, "  "+string(bytes)); err != nil {
			return err
		}
	}
	if _, err := fmt.Fprintln(w); err != nil {
		return err
	}
	if _, err := fmt.Fprintln(w, "]"); err != nil {
		return err
	}
	return nil
}))