mirror of https://github.com/k3s-io/k3s.git
Add a munger check for files that end in preformat
parent
44b0bb1ae7
commit
15e2c62a3e
|
@ -46,6 +46,9 @@ Examples:
|
||||||
// All of the munge operations to perform.
|
// All of the munge operations to perform.
|
||||||
// TODO: allow selection from command line. (e.g., just check links in the examples directory.)
|
// TODO: allow selection from command line. (e.g., just check links in the examples directory.)
|
||||||
allMunges = []munge{
|
allMunges = []munge{
|
||||||
|
// Simple "check something" functions must run first.
|
||||||
|
{"preformat-balance", checkPreformatBalance},
|
||||||
|
// Functions which modify state.
|
||||||
{"remove-whitespace", updateWhitespace},
|
{"remove-whitespace", updateWhitespace},
|
||||||
{"table-of-contents", updateTOC},
|
{"table-of-contents", updateTOC},
|
||||||
{"unversioned-warning", updateUnversionedWarning},
|
{"unversioned-warning", updateUnversionedWarning},
|
||||||
|
|
|
@ -16,6 +16,8 @@ limitations under the License.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// Blocks of ``` need to have blank lines on both sides or they don't look
|
// Blocks of ``` need to have blank lines on both sides or they don't look
|
||||||
// right in HTML.
|
// right in HTML.
|
||||||
func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
|
func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
|
||||||
|
@ -39,3 +41,11 @@ func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error)
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the file ends on a preformatted line, there must have been an imbalance.
|
||||||
|
func checkPreformatBalance(filePath string, mlines mungeLines) (mungeLines, error) {
|
||||||
|
if len(mlines) > 0 && mlines[len(mlines)-1].preformatted {
|
||||||
|
return nil, fmt.Errorf("file ends in preformatted block")
|
||||||
|
}
|
||||||
|
return mlines, nil
|
||||||
|
}
|
||||||
|
|
|
@ -55,3 +55,26 @@ func TestPreformatted(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPreformattedImbalance(t *testing.T) {
|
||||||
|
var cases = []struct {
|
||||||
|
in string
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{"", true},
|
||||||
|
{"```\nin\n```", true},
|
||||||
|
{"```\nin\n```\nout", true},
|
||||||
|
{"```", false},
|
||||||
|
{"```\nin\n```\nout\n```", false},
|
||||||
|
}
|
||||||
|
for i, c := range cases {
|
||||||
|
in := getMungeLines(c.in)
|
||||||
|
_, err := checkPreformatBalance("filename.md", in)
|
||||||
|
if err != nil && c.ok {
|
||||||
|
t.Errorf("case[%d]: expected success", i)
|
||||||
|
}
|
||||||
|
if err == nil && !c.ok {
|
||||||
|
t.Errorf("case[%d]: expected failure", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue