-
Notifications
You must be signed in to change notification settings - Fork 4
/
md.go
186 lines (151 loc) 路 3.93 KB
/
md.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package md
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
"strings"
"github.com/88250/lute"
"github.com/elliotxx/mdfmt/pkg/merrors"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
)
// fdSem guards the number of concurrently-open file descriptors.
//
// For now, this is arbitrarily set to 200, based on the observation that many
// platforms default to a kernel limit of 256. Ideally, perhaps we should derive
// it from rlimit on platforms that support that system call.
//
// File descriptors opened from outside of this package are not tracked,
// so this limit may be approximate.
var fdSem = make(chan bool, 200)
// IsMarkdownFile returns true if the file is a markdown file.
func IsMarkdownFile(p string) bool {
fi, err := os.Stat(p)
if err != nil {
return false
}
if fi.IsDir() {
return false
}
filename := fi.Name()
ext := filepath.Ext(filename)
if strings.HasPrefix(filename, ".") {
return false
}
if ext != ".md" && ext != ".markdown" {
return false
}
return true
}
// FormatMarkdown formats markdown from reader to writer.
func FormatMarkdown(in io.Reader, out io.Writer) (err error) {
defer func() {
if r := recover(); r != nil {
err = merrors.NewCrash(fmt.Errorf("%v", r), string(debug.Stack()))
}
}()
// Read
markdownContent, err := ioutil.ReadAll(in)
if err != nil {
return err
}
// Format
luteEngine := lute.New(func(l *lute.Lute) {
l.RenderOptions.AutoSpace = true
})
formatContent := luteEngine.Format("md", markdownContent)
// Output
_, err = out.Write(formatContent)
if err != nil {
return err
}
return nil
}
// ProcessMDFile formats markdown file.
func ProcessMDFile(filePath string, write, diff, list bool) error {
// Get Reader and Source
source, err := os.ReadFile(filePath)
if err != nil {
return err
}
in := bytes.NewReader(source)
// Get Writer and Target
out := bytes.NewBuffer([]byte{})
err = FormatMarkdown(in, out)
if err != nil {
return err
}
target := out.Bytes()
if !bytes.Equal(source, target) {
// List
if list {
os.Stdout.WriteString(filePath + "\n")
}
// Write
if write {
info, err := os.Stat(filePath)
if err != nil {
return err
}
fdSem <- true
err = os.WriteFile(filePath, target, info.Mode().Perm())
<-fdSem
if err != nil {
return err
}
}
// Diff
if diff {
data, err := diffWithReplaceTempFile(source, target, filePath)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Fprintf(os.Stdout, "diff -u %s %s\n", filepath.ToSlash(filePath+".orig"), filepath.ToSlash(filePath))
os.Stdout.Write(data)
}
}
if !write && !diff && !list {
_, err = os.Stdout.Write(target)
}
return err
}
func diffWithReplaceTempFile(b1, b2 []byte, filename string) ([]byte, error) {
data := bytes.NewBufferString("")
err := diff.Text("origin", "target", b1, b2, data, write.TerminalColor())
if len(data.Bytes()) > 0 {
return replaceTempFilename(data.Bytes(), filename)
}
return data.Bytes(), err
}
// replaceTempFilename replaces temporary filenames in diff with actual one.
//
// --- /tmp/gofmt316145376 2017-02-03 19:13:00.280468375 -0500
// +++ /tmp/gofmt617882815 2017-02-03 19:13:00.280468375 -0500
// ...
// ->
// --- path/to/file.md.orig 2017-02-03 19:13:00.280468375 -0500
// +++ path/to/file.md 2017-02-03 19:13:00.280468375 -0500
// ...
func replaceTempFilename(diff []byte, filename string) ([]byte, error) {
bs := bytes.SplitN(diff, []byte{'\n'}, 3)
if len(bs) < 3 {
return nil, fmt.Errorf("got unexpected diff for %s", filename)
}
// Preserve timestamps.
var t0, t1 []byte
if i := bytes.LastIndexByte(bs[0], '\t'); i != -1 {
t0 = bs[0][i:]
}
if i := bytes.LastIndexByte(bs[1], '\t'); i != -1 {
t1 = bs[1][i:]
}
// Always print filepath with slash separator.
f := filepath.ToSlash(filename)
bs[0] = []byte(fmt.Sprintf("--- %s%s", f+".orig", t0))
bs[1] = []byte(fmt.Sprintf("+++ %s%s", f, t1))
return bytes.Join(bs, []byte{'\n'}), nil
}