Skip to content

Commit

Permalink
Table column width with colgroup/col
Browse files Browse the repository at this point in the history
  • Loading branch information
pgundlach committed Jul 11, 2023
1 parent f0c7906 commit d4cdd71
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 65 deletions.
63 changes: 18 additions & 45 deletions core/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"

Expand All @@ -28,9 +27,6 @@ type commandFunc func(*xtsDocument, *goxml.Element) (xpath.Sequence, error)
var (
dataDispatcher = make(map[string]map[string]*goxml.Element)
dispatchTable map[string]commandFunc
onlyUnitRE = regexp.MustCompile(`^(sp|mm|cm|in|pt|px|pc|m)$`)
unitRE = regexp.MustCompile(`(.*?)(sp|mm|cm|in|pt|px|pc|m)`)
astRE = regexp.MustCompile(`(\d*)\*`)
)

func init() {
Expand Down Expand Up @@ -422,45 +418,12 @@ func cmdColumn(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error
if err = getXMLAttributes(xd, layoutelt, attValues); err != nil {
return nil, err
}
g := node.NewGlue()
split := strings.Split(attValues.Width, "plus")
var unitString string
var stretchString string
if len(split) == 1 {
if unitRE.MatchString(split[0]) {
unitString = split[0]
} else if astRE.MatchString(split[0]) {
stretchString = split[0]
}
} else {
if unitRE.MatchString(split[0]) {
unitString = split[0]
}
if astRE.MatchString(split[1]) {
stretchString = split[1]
}
}

if unitString != "" {
g.Width = bag.MustSp(unitString)
}
if astRE.MatchString(stretchString) {
astMatch := astRE.FindAllStringSubmatch(stretchString, -1)
if c := astMatch[0][1]; c != "" {
stretch, err := strconv.Atoi(c)
if err != nil {
return nil, err
}
g.Stretch = bag.ScaledPoint(stretch) * bag.Factor
} else {
g.Stretch = bag.Factor
}
g.StretchOrder = 1
}
cs := frontend.ColSpec{
ColumnWidth: g,
colNode := &html.Node{
Data: "col",
Type: html.ElementNode,
}
return xpath.Sequence{cs}, nil
colNode.Attr = append(colNode.Attr, html.Attribute{Key: "width", Val: attValues.Width})
return xpath.Sequence{colNode}, nil
}

func cmdColumns(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
Expand Down Expand Up @@ -1642,6 +1605,11 @@ func cmdTable(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error)
Data: "tbody",
Type: html.ElementNode,
}
tableColgroupNode := &html.Node{
Data: "colgroup",
Type: html.ElementNode,
}

for _, itm := range seq {
switch t := itm.(type) {
case *html.Node:
Expand All @@ -1650,13 +1618,18 @@ func cmdTable(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error)
tableBodyNode.AppendChild(t)
case "thead":
tableNode.AppendChild(t)
case "col":
tableColgroupNode.AppendChild(t)
default:
bag.Logger.DPanicf("cmdTable: unknown html node %s", t.Data)
}
default:
bag.Logger.DPanic("table append item, unknown type")
bag.Logger.DPanicf("table append item, unknown type %t", t)
}
// fixme: colspec!
}

if tableColgroupNode.FirstChild != nil {
tableNode.AppendChild(tableColgroupNode)
}
tableNode.AppendChild(tableBodyNode)

doc := &html.Node{
Expand Down
4 changes: 4 additions & 0 deletions core/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (

const fnNS = "urn:speedata.de/2021/xtsfunctions/en"

var (
onlyUnitRE = regexp.MustCompile(`^(sp|mm|cm|in|pt|px|pc|m)$`)
)

func init() {
goxpath.RegisterFunction(&goxpath.Function{Name: "aspect-ratio", Namespace: fnNS, F: fnAspectRatio, MinArg: 1, MaxArg: 3})
goxpath.RegisterFunction(&goxpath.Function{Name: "attribute", Namespace: fnNS, F: fnAttribute, MinArg: 1, MaxArg: 1})
Expand Down
97 changes: 78 additions & 19 deletions helper/genschema/relaxng.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func genRelaxNGSchema(commands *commandsXML, lang string, allowForeignNodes bool
var interleave, group xml.StartElement

enc := xml.NewEncoder(&outbuf)
enc.Indent("", " ")
enc.Indent("", " ")

grammar := xml.StartElement{Name: xml.Name{Local: "grammar", Space: RELAXNG}}
grammar.Attr = []xml.Attr{
Expand Down Expand Up @@ -299,28 +299,87 @@ func genRelaxNGSchema(commands *commandsXML, lang string, allowForeignNodes bool
enc.Flush()
fmt.Fprint(&outbuf, `
<!-- allow HTML in <Value> ... </Value> -->
<define name="htmlclassidstyle">
<optional><attribute name="class"/></optional>
<optional><attribute name="id"/></optional>
<optional><attribute name="style"/></optional>
</define>
<define name="html">
<zeroOrMore>
<choice>
<element name="a">
<attribute name="href"/>
<ref name="html"/>
</element>
<element name="b"><ref name="html" /></element>
<element name="br"><empty /></element>
<element name="code"><ref name="html" /></element>
<element name="i"><ref name="html" /></element>
<element name="kbd"><ref name="html" /></element>
<element name="li"><ref name="html" /></element>
<element name="p"><ref name="html" /></element>
<element name="span"><ref name="html" /><oneOrMore><attribute><anyName/></attribute></oneOrMore></element>
<element name="u"><ref name="html" /></element>
<element name="ul"><ref name="html" /></element>
<text></text>
</choice>
<choice>
<element name="a">
<attribute name="href"/>
<ref name="html"/>
</element>
<element name="b"><ref name="html" /></element>
<element name="br"><empty /></element>
<element name="code"><ref name="html" /></element>
<element name="i"><ref name="html" /></element>
<element name="kbd"><ref name="html" /></element>
<element name="li"><ref name="html" /></element>
<element name="p"><ref name="html" /></element>
<element name="span"><ref name="html" /><oneOrMore><attribute><anyName/></attribute></oneOrMore></element>
<element name="table"><ref name="htmltable" /></element>
<element name="u"><ref name="html" /></element>
<element name="ul"><ref name="html" /></element>
<text></text>
</choice>
</zeroOrMore>
</define>
<define name="htmltable">
<ref name="htmlclassidstyle"/>
<zeroOrMore>
<ref name="colgroup"/>
</zeroOrMore>
<optional>
<element name="thead">
<ref name="htmlclassidstyle" />
<oneOrMore>
<ref name="tr"/>
</oneOrMore>
</element>
</optional>
<choice>
<optional>
<element name="tbody">
<ref name="htmlclassidstyle" />
<oneOrMore>
<ref name="tr"/>
</oneOrMore>
</element>
</optional>
<oneOrMore>
<ref name="tr"/>
</oneOrMore>
</choice>
</define>
<define name="colgroup">
<element name="colgroup">
<oneOrMore>
<element name="col">
<attribute name="width"/>
<empty />
</element>
</oneOrMore>
</element>
</define>
<define name="tr">
<element name="tr">
<ref name="htmlclassidstyle"/>
<oneOrMore>
<choice>
<element name="td">
<ref name="htmlclassidstyle"></ref>
<ref name="html"/>
</element>
<element name="th">
<ref name="htmlclassidstyle"></ref>
<ref name="html"/>
</element>
</choice>
</oneOrMore>
</element>
</define>
`)
if allowForeignNodes {
enc.Flush()
Expand Down
2 changes: 1 addition & 1 deletion schema/catalog-schema-de.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<uri name="urn:speedata.de/2021/xts/en" uri="layoutschema-de.rng"/>
<uri name="urn:speedata.de/2021/xts/en" uri="layoutschema-de.xsd"/>
</catalog>

0 comments on commit d4cdd71

Please sign in to comment.