Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
95cd66d3
Commit
95cd66d3
authored
Jul 10, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use 'lines []string' for updateMacroBlock
parent
2e781fed
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
9 deletions
+16
-9
toc.go
cmd/mungedocs/toc.go
+2
-1
util.go
cmd/mungedocs/util.go
+12
-6
util_test.go
cmd/mungedocs/util_test.go
+2
-2
No files found.
cmd/mungedocs/toc.go
View file @
95cd66d3
...
...
@@ -35,7 +35,8 @@ func updateTOC(filePath string, markdown []byte) ([]byte, error) {
if
err
!=
nil
{
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
{
return
nil
,
err
}
...
...
cmd/mungedocs/util.go
View file @
95cd66d3
...
...
@@ -22,17 +22,23 @@ import (
"strings"
)
// Replaces the text between matching "beginMark" and "endMark" within "document" with "insertThis".
//
// 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
// Splits a document up into a slice of lines.
func
splitLines
(
document
[]
byte
)
[]
string
{
lines
:=
strings
.
Split
(
string
(
document
),
"
\n
"
)
// Skip trailing empty string from Split-ing
if
len
(
lines
)
>
0
&&
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
for
_
,
line
:=
range
lines
{
trimmedLine
:=
strings
.
Trim
(
line
,
"
\n
"
)
...
...
cmd/mungedocs/util_test.go
View file @
95cd66d3
...
...
@@ -34,7 +34,7 @@ func Test_updateMacroBlock(t *testing.T) {
"Lorem ipsum
\n
BEGIN
\n
foo
\n\n
END
\n
sit amet
\n
"
},
}
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
)
if
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
\n
BEGIN
\n
END
\n
END"
},
}
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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment