mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +00:00
Add a whitespace munger
Strips all trailing whitespace from non-preformatted blocks
This commit is contained in:
parent
8886a9940d
commit
3c95bd4ee3
@ -56,7 +56,7 @@ func fixHeaderLine(mlines mungeLines, newlines mungeLines, linenum int) mungeLin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Header lines need whitespace around them and after the #s.
|
// 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
|
var out mungeLines
|
||||||
for i, mline := range mlines {
|
for i, mline := range mlines {
|
||||||
if mline.preformatted {
|
if mline.preformatted {
|
||||||
|
@ -64,7 +64,7 @@ func TestHeaderLines(t *testing.T) {
|
|||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
in := getMungeLines(c.in)
|
in := getMungeLines(c.in)
|
||||||
expected := getMungeLines(c.expected)
|
expected := getMungeLines(c.expected)
|
||||||
actual, err := checkHeaderLines("filename.md", in)
|
actual, err := updateHeaderLines("filename.md", in)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
if !actual.Equal(expected) {
|
if !actual.Equal(expected) {
|
||||||
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
// Looks for lines that have kubectl commands with -f flags and files that
|
// Looks for lines that have kubectl commands with -f flags and files that
|
||||||
// don't exist.
|
// don't exist.
|
||||||
func checkKubectlFileTargets(file string, mlines mungeLines) (mungeLines, error) {
|
func updateKubectlFileTargets(file string, mlines mungeLines) (mungeLines, error) {
|
||||||
var errors []string
|
var errors []string
|
||||||
for i, mline := range mlines {
|
for i, mline := range mlines {
|
||||||
if !mline.preformatted {
|
if !mline.preformatted {
|
||||||
|
@ -132,7 +132,7 @@ func TestKubectlDashF(t *testing.T) {
|
|||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
repoRoot = ""
|
repoRoot = ""
|
||||||
in := getMungeLines(c.in)
|
in := getMungeLines(c.in)
|
||||||
_, err := checkKubectlFileTargets("filename.md", in)
|
_, err := updateKubectlFileTargets("filename.md", in)
|
||||||
if err != nil && c.ok {
|
if err != nil && c.ok {
|
||||||
t.Errorf("case[%d]: expected success, got %v", i, err)
|
t.Errorf("case[%d]: expected success, got %v", i, err)
|
||||||
}
|
}
|
||||||
|
@ -115,9 +115,9 @@ func processLink(in string, filePath string) (string, error) {
|
|||||||
return out, nil
|
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.
|
// 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
|
var out mungeLines
|
||||||
errors := []string{}
|
errors := []string{}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ func TestBadLinks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
in := getMungeLines(c.in)
|
in := getMungeLines(c.in)
|
||||||
_, err := checkLinks("filename.md", in)
|
_, err := updateLinks("filename.md", in)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ func TestGoodLinks(t *testing.T) {
|
|||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
in := getMungeLines(c.in)
|
in := getMungeLines(c.in)
|
||||||
expected := getMungeLines(c.expected)
|
expected := getMungeLines(c.expected)
|
||||||
actual, err := checkLinks("filename.md", in)
|
actual, err := updateLinks("filename.md", in)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
if !actual.Equal(expected) {
|
if !actual.Equal(expected) {
|
||||||
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
||||||
|
@ -46,13 +46,14 @@ 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{
|
||||||
|
{"remove-whitespace", updateWhitespace},
|
||||||
{"table-of-contents", updateTOC},
|
{"table-of-contents", updateTOC},
|
||||||
{"unversioned-warning", updateUnversionedWarning},
|
{"unversioned-warning", updateUnversionedWarning},
|
||||||
{"check-links", checkLinks},
|
{"md-links", updateLinks},
|
||||||
{"blank-lines-surround-preformatted", checkPreformatted},
|
{"blank-lines-surround-preformatted", updatePreformatted},
|
||||||
{"header-lines", checkHeaderLines},
|
{"header-lines", updateHeaderLines},
|
||||||
{"analytics", updateAnalytics},
|
{"analytics", updateAnalytics},
|
||||||
{"kubectl-dash-f", checkKubectlFileTargets},
|
{"kubectl-dash-f", updateKubectlFileTargets},
|
||||||
{"sync-examples", syncExamples},
|
{"sync-examples", syncExamples},
|
||||||
}
|
}
|
||||||
availableMungeList = func() string {
|
availableMungeList = func() string {
|
||||||
|
@ -18,7 +18,7 @@ package main
|
|||||||
|
|
||||||
// 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 checkPreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
|
func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
|
||||||
var out mungeLines
|
var out mungeLines
|
||||||
inpreformat := false
|
inpreformat := false
|
||||||
for i, mline := range mlines {
|
for i, mline := range mlines {
|
||||||
|
@ -48,7 +48,7 @@ func TestPreformatted(t *testing.T) {
|
|||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
in := getMungeLines(c.in)
|
in := getMungeLines(c.in)
|
||||||
expected := getMungeLines(c.expected)
|
expected := getMungeLines(c.expected)
|
||||||
actual, err := checkPreformatted("filename.md", in)
|
actual, err := updatePreformatted("filename.md", in)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
if !actual.Equal(expected) {
|
if !actual.Equal(expected) {
|
||||||
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
|
||||||
|
31
cmd/mungedocs/whitespace.go
Normal file
31
cmd/mungedocs/whitespace.go
Normal 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
|
||||||
|
}
|
45
cmd/mungedocs/whitespace_test.go
Normal file
45
cmd/mungedocs/whitespace_test.go
Normal 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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user