package tex
import (
"strings"
"github.com/d2lang/mathjax-go/internal/mml"
)
const arrayRulesProperty = "arrayRules"
type arrayRules struct {
rows func () int
frame []string
lines []string
dashed bool
}
func newArrayRules(table *mml .Node ) *arrayRules {
rules := &arrayRules {rows : func () int { return len (table .Children ) }}
table .SetProperty (arrayRulesProperty , rules )
return rules
}
func isHLine(name string ) bool { return name == "hline" || name == "hdashline" }
func misplacedHLine(name string ) error {
return texError ("Misplaced" , "Misplaced %s" , "\\" +name )
}
func (rules *arrayRules ) add (name string ) {
rows := rules .rows ()
if rows == 0 {
rules .frame = append (rules .frame , "top" )
return
}
for len (rules .lines ) < rows {
rules .lines = append (rules .lines , "none" )
}
style := "solid"
if name == "hdashline" {
style = "dashed"
}
rules .lines [rows -1 ] = style
}
func tableArrayRules(table *mml .Node ) *arrayRules {
if value , ok := table .Property (arrayRulesProperty ); ok {
if rules , ok := value .(*arrayRules ); ok {
return rules
}
}
return newArrayRules (table )
}
func finishArrayRules(table *mml .Node ) *mml .Node {
value , ok := table .Property (arrayRulesProperty )
if !ok {
return table
}
rules := value .(*arrayRules )
table .RemoveProperty (arrayRulesProperty )
if len (rules .lines ) != 0 {
if len (rules .lines ) == len (table .Children ) {
rules .frame = append (rules .frame , "bottom" )
rules .lines = rules .lines [:len (rules .lines )-1 ]
} else if len (rules .lines ) < len (table .Children )-1 {
rules .lines = append (rules .lines , "none" )
}
table .Attributes .Set ("rowlines" , strings .Join (rules .lines , " " ))
}
if len (rules .frame ) == 4 {
style := "solid"
if rules .dashed {
style = "dashed"
}
table .Attributes .Set ("frame" , style )
return table
}
if len (rules .frame ) == 0 {
return table
}
rowLines := append ([]string (nil ), rules .lines ...)
for len (rowLines ) > 1 && rowLines [len (rowLines )-1 ] == "none" && rowLines [len (rowLines )-2 ] == "none" {
rowLines = rowLines [:len (rowLines )-1 ]
}
columnLines , _ := table .Attributes .Get ("columnlines" )
columnText , rowText := sourceValueString (columnLines ), strings .Join (rowLines , " " )
table .Attributes .Set ("frame" , "" )
enclosure := setAttributes (node ("menclose" , table ), map [string ]any {"notation" : strings .Join (rules .frame , " " )})
if (columnText != "" && columnText != "none" ) || (rowText != "" && rowText != "none" ) {
enclosure .Attributes .Set ("data-padding" , 0 )
}
return enclosure
}
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 .