Skip to content

Commit

Permalink
Add a munger check for files that end in preformat
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Sep 25, 2015
1 parent 44b0bb1 commit 15e2c62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/mungedocs/mungedocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ 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{
// Simple "check something" functions must run first.
{"preformat-balance", checkPreformatBalance},
// Functions which modify state.
{"remove-whitespace", updateWhitespace},
{"table-of-contents", updateTOC},
{"unversioned-warning", updateUnversionedWarning},
Expand Down
10 changes: 10 additions & 0 deletions cmd/mungedocs/preformatted.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package main

import "fmt"

// Blocks of ``` need to have blank lines on both sides or they don't look
// right in HTML.
func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) {
Expand All @@ -39,3 +41,11 @@ func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error)
}
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
}
23 changes: 23 additions & 0 deletions cmd/mungedocs/preformatted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 15e2c62

Please sign in to comment.