mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-19 21:10:15 +02:00
parent
c4bec47e7d
commit
e27f19c12f
@ -77,56 +77,55 @@ func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
return func() Token {
|
return func() Token {
|
||||||
for token := it(); token != EOF; token = it() {
|
token := it()
|
||||||
switch {
|
|
||||||
case token.Type == Name && strings.ToLower(token.Value) == "content-type":
|
|
||||||
{
|
|
||||||
isContentType = true
|
|
||||||
}
|
|
||||||
case token.Type == Literal && isContentType:
|
|
||||||
{
|
|
||||||
isContentType = false
|
|
||||||
contentType = strings.TrimSpace(token.Value)
|
|
||||||
pos := strings.Index(contentType, ";")
|
|
||||||
if pos > 0 {
|
|
||||||
contentType = strings.TrimSpace(contentType[:pos])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case token.Type == Generic && contentType != "":
|
|
||||||
{
|
|
||||||
lexer := internal.MatchMimeType(contentType)
|
|
||||||
|
|
||||||
// application/calendar+xml can be treated as application/xml
|
|
||||||
// if there's not a better match.
|
|
||||||
if lexer == nil && strings.Contains(contentType, "+") {
|
|
||||||
slashPos := strings.Index(contentType, "/")
|
|
||||||
plusPos := strings.LastIndex(contentType, "+")
|
|
||||||
contentType = contentType[:slashPos+1] + contentType[plusPos+1:]
|
|
||||||
lexer = internal.MatchMimeType(contentType)
|
|
||||||
}
|
|
||||||
|
|
||||||
if lexer == nil {
|
|
||||||
token.Type = Text
|
|
||||||
} else {
|
|
||||||
subIterator, err = lexer.Tokenise(nil, token.Value)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return EOF
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if token == EOF {
|
||||||
|
if subIterator != nil {
|
||||||
|
return subIterator()
|
||||||
}
|
}
|
||||||
|
return EOF
|
||||||
return token
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if subIterator != nil {
|
switch {
|
||||||
for token := subIterator(); token != EOF; token = subIterator() {
|
case token.Type == Name && strings.ToLower(token.Value) == "content-type":
|
||||||
return token
|
{
|
||||||
|
isContentType = true
|
||||||
}
|
}
|
||||||
|
case token.Type == Literal && isContentType:
|
||||||
|
{
|
||||||
|
isContentType = false
|
||||||
|
contentType = strings.TrimSpace(token.Value)
|
||||||
|
pos := strings.Index(contentType, ";")
|
||||||
|
if pos > 0 {
|
||||||
|
contentType = strings.TrimSpace(contentType[:pos])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case token.Type == Generic && contentType != "":
|
||||||
|
{
|
||||||
|
lexer := internal.MatchMimeType(contentType)
|
||||||
|
|
||||||
|
// application/calendar+xml can be treated as application/xml
|
||||||
|
// if there's not a better match.
|
||||||
|
if lexer == nil && strings.Contains(contentType, "+") {
|
||||||
|
slashPos := strings.Index(contentType, "/")
|
||||||
|
plusPos := strings.LastIndex(contentType, "+")
|
||||||
|
contentType = contentType[:slashPos+1] + contentType[plusPos+1:]
|
||||||
|
lexer = internal.MatchMimeType(contentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if lexer == nil {
|
||||||
|
token.Type = Text
|
||||||
|
} else {
|
||||||
|
subIterator, err = lexer.Tokenise(nil, token.Value)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return EOF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return EOF
|
return token
|
||||||
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user