package tex
import (
"github.com/d2lang/mathjax-go/internal/mml"
)
const (
amscdMinWidthState = "\x00amscd-min-width"
amscdMinHeightState = "\x00amscd-min-height"
)
func (p *parser ) amscdCommand (name string ) (nodes []*mml .Node , handled bool , err error ) {
key := ""
switch name {
case "minCDarrowwidth" :
key = amscdMinWidthState
case "minCDarrowheight" :
key = amscdMinHeightState
default :
return nil , false , nil
}
value , err := p .readDimension (name )
if err != nil {
return nil , true , err
}
p .state .macros [key ] = macroDefinition {body : value }
return nil , true , nil
}
func (p *parser ) amscdEnvironment (name string ) (nodes []*mml .Node , handled bool , err error ) {
if name != "CD" {
return nil , false , nil
}
body , err := p .captureEnvironment (name )
if err != nil {
return nil , true , err
}
nodes , err = p .parseCD (body )
if err != nil {
return nil , true , err
}
if len (nodes ) == 0 {
return nodes , true , nil
}
table := nodes [0 ]
for _ , row := range table .Children {
if len (row .Children ) != 0 && amscdCellHasVerticalArrow (row .Children [len (row .Children )-1 ]) {
row .AppendChild (node ("mtd" ))
}
}
resetTableAttributes (table ,
"columnalign" , "center" ,
"columnspacing" , "5pt" ,
"rowspacing" , tableRowSpacing (table ),
"displaystyle" , true ,
)
minWidth := p .amscdState (amscdMinWidthState , "2.75em" )
minHeight := p .amscdState (amscdMinHeightState , "1.75em" )
table .Walk (func (current *mml .Node ) bool {
if current .Kind != "mo" {
return true
}
text := textContent (current )
switch text {
case "→" , "←" , "=" :
amscdResetAttributes (current ,
"minsize" , minWidth ,
"stretchy" , true ,
)
case "↓" , "↑" , "‖" :
amscdResetAttributes (current ,
"minsize" , minHeight ,
"stretchy" , true ,
"symmetric" , true ,
"lspace" , 0 ,
"rspace" , 0 ,
)
}
return true
})
nodes [0 ] = finishArrayRules (table )
return nodes , true , nil
}
func amscdCellHasVerticalArrow(cell *mml .Node ) bool {
found := false
cell .Walk (func (current *mml .Node ) bool {
if current .Kind == "mo" {
switch textContent (current ) {
case "↓" , "↑" , "‖" :
found = true
}
}
return !found
})
return found
}
func (p *parser ) amscdState (key , fallback string ) string {
if definition , ok := p .state .macros [key ]; ok {
return definition .body
}
return fallback
}
func amscdResetAttributes(node *mml .Node , entries ...any ) {
for _ , name := range node .Attributes .ExplicitNames () {
node .Attributes .Explicit ().Delete (name )
}
for i := 0 ; i < len (entries ); i += 2 {
node .Attributes .Set (entries [i ].(string ), entries [i +1 ])
}
}
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 .