/
formatter.go
136 lines (118 loc) · 2.93 KB
/
formatter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package project
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"github.com/stateful/runme/internal/document"
"github.com/stateful/runme/internal/document/editor"
"github.com/stateful/runme/internal/renderer/cmark"
)
type funcOutput func(string, []byte) error
func Format(files []string, basePath string, flatten bool, formatJSON bool, write bool, outputter funcOutput) error {
for _, relFile := range files {
data, err := readMarkdown(basePath, []string{relFile})
if err != nil {
return err
}
var formatted []byte
if flatten {
notebook, err := editor.Deserialize(data, false)
if err != nil {
return errors.Wrap(err, "failed to deserialize")
}
if formatJSON {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
if err := enc.Encode(notebook); err != nil {
return errors.Wrap(err, "failed to encode to JSON")
}
formatted = buf.Bytes()
} else {
formatted, err = editor.Serialize(notebook)
if err != nil {
return errors.Wrap(err, "failed to serialize")
}
}
} else {
doc := document.New(data, cmark.Render)
_, astNode, err := doc.Parse()
if err != nil {
return errors.Wrap(err, "failed to parse source")
}
formatted, err = cmark.Render(astNode, data)
if err != nil {
return errors.Wrap(err, "failed to render")
}
}
if write {
err = writeMarkdown(basePath, []string{relFile}, formatted)
} else {
err = outputter(relFile, formatted)
}
if err != nil {
return err
}
}
return nil
}
func writeMarkdown(basePath string, args []string, data []byte) error {
arg := ""
if len(args) == 1 {
arg = args[0]
}
if arg == "-" {
return errors.New("cannot write to stdin")
}
if strings.HasPrefix(arg, "https://") {
return errors.New("cannot write to HTTP location")
}
fullFilename := filepath.Join(basePath, arg)
if fullFilename == "" {
return nil
}
err := WriteMarkdownFile(fullFilename, nil, data)
return errors.Wrapf(err, "failed to write to %s", fullFilename)
}
func readMarkdown(basePath string, args []string) ([]byte, error) {
arg := ""
if len(args) == 1 {
arg = args[0]
}
var (
data []byte
err error
)
if arg == "-" {
data, err = io.ReadAll(os.Stdin)
if err != nil {
return nil, errors.Wrap(err, "failed to read from stdin")
}
} else if strings.HasPrefix(arg, "https://") {
client := http.Client{
Timeout: time.Second * 5,
}
resp, err := client.Get(arg)
if err != nil {
return nil, errors.Wrapf(err, "failed to get a file %q", arg)
}
defer func() { _ = resp.Body.Close() }()
data, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read body")
}
} else {
filePath := filepath.Join(basePath, arg)
data, err = ReadMarkdownFile(filePath, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to read from file %q", arg)
}
}
return data, nil
}