package tex
import (
"strings"
"github.com/d2lang/mathjax-go/internal/mml"
)
func matrixCasesTextEnd(source string , start int ) (end , close int , err error ) {
braces , close := 0 , -1
i := start
for i < len (source ) {
switch source [i ] {
case '{' :
braces ++
i ++
case '}' :
if braces == 0 {
return i , close , nil
}
braces --
if braces == 0 && close < 0 {
close = i - start
}
i ++
case '&' :
if braces == 0 {
return i , close , texError ("ExtraAlignTab" , "Extra alignment tab in \\cases text" )
}
i ++
case '\\' :
rest := source [i :]
if strings .HasPrefix (rest , `\\` ) || strings .HasPrefix (rest , `\cr` ) && len (rest ) > 3 && !isASCIILetter (rune (rest [3 ])) {
return i , close , nil
}
i += 2
default :
i ++
}
}
return len (source ), close , nil
}
func (p *parser ) parseMatrixCasesCell (source string , terminator byte ) (*mml .Node , error ) {
_ , close , err := matrixCasesTextEnd (source , 0 )
if err != nil {
return nil , err
}
leading := strings .TrimLeftFunc (source , internalTextSpace )
if strings .HasPrefix (leading , `\text` ) && len (leading ) > 5 && !isASCIILetter (rune (leading [5 ])) &&
close == len (strings .TrimRightFunc (source , internalTextSpace ))-1 {
return p .parseMatrixCell (source , terminator )
}
text := strings .TrimFunc (source , internalTextSpace )
if strings .HasSuffix (text , `\` ) && strings .HasSuffix (source , " " ) {
text += " "
}
sub := p .matrixCellParser ("" )
children , err := sub .internalMath (text , "" , true )
if err != nil {
return nil , err
}
return matrixCellContent (children ), nil
}
func (p *parser ) prepareMatrixCasesText () ([]*mml .Node , error ) {
end , close , err := matrixCasesTextEnd (p .source +"}" , p .pos )
if err != nil {
return nil , err
}
if end > len (p .source ) {
end = len (p .source )
}
raw := p .source [p .pos :end ]
leading := strings .TrimLeftFunc (raw , internalTextSpace )
if strings .HasPrefix (leading , `\text` ) && len (leading ) > 5 && !isASCIILetter (rune (leading [5 ])) &&
close == len (strings .TrimRightFunc (raw , internalTextSpace ))-1 {
return nil , nil
}
p .pos = end
text := strings .TrimFunc (raw , internalTextSpace )
if strings .HasSuffix (text , `\` ) && strings .HasSuffix (raw , " " ) {
text += " "
}
return p .internalMath (text , "" , true )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .