Skip to content

Commit b901b49

Browse files
committed
cmd/elvmdfmt: Split Markdown formatting into own command, supporting -w and -d.
Also change the behavior of the rendering command (elvmd) to still output HTML when given -trace.
1 parent ef5acc9 commit b901b49

File tree

7 files changed

+381
-30
lines changed

7 files changed

+381
-30
lines changed

.codecov.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ ignore:
2020
- "pkg/getopt/zstring.go"
2121
- "pkg/md/zstring.go"
2222
- "pkg/parse/zstring.go"
23-
# Exclude the copied rpc package.
23+
# Exclude the copied diff and rpc packages.
24+
- "pkg/diff"
2425
- "pkg/rpc"

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ The source for the documentation is in the
4141
All source files use the BSD 2-clause license (see [LICENSE](LICENSE)), except
4242
for the following:
4343

44-
- Files in [pkg/rpc](pkg/rpc) are released under the BSD 3-clause license,
45-
since they are copied from [Go's source code](https://github.com/golang/go).
46-
See [pkg/rpc/LICENSE](pkg/rpc/LICENSE).
44+
- Files in [pkg/diff](pkg/diff) and [pkg/rpc](pkg/rpc) are released under the
45+
BSD 3-clause license, since they are copied from
46+
[Go's source code](https://github.com/golang/go). See
47+
[pkg/diff/LICENSE](pkg/diff/LICENSE) and [pkg/rpc/LICENSE](pkg/rpc/LICENSE).
4748

4849
- Files in [pkg/persistent](pkg/persistent) and its subdirectories are
4950
released under EPL 1.0, since they are partially derived from

cmd/elvmd/main.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,41 @@ import (
44
"flag"
55
"fmt"
66
"io"
7-
"log"
87
"os"
98

109
"src.elv.sh/pkg/md"
1110
)
1211

12+
var (
13+
trace = flag.Bool("trace", false, "write internal parsing results")
14+
)
15+
1316
func main() {
14-
var (
15-
format = flag.Bool("fmt", false, "format Markdown")
16-
trace = flag.Bool("trace", false, "trace internal output by parser")
17-
)
1817
flag.Parse()
19-
if *format && *trace {
20-
fmt.Fprintln(os.Stderr, "-fmt and -trace are mutually exclusive")
21-
os.Exit(1)
22-
}
2318

24-
text, err := io.ReadAll(os.Stdin)
25-
if err != nil {
26-
log.Fatal(err)
19+
files := flag.Args()
20+
if len(files) == 0 {
21+
text, err := io.ReadAll(os.Stdin)
22+
if err != nil {
23+
fmt.Fprintln(os.Stderr, "read stdin:", err)
24+
os.Exit(2)
25+
}
26+
render(string(text))
27+
return
2728
}
28-
var codec md.StringerCodec
29-
switch {
30-
case *format:
31-
codec = &md.FmtCodec{}
32-
case *trace:
33-
codec = &md.TraceCodec{}
34-
default:
35-
codec = &md.HTMLCodec{}
29+
for _, file := range files {
30+
text, err := os.ReadFile(file)
31+
if err != nil {
32+
fmt.Fprintf(os.Stderr, "read %s: %v\n", file, err)
33+
os.Exit(2)
34+
}
35+
render(string(text))
36+
}
37+
}
38+
39+
func render(markdown string) {
40+
fmt.Print(md.RenderString(markdown, &md.HTMLCodec{}))
41+
if *trace {
42+
fmt.Print(md.RenderString(markdown, &md.TraceCodec{}))
3643
}
37-
fmt.Print(md.RenderString(string(text), codec))
3844
}

cmd/elvmdfmt/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"src.elv.sh/pkg/diff"
10+
"src.elv.sh/pkg/md"
11+
)
12+
13+
var (
14+
overwrite = flag.Bool("w", false, "write result to source file (requires -fmt)")
15+
showDiff = flag.Bool("d", false, "show diff")
16+
)
17+
18+
func main() {
19+
flag.Parse()
20+
21+
files := flag.Args()
22+
if len(files) == 0 {
23+
text, err := io.ReadAll(os.Stdin)
24+
if err != nil {
25+
fmt.Fprintln(os.Stderr, "read stdin:", err)
26+
os.Exit(2)
27+
}
28+
fmt.Print(format(string(text)))
29+
return
30+
}
31+
for _, file := range files {
32+
text, err := os.ReadFile(file)
33+
if err != nil {
34+
fmt.Fprintf(os.Stderr, "read %s: %v\n", file, err)
35+
os.Exit(2)
36+
}
37+
result := format(string(text))
38+
if *overwrite {
39+
err := os.WriteFile(file, []byte(result), 0644)
40+
if err != nil {
41+
fmt.Fprintf(os.Stderr, "write %s: %v\n", file, err)
42+
os.Exit(2)
43+
}
44+
} else if !*showDiff {
45+
fmt.Print(result)
46+
}
47+
if *showDiff {
48+
os.Stdout.Write(diff.Diff(file+".orig", text, file, []byte(result)))
49+
}
50+
}
51+
}
52+
53+
func format(original string) string {
54+
return md.RenderString(original, &md.FmtCodec{})
55+
}

pkg/diff/LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2009 The Go Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)