Commit 15e2c62a authored by Tim Hockin's avatar Tim Hockin

Add a munger check for files that end in preformat

parent 44b0bb1a
...@@ -46,6 +46,9 @@ Examples: ...@@ -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. ...@@ -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) ...@@ -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) { ...@@ -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)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment