From 3c95bd4ee395c32a04007b6a3657417b668ac3d5 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Fri, 24 Jul 2015 17:51:48 -0400 Subject: [PATCH] Add a whitespace munger Strips all trailing whitespace from non-preformatted blocks --- cmd/mungedocs/headers.go | 2 +- cmd/mungedocs/headers_test.go | 2 +- cmd/mungedocs/kubectl_dash_f.go | 2 +- cmd/mungedocs/kubectl_dash_f_test.go | 2 +- cmd/mungedocs/links.go | 4 +-- cmd/mungedocs/links_test.go | 4 +-- cmd/mungedocs/mungedocs.go | 9 +++--- cmd/mungedocs/preformatted.go | 2 +- cmd/mungedocs/preformatted_test.go | 2 +- cmd/mungedocs/whitespace.go | 31 +++++++++++++++++++ cmd/mungedocs/whitespace_test.go | 45 ++++++++++++++++++++++++++++ 11 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 cmd/mungedocs/whitespace.go create mode 100644 cmd/mungedocs/whitespace_test.go diff --git a/cmd/mungedocs/headers.go b/cmd/mungedocs/headers.go index 313714afd4d..6876a514785 100644 --- a/cmd/mungedocs/headers.go +++ b/cmd/mungedocs/headers.go @@ -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 { diff --git a/cmd/mungedocs/headers_test.go b/cmd/mungedocs/headers_test.go index 7b348b2f7f5..a73355beb55 100644 --- a/cmd/mungedocs/headers_test.go +++ b/cmd/mungedocs/headers_test.go @@ -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()) diff --git a/cmd/mungedocs/kubectl_dash_f.go b/cmd/mungedocs/kubectl_dash_f.go index cf196c12575..c4702220d2a 100644 --- a/cmd/mungedocs/kubectl_dash_f.go +++ b/cmd/mungedocs/kubectl_dash_f.go @@ -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 { diff --git a/cmd/mungedocs/kubectl_dash_f_test.go b/cmd/mungedocs/kubectl_dash_f_test.go index cf9a668856c..b6b0c243a89 100644 --- a/cmd/mungedocs/kubectl_dash_f_test.go +++ b/cmd/mungedocs/kubectl_dash_f_test.go @@ -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) } diff --git a/cmd/mungedocs/links.go b/cmd/mungedocs/links.go index a8afce8a666..60d7b9b73bd 100644 --- a/cmd/mungedocs/links.go +++ b/cmd/mungedocs/links.go @@ -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{} diff --git a/cmd/mungedocs/links_test.go b/cmd/mungedocs/links_test.go index b28d5742ab5..01889b4bfff 100644 --- a/cmd/mungedocs/links_test.go +++ b/cmd/mungedocs/links_test.go @@ -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()) diff --git a/cmd/mungedocs/mungedocs.go b/cmd/mungedocs/mungedocs.go index a1bcce392e3..f9afe89f529 100644 --- a/cmd/mungedocs/mungedocs.go +++ b/cmd/mungedocs/mungedocs.go @@ -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 { diff --git a/cmd/mungedocs/preformatted.go b/cmd/mungedocs/preformatted.go index 00e6a9f3238..f3864278ee0 100644 --- a/cmd/mungedocs/preformatted.go +++ b/cmd/mungedocs/preformatted.go @@ -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 { diff --git a/cmd/mungedocs/preformatted_test.go b/cmd/mungedocs/preformatted_test.go index 637d3598643..205a89e3ad3 100644 --- a/cmd/mungedocs/preformatted_test.go +++ b/cmd/mungedocs/preformatted_test.go @@ -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()) diff --git a/cmd/mungedocs/whitespace.go b/cmd/mungedocs/whitespace.go new file mode 100644 index 00000000000..ea3d764a219 --- /dev/null +++ b/cmd/mungedocs/whitespace.go @@ -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 +} diff --git a/cmd/mungedocs/whitespace_test.go b/cmd/mungedocs/whitespace_test.go new file mode 100644 index 00000000000..3b3570fbfaf --- /dev/null +++ b/cmd/mungedocs/whitespace_test.go @@ -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())) + } + } +}