Skip to content

Commit

Permalink
New cmd Ol, functions filecontents, decode-base64
Browse files Browse the repository at this point in the history
This adds the command Ol as well as the two layout functions
sd:file-contents() and sd:decode-base64
  • Loading branch information
pgundlach committed Jun 10, 2023
1 parent aeea989 commit a7ec488
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 109 deletions.
62 changes: 38 additions & 24 deletions core/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func init() {
"Message": cmdMessage,
"NextFrame": cmdNextFrame,
"NextRow": cmdNextRow,
"Ol": cmdOl,
"Options": cmdOptions,
"Pageformat": cmdPageformat,
"Paragraph": cmdParagraph,
Expand Down Expand Up @@ -132,7 +133,7 @@ func cmdA(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("a", seq, "", "", "cmdA", layoutelt.Line)
n, err := xd.getTextvalues("a", seq, map[string]string{}, "cmdA", layoutelt.Line)
n.Attr = append(n.Attr, html.Attribute{Key: "href", Val: attValues.Href})
return xpath.Sequence{n}, err
}
Expand Down Expand Up @@ -197,12 +198,12 @@ func cmdB(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("b", seq, "", "", "cmdB", layoutelt.Line)
n, err := xd.getTextvalues("b", seq, map[string]string{}, "cmdB", layoutelt.Line)
return xpath.Sequence{n}, err
}

func cmdBr(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
n, err := xd.getTextvalues("br", xpath.Sequence{}, "", "", "cmdBr", layoutelt.Line)
n, err := xd.getTextvalues("br", xpath.Sequence{}, map[string]string{}, "cmdBr", layoutelt.Line)
return xpath.Sequence{n}, err
}

Expand Down Expand Up @@ -835,7 +836,7 @@ func cmdI(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("i", seq, "", "", "cmdI", layoutelt.Line)
n, err := xd.getTextvalues("i", seq, map[string]string{}, "cmdI", layoutelt.Line)
return xpath.Sequence{n}, err
}

Expand Down Expand Up @@ -904,6 +905,15 @@ func cmdImage(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error)
return xpath.Sequence{hl}, nil
}

func cmdLi(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
seq, err := dispatch(xd, layoutelt, xd.data)
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("li", seq, map[string]string{}, "cmdLi", layoutelt.Line)
return xpath.Sequence{n}, err
}

func cmdLoadDataset(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
var err error
attValues := &struct {
Expand Down Expand Up @@ -1251,6 +1261,15 @@ func cmdNextRow(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, erro
return nil, nil
}

func cmdOl(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
seq, err := dispatch(xd, layoutelt, xd.data)
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("ol", seq, map[string]string{}, "cmdol", layoutelt.Line)
return xpath.Sequence{n}, err
}

func cmdOptions(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
var err error
attValues := &struct {
Expand Down Expand Up @@ -1308,12 +1327,8 @@ func cmdPageformat(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, e
func cmdParagraph(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
var err error
attValues := &struct {
Class string
ID string
Color string
Features string
FontFamily string
Textformat *string
Class string
ID string
}{}
if err = getXMLAttributes(xd, layoutelt, attValues); err != nil {
return nil, err
Expand All @@ -1326,7 +1341,12 @@ func cmdParagraph(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, er
if seq == nil {
seq = xpath.Sequence{}
}
n, err := xd.getTextvalues("p", seq, attValues.Class, attValues.ID, "cmdParagraph", layoutelt.Line)
attributes := map[string]string{
"class": attValues.Class,
"id": attValues.ID,
}

n, err := xd.getTextvalues("p", seq, attributes, "cmdParagraph", layoutelt.Line)
return xpath.Sequence{n}, err
}

Expand Down Expand Up @@ -1639,8 +1659,11 @@ func cmdSpan(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error)
if err != nil {
return nil, err
}

n, err := xd.getTextvalues("span", seq, attValues.Class, attValues.ID, "cmdSpan", layoutelt.Line)
attributes := map[string]string{
"class": attValues.Class,
"id": attValues.ID,
}
n, err := xd.getTextvalues("span", seq, attributes, "cmdSpan", layoutelt.Line)

return xpath.Sequence{n}, err
}
Expand Down Expand Up @@ -2196,16 +2219,7 @@ func cmdU(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("u", seq, "", "", "cmdU", layoutelt.Line)
return xpath.Sequence{n}, err
}

func cmdLi(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
seq, err := dispatch(xd, layoutelt, xd.data)
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("li", seq, "", "", "cmdLi", layoutelt.Line)
n, err := xd.getTextvalues("u", seq, map[string]string{}, "cmdU", layoutelt.Line)
return xpath.Sequence{n}, err
}

Expand All @@ -2214,7 +2228,7 @@ func cmdUl(xd *xtsDocument, layoutelt *goxml.Element) (xpath.Sequence, error) {
if err != nil {
return nil, err
}
n, err := xd.getTextvalues("ul", seq, "", "", "cmdul", layoutelt.Line)
n, err := xd.getTextvalues("ul", seq, map[string]string{}, "cmdul", layoutelt.Line)
return xpath.Sequence{n}, err
}

Expand Down
8 changes: 4 additions & 4 deletions core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ func debugFrontendText(fe *frontend.Text, level int) {

// Get the values from the child elements of B, Paragraph and its ilk and return
// the equivalent HTML structure.
func (xd *xtsDocument) getTextvalues(tagname string, seq xpath.Sequence, class string, id string, cmdname string, line int) (*html.Node, error) {
func (xd *xtsDocument) getTextvalues(tagname string, seq xpath.Sequence, attributes map[string]string, cmdname string, line int) (*html.Node, error) {
n := &html.Node{}
n.Data = tagname
n.Type = html.ElementNode
n.Attr = append(n.Attr, html.Attribute{Key: "class", Val: class})
n.Attr = append(n.Attr, html.Attribute{Key: "id", Val: id})
for k, v := range attributes {
n.Attr = append(n.Attr, html.Attribute{Key: k, Val: v})
}

if len(seq) == 0 && tagname == "p" {
cd := &html.Node{}
Expand All @@ -193,7 +194,6 @@ func (xd *xtsDocument) getTextvalues(tagname string, seq xpath.Sequence, class s
n.AppendChild(cd)
return n, nil
}

for _, itm := range seq {
switch t := itm.(type) {
case string:
Expand Down
36 changes: 36 additions & 0 deletions core/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"math"
"os"
"regexp"
"strconv"
"strings"

Expand All @@ -22,9 +25,11 @@ func init() {
goxpath.RegisterFunction(&goxpath.Function{Name: "attribute", Namespace: fnNS, F: fnAttribute, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "current-page", Namespace: fnNS, F: fnCurrentPage, MinArg: 0, MaxArg: 0})
goxpath.RegisterFunction(&goxpath.Function{Name: "current-row", Namespace: fnNS, F: fnCurrentRow, MinArg: 0, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "decode-base64", Namespace: fnNS, F: fnDecodeBase64, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "decode-html", Namespace: fnNS, F: fnDecodeHTML, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "dummy-text", Namespace: fnNS, F: fnDummytext, MinArg: 0, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "even", Namespace: fnNS, F: fnEven, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "file-contents", Namespace: fnNS, F: fnFileContents, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "file-exists", Namespace: fnNS, F: fnFileExists, MinArg: 1, MaxArg: 1})
goxpath.RegisterFunction(&goxpath.Function{Name: "format-number", Namespace: fnNS, F: fnFormatNumber, MinArg: 3, MaxArg: 3})
goxpath.RegisterFunction(&goxpath.Function{Name: "group-height", Namespace: fnNS, F: fnGroupheight, MinArg: 1, MaxArg: 2})
Expand All @@ -47,6 +52,8 @@ func init() {
goxpath.RegisterFunction(&goxpath.Function{Name: "total-pages", Namespace: fnNS, F: fnTotalPages, MinArg: 1, MaxArg: 1})
}

var spaceRemover = regexp.MustCompile(`(?m)\s*`)

// args are the arguments from the xpath function, fnname is the function name
// usd for error messages. The function returns one of "/MediaBox", "/CropBox",
// "/TrimBox", "/BleedBox" or "/ArtBox".
Expand Down Expand Up @@ -211,6 +218,13 @@ func fnCurrentRow(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequen
return goxpath.Sequence{int(area.CurrentRow())}, nil
}

func fnDecodeBase64(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequence, error) {
firstArg := args[0].Stringvalue()
firstArg = spaceRemover.ReplaceAllString(firstArg, "")
data, err := base64.StdEncoding.DecodeString(firstArg)
return goxpath.Sequence{data}, err
}

func fnDecodeHTML(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequence, error) {
firstArg := args[0]
text := firstArg.Stringvalue()
Expand Down Expand Up @@ -480,6 +494,28 @@ func fnGroupwidth(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequen

}

func fnFileContents(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequence, error) {
firstarg := args[0]
f, err := os.CreateTemp("", "xts-image")
if err != nil {
return nil, err
}
bag.Logger.Infof("Create temporary file %s", f.Name())
for _, itm := range firstarg {
switch t := itm.(type) {
case []uint8:
_, err = f.Write(t)
if err != nil {
return nil, err
}
default:
bag.Logger.DPanicf("Unknown data type in sd:file-contents")
}
}
defer f.Close()
return goxpath.Sequence{f.Name()}, nil
}

func fnFileExists(ctx *goxpath.Context, args []goxpath.Sequence) (goxpath.Sequence, error) {
seq := args[0]
return goxpath.Sequence{fileexists(seq.Stringvalue())}, nil
Expand Down
Loading

0 comments on commit a7ec488

Please sign in to comment.