Skip to content

Commit bc79b15

Browse files
committed
refactor: clean up cmd execution
1 parent 811d6c4 commit bc79b15

File tree

5 files changed

+47
-93
lines changed

5 files changed

+47
-93
lines changed

internal/lint/adoc.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package lint
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
7-
"os/exec"
86
"regexp"
97
"strings"
108

@@ -28,6 +26,14 @@ var adocSanitizer = strings.NewReplacer(
2826
var reSource = regexp.MustCompile(`\[source,.+\]`)
2927
var reComment = regexp.MustCompile(`// .+`)
3028

29+
var adocArgs = []string{
30+
"-s",
31+
"-a",
32+
"notitle!",
33+
"-a",
34+
"attribute-missing=drop",
35+
}
36+
3137
func (l *Linter) lintADoc(f *core.File) error {
3238
var html string
3339
var err error
@@ -43,7 +49,7 @@ func (l *Linter) lintADoc(f *core.File) error {
4349
}
4450
s = adocSanitizer.Replace(s)
4551

46-
html, err = callAdoc(f, s, exe, l.Manager.Config.Asciidoctor)
52+
html, err = callAdoc(s, exe, l.Manager.Config.Asciidoctor)
4753
if err != nil {
4854
return core.NewE100(f.Path, err)
4955
}
@@ -82,31 +88,10 @@ func (l *Linter) lintADoc(f *core.File) error {
8288
return l.lintHTMLTokens(f, []byte(html), 0)
8389
}
8490

85-
func callAdoc(_ *core.File, text, exe string, attrs map[string]string) (string, error) {
86-
var out bytes.Buffer
87-
var eut bytes.Buffer
88-
89-
var adocArgs = []string{
90-
"-s",
91-
"-a",
92-
"notitle!",
93-
"-a",
94-
"attribute-missing=drop",
95-
}
96-
91+
func callAdoc(text, exe string, attrs map[string]string) (string, error) {
9792
adocArgs = append(adocArgs, parseAttributes(attrs)...)
9893
adocArgs = append(adocArgs, []string{"--safe-mode", "secure", "-"}...)
99-
100-
cmd := exec.Command(exe, adocArgs...)
101-
cmd.Stdin = strings.NewReader(text)
102-
cmd.Stdout = &out
103-
cmd.Stderr = &eut
104-
105-
if err := cmd.Run(); err != nil {
106-
return "", errors.New(eut.String())
107-
}
108-
109-
return out.String(), nil
94+
return system.ExecuteWithInput(exe, text, adocArgs...)
11095
}
11196

11297
func parseAttributes(attrs map[string]string) []string {

internal/lint/md.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ func (l Linter) lintMarkdown(f *core.File) error {
4545
return core.NewE100(f.Path, err)
4646
}
4747

48+
f.Content = prepMarkdown(f.Content)
49+
return l.lintHTMLTokens(f, buf.Bytes(), 0)
50+
}
51+
52+
func prepMarkdown(content string) string {
4853
// NOTE: This is required to avoid finding matches inside info strings. For
4954
// example, if we're looking for 'json' we many incorrectly report the
5055
// location as being in an infostring like '```json'.
5156
//
5257
// See https://github.com/errata-ai/vale/v2/issues/248.
53-
body := reExInfo.ReplaceAllStringFunc(f.Content, func(m string) string {
58+
body := reExInfo.ReplaceAllStringFunc(content, func(m string) string {
5459
parts := strings.Split(m, "`")
5560

5661
// This ensures that we respect the number of opening backticks, which
@@ -76,6 +81,5 @@ func (l Linter) lintMarkdown(f *core.File) error {
7681
return strings.Repeat("*", nlp.StrLen(m))
7782
})
7883

79-
f.Content = body
80-
return l.lintHTMLTokens(f, buf.Bytes(), 0)
84+
return body
8185
}

internal/lint/mdx.go

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package lint
22

33
import (
4-
"bytes"
54
"errors"
6-
"os/exec"
7-
"strings"
85

96
"github.com/errata-ai/vale/v3/internal/core"
10-
"github.com/errata-ai/vale/v3/internal/nlp"
117
"github.com/errata-ai/vale/v3/internal/system"
128
)
139

@@ -25,58 +21,11 @@ func (l Linter) lintMDX(f *core.File) error {
2521
return err
2622
}
2723

28-
html, err = callVast(f, s, exe)
24+
html, err = system.ExecuteWithInput(exe, s)
2925
if err != nil {
3026
return core.NewE100(f.Path, err)
3127
}
3228

33-
// NOTE: This is required to avoid finding matches inside info strings. For
34-
// example, if we're looking for 'json' we many incorrectly report the
35-
// location as being in an infostring like '```json'.
36-
//
37-
// See https://github.com/errata-ai/vale/v2/issues/248.
38-
body := reExInfo.ReplaceAllStringFunc(f.Content, func(m string) string {
39-
parts := strings.Split(m, "`")
40-
41-
// This ensures that we respect the number of opening backticks, which
42-
// could be more than 3.
43-
//
44-
// See https://github.com/errata-ai/vale/v2/issues/271.
45-
tags := strings.Repeat("`", len(parts)-1)
46-
span := strings.Repeat("*", nlp.StrLen(parts[len(parts)-1]))
47-
48-
return tags + span
49-
})
50-
51-
// NOTE: This is required to avoid finding matches inside link references.
52-
body = reLinkRef.ReplaceAllStringFunc(body, func(m string) string {
53-
return "][" + strings.Repeat("*", nlp.StrLen(m)-3) + "]"
54-
})
55-
body = reLinkDef.ReplaceAllStringFunc(body, func(m string) string {
56-
return "[" + strings.Repeat("*", nlp.StrLen(m)-3) + "]:"
57-
})
58-
59-
// NOTE: This is required to avoid finding matches inside ordered lists.
60-
body = reNumericList.ReplaceAllStringFunc(body, func(m string) string {
61-
return strings.Repeat("*", nlp.StrLen(m))
62-
})
63-
64-
f.Content = body
29+
f.Content = prepMarkdown(f.Content)
6530
return l.lintHTMLTokens(f, []byte(html), 0)
6631
}
67-
68-
func callVast(_ *core.File, text, exe string) (string, error) {
69-
var out bytes.Buffer
70-
var eut bytes.Buffer
71-
72-
cmd := exec.Command(exe)
73-
cmd.Stdin = strings.NewReader(text)
74-
cmd.Stdout = &out
75-
cmd.Stderr = &eut
76-
77-
if err := cmd.Run(); err != nil {
78-
return "", errors.New(eut.String())
79-
}
80-
81-
return out.String(), nil
82-
}

internal/lint/rst.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package lint
22

33
import (
4-
"bytes"
54
"errors"
6-
"os/exec"
75
"regexp"
86
"strings"
97

@@ -61,17 +59,10 @@ func (l *Linter) lintRST(f *core.File) error {
6159
}
6260

6361
func callRst(text, lib, _ string) (string, error) {
64-
var out bytes.Buffer
65-
66-
cmd := exec.Command(lib, rstArgs...)
67-
cmd.Stdin = strings.NewReader(text)
68-
cmd.Stdout = &out
69-
70-
if err := cmd.Run(); err != nil {
62+
html, err := system.ExecuteWithInput(lib, text, rstArgs...)
63+
if err != nil {
7164
return "", err
7265
}
73-
74-
html := out.String()
7566
html = strings.ReplaceAll(html, "\r", "")
7667

7768
bodyStart := strings.Index(html, "<body>\n")

internal/system/cmd.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package system
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
// ExecuteWithInput runs a command with the given text as input.
11+
func ExecuteWithInput(exe, text string, args ...string) (string, error) {
12+
var out bytes.Buffer
13+
var eut bytes.Buffer
14+
15+
cmd := exec.Command(exe, args...)
16+
cmd.Stdin = strings.NewReader(text)
17+
cmd.Stdout = &out
18+
cmd.Stderr = &eut
19+
20+
if err := cmd.Run(); err != nil {
21+
return "", errors.New(eut.String())
22+
}
23+
24+
return out.String(), nil
25+
}

0 commit comments

Comments
 (0)