/
transcribe.go
110 lines (92 loc) · 2.8 KB
/
transcribe.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package api
import (
"io"
"os"
"github.com/beevik/etree"
"github.com/tliron/commonjs-goja"
"github.com/tliron/go-transcribe"
)
func CreateTranscribeExtension(stdout io.Writer, stderr io.Writer) commonjs.CreateExtensionFunc {
return func(jsContext *commonjs.Context) any {
return NewTranscribe(stdout, stderr)
}
}
//
// Transcribe
//
type Transcribe struct {
Stdout io.Writer
Stderr io.Writer
}
func NewTranscribe(stdout io.Writer, stderr io.Writer) *Transcribe {
if stdout == nil {
stdout = os.Stdout
}
if stderr == nil {
stderr = os.Stderr
}
return &Transcribe{
Stdout: stdout,
Stderr: stderr,
}
}
// Encodes and writes the value. Supported formats are "yaml", "json",
// "xjson", "xml", "cbor", "messagepack", and "go". The "cbor" and "messsagepack"
// formats will ignore the indent argument.
func (self Transcribe) Write(writer io.Writer, value any, format string, indent string) error {
transcriber := transcribe.Transcriber{
Writer: writer,
Format: format,
Indent: indent,
}
return transcriber.Write(value)
}
// Encodes and writes the value. Supported formats are "yaml", "json",
// "xjson", "xml", "cbor", "messagepack", and "go". The "cbor" and "messsagepack"
// formats will be encoded in base64 and will ignore the indent argument.
func (self Transcribe) WriteText(writer io.Writer, value any, format string, indent string) error {
transcriber := transcribe.Transcriber{
Writer: writer,
Format: format,
Indent: indent,
Base64: true,
}
return transcriber.Write(value)
}
// Encodes and prints the value to stdout. Supported formats are "yaml", "json",
// "xjson", "xml", "cbor", "messagepack", and "go". The "cbor" and "messsagepack"
// formats will be encoded in base64 and will ignore the indent argument.
func (self Transcribe) Print(value any, format string, indent string) error {
transcriber := transcribe.Transcriber{
Writer: self.Stdout,
Format: format,
ForTerminal: true,
Indent: indent,
Base64: true,
}
return transcriber.Write(value)
}
func (self Transcribe) Eprint(value any, format string, indent string) error {
transcriber := transcribe.Transcriber{
Writer: self.Stderr,
Format: format,
ForTerminal: true,
Indent: indent,
Base64: true,
}
return transcriber.Write(value)
}
// Encodes the value into a string.Supported formats are "yaml", "json",
// "xjson", "xml", "cbor", "messagepack", and "go". The "cbor" and "messsagepack"
// formats will be encoded in base64 and will ignore the indent argument.
func (self Transcribe) Stringify(value any, format string, indent string) (string, error) {
transcriber := transcribe.Transcriber{
Format: format,
Indent: indent,
Base64: true,
}
return transcriber.Stringify(value)
}
func (self Transcribe) NewXmlDocument() *etree.Document {
return etree.NewDocument()
}