Zettelstore Client

Check-in Differences
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From v0.11.0 To trunk

2023-05-24
18:39
Update to latest sxpf ... (Leaf check-in: d929a2bc57 user: stern tags: trunk)
2023-05-22
16:39
Adapt to sxpf ... (check-in: 587c1eee26 user: stern tags: trunk)
2023-04-02
16:14
Add metadata key 'expire' ... (check-in: e11f223228 user: stern tags: trunk)
2023-03-27
14:37
Version 0.11.0 ... (check-in: 331cad470c user: stern tags: trunk, release, v0.11.0)
14:34
Version 0.11.0 ... (check-in: 712c21afc8 user: stern tags: trunk, release, v0.10.0)

Changes to api/api.go.

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	ZettelCanCreate              // Current user is allowed to create a new zettel
	ZettelCanRead                // Requesting user is allowed to read the zettel
	ZettelCanWrite               // Requesting user is allowed to update the zettel
	ZettelCanRename              // Requesting user is allowed to provide the zettel with a new identifier
	ZettelCanDelete              // Requesting user is allowed to delete the zettel
)

// AuthJSON contains the result of an authentication call.
type AuthJSON struct {
	Token   string `json:"token"`
	Type    string `json:"token_type"`
	Expires int    `json:"expires_in"`
}

// ZidJSON contains the identifier data of a zettel.
type ZidJSON struct {
	ID ZettelID `json:"id"`
}

// MetaJSON contains the metadata of a zettel.
type MetaJSON struct {







<
<
<
<
<
<
<







43
44
45
46
47
48
49







50
51
52
53
54
55
56
	ZettelCanCreate              // Current user is allowed to create a new zettel
	ZettelCanRead                // Requesting user is allowed to read the zettel
	ZettelCanWrite               // Requesting user is allowed to update the zettel
	ZettelCanRename              // Requesting user is allowed to provide the zettel with a new identifier
	ZettelCanDelete              // Requesting user is allowed to delete the zettel
)








// ZidJSON contains the identifier data of a zettel.
type ZidJSON struct {
	ID ZettelID `json:"id"`
}

// MetaJSON contains the metadata of a zettel.
type MetaJSON struct {

Changes to api/const.go.

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	ZidBoxManager           = ZettelID("00000000000020")
	ZidMetadataKey          = ZettelID("00000000000090")
	ZidParser               = ZettelID("00000000000092")
	ZidStartupConfiguration = ZettelID("00000000000096")
	ZidConfiguration        = ZettelID("00000000000100")

	// WebUI HTML templates are in the range 10000..19999
	ZidBaseTemplate    = ZettelID("00000000010100")
	ZidLoginTemplate   = ZettelID("00000000010200")
	ZidListTemplate    = ZettelID("00000000010300")
	ZidZettelTemplate  = ZettelID("00000000010401")
	ZidInfoTemplate    = ZettelID("00000000010402")
	ZidFormTemplate    = ZettelID("00000000010403")
	ZidRenameTemplate  = ZettelID("00000000010404")
	ZidDeleteTemplate  = ZettelID("00000000010405")
	ZidContextTemplate = ZettelID("00000000010406")
	ZidErrorTemplate   = ZettelID("00000000010700")

	// CSS-related zettel are in the range 20000..29999
	ZidBaseCSS    = ZettelID("00000000020001")
	ZidUserCSS    = ZettelID("00000000025001")
	ZidRoleCSSMap = ZettelID("00000000029000") // Maps roles to CSS zettel, which should be in the range 29001..29999.

	// WebUI JS zettel are in the range 30000..39999







|
|
|
|
|
|
|
<
|
|







25
26
27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
	ZidBoxManager           = ZettelID("00000000000020")
	ZidMetadataKey          = ZettelID("00000000000090")
	ZidParser               = ZettelID("00000000000092")
	ZidStartupConfiguration = ZettelID("00000000000096")
	ZidConfiguration        = ZettelID("00000000000100")

	// WebUI HTML templates are in the range 10000..19999
	ZidBaseTemplate   = ZettelID("00000000010100")
	ZidLoginTemplate  = ZettelID("00000000010200")
	ZidListTemplate   = ZettelID("00000000010300")
	ZidZettelTemplate = ZettelID("00000000010401")
	ZidInfoTemplate   = ZettelID("00000000010402")
	ZidFormTemplate   = ZettelID("00000000010403")
	ZidRenameTemplate = ZettelID("00000000010404")

	ZidDeleteTemplate = ZettelID("00000000010405")
	ZidErrorTemplate  = ZettelID("00000000010700")

	// CSS-related zettel are in the range 20000..29999
	ZidBaseCSS    = ZettelID("00000000020001")
	ZidUserCSS    = ZettelID("00000000025001")
	ZidRoleCSSMap = ZettelID("00000000029000") // Maps roles to CSS zettel, which should be in the range 29001..29999.

	// WebUI JS zettel are in the range 30000..39999
88
89
90
91
92
93
94

95
96
97
98
99
100
101
102
103
104

105

106
107
108
109
110
111
112
	KeyBack         = "back"
	KeyBackward     = "backward"
	KeyBoxNumber    = "box-number"
	KeyCopyright    = "copyright"
	KeyCreated      = "created"
	KeyCredential   = "credential"
	KeyDead         = "dead"

	KeyFolge        = "folge"
	KeyForward      = "forward"
	KeyLang         = "lang"
	KeyLicense      = "license"
	KeyModified     = "modified"
	KeyPrecursor    = "precursor"
	KeyPredecessor  = "predecessor"
	KeyPublished    = "published"
	KeyQuery        = "query"
	KeyReadOnly     = "read-only"

	KeySuccessors   = "successors"

	KeySummary      = "summary"
	KeyURL          = "url"
	KeyUselessFiles = "useless-files"
	KeyUserID       = "user-id"
	KeyUserRole     = "user-role"
	KeyVisibility   = "visibility"
)







>










>

>







87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	KeyBack         = "back"
	KeyBackward     = "backward"
	KeyBoxNumber    = "box-number"
	KeyCopyright    = "copyright"
	KeyCreated      = "created"
	KeyCredential   = "credential"
	KeyDead         = "dead"
	KeyExpire       = "expire"
	KeyFolge        = "folge"
	KeyForward      = "forward"
	KeyLang         = "lang"
	KeyLicense      = "license"
	KeyModified     = "modified"
	KeyPrecursor    = "precursor"
	KeyPredecessor  = "predecessor"
	KeyPublished    = "published"
	KeyQuery        = "query"
	KeyReadOnly     = "read-only"
	KeySubordinates = "subordinates"
	KeySuccessors   = "successors"
	KeySuperior     = "superior"
	KeySummary      = "summary"
	KeyURL          = "url"
	KeyUselessFiles = "useless-files"
	KeyUserID       = "user-id"
	KeyUserRole     = "user-role"
	KeyVisibility   = "visibility"
)
123
124
125
126
127
128
129

130
131
132
133
134
135
136
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"
	ValueSyntaxMustache    = "mustache"
	ValueSyntaxNone        = "none"
	ValueSyntaxSVG         = "svg"

	ValueSyntaxText        = "text"
	ValueSyntaxZmk         = "zmk"
	ValueUserRoleCreator   = "creator"
	ValueUserRoleOwner     = "owner"
	ValueUserRoleReader    = "reader"
	ValueUserRoleWriter    = "writer"
	ValueVisibilityCreator = "creator"







>







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"
	ValueSyntaxMustache    = "mustache"
	ValueSyntaxNone        = "none"
	ValueSyntaxSVG         = "svg"
	ValueSyntaxSxn         = "sxn"
	ValueSyntaxText        = "text"
	ValueSyntaxZmk         = "zmk"
	ValueUserRoleCreator   = "creator"
	ValueUserRoleOwner     = "owner"
	ValueUserRoleReader    = "reader"
	ValueUserRoleWriter    = "writer"
	ValueVisibilityCreator = "creator"
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

179
180
181
182

183
184
185
186
187
188
189
190

191
192
193
194

195
196
197
198
199
200
201
	HeaderDestination = "Destination"
	HeaderLocation    = "Location"
)

// Values for HTTP query parameter.
const (
	QueryKeyCommand   = "cmd"
	QueryKeyCost      = "cost"
	QueryKeyDir       = "dir"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyLimit     = "limit"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"
	QueryKeySeed      = "_seed"
)

// Supported dir values.
const (
	DirBackward = "backward"
	DirForward  = "forward"
)

// Supported encoding values.
const (
	EncodingHTML  = "html"
	EncodingMD    = "md"
	EncodingSexpr = "sexpr"
	EncodingSHTML = "shtml"

	EncodingText  = "text"
	EncodingZMK   = "zmk"

	EncodingPlain = "plain"

	EncodingJson  = "json"
)

var mapEncodingEnum = map[string]EncodingEnum{
	EncodingHTML:  EncoderHTML,
	EncodingMD:    EncoderMD,
	EncodingSexpr: EncoderSexpr,
	EncodingSHTML: EncoderSHTML,

	EncodingText:  EncoderText,
	EncodingZMK:   EncoderZmk,

	EncodingPlain: EncoderPlain,

	EncodingJson:  EncoderJson,
}
var mapEnumEncoding = map[EncodingEnum]string{}

func init() {
	for k, v := range mapEncodingEnum {
		mapEnumEncoding[v] = k







<
<


<






<
<
<
<
<
<




<

>




>






<

>




>







152
153
154
155
156
157
158


159
160

161
162
163
164
165
166






167
168
169
170

171
172
173
174
175
176
177
178
179
180
181
182
183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
	HeaderDestination = "Destination"
	HeaderLocation    = "Location"
)

// Values for HTTP query parameter.
const (
	QueryKeyCommand   = "cmd"


	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"

	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"
	QueryKeySeed      = "_seed"
)







// Supported encoding values.
const (
	EncodingHTML  = "html"
	EncodingMD    = "md"

	EncodingSHTML = "shtml"
	EncodingSz    = "sz"
	EncodingText  = "text"
	EncodingZMK   = "zmk"

	EncodingPlain = "plain"
	EncodingData  = "data"
	EncodingJson  = "json"
)

var mapEncodingEnum = map[string]EncodingEnum{
	EncodingHTML:  EncoderHTML,
	EncodingMD:    EncoderMD,

	EncodingSHTML: EncoderSHTML,
	EncodingSz:    EncoderSz,
	EncodingText:  EncoderText,
	EncodingZMK:   EncoderZmk,

	EncodingPlain: EncoderPlain,
	EncodingData:  EncoderData,
	EncodingJson:  EncoderJson,
}
var mapEnumEncoding = map[EncodingEnum]string{}

func init() {
	for k, v := range mapEncodingEnum {
		mapEnumEncoding[v] = k
214
215
216
217
218
219
220
221
222
223
224
225
226

227
228
229
230
231
232
233
type EncodingEnum uint8

// Values for EncoderEnum
const (
	EncoderUnknown EncodingEnum = iota
	EncoderHTML
	EncoderMD
	EncoderSexpr
	EncoderSHTML
	EncoderText
	EncoderZmk

	EncoderPlain

	EncoderJson
)

// String representation of an encoder key.
func (e EncodingEnum) String() string {
	if f, ok := mapEnumEncoding[e]; ok {
		return f







|
|




>







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
type EncodingEnum uint8

// Values for EncoderEnum
const (
	EncoderUnknown EncodingEnum = iota
	EncoderHTML
	EncoderMD
	EncoderSHTML
	EncoderSz
	EncoderText
	EncoderZmk

	EncoderPlain
	EncoderData
	EncoderJson
)

// String representation of an encoder key.
func (e EncodingEnum) String() string {
	if f, ok := mapEnumEncoding[e]; ok {
		return f
250
251
252
253
254
255
256

257
258
259


260
261
262
263
264
265
266
267
268
	CommandAuthenticated = Command("authenticated")
	CommandRefresh       = Command("refresh")
)

// Supported search operator representations
const (
	ActionSeparator        = "|"

	ExistOperator          = "?"
	ExistNotOperator       = "!?"
	SearchOperatorNot      = "!"


	SearchOperatorHas      = ":"
	SearchOperatorHasNot   = "!:"
	SearchOperatorPrefix   = ">"
	SearchOperatorNoPrefix = "!>"
	SearchOperatorSuffix   = "<"
	SearchOperatorNoSuffix = "!<"
	SearchOperatorMatch    = "~"
	SearchOperatorNoMatch  = "!~"
)







>



>
>









247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	CommandAuthenticated = Command("authenticated")
	CommandRefresh       = Command("refresh")
)

// Supported search operator representations
const (
	ActionSeparator        = "|"
	ContextDirective       = "CONTEXT"
	ExistOperator          = "?"
	ExistNotOperator       = "!?"
	SearchOperatorNot      = "!"
	SearchOperatorEqual    = "="
	SearchOperatorNotEqual = "!="
	SearchOperatorHas      = ":"
	SearchOperatorHasNot   = "!:"
	SearchOperatorPrefix   = ">"
	SearchOperatorNoPrefix = "!>"
	SearchOperatorSuffix   = "<"
	SearchOperatorNoSuffix = "!<"
	SearchOperatorMatch    = "~"
	SearchOperatorNoMatch  = "!~"
)

Changes to client/client.go.

12
13
14
15
16
17
18

19
20
21
22
23
24
25
package client

import (
	"bufio"
	"bytes"
	"context"
	"encoding/json"

	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"







>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package client

import (
	"bufio"
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
162
163
164
165
166
167
168
169
170
171
172
173
174


















175
176
177
178
179
180
181
182
183
184
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var tinfo api.AuthJSON
	err = dec.Decode(&tinfo)
	if err != nil {
		return err
	}


















	c.token = tinfo.Token
	c.tokenType = tinfo.Type
	c.expires = time.Now().Add(time.Duration(tinfo.Expires*10/9) * time.Second)
	return nil
}

func (c *Client) updateToken(ctx context.Context) error {
	if c.username == "" {
		return nil
	}







|
<
|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|







163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return statusToError(resp)
	}
	rd := reader.MakeReader(resp.Body)

	obj, err := rd.Read()
	if err != nil {
		return err
	}
	lst, ok := sxpf.GetList(obj)
	if !ok {
		return fmt.Errorf("list expected, but got %t/%v", obj, obj)
	}
	tokenType, ok := sxpf.GetString(lst.Car())
	if !ok {
		return fmt.Errorf("no token type found: %v/%v", lst, lst.Car())
	}
	lstToken := lst.Tail()
	token, ok := sxpf.GetString(lstToken.Car())
	if !ok || len(token) < 20 {
		return fmt.Errorf("no valid token found: %v/%v", lst, lstToken.Car())
	}
	lstExpire := lstToken.Tail()
	expire, ok := sxpf.GetNumber(lstExpire.Car())
	if !ok {
		return fmt.Errorf("no valid expire: %v/%v", lst, lstExpire.Car())
	}
	c.token = token.String()
	c.tokenType = tokenType.String()
	c.expires = time.Now().Add(time.Duration(expire.(sxpf.Int64)*10/9) * time.Second)
	return nil
}

func (c *Client) updateToken(ctx context.Context) error {
	if c.username == "" {
		return nil
	}
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
	case http.StatusNoContent:
	default:
		return nil, statusToError(resp)
	}
	return io.ReadAll(resp.Body)
}

// GetParsedSexpr returns an parsed zettel as a Sexpr-decoded data structure.
func (c *Client) GetParsedSexpr(ctx context.Context, zid api.ZettelID, part string, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	return c.getSexpr(ctx, zid, part, true, sf)
}

// GetEvaluatedSexpr returns an evaluated zettel as a Sexpr-decoded data structure.
func (c *Client) GetEvaluatedSexpr(ctx context.Context, zid api.ZettelID, part string, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	return c.getSexpr(ctx, zid, part, false, sf)
}

func (c *Client) getSexpr(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSexpr)
	if part != "" {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)







|
|
|


|
|
|


|

|







396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
	case http.StatusNoContent:
	default:
		return nil, statusToError(resp)
	}
	return io.ReadAll(resp.Body)
}

// GetParsedSz returns an parsed zettel as a Sexpr-decoded data structure.
func (c *Client) GetParsedSz(ctx context.Context, zid api.ZettelID, part string, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	return c.getSz(ctx, zid, part, true, sf)
}

// GetEvaluatedSz returns an evaluated zettel as a Sexpr-decoded data structure.
func (c *Client) GetEvaluatedSz(ctx context.Context, zid api.ZettelID, part string, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	return c.getSz(ctx, zid, part, false, sf)
}

func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSz)
	if part != "" {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
	return out.Meta, nil
}

// GetZettelOrder returns metadata of the given zettel and, more important,
// metadata of zettel that are referenced in a list within the first zettel.
func (c *Client) GetZettelOrder(ctx context.Context, zid api.ZettelID) (*api.ZidMetaRelatedList, error) {
	ub := c.newURLBuilder('o').SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.ZidMetaRelatedList
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
	}
	return &out, nil
}

// ContextDirection specifies how the context should be calculated.
type ContextDirection uint8

// Allowed values for ContextDirection
const (
	_ ContextDirection = iota
	DirBoth
	DirBackward
	DirForward
)

// GetZettelContext returns metadata of the given zettel and, more important,
// metadata of zettel that for the context of the first zettel.
func (c *Client) GetZettelContext(
	ctx context.Context, zid api.ZettelID, dir ContextDirection, cost, limit int) (
	*api.ZidMetaRelatedList, error,
) {
	ub := c.newURLBuilder('x').SetZid(zid)
	switch dir {
	case DirBackward:
		ub.AppendKVQuery(api.QueryKeyDir, api.DirBackward)
	case DirForward:
		ub.AppendKVQuery(api.QueryKeyDir, api.DirForward)
	}
	if cost > 0 {
		ub.AppendKVQuery(api.QueryKeyCost, strconv.Itoa(cost))
	}
	if limit > 0 {
		ub.AppendKVQuery(api.QueryKeyLimit, strconv.Itoa(limit))
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







452
453
454
455
456
457
458















































459
460
461
462
463
464
465
	return out.Meta, nil
}

// GetZettelOrder returns metadata of the given zettel and, more important,
// metadata of zettel that are referenced in a list within the first zettel.
func (c *Client) GetZettelOrder(ctx context.Context, zid api.ZettelID) (*api.ZidMetaRelatedList, error) {
	ub := c.newURLBuilder('o').SetZid(zid)















































	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)

Changes to client/client_test.go.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	"net/http"
	"net/url"
	"testing"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/api"
	"zettelstore.de/c/client"
	"zettelstore.de/c/sexpr"
)

func TestZettelList(t *testing.T) {
	c := getClient()
	_, err := c.ListZettel(context.Background(), "")
	if err != nil {
		t.Error(err)







|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	"net/http"
	"net/url"
	"testing"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/api"
	"zettelstore.de/c/client"
	"zettelstore.de/c/sz"
)

func TestZettelList(t *testing.T) {
	c := getClient()
	_, err := c.ListZettel(context.Background(), "")
	if err != nil {
		t.Error(err)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		} else {
			t.Error(err)
		}
		return
	}
}

func TestGetSexprZettel(t *testing.T) {
	c := getClient()
	sf := sxpf.MakeMappedFactory()
	var zetSyms sexpr.ZettelSymbols
	zetSyms.InitializeZettelSymbols(sf)
	value, err := c.GetEvaluatedSexpr(context.Background(), api.ZidDefaultHome, api.PartContent, sf)
	if err != nil {
		t.Error(err)
		return
	}
	if value.IsNil() {
		t.Error("No data")
	}







|


|

|







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
		} else {
			t.Error(err)
		}
		return
	}
}

func TestGetSzZettel(t *testing.T) {
	c := getClient()
	sf := sxpf.MakeMappedFactory()
	var zetSyms sz.ZettelSymbols
	zetSyms.InitializeZettelSymbols(sf)
	value, err := c.GetEvaluatedSz(context.Background(), api.ZidDefaultHome, api.PartContent, sf)
	if err != nil {
		t.Error(err)
		return
	}
	if value.IsNil() {
		t.Error("No data")
	}

Changes to go.mod.

1
2
3
4
5
6
7
8
module zettelstore.de/c

go 1.20

require (
	codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197
	codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475
)





|
|

1
2
3
4
5
6
7
8
module zettelstore.de/c

go 1.20

require (
	codeberg.org/t73fde/sxhtml v0.0.0-20230524183754-f747d713562e
	codeberg.org/t73fde/sxpf v0.0.0-20230524183624-37758f88d90e
)

Changes to go.sum.

1
2
3
4
codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197 h1:6kX7TY25agLFlHNvByO1Jc3GrBA7mu7aOa8tCOniUew=
codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197/go.mod h1:Dp3EwBSsE3TvdPw9QZ4Wm25ZragluVT2OayRFRiq6jk=
codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475 h1:0OTzV3FYY/Y7YsaVaSzF4Wd17pXzdH6DaSvMeqteJc4=
codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475/go.mod h1:iSbMygOmtRQYp8pryNKYzRuMibYDSR80smU2b6qm1bc=
|
|
|
|
1
2
3
4
codeberg.org/t73fde/sxhtml v0.0.0-20230524183754-f747d713562e h1:RgH2VD9/Dg/ceHSdqT4HeE4ACR6GyX2i/TIXLnyDAEk=
codeberg.org/t73fde/sxhtml v0.0.0-20230524183754-f747d713562e/go.mod h1:SXZmbDu/3buh48SxZSXp8j4CGyUS7T3M4Pn/2pG6oIs=
codeberg.org/t73fde/sxpf v0.0.0-20230524183624-37758f88d90e h1:7AJv6wz7Pe/5vc9oqRdB6IVpJyrTBkSMA31yaIBn4CY=
codeberg.org/t73fde/sxpf v0.0.0-20230524183624-37758f88d90e/go.mod h1:X+XmeukFGzykXmTnR1tyFmyB5kV7UdO8V1sX5gMwdRQ=

Deleted sexpr/const.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sexpr

import "codeberg.org/t73fde/sxpf"

// Various constants for Zettel data. Some of them are technically variables.

const (
	// Symbols for Metanodes
	NameSymBlock  = "BLOCK"
	NameSymInline = "INLINE"
	NameSymList   = "LIST"
	NameSymMeta   = "META"
	NameSymQuote  = "quote"

	// Symbols for Zettel node types.
	NameSymBLOB            = "BLOB"
	NameSymCell            = "CELL"
	NameSymCellCenter      = "CELL-CENTER"
	NameSymCellLeft        = "CELL-LEFT"
	NameSymCellRight       = "CELL-RIGHT"
	NameSymCite            = "CITE"
	NameSymDescription     = "DESCRIPTION"
	NameSymEmbed           = "EMBED"
	NameSymEmbedBLOB       = "EMBED-BLOB"
	NameSymEndnote         = "ENDNOTE"
	NameSymFormatEmph      = "FORMAT-EMPH"
	NameSymFormatDelete    = "FORMAT-DELETE"
	NameSymFormatInsert    = "FORMAT-INSERT"
	NameSymFormatQuote     = "FORMAT-QUOTE"
	NameSymFormatSpan      = "FORMAT-SPAN"
	NameSymFormatSub       = "FORMAT-SUB"
	NameSymFormatSuper     = "FORMAT-SUPER"
	NameSymFormatStrong    = "FORMAT-STRONG"
	NameSymHard            = "HARD"
	NameSymHeading         = "HEADING"
	NameSymLinkInvalid     = "LINK-INVALID"
	NameSymLinkZettel      = "LINK-ZETTEL"
	NameSymLinkSelf        = "LINK-SELF"
	NameSymLinkFound       = "LINK-FOUND"
	NameSymLinkBroken      = "LINK-BROKEN"
	NameSymLinkHosted      = "LINK-HOSTED"
	NameSymLinkBased       = "LINK-BASED"
	NameSymLinkQuery       = "LINK-QUERY"
	NameSymLinkExternal    = "LINK-EXTERNAL"
	NameSymListOrdered     = "ORDERED"
	NameSymListUnordered   = "UNORDERED"
	NameSymListQuote       = "QUOTATION"
	NameSymLiteralProg     = "LITERAL-CODE"
	NameSymLiteralComment  = "LITERAL-COMMENT"
	NameSymLiteralHTML     = "LITERAL-HTML"
	NameSymLiteralInput    = "LITERAL-INPUT"
	NameSymLiteralMath     = "LITERAL-MATH"
	NameSymLiteralOutput   = "LITERAL-OUTPUT"
	NameSymLiteralZettel   = "LITERAL-ZETTEL"
	NameSymMark            = "MARK"
	NameSymPara            = "PARA"
	NameSymRegionBlock     = "REGION-BLOCK"
	NameSymRegionQuote     = "REGION-QUOTE"
	NameSymRegionVerse     = "REGION-VERSE"
	NameSymSoft            = "SOFT"
	NameSymSpace           = "SPACE"
	NameSymTable           = "TABLE"
	NameSymText            = "TEXT"
	NameSymThematic        = "THEMATIC"
	NameSymTransclude      = "TRANSCLUDE"
	NameSymUnknown         = "UNKNOWN-NODE"
	NameSymVerbatimComment = "VERBATIM-COMMENT"
	NameSymVerbatimEval    = "VERBATIM-EVAL"
	NameSymVerbatimHTML    = "VERBATIM-HTML"
	NameSymVerbatimMath    = "VERBATIM-MATH"
	NameSymVerbatimProg    = "VERBATIM-CODE"
	NameSymVerbatimZettel  = "VERBATIM-ZETTEL"

	// Constant symbols for reference states.
	NameSymRefStateInvalid  = "INVALID"
	NameSymRefStateZettel   = "ZETTEL"
	NameSymRefStateSelf     = "SELF"
	NameSymRefStateFound    = "FOUND"
	NameSymRefStateBroken   = "BROKEN"
	NameSymRefStateHosted   = "HOSTED"
	NameSymRefStateBased    = "BASED"
	NameSymRefStateQuery    = "QUERY"
	NameSymRefStateExternal = "EXTERNAL"

	// Symbols for metadata types.
	NameSymTypeCredential   = "CREDENTIAL"
	NameSymTypeEmpty        = "EMPTY-STRING"
	NameSymTypeID           = "ZID"
	NameSymTypeIDSet        = "ZID-SET"
	NameSymTypeNumber       = "NUMBER"
	NameSymTypeString       = "STRING"
	NameSymTypeTagSet       = "TAG-SET"
	NameSymTypeTimestamp    = "TIMESTAMP"
	NameSymTypeURL          = "URL"
	NameSymTypeWord         = "WORD"
	NameSymTypeWordSet      = "WORD-SET"
	NameSymTypeZettelmarkup = "ZETTELMARKUP"
)

// ZettelSymbols collect all symbols needed to represent zettel data.
type ZettelSymbols struct {
	// Symbols for Metanodes
	SymBlock  *sxpf.Symbol
	SymInline *sxpf.Symbol
	SymList   *sxpf.Symbol
	SymMeta   *sxpf.Symbol
	SymQuote  *sxpf.Symbol

	// Symbols for Zettel node types.
	SymBLOB            *sxpf.Symbol
	SymCell            *sxpf.Symbol
	SymCellCenter      *sxpf.Symbol
	SymCellLeft        *sxpf.Symbol
	SymCellRight       *sxpf.Symbol
	SymCite            *sxpf.Symbol
	SymDescription     *sxpf.Symbol
	SymEmbed           *sxpf.Symbol
	SymEmbedBLOB       *sxpf.Symbol
	SymEndnote         *sxpf.Symbol
	SymFormatEmph      *sxpf.Symbol
	SymFormatDelete    *sxpf.Symbol
	SymFormatInsert    *sxpf.Symbol
	SymFormatQuote     *sxpf.Symbol
	SymFormatSpan      *sxpf.Symbol
	SymFormatSub       *sxpf.Symbol
	SymFormatSuper     *sxpf.Symbol
	SymFormatStrong    *sxpf.Symbol
	SymHard            *sxpf.Symbol
	SymHeading         *sxpf.Symbol
	SymLinkInvalid     *sxpf.Symbol
	SymLinkZettel      *sxpf.Symbol
	SymLinkSelf        *sxpf.Symbol
	SymLinkFound       *sxpf.Symbol
	SymLinkBroken      *sxpf.Symbol
	SymLinkHosted      *sxpf.Symbol
	SymLinkBased       *sxpf.Symbol
	SymLinkQuery       *sxpf.Symbol
	SymLinkExternal    *sxpf.Symbol
	SymListOrdered     *sxpf.Symbol
	SymListUnordered   *sxpf.Symbol
	SymListQuote       *sxpf.Symbol
	SymLiteralProg     *sxpf.Symbol
	SymLiteralComment  *sxpf.Symbol
	SymLiteralHTML     *sxpf.Symbol
	SymLiteralInput    *sxpf.Symbol
	SymLiteralMath     *sxpf.Symbol
	SymLiteralOutput   *sxpf.Symbol
	SymLiteralZettel   *sxpf.Symbol
	SymMark            *sxpf.Symbol
	SymPara            *sxpf.Symbol
	SymRegionBlock     *sxpf.Symbol
	SymRegionQuote     *sxpf.Symbol
	SymRegionVerse     *sxpf.Symbol
	SymSoft            *sxpf.Symbol
	SymSpace           *sxpf.Symbol
	SymTable           *sxpf.Symbol
	SymText            *sxpf.Symbol
	SymThematic        *sxpf.Symbol
	SymTransclude      *sxpf.Symbol
	SymUnknown         *sxpf.Symbol
	SymVerbatimComment *sxpf.Symbol
	SymVerbatimEval    *sxpf.Symbol
	SymVerbatimHTML    *sxpf.Symbol
	SymVerbatimMath    *sxpf.Symbol
	SymVerbatimProg    *sxpf.Symbol
	SymVerbatimZettel  *sxpf.Symbol

	// Constant symbols for reference states.

	SymRefStateInvalid  *sxpf.Symbol
	SymRefStateZettel   *sxpf.Symbol
	SymRefStateSelf     *sxpf.Symbol
	SymRefStateFound    *sxpf.Symbol
	SymRefStateBroken   *sxpf.Symbol
	SymRefStateHosted   *sxpf.Symbol
	SymRefStateBased    *sxpf.Symbol
	SymRefStateQuery    *sxpf.Symbol
	SymRefStateExternal *sxpf.Symbol

	// Symbols for metadata types

	SymTypeCredential   *sxpf.Symbol
	SymTypeEmpty        *sxpf.Symbol
	SymTypeID           *sxpf.Symbol
	SymTypeIDSet        *sxpf.Symbol
	SymTypeNumber       *sxpf.Symbol
	SymTypeString       *sxpf.Symbol
	SymTypeTagSet       *sxpf.Symbol
	SymTypeTimestamp    *sxpf.Symbol
	SymTypeURL          *sxpf.Symbol
	SymTypeWord         *sxpf.Symbol
	SymTypeWordSet      *sxpf.Symbol
	SymTypeZettelmarkup *sxpf.Symbol
}

func (zs *ZettelSymbols) InitializeZettelSymbols(sf sxpf.SymbolFactory) {
	// Symbols for Metanodes
	zs.SymBlock = sf.MustMake(NameSymBlock)
	zs.SymInline = sf.MustMake(NameSymInline)
	zs.SymList = sf.MustMake(NameSymList)
	zs.SymMeta = sf.MustMake(NameSymMeta)
	zs.SymQuote = sf.MustMake(NameSymQuote)

	// Symbols for Zettel node types.
	zs.SymBLOB = sf.MustMake(NameSymBLOB)
	zs.SymCell = sf.MustMake(NameSymCell)
	zs.SymCellCenter = sf.MustMake(NameSymCellCenter)
	zs.SymCellLeft = sf.MustMake(NameSymCellLeft)
	zs.SymCellRight = sf.MustMake(NameSymCellRight)
	zs.SymCite = sf.MustMake(NameSymCite)
	zs.SymDescription = sf.MustMake(NameSymDescription)
	zs.SymEmbed = sf.MustMake(NameSymEmbed)
	zs.SymEmbedBLOB = sf.MustMake(NameSymEmbedBLOB)
	zs.SymEndnote = sf.MustMake(NameSymEndnote)
	zs.SymFormatEmph = sf.MustMake(NameSymFormatEmph)
	zs.SymFormatDelete = sf.MustMake(NameSymFormatDelete)
	zs.SymFormatInsert = sf.MustMake(NameSymFormatInsert)
	zs.SymFormatQuote = sf.MustMake(NameSymFormatQuote)
	zs.SymFormatSpan = sf.MustMake(NameSymFormatSpan)
	zs.SymFormatSub = sf.MustMake(NameSymFormatSub)
	zs.SymFormatSuper = sf.MustMake(NameSymFormatSuper)
	zs.SymFormatStrong = sf.MustMake(NameSymFormatStrong)
	zs.SymHard = sf.MustMake(NameSymHard)
	zs.SymHeading = sf.MustMake(NameSymHeading)
	zs.SymLinkInvalid = sf.MustMake(NameSymLinkInvalid)
	zs.SymLinkZettel = sf.MustMake(NameSymLinkZettel)
	zs.SymLinkSelf = sf.MustMake(NameSymLinkSelf)
	zs.SymLinkFound = sf.MustMake(NameSymLinkFound)
	zs.SymLinkBroken = sf.MustMake(NameSymLinkBroken)
	zs.SymLinkHosted = sf.MustMake(NameSymLinkHosted)
	zs.SymLinkBased = sf.MustMake(NameSymLinkBased)
	zs.SymLinkQuery = sf.MustMake(NameSymLinkQuery)
	zs.SymLinkExternal = sf.MustMake(NameSymLinkExternal)
	zs.SymListOrdered = sf.MustMake(NameSymListOrdered)
	zs.SymListUnordered = sf.MustMake(NameSymListUnordered)
	zs.SymListQuote = sf.MustMake(NameSymListQuote)
	zs.SymLiteralProg = sf.MustMake(NameSymLiteralProg)
	zs.SymLiteralComment = sf.MustMake(NameSymLiteralComment)
	zs.SymLiteralHTML = sf.MustMake(NameSymLiteralHTML)
	zs.SymLiteralInput = sf.MustMake(NameSymLiteralInput)
	zs.SymLiteralMath = sf.MustMake(NameSymLiteralMath)
	zs.SymLiteralOutput = sf.MustMake(NameSymLiteralOutput)
	zs.SymLiteralZettel = sf.MustMake(NameSymLiteralZettel)
	zs.SymMark = sf.MustMake(NameSymMark)
	zs.SymPara = sf.MustMake(NameSymPara)
	zs.SymRegionBlock = sf.MustMake(NameSymRegionBlock)
	zs.SymRegionQuote = sf.MustMake(NameSymRegionQuote)
	zs.SymRegionVerse = sf.MustMake(NameSymRegionVerse)
	zs.SymSoft = sf.MustMake(NameSymSoft)
	zs.SymSpace = sf.MustMake(NameSymSpace)
	zs.SymTable = sf.MustMake(NameSymTable)
	zs.SymText = sf.MustMake(NameSymText)
	zs.SymThematic = sf.MustMake(NameSymThematic)
	zs.SymTransclude = sf.MustMake(NameSymTransclude)
	zs.SymUnknown = sf.MustMake(NameSymUnknown)
	zs.SymVerbatimComment = sf.MustMake(NameSymVerbatimComment)
	zs.SymVerbatimEval = sf.MustMake(NameSymVerbatimEval)
	zs.SymVerbatimHTML = sf.MustMake(NameSymVerbatimHTML)
	zs.SymVerbatimMath = sf.MustMake(NameSymVerbatimMath)
	zs.SymVerbatimProg = sf.MustMake(NameSymVerbatimProg)
	zs.SymVerbatimZettel = sf.MustMake(NameSymVerbatimZettel)

	// Constant symbols for reference states.
	zs.SymRefStateInvalid = sf.MustMake(NameSymRefStateInvalid)
	zs.SymRefStateZettel = sf.MustMake(NameSymRefStateZettel)
	zs.SymRefStateSelf = sf.MustMake(NameSymRefStateSelf)
	zs.SymRefStateFound = sf.MustMake(NameSymRefStateFound)
	zs.SymRefStateBroken = sf.MustMake(NameSymRefStateBroken)
	zs.SymRefStateHosted = sf.MustMake(NameSymRefStateHosted)
	zs.SymRefStateBased = sf.MustMake(NameSymRefStateBased)
	zs.SymRefStateQuery = sf.MustMake(NameSymRefStateQuery)
	zs.SymRefStateExternal = sf.MustMake(NameSymRefStateExternal)

	// Symbols for metadata types.
	zs.SymTypeCredential = sf.MustMake(NameSymTypeCredential)
	zs.SymTypeEmpty = sf.MustMake(NameSymTypeEmpty)
	zs.SymTypeID = sf.MustMake(NameSymTypeID)
	zs.SymTypeIDSet = sf.MustMake(NameSymTypeIDSet)
	zs.SymTypeNumber = sf.MustMake(NameSymTypeNumber)
	zs.SymTypeString = sf.MustMake(NameSymTypeString)
	zs.SymTypeTagSet = sf.MustMake(NameSymTypeTagSet)
	zs.SymTypeTimestamp = sf.MustMake(NameSymTypeTimestamp)
	zs.SymTypeURL = sf.MustMake(NameSymTypeURL)
	zs.SymTypeWord = sf.MustMake(NameSymTypeWord)
	zs.SymTypeWordSet = sf.MustMake(NameSymTypeWordSet)
	zs.SymTypeZettelmarkup = sf.MustMake(NameSymTypeZettelmarkup)
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


















































































































































































































































































































































































































































































































































































































Deleted sexpr/const_test.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sexpr_test

import (
	"testing"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/sexpr"
)

func BenchmarkInitializeZettelSymbols(b *testing.B) {
	sf := sxpf.MakeMappedFactory()
	for i := 0; i < b.N; i++ {
		var zs sexpr.ZettelSymbols
		zs.InitializeZettelSymbols(sf)
	}
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




















































Deleted sexpr/sexpr.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sexpr

import (
	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/attrs"
)

// GetAttributes traverses a s-expression list and returns an attribute structure.
func GetAttributes(seq *sxpf.List) (result attrs.Attributes) {
	for elem := seq; elem != nil; elem = elem.Tail() {
		p, ok := elem.Car().(*sxpf.List)
		if !ok || p == nil {
			continue
		}
		key := p.Car()
		if !sxpf.IsAtom(key) {
			continue
		}
		val := p.Cdr()
		if tail, ok2 := val.(*sxpf.List); ok2 {
			val = tail.Car()
		}
		if !sxpf.IsAtom(val) {
			continue
		}
		result = result.Set(key.String(), val.String())
	}
	return result
}

// GetMetaContent returns the metadata and the content of a sexpr encoded zettel.
func GetMetaContent(zettel sxpf.Object) (Meta, *sxpf.List) {
	if pair, ok := zettel.(*sxpf.List); ok {
		m := pair.Car()
		if s := pair.Tail(); s != nil {
			if content, ok2 := s.Car().(*sxpf.List); ok2 {
				return MakeMeta(m), content
			}
		}
		return MakeMeta(m), nil
	}
	return nil, nil
}

type Meta map[string]MetaValue
type MetaValue struct {
	Type  string
	Key   string
	Value sxpf.Object
}

func MakeMeta(val sxpf.Object) Meta {
	if result := doMakeMeta(val); len(result) > 0 {
		return result
	}
	return nil
}
func doMakeMeta(val sxpf.Object) Meta {
	result := make(map[string]MetaValue)
	for {
		if sxpf.IsNil(val) {
			return result
		}
		lst, ok := val.(*sxpf.List)
		if !ok {
			return result
		}
		if mv, ok2 := makeMetaValue(lst); ok2 {
			result[mv.Key] = mv
		}
		val = lst.Cdr()
	}
}
func makeMetaValue(pair *sxpf.List) (MetaValue, bool) {
	var result MetaValue
	typePair, ok := pair.Car().(*sxpf.List)
	if !ok {
		return result, false
	}
	typeVal, ok := typePair.Car().(*sxpf.Symbol)
	if !ok {
		return result, false
	}
	keyPair, ok := typePair.Cdr().(*sxpf.List)
	if !ok {
		return result, false
	}
	keyStr, ok := keyPair.Car().(sxpf.String)
	if !ok {
		return result, false
	}
	valPair, ok := keyPair.Cdr().(*sxpf.List)
	if !ok {
		return result, false
	}
	result.Type = typeVal.CanonicalName()
	result.Key = keyStr.String()
	result.Value = valPair.Car()
	return result, true
}

func (m Meta) GetString(key string) string {
	if v, found := m[key]; found {
		return v.Value.String()
	}
	return ""
}

func (m Meta) GetList(key string) *sxpf.List {
	if mv, found := m[key]; found {
		if seq, ok := mv.Value.(*sxpf.List); ok {
			return seq
		}
	}
	return nil
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




























































































































































































































































Changes to shtml/shtml.go.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	"codeberg.org/t73fde/sxhtml"
	"codeberg.org/t73fde/sxpf"
	"codeberg.org/t73fde/sxpf/builtins/quote"
	"codeberg.org/t73fde/sxpf/eval"
	"zettelstore.de/c/api"
	"zettelstore.de/c/attrs"
	"zettelstore.de/c/sexpr"
	"zettelstore.de/c/text"
)

// Transformer will transform a s-expression that encodes the zettel AST into an s-expression
// that represents HTML.
type Transformer struct {
	sf            sxpf.SymbolFactory







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	"codeberg.org/t73fde/sxhtml"
	"codeberg.org/t73fde/sxpf"
	"codeberg.org/t73fde/sxpf/builtins/quote"
	"codeberg.org/t73fde/sxpf/eval"
	"zettelstore.de/c/api"
	"zettelstore.de/c/attrs"
	"zettelstore.de/c/sz"
	"zettelstore.de/c/text"
)

// Transformer will transform a s-expression that encodes the zettel AST into an s-expression
// that represents HTML.
type Transformer struct {
	sf            sxpf.SymbolFactory
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
			panic("Invalid AST SymbolFactory")
		}
	} else {
		astSF = sxpf.MakeMappedFactory()
	}
	astEnv := sxpf.MakeRootEnvironment()
	engine := eval.MakeEngine(astSF, astEnv, eval.MakeDefaultParser(), eval.MakeSimpleExecutor())
	quote.InstallQuote(engine, sexpr.NameSymQuote, nil, 0)
	te := TransformEnv{
		tr:      tr,
		astSF:   astSF,
		astEnv:  astEnv,
		err:     nil,
		textEnc: text.NewEncoder(astSF),
	}







|







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
			panic("Invalid AST SymbolFactory")
		}
	} else {
		astSF = sxpf.MakeMappedFactory()
	}
	astEnv := sxpf.MakeRootEnvironment()
	engine := eval.MakeEngine(astSF, astEnv, eval.MakeDefaultParser(), eval.MakeSimpleExecutor())
	quote.InstallQuoteSyntax(astEnv, astSF.MustMake(sz.NameSymQuote))
	te := TransformEnv{
		tr:      tr,
		astSF:   astSF,
		astEnv:  astEnv,
		err:     nil,
		textEnc: text.NewEncoder(astSF),
	}
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

390
391
392
393
394

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

535
536

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671

672
673

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
	tr          *Transformer
	astSF       sxpf.SymbolFactory
	astEnv      sxpf.Environment
	err         error
	textEnc     *text.Encoder
	symNoEscape *sxpf.Symbol
	symAttr     *sxpf.Symbol
	symMeta     *sxpf.Symbol
	symA        *sxpf.Symbol
	symSpan     *sxpf.Symbol
	symP        *sxpf.Symbol
}

func (te *TransformEnv) initialize() {
	te.symNoEscape = te.Make(sxhtml.NameSymNoEscape)
	te.symAttr = te.tr.symAttr
	te.symMeta = te.tr.symMeta
	te.symA = te.tr.symA
	te.symSpan = te.tr.symSpan
	te.symP = te.Make("p")

	te.bind(sexpr.NameSymList, 0, listArgs)
	te.bindMetadata()
	te.bindBlocks()
	te.bindInlines()
}

func listArgs(args *sxpf.List) sxpf.Object { return args }

func (te *TransformEnv) bindMetadata() {
	te.bind(sexpr.NameSymMeta, 0, listArgs)
	te.bind(sexpr.NameSymTypeZettelmarkup, 2, func(args *sxpf.List) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getString(args).String()).
			Set("content", te.textEnc.Encode(te.getList(args.Tail())))
		return te.transformMeta(a)
	})
	metaString := func(args *sxpf.List) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getString(args).String()).
			Set("content", te.getString(args.Tail()).String())
		return te.transformMeta(a)
	}
	te.bind(sexpr.NameSymTypeCredential, 2, metaString)
	te.bind(sexpr.NameSymTypeEmpty, 2, metaString)
	te.bind(sexpr.NameSymTypeID, 2, metaString)
	te.bind(sexpr.NameSymTypeNumber, 2, metaString)
	te.bind(sexpr.NameSymTypeString, 2, metaString)
	te.bind(sexpr.NameSymTypeTimestamp, 2, metaString)
	te.bind(sexpr.NameSymTypeURL, 2, metaString)
	te.bind(sexpr.NameSymTypeWord, 2, metaString)
	metaSet := func(args *sxpf.List) sxpf.Object {
		var sb strings.Builder
		for elem := te.getList(args.Tail()); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(te.getString(elem).String())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", te.getString(args).String()).
			Set("content", s)
		return te.transformMeta(a)
	}
	te.bind(sexpr.NameSymTypeIDSet, 2, metaSet)
	te.bind(sexpr.NameSymTypeTagSet, 2, metaSet)
	te.bind(sexpr.NameSymTypeWordSet, 2, metaSet)
}

func (te *TransformEnv) bindBlocks() {
	te.bind(sexpr.NameSymBlock, 0, listArgs)
	te.bind(sexpr.NameSymPara, 0, func(args *sxpf.List) sxpf.Object {
		for ; args != nil; args = args.Tail() {
			lst, ok := sxpf.GetList(args.Car())
			if !ok || lst != nil {
				break
			}
		}


		return args.Cons(te.symP)
	})
	te.bind(sexpr.NameSymHeading, 5, func(args *sxpf.List) sxpf.Object {
		nLevel := te.getInt64(args)
		if nLevel <= 0 {
			te.err = fmt.Errorf("%v is a negative level", nLevel)
			return sxpf.Nil()
		}
		level := strconv.FormatInt(nLevel+te.tr.headingOffset, 10)

		argAttr := args.Tail()
		a := te.getAttributes(argAttr)
		argFragment := argAttr.Tail().Tail()
		if fragment := te.getString(argFragment).String(); fragment != "" {
			a = a.Set("id", te.tr.unique+fragment)
		}

		if result, ok := sxpf.GetList(argFragment.Tail().Car()); ok && result != nil {
			if len(a) > 0 {
				result = result.Cons(te.transformAttribute(a))
			}
			return result.Cons(te.Make("h" + level))
		}
		return sxpf.MakeList(te.Make("h"+level), sxpf.MakeString("<MISSING TEXT>"))
	})
	te.bind(sexpr.NameSymThematic, 0, func(args *sxpf.List) sxpf.Object {
		result := sxpf.Nil()
		if args != nil {
			if attrList := te.getList(args); attrList != nil {
				result = result.Cons(te.transformAttribute(sexpr.GetAttributes(attrList)))
			}
		}
		return result.Cons(te.Make("hr"))
	})
	te.bind(sexpr.NameSymListOrdered, 0, te.makeListFn("ol"))
	te.bind(sexpr.NameSymListUnordered, 0, te.makeListFn("ul"))
	te.bind(sexpr.NameSymDescription, 0, func(args *sxpf.List) sxpf.Object {
		if args == nil {
			return sxpf.Nil()
		}
		items := sxpf.Nil().Cons(te.Make("dl"))
		curItem := items
		for elem := args; elem != nil; elem = elem.Tail() {
			term := te.getList(elem)
			curItem = curItem.AppendBang(term.Cons(te.Make("dt")))
			elem = elem.Tail()

			if elem == nil {
				break
			}
			ddBlock := te.getList(elem)
			if ddBlock == nil {
				break
			}
			for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() {
				dditem := te.getList(ddlst)
				curItem = curItem.AppendBang(dditem.Cons(te.Make("dd")))
			}
		}
		return items
	})

	te.bind(sexpr.NameSymListQuote, 0, func(args *sxpf.List) sxpf.Object {
		if args == nil {
			return sxpf.Nil()
		}
		result := sxpf.Nil().Cons(te.Make("blockquote"))
		currResult := result
		for elem := args; elem != nil; elem = elem.Tail() {
			if quote, ok := elem.Car().(*sxpf.List); ok {
				currResult = currResult.AppendBang(quote.Cons(te.symP))
			}
		}
		return result
	})

	te.bind(sexpr.NameSymTable, 1, func(args *sxpf.List) sxpf.Object {
		thead := sxpf.Nil()
		if header := te.getList(args); header != nil {
			thead = sxpf.Nil().Cons(te.transformTableRow(header)).Cons(te.Make("thead"))
		}

		tbody := sxpf.Nil()
		if argBody := args.Tail(); argBody != nil {
			tbody = sxpf.Nil().Cons(te.Make("tbody"))
			curBody := tbody
			for row := argBody; row != nil; row = row.Tail() {
				curBody = curBody.AppendBang(te.transformTableRow(te.getList(row)))
			}
		}

		table := sxpf.Nil()
		if tbody != nil {
			table = table.Cons(tbody)
		}
		if thead != nil {
			table = table.Cons(thead)
		}
		if table == nil {
			return sxpf.Nil()
		}
		return table.Cons(te.Make("table"))
	})
	te.bind(sexpr.NameSymCell, 0, te.makeCellFn(""))
	te.bind(sexpr.NameSymCellCenter, 0, te.makeCellFn("center"))
	te.bind(sexpr.NameSymCellLeft, 0, te.makeCellFn("left"))
	te.bind(sexpr.NameSymCellRight, 0, te.makeCellFn("right"))

	te.bind(sexpr.NameSymRegionBlock, 2, te.makeRegionFn(te.Make("div"), true))
	te.bind(sexpr.NameSymRegionQuote, 2, te.makeRegionFn(te.Make("blockquote"), false))
	te.bind(sexpr.NameSymRegionVerse, 2, te.makeRegionFn(te.Make("div"), false))

	te.bind(sexpr.NameSymVerbatimComment, 1, func(args *sxpf.List) sxpf.Object {
		if te.getAttributes(args).HasDefault() {

			if s := te.getString(args.Tail()); s != "" {
				t := sxpf.MakeString(s.String())
				return sxpf.Nil().Cons(t).Cons(te.Make(sxhtml.NameSymBlockComment))
			}
		}

		return nil
	})

	te.bind(sexpr.NameSymVerbatimEval, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args).AddClass("zs-eval"), te.getString(args.Tail()))
	})
	te.bind(sexpr.NameSymVerbatimHTML, 2, te.transformHTML)
	te.bind(sexpr.NameSymVerbatimMath, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args).AddClass("zs-math"), te.getString(args.Tail()))
	})
	te.bind(sexpr.NameSymVerbatimProg, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		content := te.getString(args.Tail())
		if a.HasDefault() {
			content = sxpf.MakeString(visibleReplacer.Replace(content.String()))
		}
		return te.transformVerbatim(a, content)
	})
	te.bind(sexpr.NameSymVerbatimZettel, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil() })

	te.bind(sexpr.NameSymBLOB, 3, func(args *sxpf.List) sxpf.Object {
		argSyntax := args.Tail()
		return te.transformBLOB(te.getList(args), te.getString(argSyntax), te.getString(argSyntax.Tail()))
	})

	te.bind(sexpr.NameSymTransclude, 2, func(args *sxpf.List) sxpf.Object {
		ref, ok := args.Tail().Car().(*sxpf.List)
		if !ok {
			return sxpf.Nil()
		}
		refKind := ref.Car()
		if sxpf.IsNil(refKind) {
			return sxpf.Nil()
		}
		if refValue := te.getString(ref.Tail()); refValue != "" {
			if te.astSF.MustMake(sexpr.NameSymRefStateExternal).IsEqual(refKind) {
				a := te.getAttributes(args).Set("src", refValue.String()).AddClass("external")
				return sxpf.Nil().Cons(sxpf.Nil().Cons(te.transformAttribute(a)).Cons(te.Make("img"))).Cons(te.symP)
			}
			return sxpf.MakeList(
				te.Make(sxhtml.NameSymInlineComment),
				sxpf.MakeString("transclude"),
				refKind,
				sxpf.MakeString("->"),
				refValue,
			)
		}
		return args
	})
}

func (te *TransformEnv) makeListFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args *sxpf.List) sxpf.Object {
		result := sxpf.Nil().Cons(sym)
		last := result
		for elem := args; elem != nil; elem = elem.Tail() {
			item := sxpf.Nil().Cons(te.Make("li"))
			if res, ok := elem.Car().(*sxpf.List); ok {
				item.ExtendBang(res)
			}
			last = last.AppendBang(item)
		}
		return result
	}
}
func (te *TransformEnv) transformTableRow(cells *sxpf.List) *sxpf.List {
	row := sxpf.Nil().Cons(te.Make("tr"))
	if cells == nil {
		return sxpf.Nil()
	}
	curRow := row
	for cell := cells; cell != nil; cell = cell.Tail() {
		curRow = curRow.AppendBang(cell.Car())
	}
	return row
}

func (te *TransformEnv) makeCellFn(align string) transformFn {
	return func(args *sxpf.List) sxpf.Object {
		tdata := args
		if align != "" {
			tdata = tdata.Cons(te.transformAttribute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(te.Make("td"))
	}
}

func (te *TransformEnv) makeRegionFn(sym *sxpf.Symbol, genericToClass bool) transformFn {
	return func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		result := sxpf.Nil()
		if len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		result = result.Cons(sym)
		currResult := result.Last()
		blockArg := args.Tail()
		if region, ok := blockArg.Car().(*sxpf.List); ok {
			currResult = currResult.ExtendBang(region)
		}
		if citeArg := blockArg.Tail(); citeArg != nil {

			if cite, ok := citeArg.Car().(*sxpf.List); ok && cite != nil {
				currResult.AppendBang(cite.Cons(te.Make("cite")))
			}
		}
		return result
	}
}

func (te *TransformEnv) transformVerbatim(a attrs.Attributes, s sxpf.String) sxpf.Object {
	a = setProgLang(a)
	code := sxpf.Nil().Cons(s)
	if al := te.transformAttribute(a); al != nil {
		code = code.Cons(al)
	}
	code = code.Cons(te.Make("code"))
	return sxpf.Nil().Cons(code).Cons(te.Make("pre"))
}

func (te *TransformEnv) bindInlines() {
	te.bind(sexpr.NameSymInline, 0, listArgs)
	te.bind(sexpr.NameSymText, 1, func(args *sxpf.List) sxpf.Object { return te.getString(args) })
	te.bind(sexpr.NameSymSpace, 0, func(args *sxpf.List) sxpf.Object {
		if args.IsNil() {
			return sxpf.MakeString(" ")
		}
		return te.getString(args)
	})
	te.bind(sexpr.NameSymSoft, 0, func(*sxpf.List) sxpf.Object { return sxpf.MakeString(" ") })
	brSym := te.Make("br")
	te.bind(sexpr.NameSymHard, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil().Cons(brSym) })

	te.bind(sexpr.NameSymLinkInvalid, 2, func(args *sxpf.List) sxpf.Object {
		// a := te.getAttributes(args)

		refArg := args.Tail()
		inline := refArg.Tail()

		if inline == nil {
			inline = sxpf.Nil().Cons(refArg.Car())
		}
		return inline.Cons(te.symSpan)
	})
	transformHREF := func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.Set("href", refValue.String()), refValue, args.Tail().Tail())
	}
	te.bind(sexpr.NameSymLinkZettel, 2, transformHREF)
	te.bind(sexpr.NameSymLinkSelf, 2, transformHREF)
	te.bind(sexpr.NameSymLinkFound, 2, transformHREF)
	te.bind(sexpr.NameSymLinkBroken, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.AddClass("broken"), refValue, args.Tail().Tail())
	})
	te.bind(sexpr.NameSymLinkHosted, 2, transformHREF)
	te.bind(sexpr.NameSymLinkBased, 2, transformHREF)
	te.bind(sexpr.NameSymLinkQuery, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		refValue := te.getString(args.Tail())
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String())
		return te.transformLink(a.Set("href", query), refValue, args.Tail().Tail())
	})
	te.bind(sexpr.NameSymLinkExternal, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args.Tail().Tail())
	})

	te.bind(sexpr.NameSymEmbed, 3, func(args *sxpf.List) sxpf.Object {
		argRef := args.Tail()
		ref := te.getList(argRef)
		syntax := te.getString(argRef.Tail())
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sxpf.MakeList(
				te.symAttr,
				sxpf.Cons(te.Make("type"), sxpf.MakeString("image/svg+xml")),
				sxpf.Cons(te.Make("src"), sxpf.MakeString("/"+te.getString(ref.Tail()).String()+".svg")),
			)
			return sxpf.MakeList(
				te.Make("figure"),
				sxpf.MakeList(
					te.Make("embed"),
					embedAttr,
				),
			)
		}
		a := te.getAttributes(args)
		a = a.Set("src", string(te.getString(ref.Tail())))
		var sb strings.Builder
		te.flattenText(&sb, ref.Tail().Tail().Tail())
		if d := sb.String(); d != "" {
			a = a.Set("alt", d)
		}
		return sxpf.MakeList(te.Make("img"), te.transformAttribute(a))
	})
	te.bind(sexpr.NameSymEmbedBLOB, 3, func(args *sxpf.List) sxpf.Object {
		argSyntax := args.Tail()
		a, syntax, data := te.getAttributes(args), te.getString(argSyntax), te.getString(argSyntax.Tail())
		summary, _ := a.Get(api.KeySummary)
		return te.transformBLOB(
			sxpf.MakeList(te.astSF.MustMake(sexpr.NameSymInline), sxpf.MakeString(summary)),
			syntax,
			data,
		)
	})

	te.bind(sexpr.NameSymCite, 2, func(args *sxpf.List) sxpf.Object {
		result := sxpf.Nil()
		argKey := args.Tail()
		if key := te.getString(argKey); key != "" {
			if text := argKey.Tail(); text != nil {
				result = text.Cons(sxpf.MakeString(", "))
			}
			result = result.Cons(key)
		}
		if a := te.getAttributes(args); len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(te.symSpan)
	})

	te.bind(sexpr.NameSymMark, 3, func(args *sxpf.List) sxpf.Object {
		argFragment := args.Tail().Tail()
		result := argFragment.Tail()
		if !te.tr.noLinks {
			if fragment := te.getString(argFragment); fragment != "" {
				a := attrs.Attributes{"id": fragment.String() + te.tr.unique}
				return result.Cons(te.transformAttribute(a)).Cons(te.symA)
			}
		}
		return result.Cons(te.symSpan)
	})

	te.bind(sexpr.NameSymEndnote, 1, func(args *sxpf.List) sxpf.Object {
		attrPlist := sxpf.Nil()
		if a := te.getAttributes(args); len(a) > 0 {
			if attrs := te.transformAttribute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		text, ok := args.Tail().Car().(*sxpf.List)
		if !ok {
			return sxpf.Nil()
		}
		te.tr.endnotes = append(te.tr.endnotes, endnoteInfo{noteAST: text, noteHx: nil, attrs: attrPlist})
		noteNum := strconv.Itoa(len(te.tr.endnotes))
		noteID := te.tr.unique + noteNum
		hrefAttr := sxpf.Nil().Cons(sxpf.Cons(te.Make("role"), sxpf.MakeString("doc-noteref"))).
			Cons(sxpf.Cons(te.Make("href"), sxpf.MakeString("#fn:"+noteID))).
			Cons(sxpf.Cons(te.tr.symClass, sxpf.MakeString("zs-noteref"))).
			Cons(te.symAttr)
		href := sxpf.Nil().Cons(sxpf.MakeString(noteNum)).Cons(hrefAttr).Cons(te.symA)
		supAttr := sxpf.Nil().Cons(sxpf.Cons(te.Make("id"), sxpf.MakeString("fnref:"+noteID))).Cons(te.symAttr)
		return sxpf.Nil().Cons(href).Cons(supAttr).Cons(te.Make("sup"))
	})

	te.bind(sexpr.NameSymFormatDelete, 1, te.makeFormatFn("del"))
	te.bind(sexpr.NameSymFormatEmph, 1, te.makeFormatFn("em"))
	te.bind(sexpr.NameSymFormatInsert, 1, te.makeFormatFn("ins"))
	te.bind(sexpr.NameSymFormatQuote, 1, te.transformQuote)
	te.bind(sexpr.NameSymFormatSpan, 1, te.makeFormatFn("span"))
	te.bind(sexpr.NameSymFormatStrong, 1, te.makeFormatFn("strong"))
	te.bind(sexpr.NameSymFormatSub, 1, te.makeFormatFn("sub"))
	te.bind(sexpr.NameSymFormatSuper, 1, te.makeFormatFn("sup"))

	te.bind(sexpr.NameSymLiteralComment, 1, func(args *sxpf.List) sxpf.Object {
		if te.getAttributes(args).HasDefault() {

			if s := te.getString(args.Tail()); s != "" {
				return sxpf.Nil().Cons(s).Cons(te.Make(sxhtml.NameSymInlineComment))

			}
		}
		return sxpf.Nil()
	})
	te.bind(sexpr.NameSymLiteralHTML, 2, te.transformHTML)
	kbdSym := te.Make("kbd")
	te.bind(sexpr.NameSymLiteralInput, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, kbdSym)
	})
	codeSym := te.Make("code")
	te.bind(sexpr.NameSymLiteralMath, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args).AddClass("zs-math")
		return te.transformLiteral(args, a, codeSym)
	})
	sampSym := te.Make("samp")
	te.bind(sexpr.NameSymLiteralOutput, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, sampSym)
	})
	te.bind(sexpr.NameSymLiteralProg, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, codeSym)
	})

	te.bind(sexpr.NameSymLiteralZettel, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil() })
}

func (te *TransformEnv) makeFormatFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		res := args.Tail()
		if len(a) > 0 {
			res = res.Cons(te.transformAttribute(a))
		}
		return res.Cons(sym)
	}
}
func (te *TransformEnv) transformQuote(args *sxpf.List) sxpf.Object {
	const langAttr = "lang"
	a := te.getAttributes(args)
	langVal, found := a.Get(langAttr)
	if found {
		a = a.Remove(langAttr)
	}
	if val, found2 := a.Get(""); found2 {
		a = a.Remove("").AddClass(val)
	}
	res := args.Tail()
	if len(a) > 0 {
		res = res.Cons(te.transformAttribute(a))
	}
	res = res.Cons(te.Make("q"))
	if found {
		res = sxpf.Nil().Cons(res).Cons(te.transformAttribute(attrs.Attributes{}.Set(langAttr, langVal))).Cons(te.symSpan)
	}
	return res
}

var visibleReplacer = strings.NewReplacer(" ", "\u2423")

func (te *TransformEnv) transformLiteral(args *sxpf.List, a attrs.Attributes, sym *sxpf.Symbol) sxpf.Object {
	if a == nil {
		a = te.getAttributes(args)
	}
	a = setProgLang(a)
	literal := te.getString(args.Tail()).String()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sxpf.Nil().Cons(sxpf.MakeString(literal))
	if len(a) > 0 {
		res = res.Cons(te.transformAttribute(a))
	}
	return res.Cons(sym)
}

func setProgLang(a attrs.Attributes) attrs.Attributes {
	if val, found := a.Get(""); found {
		a = a.AddClass("language-" + val).Remove("")
	}
	return a
}

func (te *TransformEnv) transformHTML(args *sxpf.List) sxpf.Object {
	if s := te.getString(args.Tail()); s != "" && IsSafe(s.String()) {
		return sxpf.Nil().Cons(s).Cons(te.symNoEscape)
	}
	return nil
}

func (te *TransformEnv) transformBLOB(description *sxpf.List, syntax, data sxpf.String) sxpf.Object {
	if data == "" {







<








<




|





|


|
|

|
|


|

|
|


|
|
|
|
|
|
|
|
|

|

|






|



|
|
|



|
|
|
|
|
|
<
<
>
>
|

|
|






<
|
<
|



|







|

|
|
|




|
|
|
|




|
|

<
>
|


|




|






|





|
|






|

|




|


|
















|
|
|
|

|
|
|

|
|
>
|
|
|
|
|
>



|
|

|
|
|

|
|
|





|

|
<
|


|
|







|
|
|










|





|


|

|




















|
|








|
|










|
<
|


<
>
|


















|
|
|
|


|

|

|

|

>
|
|
>

|



|
|
|
|

|
|
|
|
|
|
|

|
|
|
|
|

|

|
|
|
|


|
<
|
|














|
|







|
<
|


|





|

<
|
|
|



|








|
<
|

|







|

|





|















|
|
|
|
|
|
|
|

|
|
>
|
|
>




|

|



|
|



|


|



|




|
|



|






|

|







|












|

|


|


















|
|







197
198
199
200
201
202
203

204
205
206
207
208
209
210
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272


273
274
275
276
277
278
279
280
281
282
283
284

285

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

494
495
496

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

594
595
596
597
598
599
600
601
602
603
604

605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
	tr          *Transformer
	astSF       sxpf.SymbolFactory
	astEnv      sxpf.Environment
	err         error
	textEnc     *text.Encoder
	symNoEscape *sxpf.Symbol
	symAttr     *sxpf.Symbol

	symA        *sxpf.Symbol
	symSpan     *sxpf.Symbol
	symP        *sxpf.Symbol
}

func (te *TransformEnv) initialize() {
	te.symNoEscape = te.Make(sxhtml.NameSymNoEscape)
	te.symAttr = te.tr.symAttr

	te.symA = te.tr.symA
	te.symSpan = te.tr.symSpan
	te.symP = te.Make("p")

	te.bind(sz.NameSymList, 0, listArgs)
	te.bindMetadata()
	te.bindBlocks()
	te.bindInlines()
}

func listArgs(args []sxpf.Object) sxpf.Object { return sxpf.MakeList(args...) }

func (te *TransformEnv) bindMetadata() {
	te.bind(sz.NameSymMeta, 0, listArgs)
	te.bind(sz.NameSymTypeZettelmarkup, 2, func(args []sxpf.Object) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getString(args[0]).String()).
			Set("content", te.textEnc.Encode(te.getList(args[1])))
		return te.transformMeta(a)
	})
	metaString := func(args []sxpf.Object) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getSymbol(args[0]).Name()).
			Set("content", te.getString(args[1]).String())
		return te.transformMeta(a)
	}
	te.bind(sz.NameSymTypeCredential, 2, metaString)
	te.bind(sz.NameSymTypeEmpty, 2, metaString)
	te.bind(sz.NameSymTypeID, 2, metaString)
	te.bind(sz.NameSymTypeNumber, 2, metaString)
	te.bind(sz.NameSymTypeString, 2, metaString)
	te.bind(sz.NameSymTypeTimestamp, 2, metaString)
	te.bind(sz.NameSymTypeURL, 2, metaString)
	te.bind(sz.NameSymTypeWord, 2, metaString)
	metaSet := func(args []sxpf.Object) sxpf.Object {
		var sb strings.Builder
		for elem := te.getList(args[1]); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(te.getString(elem.Car()).String())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", te.getSymbol(args[0]).Name()).
			Set("content", s)
		return te.transformMeta(a)
	}
	te.bind(sz.NameSymTypeIDSet, 2, metaSet)
	te.bind(sz.NameSymTypeTagSet, 2, metaSet)
	te.bind(sz.NameSymTypeWordSet, 2, metaSet)
}

func (te *TransformEnv) bindBlocks() {
	te.bind(sz.NameSymBlock, 0, listArgs)
	te.bind(sz.NameSymPara, 0, func(args []sxpf.Object) sxpf.Object {
		// for ; args != nil; args = args.Tail() {
		// 	lst, ok := sxpf.GetList(args.Car())
		// 	if !ok || lst != nil {
		// 		break


		// 	}
		// }
		return sxpf.MakeList(args...).Cons(te.symP)
	})
	te.bind(sz.NameSymHeading, 5, func(args []sxpf.Object) sxpf.Object {
		nLevel := te.getInt64(args[0])
		if nLevel <= 0 {
			te.err = fmt.Errorf("%v is a negative level", nLevel)
			return sxpf.Nil()
		}
		level := strconv.FormatInt(nLevel+te.tr.headingOffset, 10)


		a := te.getAttributes(args[1])

		if fragment := te.getString(args[3]).String(); fragment != "" {
			a = a.Set("id", te.tr.unique+fragment)
		}

		if result, ok := sxpf.GetList(args[4]); ok && result != nil {
			if len(a) > 0 {
				result = result.Cons(te.transformAttribute(a))
			}
			return result.Cons(te.Make("h" + level))
		}
		return sxpf.MakeList(te.Make("h"+level), sxpf.MakeString("<MISSING TEXT>"))
	})
	te.bind(sz.NameSymThematic, 0, func(args []sxpf.Object) sxpf.Object {
		result := sxpf.Nil()
		if len(args) > 0 {
			if attrList := te.getList(args[0]); attrList != nil {
				result = result.Cons(te.transformAttribute(sz.GetAttributes(attrList)))
			}
		}
		return result.Cons(te.Make("hr"))
	})
	te.bind(sz.NameSymListOrdered, 0, te.makeListFn("ol"))
	te.bind(sz.NameSymListUnordered, 0, te.makeListFn("ul"))
	te.bind(sz.NameSymDescription, 0, func(args []sxpf.Object) sxpf.Object {
		if len(args) == 0 {
			return sxpf.Nil()
		}
		items := sxpf.Nil().Cons(te.Make("dl"))
		curItem := items
		for pos := 0; pos < len(args); pos++ {
			term := te.getList(args[pos])
			curItem = curItem.AppendBang(term.Cons(te.Make("dt")))

			pos++
			if pos >= len(args) {
				break
			}
			ddBlock := te.getList(args[pos])
			if ddBlock == nil {
				break
			}
			for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() {
				dditem := te.getList(ddlst.Car())
				curItem = curItem.AppendBang(dditem.Cons(te.Make("dd")))
			}
		}
		return items
	})

	te.bind(sz.NameSymListQuote, 0, func(args []sxpf.Object) sxpf.Object {
		if args == nil {
			return sxpf.Nil()
		}
		result := sxpf.Nil().Cons(te.Make("blockquote"))
		currResult := result
		for _, elem := range args {
			if quote, ok := sxpf.GetList(elem); ok {
				currResult = currResult.AppendBang(quote.Cons(te.symP))
			}
		}
		return result
	})

	te.bind(sz.NameSymTable, 1, func(args []sxpf.Object) sxpf.Object {
		thead := sxpf.Nil()
		if header := te.getList(args[0]); !sxpf.IsNil(header) {
			thead = sxpf.Nil().Cons(te.transformTableRow(header)).Cons(te.Make("thead"))
		}

		tbody := sxpf.Nil()
		if len(args) > 1 {
			tbody = sxpf.Nil().Cons(te.Make("tbody"))
			curBody := tbody
			for _, row := range args[1:] {
				curBody = curBody.AppendBang(te.transformTableRow(te.getList(row)))
			}
		}

		table := sxpf.Nil()
		if tbody != nil {
			table = table.Cons(tbody)
		}
		if thead != nil {
			table = table.Cons(thead)
		}
		if table == nil {
			return sxpf.Nil()
		}
		return table.Cons(te.Make("table"))
	})
	te.bind(sz.NameSymCell, 0, te.makeCellFn(""))
	te.bind(sz.NameSymCellCenter, 0, te.makeCellFn("center"))
	te.bind(sz.NameSymCellLeft, 0, te.makeCellFn("left"))
	te.bind(sz.NameSymCellRight, 0, te.makeCellFn("right"))

	te.bind(sz.NameSymRegionBlock, 2, te.makeRegionFn(te.Make("div"), true))
	te.bind(sz.NameSymRegionQuote, 2, te.makeRegionFn(te.Make("blockquote"), false))
	te.bind(sz.NameSymRegionVerse, 2, te.makeRegionFn(te.Make("div"), false))

	te.bind(sz.NameSymVerbatimComment, 1, func(args []sxpf.Object) sxpf.Object {
		if te.getAttributes(args[0]).HasDefault() {
			if len(args) > 1 {
				if s := te.getString(args[1]); s != "" {
					t := sxpf.MakeString(s.String())
					return sxpf.Nil().Cons(t).Cons(te.Make(sxhtml.NameSymBlockComment))
				}
			}
		}
		return nil
	})

	te.bind(sz.NameSymVerbatimEval, 2, func(args []sxpf.Object) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args[0]).AddClass("zs-eval"), te.getString(args[1]))
	})
	te.bind(sz.NameSymVerbatimHTML, 2, te.transformHTML)
	te.bind(sz.NameSymVerbatimMath, 2, func(args []sxpf.Object) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args[0]).AddClass("zs-math"), te.getString(args[1]))
	})
	te.bind(sz.NameSymVerbatimProg, 2, func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		content := te.getString(args[1])
		if a.HasDefault() {
			content = sxpf.MakeString(visibleReplacer.Replace(content.String()))
		}
		return te.transformVerbatim(a, content)
	})
	te.bind(sz.NameSymVerbatimZettel, 0, func([]sxpf.Object) sxpf.Object { return sxpf.Nil() })

	te.bind(sz.NameSymBLOB, 3, func(args []sxpf.Object) sxpf.Object {

		return te.transformBLOB(te.getList(args[0]), te.getString(args[1]), te.getString(args[2]))
	})

	te.bind(sz.NameSymTransclude, 2, func(args []sxpf.Object) sxpf.Object {
		ref, ok := sxpf.GetList(args[1])
		if !ok {
			return sxpf.Nil()
		}
		refKind := ref.Car()
		if sxpf.IsNil(refKind) {
			return sxpf.Nil()
		}
		if refValue := te.getString(ref.Tail().Car()); refValue != "" {
			if te.astSF.MustMake(sz.NameSymRefStateExternal).IsEqual(refKind) {
				a := te.getAttributes(args[0]).Set("src", refValue.String()).AddClass("external")
				return sxpf.Nil().Cons(sxpf.Nil().Cons(te.transformAttribute(a)).Cons(te.Make("img"))).Cons(te.symP)
			}
			return sxpf.MakeList(
				te.Make(sxhtml.NameSymInlineComment),
				sxpf.MakeString("transclude"),
				refKind,
				sxpf.MakeString("->"),
				refValue,
			)
		}
		return sxpf.MakeList(args...)
	})
}

func (te *TransformEnv) makeListFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args []sxpf.Object) sxpf.Object {
		result := sxpf.Nil().Cons(sym)
		last := result
		for _, elem := range args {
			item := sxpf.Nil().Cons(te.Make("li"))
			if res, ok := sxpf.GetList(elem); ok {
				item.ExtendBang(res)
			}
			last = last.AppendBang(item)
		}
		return result
	}
}
func (te *TransformEnv) transformTableRow(cells *sxpf.List) *sxpf.List {
	row := sxpf.Nil().Cons(te.Make("tr"))
	if cells == nil {
		return sxpf.Nil()
	}
	curRow := row
	for cell := cells; cell != nil; cell = cell.Tail() {
		curRow = curRow.AppendBang(cell.Car())
	}
	return row
}

func (te *TransformEnv) makeCellFn(align string) transformFn {
	return func(args []sxpf.Object) sxpf.Object {
		tdata := sxpf.MakeList(args...)
		if align != "" {
			tdata = tdata.Cons(te.transformAttribute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(te.Make("td"))
	}
}

func (te *TransformEnv) makeRegionFn(sym *sxpf.Symbol, genericToClass bool) transformFn {
	return func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		result := sxpf.Nil()
		if len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		result = result.Cons(sym)
		currResult := result.LastPair()

		if region, ok := sxpf.GetList(args[1]); ok {
			currResult = currResult.ExtendBang(region)
		}

		if len(args) > 2 {
			if cite, ok := sxpf.GetList(args[2]); ok && cite != nil {
				currResult.AppendBang(cite.Cons(te.Make("cite")))
			}
		}
		return result
	}
}

func (te *TransformEnv) transformVerbatim(a attrs.Attributes, s sxpf.String) sxpf.Object {
	a = setProgLang(a)
	code := sxpf.Nil().Cons(s)
	if al := te.transformAttribute(a); al != nil {
		code = code.Cons(al)
	}
	code = code.Cons(te.Make("code"))
	return sxpf.Nil().Cons(code).Cons(te.Make("pre"))
}

func (te *TransformEnv) bindInlines() {
	te.bind(sz.NameSymInline, 0, listArgs)
	te.bind(sz.NameSymText, 1, func(args []sxpf.Object) sxpf.Object { return te.getString(args[0]) })
	te.bind(sz.NameSymSpace, 0, func(args []sxpf.Object) sxpf.Object {
		if len(args) == 0 {
			return sxpf.MakeString(" ")
		}
		return te.getString(args[0])
	})
	te.bind(sz.NameSymSoft, 0, func([]sxpf.Object) sxpf.Object { return sxpf.MakeString(" ") })
	brSym := te.Make("br")
	te.bind(sz.NameSymHard, 0, func([]sxpf.Object) sxpf.Object { return sxpf.Nil().Cons(brSym) })

	te.bind(sz.NameSymLinkInvalid, 2, func(args []sxpf.Object) sxpf.Object {
		// a := te.getAttributes(args)
		var inline *sxpf.List
		if len(args) > 2 {
			inline = sxpf.MakeList(args[2:]...)
		}
		if inline == nil {
			inline = sxpf.Nil().Cons(args[1])
		}
		return inline.Cons(te.symSpan)
	})
	transformHREF := func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		refValue := te.getString(args[1])
		return te.transformLink(a.Set("href", refValue.String()), refValue, args[2:])
	}
	te.bind(sz.NameSymLinkZettel, 2, transformHREF)
	te.bind(sz.NameSymLinkSelf, 2, transformHREF)
	te.bind(sz.NameSymLinkFound, 2, transformHREF)
	te.bind(sz.NameSymLinkBroken, 2, func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		refValue := te.getString(args[1])
		return te.transformLink(a.AddClass("broken"), refValue, args[2:])
	})
	te.bind(sz.NameSymLinkHosted, 2, transformHREF)
	te.bind(sz.NameSymLinkBased, 2, transformHREF)
	te.bind(sz.NameSymLinkQuery, 2, func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		refValue := te.getString(args[1])
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String())
		return te.transformLink(a.Set("href", query), refValue, args[2:])
	})
	te.bind(sz.NameSymLinkExternal, 2, func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		refValue := te.getString(args[1])
		return te.transformLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args[2:])
	})

	te.bind(sz.NameSymEmbed, 3, func(args []sxpf.Object) sxpf.Object {

		ref := te.getList(args[1])
		syntax := te.getString(args[2])
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sxpf.MakeList(
				te.symAttr,
				sxpf.Cons(te.Make("type"), sxpf.MakeString("image/svg+xml")),
				sxpf.Cons(te.Make("src"), sxpf.MakeString("/"+te.getString(ref.Tail()).String()+".svg")),
			)
			return sxpf.MakeList(
				te.Make("figure"),
				sxpf.MakeList(
					te.Make("embed"),
					embedAttr,
				),
			)
		}
		a := te.getAttributes(args[0])
		a = a.Set("src", string(te.getString(ref.Tail().Car())))
		var sb strings.Builder
		te.flattenText(&sb, ref.Tail().Tail().Tail())
		if d := sb.String(); d != "" {
			a = a.Set("alt", d)
		}
		return sxpf.MakeList(te.Make("img"), te.transformAttribute(a))
	})
	te.bind(sz.NameSymEmbedBLOB, 3, func(args []sxpf.Object) sxpf.Object {

		a, syntax, data := te.getAttributes(args[0]), te.getString(args[1]), te.getString(args[2])
		summary, _ := a.Get(api.KeySummary)
		return te.transformBLOB(
			sxpf.MakeList(te.astSF.MustMake(sz.NameSymInline), sxpf.MakeString(summary)),
			syntax,
			data,
		)
	})

	te.bind(sz.NameSymCite, 2, func(args []sxpf.Object) sxpf.Object {
		result := sxpf.Nil()

		if key := te.getString(args[1]); key != "" {
			if len(args) > 2 {
				result = sxpf.MakeList(args[2:]...).Cons(sxpf.MakeString(", "))
			}
			result = result.Cons(key)
		}
		if a := te.getAttributes(args[0]); len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(te.symSpan)
	})

	te.bind(sz.NameSymMark, 3, func(args []sxpf.Object) sxpf.Object {

		result := sxpf.MakeList(args[3:]...)
		if !te.tr.noLinks {
			if fragment := te.getString(args[2]); fragment != "" {
				a := attrs.Attributes{"id": fragment.String() + te.tr.unique}
				return result.Cons(te.transformAttribute(a)).Cons(te.symA)
			}
		}
		return result.Cons(te.symSpan)
	})

	te.bind(sz.NameSymEndnote, 1, func(args []sxpf.Object) sxpf.Object {
		attrPlist := sxpf.Nil()
		if a := te.getAttributes(args[0]); len(a) > 0 {
			if attrs := te.transformAttribute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		text, ok := sxpf.GetList(args[1])
		if !ok {
			return sxpf.Nil()
		}
		te.tr.endnotes = append(te.tr.endnotes, endnoteInfo{noteAST: text, noteHx: nil, attrs: attrPlist})
		noteNum := strconv.Itoa(len(te.tr.endnotes))
		noteID := te.tr.unique + noteNum
		hrefAttr := sxpf.Nil().Cons(sxpf.Cons(te.Make("role"), sxpf.MakeString("doc-noteref"))).
			Cons(sxpf.Cons(te.Make("href"), sxpf.MakeString("#fn:"+noteID))).
			Cons(sxpf.Cons(te.tr.symClass, sxpf.MakeString("zs-noteref"))).
			Cons(te.symAttr)
		href := sxpf.Nil().Cons(sxpf.MakeString(noteNum)).Cons(hrefAttr).Cons(te.symA)
		supAttr := sxpf.Nil().Cons(sxpf.Cons(te.Make("id"), sxpf.MakeString("fnref:"+noteID))).Cons(te.symAttr)
		return sxpf.Nil().Cons(href).Cons(supAttr).Cons(te.Make("sup"))
	})

	te.bind(sz.NameSymFormatDelete, 1, te.makeFormatFn("del"))
	te.bind(sz.NameSymFormatEmph, 1, te.makeFormatFn("em"))
	te.bind(sz.NameSymFormatInsert, 1, te.makeFormatFn("ins"))
	te.bind(sz.NameSymFormatQuote, 1, te.transformQuote)
	te.bind(sz.NameSymFormatSpan, 1, te.makeFormatFn("span"))
	te.bind(sz.NameSymFormatStrong, 1, te.makeFormatFn("strong"))
	te.bind(sz.NameSymFormatSub, 1, te.makeFormatFn("sub"))
	te.bind(sz.NameSymFormatSuper, 1, te.makeFormatFn("sup"))

	te.bind(sz.NameSymLiteralComment, 1, func(args []sxpf.Object) sxpf.Object {
		if te.getAttributes(args[0]).HasDefault() {
			if len(args) > 1 {
				if s := te.getString(args[1]); s != "" {
					return sxpf.Nil().Cons(s).Cons(te.Make(sxhtml.NameSymInlineComment))
				}
			}
		}
		return sxpf.Nil()
	})
	te.bind(sz.NameSymLiteralHTML, 2, te.transformHTML)
	kbdSym := te.Make("kbd")
	te.bind(sz.NameSymLiteralInput, 2, func(args []sxpf.Object) sxpf.Object {
		return te.transformLiteral(args, nil, kbdSym)
	})
	codeSym := te.Make("code")
	te.bind(sz.NameSymLiteralMath, 2, func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0]).AddClass("zs-math")
		return te.transformLiteral(args, a, codeSym)
	})
	sampSym := te.Make("samp")
	te.bind(sz.NameSymLiteralOutput, 2, func(args []sxpf.Object) sxpf.Object {
		return te.transformLiteral(args, nil, sampSym)
	})
	te.bind(sz.NameSymLiteralProg, 2, func(args []sxpf.Object) sxpf.Object {
		return te.transformLiteral(args, nil, codeSym)
	})

	te.bind(sz.NameSymLiteralZettel, 0, func([]sxpf.Object) sxpf.Object { return sxpf.Nil() })
}

func (te *TransformEnv) makeFormatFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args []sxpf.Object) sxpf.Object {
		a := te.getAttributes(args[0])
		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		res := sxpf.MakeList(args[1:]...)
		if len(a) > 0 {
			res = res.Cons(te.transformAttribute(a))
		}
		return res.Cons(sym)
	}
}
func (te *TransformEnv) transformQuote(args []sxpf.Object) sxpf.Object {
	const langAttr = "lang"
	a := te.getAttributes(args[0])
	langVal, found := a.Get(langAttr)
	if found {
		a = a.Remove(langAttr)
	}
	if val, found2 := a.Get(""); found2 {
		a = a.Remove("").AddClass(val)
	}
	res := sxpf.MakeList(args[1:]...)
	if len(a) > 0 {
		res = res.Cons(te.transformAttribute(a))
	}
	res = res.Cons(te.Make("q"))
	if found {
		res = sxpf.Nil().Cons(res).Cons(te.transformAttribute(attrs.Attributes{}.Set(langAttr, langVal))).Cons(te.symSpan)
	}
	return res
}

var visibleReplacer = strings.NewReplacer(" ", "\u2423")

func (te *TransformEnv) transformLiteral(args []sxpf.Object, a attrs.Attributes, sym *sxpf.Symbol) sxpf.Object {
	if a == nil {
		a = te.getAttributes(args[0])
	}
	a = setProgLang(a)
	literal := te.getString(args[1]).String()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sxpf.Nil().Cons(sxpf.MakeString(literal))
	if len(a) > 0 {
		res = res.Cons(te.transformAttribute(a))
	}
	return res.Cons(sym)
}

func setProgLang(a attrs.Attributes) attrs.Attributes {
	if val, found := a.Get(""); found {
		a = a.AddClass("language-" + val).Remove("")
	}
	return a
}

func (te *TransformEnv) transformHTML(args []sxpf.Object) sxpf.Object {
	if s := te.getString(args[1]); s != "" && IsSafe(s.String()) {
		return sxpf.Nil().Cons(s).Cons(te.symNoEscape)
	}
	return nil
}

func (te *TransformEnv) transformBLOB(description *sxpf.List, syntax, data sxpf.String) sxpf.Object {
	if data == "" {
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826










827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
			sb.WriteString(obj.String())
		case *sxpf.List:
			te.flattenText(sb, obj)
		}
	}
}

type transformFn func(*sxpf.List) sxpf.Object

func (te *TransformEnv) bind(name string, minArity int, fn transformFn) {
	te.astEnv.Bind(te.astSF.MustMake(name), eval.MakeBuiltin(name, func(_ sxpf.Environment, args *sxpf.List) (sxpf.Object, error) {
		if nArgs := args.Length(); nArgs < minArity {
			return sxpf.Nil(), fmt.Errorf("not enough arguments (%d) for form %v (%d)", nArgs, name, minArity)
		}
		res := fn(args)
		return res, te.err
	}))
}

func (te *TransformEnv) Rebind(name string, fn func(sxpf.Environment, *sxpf.List, sxpf.Callable) sxpf.Object) {
	sym := te.astSF.MustMake(name)
	obj, found := te.astEnv.Lookup(sym)
	if !found {
		panic(sym.String())
	}
	preFn, ok := obj.(sxpf.Callable)
	if !ok {
		panic(sym.String())
	}
	te.astEnv.Bind(sym, eval.MakeBuiltin(name, func(env sxpf.Environment, args *sxpf.List) (sxpf.Object, error) {
		res := fn(env, args, preFn)
		return res, te.err
	}))
}

func (te *TransformEnv) Make(name string) *sxpf.Symbol { return te.tr.Make(name) }










func (te *TransformEnv) getString(lst *sxpf.List) sxpf.String {
	if te.err != nil {
		return ""
	}
	val := lst.Car()
	if s, ok := val.(sxpf.String); ok {
		return s
	}
	te.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
func (te *TransformEnv) getInt64(lst *sxpf.List) int64 {
	if te.err != nil {
		return -1017
	}
	val := lst.Car()
	if num, ok := val.(*sxpf.Number); ok {
		return num.GetInt64()
	}
	te.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}
func (te *TransformEnv) getList(lst *sxpf.List) *sxpf.List {
	if te.err == nil {
		val := lst.Car()
		if res, ok := val.(*sxpf.List); ok {
			return res
		}
		te.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return sxpf.Nil()
}
func (te *TransformEnv) getAttributes(args *sxpf.List) attrs.Attributes {
	return sexpr.GetAttributes(te.getList(args))
}

func (te *TransformEnv) transformLink(a attrs.Attributes, refValue sxpf.String, inline *sxpf.List) sxpf.Object {
	result := inline
	if inline.IsNil() {
		result = sxpf.Nil().Cons(refValue)
	}
	if te.tr.noLinks {
		return result.Cons(te.symSpan)
	}
	return result.Cons(te.transformAttribute(a)).Cons(te.symA)
}







|


|
|







|





|



|
|





>
>
>
>
>
>
>
>
>
>
|



<
|





|



<
|
|




|

<
|






|
|


|
|
|







787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836

837
838
839
840
841
842
843
844
845
846

847
848
849
850
851
852
853
854

855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
			sb.WriteString(obj.String())
		case *sxpf.List:
			te.flattenText(sb, obj)
		}
	}
}

type transformFn func([]sxpf.Object) sxpf.Object

func (te *TransformEnv) bind(name string, minArity int, fn transformFn) {
	te.astEnv.Bind(te.astSF.MustMake(name), eval.MakeBuiltinA(name, func(args []sxpf.Object) (sxpf.Object, error) {
		if nArgs := len(args); nArgs < minArity {
			return sxpf.Nil(), fmt.Errorf("not enough arguments (%d) for form %v (%d)", nArgs, name, minArity)
		}
		res := fn(args)
		return res, te.err
	}))
}

func (te *TransformEnv) Rebind(name string, fn func([]sxpf.Object, eval.Callable) sxpf.Object) {
	sym := te.astSF.MustMake(name)
	obj, found := te.astEnv.Lookup(sym)
	if !found {
		panic(sym.String())
	}
	preFn, ok := eval.GetCallable(obj)
	if !ok {
		panic(sym.String())
	}
	te.astEnv.Bind(sym, eval.MakeBuiltinA(name, func(args []sxpf.Object) (sxpf.Object, error) {
		res := fn(args, preFn)
		return res, te.err
	}))
}

func (te *TransformEnv) Make(name string) *sxpf.Symbol { return te.tr.Make(name) }
func (te *TransformEnv) getSymbol(val sxpf.Object) *sxpf.Symbol {
	if te.err != nil {
		return nil
	}
	if sym, ok := sxpf.GetSymbol(val); ok {
		return sym
	}
	te.err = fmt.Errorf("%v/%T is not a symbol", val, val)
	return nil
}
func (te *TransformEnv) getString(val sxpf.Object) sxpf.String {
	if te.err != nil {
		return ""
	}

	if s, ok := sxpf.GetString(val); ok {
		return s
	}
	te.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
func (te *TransformEnv) getInt64(val sxpf.Object) int64 {
	if te.err != nil {
		return -1017
	}

	if num, ok := sxpf.GetNumber(val); ok {
		return int64(num.(sxpf.Int64))
	}
	te.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}
func (te *TransformEnv) getList(val sxpf.Object) *sxpf.List {
	if te.err == nil {

		if res, ok := sxpf.GetList(val); ok {
			return res
		}
		te.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return sxpf.Nil()
}
func (te *TransformEnv) getAttributes(args sxpf.Object) attrs.Attributes {
	return sz.GetAttributes(te.getList(args))
}

func (te *TransformEnv) transformLink(a attrs.Attributes, refValue sxpf.String, inline []sxpf.Object) sxpf.Object {
	result := sxpf.MakeList(inline...)
	if len(inline) == 0 {
		result = sxpf.Nil().Cons(refValue)
	}
	if te.tr.noLinks {
		return result.Cons(te.symSpan)
	}
	return result.Cons(te.transformAttribute(a)).Cons(te.symA)
}

Added sz/const.go.



















































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz

import "codeberg.org/t73fde/sxpf"

// Various constants for Zettel data. Some of them are technically variables.

const (
	// Symbols for Metanodes
	NameSymBlock  = "BLOCK"
	NameSymInline = "INLINE"
	NameSymList   = "list"
	NameSymMeta   = "META"
	NameSymQuote  = "quote"

	// Symbols for Zettel node types.
	NameSymBLOB            = "BLOB"
	NameSymCell            = "CELL"
	NameSymCellCenter      = "CELL-CENTER"
	NameSymCellLeft        = "CELL-LEFT"
	NameSymCellRight       = "CELL-RIGHT"
	NameSymCite            = "CITE"
	NameSymDescription     = "DESCRIPTION"
	NameSymEmbed           = "EMBED"
	NameSymEmbedBLOB       = "EMBED-BLOB"
	NameSymEndnote         = "ENDNOTE"
	NameSymFormatEmph      = "FORMAT-EMPH"
	NameSymFormatDelete    = "FORMAT-DELETE"
	NameSymFormatInsert    = "FORMAT-INSERT"
	NameSymFormatQuote     = "FORMAT-QUOTE"
	NameSymFormatSpan      = "FORMAT-SPAN"
	NameSymFormatSub       = "FORMAT-SUB"
	NameSymFormatSuper     = "FORMAT-SUPER"
	NameSymFormatStrong    = "FORMAT-STRONG"
	NameSymHard            = "HARD"
	NameSymHeading         = "HEADING"
	NameSymLinkInvalid     = "LINK-INVALID"
	NameSymLinkZettel      = "LINK-ZETTEL"
	NameSymLinkSelf        = "LINK-SELF"
	NameSymLinkFound       = "LINK-FOUND"
	NameSymLinkBroken      = "LINK-BROKEN"
	NameSymLinkHosted      = "LINK-HOSTED"
	NameSymLinkBased       = "LINK-BASED"
	NameSymLinkQuery       = "LINK-QUERY"
	NameSymLinkExternal    = "LINK-EXTERNAL"
	NameSymListOrdered     = "ORDERED"
	NameSymListUnordered   = "UNORDERED"
	NameSymListQuote       = "QUOTATION"
	NameSymLiteralProg     = "LITERAL-CODE"
	NameSymLiteralComment  = "LITERAL-COMMENT"
	NameSymLiteralHTML     = "LITERAL-HTML"
	NameSymLiteralInput    = "LITERAL-INPUT"
	NameSymLiteralMath     = "LITERAL-MATH"
	NameSymLiteralOutput   = "LITERAL-OUTPUT"
	NameSymLiteralZettel   = "LITERAL-ZETTEL"
	NameSymMark            = "MARK"
	NameSymPara            = "PARA"
	NameSymRegionBlock     = "REGION-BLOCK"
	NameSymRegionQuote     = "REGION-QUOTE"
	NameSymRegionVerse     = "REGION-VERSE"
	NameSymSoft            = "SOFT"
	NameSymSpace           = "SPACE"
	NameSymTable           = "TABLE"
	NameSymText            = "TEXT"
	NameSymThematic        = "THEMATIC"
	NameSymTransclude      = "TRANSCLUDE"
	NameSymUnknown         = "UNKNOWN-NODE"
	NameSymVerbatimComment = "VERBATIM-COMMENT"
	NameSymVerbatimEval    = "VERBATIM-EVAL"
	NameSymVerbatimHTML    = "VERBATIM-HTML"
	NameSymVerbatimMath    = "VERBATIM-MATH"
	NameSymVerbatimProg    = "VERBATIM-CODE"
	NameSymVerbatimZettel  = "VERBATIM-ZETTEL"

	// Constant symbols for reference states.
	NameSymRefStateInvalid  = "INVALID"
	NameSymRefStateZettel   = "ZETTEL"
	NameSymRefStateSelf     = "SELF"
	NameSymRefStateFound    = "FOUND"
	NameSymRefStateBroken   = "BROKEN"
	NameSymRefStateHosted   = "HOSTED"
	NameSymRefStateBased    = "BASED"
	NameSymRefStateQuery    = "QUERY"
	NameSymRefStateExternal = "EXTERNAL"

	// Symbols for metadata types.
	NameSymTypeCredential   = "CREDENTIAL"
	NameSymTypeEmpty        = "EMPTY-STRING"
	NameSymTypeID           = "ZID"
	NameSymTypeIDSet        = "ZID-SET"
	NameSymTypeNumber       = "NUMBER"
	NameSymTypeString       = "STRING"
	NameSymTypeTagSet       = "TAG-SET"
	NameSymTypeTimestamp    = "TIMESTAMP"
	NameSymTypeURL          = "URL"
	NameSymTypeWord         = "WORD"
	NameSymTypeWordSet      = "WORD-SET"
	NameSymTypeZettelmarkup = "ZETTELMARKUP"
)

// ZettelSymbols collect all symbols needed to represent zettel data.
type ZettelSymbols struct {
	// Symbols for Metanodes
	SymBlock  *sxpf.Symbol
	SymInline *sxpf.Symbol
	SymList   *sxpf.Symbol
	SymMeta   *sxpf.Symbol
	SymQuote  *sxpf.Symbol

	// Symbols for Zettel node types.
	SymBLOB            *sxpf.Symbol
	SymCell            *sxpf.Symbol
	SymCellCenter      *sxpf.Symbol
	SymCellLeft        *sxpf.Symbol
	SymCellRight       *sxpf.Symbol
	SymCite            *sxpf.Symbol
	SymDescription     *sxpf.Symbol
	SymEmbed           *sxpf.Symbol
	SymEmbedBLOB       *sxpf.Symbol
	SymEndnote         *sxpf.Symbol
	SymFormatEmph      *sxpf.Symbol
	SymFormatDelete    *sxpf.Symbol
	SymFormatInsert    *sxpf.Symbol
	SymFormatQuote     *sxpf.Symbol
	SymFormatSpan      *sxpf.Symbol
	SymFormatSub       *sxpf.Symbol
	SymFormatSuper     *sxpf.Symbol
	SymFormatStrong    *sxpf.Symbol
	SymHard            *sxpf.Symbol
	SymHeading         *sxpf.Symbol
	SymLinkInvalid     *sxpf.Symbol
	SymLinkZettel      *sxpf.Symbol
	SymLinkSelf        *sxpf.Symbol
	SymLinkFound       *sxpf.Symbol
	SymLinkBroken      *sxpf.Symbol
	SymLinkHosted      *sxpf.Symbol
	SymLinkBased       *sxpf.Symbol
	SymLinkQuery       *sxpf.Symbol
	SymLinkExternal    *sxpf.Symbol
	SymListOrdered     *sxpf.Symbol
	SymListUnordered   *sxpf.Symbol
	SymListQuote       *sxpf.Symbol
	SymLiteralProg     *sxpf.Symbol
	SymLiteralComment  *sxpf.Symbol
	SymLiteralHTML     *sxpf.Symbol
	SymLiteralInput    *sxpf.Symbol
	SymLiteralMath     *sxpf.Symbol
	SymLiteralOutput   *sxpf.Symbol
	SymLiteralZettel   *sxpf.Symbol
	SymMark            *sxpf.Symbol
	SymPara            *sxpf.Symbol
	SymRegionBlock     *sxpf.Symbol
	SymRegionQuote     *sxpf.Symbol
	SymRegionVerse     *sxpf.Symbol
	SymSoft            *sxpf.Symbol
	SymSpace           *sxpf.Symbol
	SymTable           *sxpf.Symbol
	SymText            *sxpf.Symbol
	SymThematic        *sxpf.Symbol
	SymTransclude      *sxpf.Symbol
	SymUnknown         *sxpf.Symbol
	SymVerbatimComment *sxpf.Symbol
	SymVerbatimEval    *sxpf.Symbol
	SymVerbatimHTML    *sxpf.Symbol
	SymVerbatimMath    *sxpf.Symbol
	SymVerbatimProg    *sxpf.Symbol
	SymVerbatimZettel  *sxpf.Symbol

	// Constant symbols for reference states.

	SymRefStateInvalid  *sxpf.Symbol
	SymRefStateZettel   *sxpf.Symbol
	SymRefStateSelf     *sxpf.Symbol
	SymRefStateFound    *sxpf.Symbol
	SymRefStateBroken   *sxpf.Symbol
	SymRefStateHosted   *sxpf.Symbol
	SymRefStateBased    *sxpf.Symbol
	SymRefStateQuery    *sxpf.Symbol
	SymRefStateExternal *sxpf.Symbol

	// Symbols for metadata types

	SymTypeCredential   *sxpf.Symbol
	SymTypeEmpty        *sxpf.Symbol
	SymTypeID           *sxpf.Symbol
	SymTypeIDSet        *sxpf.Symbol
	SymTypeNumber       *sxpf.Symbol
	SymTypeString       *sxpf.Symbol
	SymTypeTagSet       *sxpf.Symbol
	SymTypeTimestamp    *sxpf.Symbol
	SymTypeURL          *sxpf.Symbol
	SymTypeWord         *sxpf.Symbol
	SymTypeWordSet      *sxpf.Symbol
	SymTypeZettelmarkup *sxpf.Symbol
}

func (zs *ZettelSymbols) InitializeZettelSymbols(sf sxpf.SymbolFactory) {
	// Symbols for Metanodes
	zs.SymBlock = sf.MustMake(NameSymBlock)
	zs.SymInline = sf.MustMake(NameSymInline)
	zs.SymList = sf.MustMake(NameSymList)
	zs.SymMeta = sf.MustMake(NameSymMeta)
	zs.SymQuote = sf.MustMake(NameSymQuote)

	// Symbols for Zettel node types.
	zs.SymBLOB = sf.MustMake(NameSymBLOB)
	zs.SymCell = sf.MustMake(NameSymCell)
	zs.SymCellCenter = sf.MustMake(NameSymCellCenter)
	zs.SymCellLeft = sf.MustMake(NameSymCellLeft)
	zs.SymCellRight = sf.MustMake(NameSymCellRight)
	zs.SymCite = sf.MustMake(NameSymCite)
	zs.SymDescription = sf.MustMake(NameSymDescription)
	zs.SymEmbed = sf.MustMake(NameSymEmbed)
	zs.SymEmbedBLOB = sf.MustMake(NameSymEmbedBLOB)
	zs.SymEndnote = sf.MustMake(NameSymEndnote)
	zs.SymFormatEmph = sf.MustMake(NameSymFormatEmph)
	zs.SymFormatDelete = sf.MustMake(NameSymFormatDelete)
	zs.SymFormatInsert = sf.MustMake(NameSymFormatInsert)
	zs.SymFormatQuote = sf.MustMake(NameSymFormatQuote)
	zs.SymFormatSpan = sf.MustMake(NameSymFormatSpan)
	zs.SymFormatSub = sf.MustMake(NameSymFormatSub)
	zs.SymFormatSuper = sf.MustMake(NameSymFormatSuper)
	zs.SymFormatStrong = sf.MustMake(NameSymFormatStrong)
	zs.SymHard = sf.MustMake(NameSymHard)
	zs.SymHeading = sf.MustMake(NameSymHeading)
	zs.SymLinkInvalid = sf.MustMake(NameSymLinkInvalid)
	zs.SymLinkZettel = sf.MustMake(NameSymLinkZettel)
	zs.SymLinkSelf = sf.MustMake(NameSymLinkSelf)
	zs.SymLinkFound = sf.MustMake(NameSymLinkFound)
	zs.SymLinkBroken = sf.MustMake(NameSymLinkBroken)
	zs.SymLinkHosted = sf.MustMake(NameSymLinkHosted)
	zs.SymLinkBased = sf.MustMake(NameSymLinkBased)
	zs.SymLinkQuery = sf.MustMake(NameSymLinkQuery)
	zs.SymLinkExternal = sf.MustMake(NameSymLinkExternal)
	zs.SymListOrdered = sf.MustMake(NameSymListOrdered)
	zs.SymListUnordered = sf.MustMake(NameSymListUnordered)
	zs.SymListQuote = sf.MustMake(NameSymListQuote)
	zs.SymLiteralProg = sf.MustMake(NameSymLiteralProg)
	zs.SymLiteralComment = sf.MustMake(NameSymLiteralComment)
	zs.SymLiteralHTML = sf.MustMake(NameSymLiteralHTML)
	zs.SymLiteralInput = sf.MustMake(NameSymLiteralInput)
	zs.SymLiteralMath = sf.MustMake(NameSymLiteralMath)
	zs.SymLiteralOutput = sf.MustMake(NameSymLiteralOutput)
	zs.SymLiteralZettel = sf.MustMake(NameSymLiteralZettel)
	zs.SymMark = sf.MustMake(NameSymMark)
	zs.SymPara = sf.MustMake(NameSymPara)
	zs.SymRegionBlock = sf.MustMake(NameSymRegionBlock)
	zs.SymRegionQuote = sf.MustMake(NameSymRegionQuote)
	zs.SymRegionVerse = sf.MustMake(NameSymRegionVerse)
	zs.SymSoft = sf.MustMake(NameSymSoft)
	zs.SymSpace = sf.MustMake(NameSymSpace)
	zs.SymTable = sf.MustMake(NameSymTable)
	zs.SymText = sf.MustMake(NameSymText)
	zs.SymThematic = sf.MustMake(NameSymThematic)
	zs.SymTransclude = sf.MustMake(NameSymTransclude)
	zs.SymUnknown = sf.MustMake(NameSymUnknown)
	zs.SymVerbatimComment = sf.MustMake(NameSymVerbatimComment)
	zs.SymVerbatimEval = sf.MustMake(NameSymVerbatimEval)
	zs.SymVerbatimHTML = sf.MustMake(NameSymVerbatimHTML)
	zs.SymVerbatimMath = sf.MustMake(NameSymVerbatimMath)
	zs.SymVerbatimProg = sf.MustMake(NameSymVerbatimProg)
	zs.SymVerbatimZettel = sf.MustMake(NameSymVerbatimZettel)

	// Constant symbols for reference states.
	zs.SymRefStateInvalid = sf.MustMake(NameSymRefStateInvalid)
	zs.SymRefStateZettel = sf.MustMake(NameSymRefStateZettel)
	zs.SymRefStateSelf = sf.MustMake(NameSymRefStateSelf)
	zs.SymRefStateFound = sf.MustMake(NameSymRefStateFound)
	zs.SymRefStateBroken = sf.MustMake(NameSymRefStateBroken)
	zs.SymRefStateHosted = sf.MustMake(NameSymRefStateHosted)
	zs.SymRefStateBased = sf.MustMake(NameSymRefStateBased)
	zs.SymRefStateQuery = sf.MustMake(NameSymRefStateQuery)
	zs.SymRefStateExternal = sf.MustMake(NameSymRefStateExternal)

	// Symbols for metadata types.
	zs.SymTypeCredential = sf.MustMake(NameSymTypeCredential)
	zs.SymTypeEmpty = sf.MustMake(NameSymTypeEmpty)
	zs.SymTypeID = sf.MustMake(NameSymTypeID)
	zs.SymTypeIDSet = sf.MustMake(NameSymTypeIDSet)
	zs.SymTypeNumber = sf.MustMake(NameSymTypeNumber)
	zs.SymTypeString = sf.MustMake(NameSymTypeString)
	zs.SymTypeTagSet = sf.MustMake(NameSymTypeTagSet)
	zs.SymTypeTimestamp = sf.MustMake(NameSymTypeTimestamp)
	zs.SymTypeURL = sf.MustMake(NameSymTypeURL)
	zs.SymTypeWord = sf.MustMake(NameSymTypeWord)
	zs.SymTypeWordSet = sf.MustMake(NameSymTypeWordSet)
	zs.SymTypeZettelmarkup = sf.MustMake(NameSymTypeZettelmarkup)
}

Added sz/const_test.go.





















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz_test

import (
	"testing"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/sz"
)

func BenchmarkInitializeZettelSymbols(b *testing.B) {
	sf := sxpf.MakeMappedFactory()
	for i := 0; i < b.N; i++ {
		var zs sz.ZettelSymbols
		zs.InitializeZettelSymbols(sf)
	}
}

Added sz/sz.go.













































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz

import (
	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/attrs"
)

// GetAttributes traverses a s-expression list and returns an attribute structure.
func GetAttributes(seq *sxpf.List) (result attrs.Attributes) {
	for elem := seq; elem != nil; elem = elem.Tail() {
		p, ok := sxpf.GetList(elem.Car())
		if !ok || p == nil {
			continue
		}
		key := p.Car()
		if !key.IsAtom() {
			continue
		}
		val := p.Cdr()
		if tail, ok2 := sxpf.GetList(val); ok2 {
			val = tail.Car()
		}
		if !val.IsAtom() {
			continue
		}
		result = result.Set(key.String(), val.String())
	}
	return result
}

// GetMetaContent returns the metadata and the content of a sz encoded zettel.
func GetMetaContent(zettel sxpf.Object) (Meta, *sxpf.List) {
	if pair, ok := sxpf.GetList(zettel); ok {
		m := pair.Car()
		if s := pair.Tail(); s != nil {
			if content, ok2 := sxpf.GetList(s.Car()); ok2 {
				return MakeMeta(m), content
			}
		}
		return MakeMeta(m), nil
	}
	return nil, nil
}

type Meta map[string]MetaValue
type MetaValue struct {
	Type  string
	Key   string
	Value sxpf.Object
}

func MakeMeta(obj sxpf.Object) Meta {
	if result := doMakeMeta(obj); len(result) > 0 {
		return result
	}
	return nil
}
func doMakeMeta(obj sxpf.Object) Meta {
	result := make(map[string]MetaValue)
	for {
		if sxpf.IsNil(obj) {
			return result
		}
		lst, ok := sxpf.GetList(obj)
		if !ok {
			return result
		}
		if mv, ok2 := makeMetaValue(lst); ok2 {
			result[mv.Key] = mv
		}
		obj = lst.Cdr()
	}
}
func makeMetaValue(mnode *sxpf.List) (MetaValue, bool) {
	var result MetaValue
	mval, ok := sxpf.GetList(mnode.Car())
	if !ok {
		return result, false
	}
	typeSym, ok := sxpf.GetSymbol(mval.Car())
	if !ok {
		return result, false
	}
	keyPair, ok := sxpf.GetList(mval.Cdr())
	if !ok {
		return result, false
	}
	keyList, ok := sxpf.GetList(keyPair.Car())
	if !ok {
		return result, false
	}
	quoteSym, ok := sxpf.GetSymbol(keyList.Car())
	if !ok || quoteSym.Name() != "quote" {
		return result, false
	}
	keySym, ok := sxpf.GetSymbol(keyList.Tail().Car())
	if !ok {
		return result, false
	}
	valPair, ok := sxpf.GetList(keyPair.Cdr())
	if !ok {
		return result, false
	}
	result.Type = typeSym.Name()
	result.Key = keySym.Name()
	result.Value = valPair.Car()
	return result, true
}

func (m Meta) GetString(key string) string {
	if v, found := m[key]; found {
		return v.Value.String()
	}
	return ""
}

func (m Meta) GetList(key string) *sxpf.List {
	if mv, found := m[key]; found {
		if seq, ok := sxpf.GetList(mv.Value); ok {
			return seq
		}
	}
	return nil
}

Changes to text/text.go.

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Package text provides types, constants and function to work with text output.
package text

import (
	"strings"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/sexpr"
)

// Encoder is the structure to hold relevant data to execute the encoding.
type Encoder struct {
	sf sxpf.SymbolFactory
	sb strings.Builder








|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Package text provides types, constants and function to work with text output.
package text

import (
	"strings"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/sz"
)

// Encoder is the structure to hold relevant data to execute the encoding.
type Encoder struct {
	sf sxpf.SymbolFactory
	sb strings.Builder

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
func NewEncoder(sf sxpf.SymbolFactory) *Encoder {
	if sf == nil {
		return nil
	}
	enc := &Encoder{
		sf:       sf,
		sb:       strings.Builder{},
		symText:  sf.MustMake(sexpr.NameSymText),
		symSpace: sf.MustMake(sexpr.NameSymSpace),
		symSoft:  sf.MustMake(sexpr.NameSymSoft),
		symHard:  sf.MustMake(sexpr.NameSymHard),
		symQuote: sf.MustMake(sexpr.NameSymQuote),
	}
	return enc
}

func (enc *Encoder) Encode(lst *sxpf.List) string {
	enc.executeList(lst)
	result := enc.sb.String()







|
|
|
|
|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
func NewEncoder(sf sxpf.SymbolFactory) *Encoder {
	if sf == nil {
		return nil
	}
	enc := &Encoder{
		sf:       sf,
		sb:       strings.Builder{},
		symText:  sf.MustMake(sz.NameSymText),
		symSpace: sf.MustMake(sz.NameSymSpace),
		symSoft:  sf.MustMake(sz.NameSymSoft),
		symHard:  sf.MustMake(sz.NameSymHard),
		symQuote: sf.MustMake(sz.NameSymQuote),
	}
	return enc
}

func (enc *Encoder) Encode(lst *sxpf.List) string {
	enc.executeList(lst)
	result := enc.sb.String()

Changes to text/text_test.go.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	"testing"

	"codeberg.org/t73fde/sxpf"
	"codeberg.org/t73fde/sxpf/reader"
	"zettelstore.de/c/text"
)

func TestSexprText(t *testing.T) {
	testcases := []struct {
		src string
		exp string
	}{
		{"()", ""},
		{`(INLINE (TEXT "a"))`, "a"},
		{`(INLINE (SPACE "a"))`, " "},







|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	"testing"

	"codeberg.org/t73fde/sxpf"
	"codeberg.org/t73fde/sxpf/reader"
	"zettelstore.de/c/text"
)

func TestSzText(t *testing.T) {
	testcases := []struct {
		src string
		exp string
	}{
		{"()", ""},
		{`(INLINE (TEXT "a"))`, "a"},
		{`(INLINE (SPACE "a"))`, " "},

Changes to www/changes.wiki.

1
2
3
4

5
6
7
8
9
10
11
<title>Change Log</title>

<a name="0_12"></a>
<h2>Changes for Version 0.12.0 (pending)</h2>


<a name="0_11"></a>
<h2>Changes for Version 0.11.0 (2023-03-27)</h2>
  *  Remove all zjson related declarations.
  *  Generate HTML via SxHTML, not manually and direct.

<a name="0_10"></a>




>







1
2
3
4
5
6
7
8
9
10
11
12
<title>Change Log</title>

<a name="0_12"></a>
<h2>Changes for Version 0.12.0 (pending)</h2>
  *  Rename "sexpr" to "sz".

<a name="0_11"></a>
<h2>Changes for Version 0.11.0 (2023-03-27)</h2>
  *  Remove all zjson related declarations.
  *  Generate HTML via SxHTML, not manually and direct.

<a name="0_10"></a>