mirror of
https://github.com/alecthomas/chroma.git
synced 2025-07-13 01:10:14 +02:00
move SplitTokensIntoLines into chroma/iterator.go, fixes issue #190
This commit is contained in:
committed by
Alec Thomas
parent
9c3abeae1d
commit
a4f179974b
25
iterator.go
25
iterator.go
@ -1,5 +1,7 @@
|
||||
package chroma
|
||||
|
||||
import "strings"
|
||||
|
||||
// An Iterator across tokens.
|
||||
//
|
||||
// nil will be returned at the end of the Token stream.
|
||||
@ -41,3 +43,26 @@ func Literator(tokens ...Token) Iterator {
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
func SplitTokensIntoLines(tokens []Token) (out [][]Token) {
|
||||
var line []Token
|
||||
for _, token := range tokens {
|
||||
for strings.Contains(token.Value, "\n") {
|
||||
parts := strings.SplitAfterN(token.Value, "\n", 2)
|
||||
// Token becomes the tail.
|
||||
token.Value = parts[1]
|
||||
|
||||
// Append the head to the line and flush the line.
|
||||
clone := token.Clone()
|
||||
clone.Value = parts[0]
|
||||
line = append(line, clone)
|
||||
out = append(out, line)
|
||||
line = nil
|
||||
}
|
||||
line = append(line, token)
|
||||
}
|
||||
if len(line) > 0 {
|
||||
out = append(out, line)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user