chroma-markdown/vendor/github.com/alecthomas/chroma/iterator.go

77 lines
1.7 KiB
Go
Raw Normal View History

2017-10-22 05:37:38 +02:00
package chroma
import "strings"
2017-10-22 05:37:38 +02:00
// An Iterator across tokens.
//
// EOF will be returned at the end of the Token stream.
2017-10-22 05:37:38 +02:00
//
// If an error occurs within an Iterator, it may propagate this in a panic. Formatters should recover.
type Iterator func() Token
2017-10-22 05:37:38 +02:00
// Tokens consumes all tokens from the iterator and returns them as a slice.
func (i Iterator) Tokens() []Token {
var out []Token
for t := i(); t != EOF; t = i() {
2017-10-22 05:37:38 +02:00
out = append(out, t)
}
return out
}
// Concaterator concatenates tokens from a series of iterators.
func Concaterator(iterators ...Iterator) Iterator {
return func() Token {
2017-10-22 05:37:38 +02:00
for len(iterators) > 0 {
t := iterators[0]()
if t != EOF {
2017-10-22 05:37:38 +02:00
return t
}
iterators = iterators[1:]
}
return EOF
2017-10-22 05:37:38 +02:00
}
}
// Literator converts a sequence of literal Tokens into an Iterator.
func Literator(tokens ...Token) Iterator {
return func() Token {
2017-10-22 05:37:38 +02:00
if len(tokens) == 0 {
return EOF
2017-10-22 05:37:38 +02:00
}
token := tokens[0]
tokens = tokens[1:]
return token
}
}
2019-07-01 05:14:09 +02:00
// SplitTokensIntoLines splits tokens containing newlines in two.
func SplitTokensIntoLines(tokens []Token) (out [][]Token) {
2019-07-01 05:14:09 +02:00
var line []Token // nolint: prealloc
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)
}
// Strip empty trailing token line.
if len(out) > 0 {
last := out[len(out)-1]
if len(last) == 1 && last[0].Value == "" {
out = out[:len(out)-1]
}
}
return
}