Add a whitespace munger

Strips all trailing whitespace from non-preformatted blocks
This commit is contained in:
Eric Paris 2015-07-24 17:51:48 -04:00
parent 8886a9940d
commit 3c95bd4ee3
11 changed files with 91 additions and 14 deletions

View File

@ -56,7 +56,7 @@ func fixHeaderLine(mlines mungeLines, newlines mungeLines, linenum int) mungeLin
}
// Header lines need whitespace around them and after the #s.
func checkHeaderLines(filePath string, mlines mungeLines) (mungeLines, error) {
func updateHeaderLines(filePath string, mlines mungeLines) (mungeLines, error) {
var out mungeLines
for i, mline := range mlines {
if mline.preformatted {

View File

@ -64,7 +64,7 @@ func TestHeaderLines(t *testing.T) {
for i, c := range cases {
in := getMungeLines(c.in)
expected := getMungeLines(c.expected)
actual, err := checkHeaderLines("filename.md", in)
actual, err := updateHeaderLines("filename.md", in)
assert.NoError(t, err)
if !actual.Equal(expected) {
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())

View File

@ -25,7 +25,7 @@ import (
// Looks for lines that have kubectl commands with -f flags and files that
// don't exist.
func checkKubectlFileTargets(file string, mlines mungeLines) (mungeLines, error) {
func updateKubectlFileTargets(file string, mlines mungeLines) (mungeLines, error) {
var errors []string
for i, mline := range mlines {
if !mline.preformatted {

View File

@ -132,7 +132,7 @@ func TestKubectlDashF(t *testing.T) {
for i, c := range cases {
repoRoot = ""
in := getMungeLines(c.in)
_, err := checkKubectlFileTargets("filename.md", in)
_, err := updateKubectlFileTargets("filename.md", in)
if err != nil && c.ok {
t.Errorf("case[%d]: expected success, got %v", i, err)
}

View File

@ -115,9 +115,9 @@ func processLink(in string, filePath string) (string, error) {
return out, nil
}
// checkLinks assumes lines has links in markdown syntax, and verifies that
// updateLinks assumes lines has links in markdown syntax, and verifies that
// any relative links actually point to files that exist.
func checkLinks(filePath string, mlines mungeLines) (mungeLines, error) {
func updateLinks(filePath string, mlines mungeLines) (mungeLines, error) {
var out mungeLines
errors := []string{}

View File

@ -35,7 +35,7 @@ func TestBadLinks(t *testing.T) {
}
for _, c := range cases {
in := getMungeLines(c.in)
_, err := checkLinks("filename.md", in)
_, err := updateLinks("filename.md", in)
assert.Error(t, err)
}
}
@ -67,7 +67,7 @@ func TestGoodLinks(t *testing.T) {
for i, c := range cases {
in := getMungeLines(c.in)
expected := getMungeLines(c.expected)
actual, err := checkLinks("filename.md", in)
actual, err := updateLinks("filename.md", in)
assert.NoError(t, err)
if !actual.Equal(expected) {
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())

View File

@ -46,13 +46,14 @@ Examples:
// All of the munge operations to perform.
// TODO: allow selection from command line. (e.g., just check links in the examples directory.)
allMunges = []munge{
{"remove-whitespace", updateWhitespace},
{"table-of-contents", updateTOC},
{"unversioned-warning", updateUnversionedWarning},
{"check-links", checkLinks},
{"blank-lines-surround-preformatted", checkPreformatted},
{"header-lines", checkHeaderLines},
{"md-links", updateLinks},
{"blank-lines-surround-preformatted", updatePreformatted},
{"header-lines", updateHeaderLines},
{"analytics", updateAnalytics},
{"kubectl-dash-f", checkKubectlFileTargets},
{"kubectl-dash-f", updateKubectlFileTargets},
{"sync-examples", syncExamples},
}
availableMungeList = func() string {

View File

@ -18,7 +18,7 @@ package main
// Blocks of ``` need to have blank lines on both sides or they don't look
// right in HTML.
func checkPreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
var out mungeLines
inpreformat := false
for i, mline := range mlines {

View File

@ -48,7 +48,7 @@ func TestPreformatted(t *testing.T) {
for i, c := range cases {
in := getMungeLines(c.in)
expected := getMungeLines(c.expected)
actual, err := checkPreformatted("filename.md", in)
actual, err := updatePreformatted("filename.md", in)
assert.NoError(t, err)
if !actual.Equal(expected) {
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())

View File

@ -0,0 +1,31 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// Remove all trailing whitespace
func updateWhitespace(file string, mlines mungeLines) (mungeLines, error) {
var out mungeLines
for _, mline := range mlines {
if mline.preformatted {
out = append(out, mline)
continue
}
newline := trimRightSpace(mline.data)
out = append(out, newMungeLine(newline))
}
return out, nil
}

View File

@ -0,0 +1,45 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_updateWhiteSpace(t *testing.T) {
var cases = []struct {
in string
expected string
}{
{"", ""},
{"\n", "\n"},
{" \t \t \n", "\n"},
{"bob \t", "bob"},
{"```\n \n```\n", "```\n \n```\n"},
}
for i, c := range cases {
in := getMungeLines(c.in)
expected := getMungeLines(c.expected)
actual, err := updateWhitespace("filename.md", in)
assert.NoError(t, err)
if !expected.Equal(actual) {
t.Errorf("Case[%d] Expected Whitespace '%v' but got '%v'", i, string(expected.Bytes()), string(actual.Bytes()))
}
}
}