-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
main.go
86 lines (78 loc) · 2.12 KB
/
main.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
// Command elvmdfmt reformats Markdown sources.
//
// This command is used to reformat all Markdown files in this repo; see the
// [contributor's manual] on how to use it.
//
// For general information about the Markdown implementation used by this
// command, see [src.elv.sh/pkg/md].
//
// [contributor's manual]: https://github.com/elves/elvish/blob/master/CONTRIBUTING.md#formatting
package main
import (
"flag"
"fmt"
"html"
"io"
"os"
"src.elv.sh/pkg/diff"
"src.elv.sh/pkg/md"
)
var (
overwrite = flag.Bool("w", false, "write result to source file (requires -fmt)")
showDiff = flag.Bool("d", false, "show diff")
width = flag.Int("width", 0, "if > 0, reflow content to width")
)
func main() {
md.UnescapeHTML = html.UnescapeString
flag.Parse()
files := flag.Args()
if len(files) == 0 {
text, err := io.ReadAll(os.Stdin)
handleReadError("stdin", err)
result, unsupported := format(string(text))
fmt.Print(result)
handleUnsupported("stdin", unsupported)
return
}
for _, file := range files {
text, err := os.ReadFile(file)
handleReadError(file, err)
result, unsupported := format(string(text))
handleUnsupported(file, unsupported)
if *overwrite {
err := os.WriteFile(file, []byte(result), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "write %s: %v\n", file, err)
os.Exit(2)
}
} else if !*showDiff {
fmt.Print(result)
}
if *showDiff {
os.Stdout.Write(diff.Diff(file+".orig", text, file, []byte(result)))
}
}
}
func format(original string) (string, *md.FmtUnsupported) {
codec := &md.FmtCodec{Width: *width}
formatted := md.RenderString(original, codec)
return formatted, codec.Unsupported()
}
func handleReadError(name string, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "read %s: %v\n", name, err)
os.Exit(2)
}
}
func handleUnsupported(name string, u *md.FmtUnsupported) {
if u == nil {
return
}
if u.NestedEmphasisOrStrongEmphasis {
fmt.Fprintln(os.Stderr, name, "contains nested emphasis or strong emphasis")
}
if u.ConsecutiveEmphasisOrStrongEmphasis {
fmt.Fprintln(os.Stderr, name, "contains consecutive emphasis or strong emphasis")
}
os.Exit(2)
}