use 'lines []string' for updateMacroBlock

This commit is contained in:
Tim Hockin
2015-07-10 15:54:12 -07:00
parent 2e781fed49
commit 95cd66d3a0
3 changed files with 16 additions and 9 deletions

View File

@@ -35,7 +35,8 @@ func updateTOC(filePath string, markdown []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
updatedMarkdown, err := updateMacroBlock(markdown, "<!-- BEGIN GENERATED TOC -->", "<!-- END GENERATED TOC -->", string(toc)) lines := splitLines(markdown)
updatedMarkdown, err := updateMacroBlock(lines, "<!-- BEGIN GENERATED TOC -->", "<!-- END GENERATED TOC -->", string(toc))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -22,17 +22,23 @@ import (
"strings" "strings"
) )
// Replaces the text between matching "beginMark" and "endMark" within "document" with "insertThis". // Splits a document up into a slice of lines.
// func splitLines(document []byte) []string {
// Delimiters should occupy own line.
// Returns copy of document with modifications.
func updateMacroBlock(document []byte, beginMark, endMark, insertThis string) ([]byte, error) {
var buffer bytes.Buffer
lines := strings.Split(string(document), "\n") lines := strings.Split(string(document), "\n")
// Skip trailing empty string from Split-ing // Skip trailing empty string from Split-ing
if len(lines) > 0 && lines[len(lines)-1] == "" { if len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1] lines = lines[:len(lines)-1]
} }
return lines
}
// Replaces the text between matching "beginMark" and "endMark" within the
// document represented by "lines" with "insertThis".
//
// Delimiters should occupy own line.
// Returns copy of document with modifications.
func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]byte, error) {
var buffer bytes.Buffer
betweenBeginAndEnd := false betweenBeginAndEnd := false
for _, line := range lines { for _, line := range lines {
trimmedLine := strings.Trim(line, " \n") trimmedLine := strings.Trim(line, " \n")

View File

@@ -34,7 +34,7 @@ func Test_updateMacroBlock(t *testing.T) {
"Lorem ipsum \n BEGIN\nfoo\n\nEND\nsit amet\n"}, "Lorem ipsum \n BEGIN\nfoo\n\nEND\nsit amet\n"},
} }
for _, c := range cases { for _, c := range cases {
actual, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo\n") actual, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo\n")
assert.NoError(t, err) assert.NoError(t, err)
if c.out != string(actual) { if c.out != string(actual) {
t.Errorf("Expected '%v' but got '%v'", c.out, string(actual)) t.Errorf("Expected '%v' but got '%v'", c.out, string(actual))
@@ -56,7 +56,7 @@ func Test_updateMacroBlock_errors(t *testing.T) {
{"BEGIN\nBEGIN\nEND\nEND"}, {"BEGIN\nBEGIN\nEND\nEND"},
} }
for _, c := range cases { for _, c := range cases {
_, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo") _, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo")
assert.Error(t, err) assert.Error(t, err)
} }
} }