package tex
import (
"fmt"
"github.com/d2lang/mathjax-go/internal/mml"
)
const poppedScriptOrigin = "poppedScriptOrigin"
func (p *parser ) publishPendingScript (attachment *scriptAttachment ) *mml .Node {
view := attachment .pendingBase ()
view .Properties = view .Properties .Clone ()
view .SetProperty (poppedScriptOrigin , true )
view .Children = append ([]*mml .Node (nil ), view .Children ...)
view .SetChildren (view .Children )
p .notePoppedScript (view )
return view
}
func (p *parser ) notePoppedScript (n *mml .Node ) {
if marked , _ := n .Property (poppedScriptOrigin ); marked == true {
p .state .poppedScripts = append (p .state .poppedScripts , n )
}
}
func scriptInTree(n , root *mml .Node ) bool {
for n != nil && n != root {
n = n .Parent
}
return n != nil
}
func cleanPoppedScripts(root *mml .Node , registered []*mml .Node ) (*mml .Node , error ) {
for _ , family := range []struct { generic , low , up string }{
{"msubsup" , "msub" , "msup" },
{"munderover" , "munder" , "mover" },
} {
for _ , current := range registered {
if current .Kind != family .generic || !scriptInTree (current , root ) {
continue
}
var low , up *mml .Node
if len (current .Children ) > 1 {
low = current .Children [1 ]
}
if len (current .Children ) > 2 {
up = current .Children [2 ]
}
if low != nil && up != nil {
continue
}
if low == nil && up == nil {
return nil , fmt .Errorf ("incomplete %s has no script child" , current .Kind )
}
kind , script := family .low , low
if low == nil {
kind , script = family .up , up
}
parent := current .Parent
replacement := node (kind , current .Children [0 ], script )
replacement .Attributes = current .Attributes
replacement .Properties = current .Properties .Clone ()
if value , ok := current .Property ("texClass" ); ok {
if class , ok := sourceInt (value ); ok {
replacement .TeXClass = mml .TeXClass (class )
}
}
if parent == nil {
root = replacement
} else if err := parent .ReplaceChild (replacement , current ); err != nil {
return nil , err
}
}
}
return root , nil
}
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 .