Zettelstore Client

Check-in Differences
Login

Check-in Differences

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

Difference From v0.18.0 To trunk

2024-08-26
12:46
Fix generating external links in shtml; some refactorings ... (Leaf check-in: 97640fce44 user: stern tags: trunk)
2024-08-19
09:25
SHTML: generate (th... for table cell in header ... (check-in: 6ca051ffec user: stern tags: trunk)
2024-07-11
16:29
Remove "rename" operation and associated constants ... (check-in: a203b14b7e user: stern tags: trunk)
14:40
Version 0.18.0 ... (check-in: b141c81ad9 user: stern tags: release, trunk, v0.18.0)
2024-07-10
15:15
Reserve ZID for computed zettel about zettel ... (check-in: ea5260d4a2 user: stern tags: trunk)

Changes to api/api.go.

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

// Values for ZettelRights, can be or-ed
const (
	ZettelCanNone   ZettelRights = 1 << iota
	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
	ZettelMaxRight               // Sentinel value
)

// MetaRights contains the metadata of a zettel, and its rights.
type MetaRights struct {
	Meta   ZettelMeta







|







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

// Values for ZettelRights, can be or-ed
const (
	ZettelCanNone   ZettelRights = 1 << iota
	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
	placeholder_1                // Was assigned to rename right, which is now removed
	ZettelCanDelete              // Requesting user is allowed to delete the zettel
	ZettelMaxRight               // Sentinel value
)

// MetaRights contains the metadata of a zettel, and its rights.
type MetaRights struct {
	Meta   ZettelMeta

Changes to api/const.go.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	// WebUI HTML templates are in the range 10000..19999
	ZidBaseTemplate   = ZettelID("00000000010100") // -> 000s
	ZidLoginTemplate  = ZettelID("00000000010200") // -> 000t
	ZidListTemplate   = ZettelID("00000000010300") // -> 000u
	ZidZettelTemplate = ZettelID("00000000010401") // -> 000v
	ZidInfoTemplate   = ZettelID("00000000010402") // -> 000w
	ZidFormTemplate   = ZettelID("00000000010403") // -> 000x
	ZidRenameTemplate = ZettelID("00000000010404") // -> 001z
	ZidDeleteTemplate = ZettelID("00000000010405") // -> 000y
	ZidErrorTemplate  = ZettelID("00000000010700") // -> 000z

	// WebUI sxn code zettel are in the range 19000..19999
	ZidSxnStart = ZettelID("00000000019000") // -> 000q
	ZidSxnBase  = ZettelID("00000000019990") // -> 000r








<







45
46
47
48
49
50
51

52
53
54
55
56
57
58
	// WebUI HTML templates are in the range 10000..19999
	ZidBaseTemplate   = ZettelID("00000000010100") // -> 000s
	ZidLoginTemplate  = ZettelID("00000000010200") // -> 000t
	ZidListTemplate   = ZettelID("00000000010300") // -> 000u
	ZidZettelTemplate = ZettelID("00000000010401") // -> 000v
	ZidInfoTemplate   = ZettelID("00000000010402") // -> 000w
	ZidFormTemplate   = ZettelID("00000000010403") // -> 000x

	ZidDeleteTemplate = ZettelID("00000000010405") // -> 000y
	ZidErrorTemplate  = ZettelID("00000000010700") // -> 000z

	// WebUI sxn code zettel are in the range 19000..19999
	ZidSxnStart = ZettelID("00000000019000") // -> 000q
	ZidSxnBase  = ZettelID("00000000019990") // -> 000r

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	ValueVisibilityLogin   = "login"
	ValueVisibilityOwner   = "owner"
	ValueVisibilityPublic  = "public"
)

// Additional HTTP constants.
const (
	MethodMove = "MOVE" // HTTP method for renaming a zettel

	HeaderAccept      = "Accept"
	HeaderContentType = "Content-Type"
	HeaderDestination = "Destination"
	HeaderLocation    = "Location"
)

// Values for HTTP query parameter.







<
<







172
173
174
175
176
177
178


179
180
181
182
183
184
185
	ValueVisibilityLogin   = "login"
	ValueVisibilityOwner   = "owner"
	ValueVisibilityPublic  = "public"
)

// Additional HTTP constants.
const (


	HeaderAccept      = "Accept"
	HeaderContentType = "Content-Type"
	HeaderDestination = "Destination"
	HeaderLocation    = "Location"
)

// Values for HTTP query parameter.

Changes to attrs/attrs.go.

11
12
13
14
15
16
17

18
19
20
21
22
23
24
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package attrs stores attributes of zettel parts.
package attrs

import (

	"strings"

	"t73f.de/r/zsc/maps"
)

// Attributes store additional information about some node types.
type Attributes map[string]string







>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package attrs stores attributes of zettel parts.
package attrs

import (
	"slices"
	"strings"

	"t73f.de/r/zsc/maps"
)

// Attributes store additional information about some node types.
type Attributes map[string]string
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

func (a Attributes) Remove(key string) Attributes {
	if a != nil {
		delete(a, key)
	}
	return a
}

// AddClass adds a value to the class attribute.
func (a Attributes) AddClass(class string) Attributes {
	if a == nil {
		return map[string]string{"class": class}
	}
	classes := a.GetClasses()
	for _, cls := range classes {
		if cls == class {


			return a
		}
	}
	classes = append(classes, class)

	a["class"] = strings.Join(classes, " ")


	return a
}

// GetClasses returns the class values as a string slice
func (a Attributes) GetClasses() []string {
	if a == nil {
		return nil
	}
	classes, ok := a["class"]
	if !ok {
		return nil
	}


	return strings.Fields(classes)
}

// HasClass returns true, if attributes contains the given class.
func (a Attributes) HasClass(s string) bool {
	if a == nil {
		return false
	}
	classes, found := a["class"]
	if !found {
		return false
	}
	return strings.Contains(" "+classes+" ", " "+s+" ")
}








|
|

|

|
|
|
>
>
|
|
|
<
>
|
>
>
|
|
|
<
<
<
|
|
<
<
<
|
>
>
|


|
|
<
<
|
|
<
|
|
|
<
>
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
func (a Attributes) Remove(key string) Attributes {
	if a != nil {
		delete(a, key)
	}
	return a
}

// Add a value to an attribute key.
func (a Attributes) Add(key, value string) Attributes {
	if a == nil {
		return map[string]string{key: value}
	}
	values := a.Values(key)
	if !slices.Contains(values, value) {
		values = append(values, value)
		a[key] = strings.Join(values, " ")
	}
	return a
}


// Values are the space separated values of an attribute.
func (a Attributes) Values(key string) []string {
	if a != nil {
		if value, ok := a[key]; ok {
			return strings.Fields(value)
		}
	}



	return nil
}




// Has the attribute key a value?
func (a Attributes) Has(key, value string) bool {
	return slices.Contains(a.Values(key), value)
}

// AddClass adds a value to the class attribute.
func (a Attributes) AddClass(class string) Attributes { return a.Add("class", class) }



// GetClasses returns the class values as a string slice

func (a Attributes) GetClasses() []string { return a.Values("class") }

// HasClass returns true, if attributes contains the given class.

func (a Attributes) HasClass(s string) bool { return a.Has("class", s) }

Changes to attrs/attrs_test.go.

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
func TestHasClass(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		classes string
		class   string
		exp     bool
	}{
		{"", "", true},
		{"x", "", false},
		{"x", "x", true},
		{"x", "y", false},
		{"abc def ghi", "abc", true},
		{"abc def ghi", "def", true},
		{"abc def ghi", "ghi", true},
		{"ab de gi", "b", false},







|







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
func TestHasClass(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		classes string
		class   string
		exp     bool
	}{
		{"", "", false},
		{"x", "", false},
		{"x", "x", true},
		{"x", "y", false},
		{"abc def ghi", "abc", true},
		{"abc def ghi", "def", true},
		{"abc def ghi", "ghi", true},
		{"ab de gi", "b", false},

Changes to client/client.go.

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
func (c *Client) executeRequest(req *http.Request) (*http.Response, error) {
	if c.token != "" {
		req.Header.Add("Authorization", c.tokenType+" "+c.token)
	}
	resp, err := c.client.Do(req)
	if err != nil {
		if resp != nil && resp.Body != nil {
			resp.Body.Close()
		}
		return nil, err
	}
	return resp, err
}

func (c *Client) buildAndExecuteRequest(
	ctx context.Context, method string, ub *api.URLBuilder, body io.Reader, h http.Header) (*http.Response, error) {




	req, err := c.newRequest(ctx, method, ub, body)
	if err != nil {
		return nil, err
	}
	err = c.updateToken(ctx)
	if err != nil {
		return nil, err
	}
	for key, val := range h {
		req.Header[key] = append(req.Header[key], val...)
	}
	return c.executeRequest(req)
}

// SetAuth sets authentication data.
func (c *Client) SetAuth(username, password string) {
	c.username = username
	c.password = password







|







|
>
>
>
>








<
<
<







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
func (c *Client) executeRequest(req *http.Request) (*http.Response, error) {
	if c.token != "" {
		req.Header.Add("Authorization", c.tokenType+" "+c.token)
	}
	resp, err := c.client.Do(req)
	if err != nil {
		if resp != nil && resp.Body != nil {
			_ = resp.Body.Close()
		}
		return nil, err
	}
	return resp, err
}

func (c *Client) buildAndExecuteRequest(
	ctx context.Context,
	method string,
	ub *api.URLBuilder,
	body io.Reader,
) (*http.Response, error) {
	req, err := c.newRequest(ctx, method, ub, body)
	if err != nil {
		return nil, err
	}
	err = c.updateToken(ctx)
	if err != nil {
		return nil, err
	}



	return c.executeRequest(req)
}

// SetAuth sets authentication data.
func (c *Client) SetAuth(username, password string) {
	c.username = username
	c.password = password
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	}
	return c.executeAuthRequest(req)
}

// CreateZettel creates a new zettel and returns its URL.
func (c *Client) CreateZettel(ctx context.Context, data []byte) (api.ZettelID, error) {
	ub := c.NewURLBuilder('z')
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data), nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusCreated {
		return api.InvalidZID, statusToError(resp)
	}







|







231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	}
	return c.executeAuthRequest(req)
}

// CreateZettel creates a new zettel and returns its URL.
func (c *Client) CreateZettel(ctx context.Context, data []byte) (api.ZettelID, error) {
	ub := c.NewURLBuilder('z')
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data))
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusCreated {
		return api.InvalidZID, statusToError(resp)
	}
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
// CreateZettelData creates a new zettel and returns its URL.
func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (api.ZettelID, error) {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
		return api.InvalidZID, err
	}
	ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf, nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if resp.StatusCode != http.StatusCreated {
		return api.InvalidZID, statusToError(resp)
	}
	if err != nil {
		return api.InvalidZID, err
	}
	return makeZettelID(obj)
}

var bsLF = []byte{'\n'}

// QueryZettel returns a list of all Zettel.
func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) {
	ub := c.NewURLBuilder('z').AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	switch resp.StatusCode {
	case http.StatusOK:







|




















|







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
// CreateZettelData creates a new zettel and returns its URL.
func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (api.ZettelID, error) {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
		return api.InvalidZID, err
	}
	ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if resp.StatusCode != http.StatusCreated {
		return api.InvalidZID, statusToError(resp)
	}
	if err != nil {
		return api.InvalidZID, err
	}
	return makeZettelID(obj)
}

var bsLF = []byte{'\n'}

// QueryZettel returns a list of all Zettel.
func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) {
	ub := c.NewURLBuilder('z').AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	switch resp.StatusCode {
	case http.StatusOK:
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	}
	return lines, nil
}

// QueryZettelData returns a list of zettel metadata.
func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) {
	ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData).AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return "", "", nil, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	switch resp.StatusCode {







|







303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	}
	return lines, nil
}

// QueryZettelData returns a list of zettel metadata.
func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) {
	ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData).AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return "", "", nil, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	switch resp.StatusCode {
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
}

func (c *Client) fetchTagOrRoleZettel(ctx context.Context, key, val string) (api.ZettelID, error) {
	if c.client.CheckRedirect == nil {
		panic("client does not allow to track redirect")
	}
	ub := c.NewURLBuilder('z').AppendKVQuery(key, val)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		return api.InvalidZID, err







|







449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
}

func (c *Client) fetchTagOrRoleZettel(ctx context.Context, key, val string) (api.ZettelID, error) {
	if c.client.CheckRedirect == nil {
		panic("client does not allow to track redirect")
	}
	ub := c.NewURLBuilder('z').AppendKVQuery(key, val)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		return api.InvalidZID, err
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

// GetZettel returns a zettel as a string.
func (c *Client) GetZettel(ctx context.Context, zid api.ZettelID, part string) ([]byte, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	if part != "" && part != api.PartContent {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	return data, err
}

// GetZettelData returns a zettel as a struct of its parts.
func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartZettel)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err == nil {
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			return api.ZettelData{}, statusToError(resp)
		}
		rdr := sxreader.MakeReader(resp.Body)
		obj, err2 := rdr.Read()







|




















|







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

// GetZettel returns a zettel as a string.
func (c *Client) GetZettel(ctx context.Context, zid api.ZettelID, part string) ([]byte, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	if part != "" && part != api.PartContent {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	return data, err
}

// GetZettelData returns a zettel as a struct of its parts.
func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartZettel)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err == nil {
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			return api.ZettelData{}, statusToError(resp)
		}
		rdr := sxreader.MakeReader(resp.Body)
		obj, err2 := rdr.Read()
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, enc.String())
	ub.AppendKVQuery(api.QueryKeyPart, api.PartContent)
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:







|







532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, enc.String())
	ub.AppendKVQuery(api.QueryKeyPart, api.PartContent)
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
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
	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)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8)).Read()
}

// GetMetaData returns the metadata of a zettel.
func (c *Client) GetMetaData(ctx context.Context, zid api.ZettelID) (api.MetaRights, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartMeta)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.MetaRights{}, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if resp.StatusCode != http.StatusOK {







|















|







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
	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)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8)).Read()
}

// GetMetaData returns the metadata of a zettel.
func (c *Client) GetMetaData(ctx context.Context, zid api.ZettelID) (api.MetaRights, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartMeta)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return api.MetaRights{}, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if resp.StatusCode != http.StatusOK {
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
		Rights: rights,
	}, nil
}

// UpdateZettel updates an existing zettel.
func (c *Client) UpdateZettel(ctx context.Context, zid api.ZettelID, data []byte) error {
	ub := c.NewURLBuilder('z').SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data), nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// UpdateZettelData updates an existing zettel.
func (c *Client) UpdateZettelData(ctx context.Context, zid api.ZettelID, data api.ZettelData) error {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
		return err
	}
	ub := c.NewURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// RenameZettel renames a zettel.
//
// This function is deprecated and will be removed in v0.19 (or later).
func (c *Client) RenameZettel(ctx context.Context, oldZid, newZid api.ZettelID) error {
	ub := c.NewURLBuilder('z').SetZid(oldZid)
	h := http.Header{
		api.HeaderDestination: {c.NewURLBuilder('z').SetZid(newZid).String()},
	}
	resp, err := c.buildAndExecuteRequest(ctx, api.MethodMove, ub, nil, h)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// DeleteZettel deletes a zettel with the given identifier.
func (c *Client) DeleteZettel(ctx context.Context, zid api.ZettelID) error {
	ub := c.NewURLBuilder('z').SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// ExecuteCommand will execute a given command at the Zettelstore.
func (c *Client) ExecuteCommand(ctx context.Context, command api.Command) error {
	ub := c.NewURLBuilder('x').AppendKVQuery(api.QueryKeyCommand, string(command))
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// GetVersionInfo returns version information.
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.NewURLBuilder('x'), nil, nil)
	if err != nil {
		return VersionInfo{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return VersionInfo{}, statusToError(resp)
	}







|

















|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<













|













|












|







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
		Rights: rights,
	}, nil
}

// UpdateZettel updates an existing zettel.
func (c *Client) UpdateZettel(ctx context.Context, zid api.ZettelID, data []byte) error {
	ub := c.NewURLBuilder('z').SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data))
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// UpdateZettelData updates an existing zettel.
func (c *Client) UpdateZettelData(ctx context.Context, zid api.ZettelID, data api.ZettelData) error {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
		return err
	}
	ub := c.NewURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf)



















	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// DeleteZettel deletes a zettel with the given identifier.
func (c *Client) DeleteZettel(ctx context.Context, zid api.ZettelID) error {
	ub := c.NewURLBuilder('z').SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// ExecuteCommand will execute a given command at the Zettelstore.
func (c *Client) ExecuteCommand(ctx context.Context, command api.Command) error {
	ub := c.NewURLBuilder('x').AppendKVQuery(api.QueryKeyCommand, string(command))
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

// GetVersionInfo returns version information.
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.NewURLBuilder('x'), nil)
	if err != nil {
		return VersionInfo{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return VersionInfo{}, statusToError(resp)
	}
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
		return zid, nil
	}
	return api.InvalidZID, fmt.Errorf("invalid identifier for application %v: %v", appname, val)
}

// Get executes a GET request to the given URL and returns the read data.
func (c *Client) Get(ctx context.Context, ub *api.URLBuilder) ([]byte, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:







|







733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
		return zid, nil
	}
	return api.InvalidZID, fmt.Errorf("invalid identifier for application %v: %v", appname, val)
}

// Get executes a GET request to the given URL and returns the read data.
func (c *Client) Get(ctx context.Context, ub *api.URLBuilder) ([]byte, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:

Changes to go.mod.

1
2
3
4
5
6
7
8
9
module t73f.de/r/zsc

go 1.22

require (
	t73f.de/r/sx v0.0.0-20240513163553-ec4fcc6539ca
	t73f.de/r/sxwebs v0.0.0-20240613142113-66fc5a284245
	t73f.de/r/webs v0.0.0-20240617100047-8730e9917915
)


|


|
|
|

1
2
3
4
5
6
7
8
9
module t73f.de/r/zsc

go 1.23

require (
	t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5
	t73f.de/r/sxwebs v0.0.0-20240814085618-5b4b5c496c94
	t73f.de/r/webs v0.0.0-20240814085020-19dac746d568
)

Changes to go.sum.

1
2
3
4
5
6
t73f.de/r/sx v0.0.0-20240513163553-ec4fcc6539ca h1:vvDqiuUfBLf+t/gpiSyqIFAdvZ7FLigOH38bqMY+v8k=
t73f.de/r/sx v0.0.0-20240513163553-ec4fcc6539ca/go.mod h1:G9pD1j2R6y9ZkPBb81mSnmwaAvTOg7r6jKp/OF7WeFA=
t73f.de/r/sxwebs v0.0.0-20240613142113-66fc5a284245 h1:raE7KUgoGsp2DzXOko9dDXEsSJ/VvoXCDYeICx7i6uo=
t73f.de/r/sxwebs v0.0.0-20240613142113-66fc5a284245/go.mod h1:ErPBVUyE2fOktL/8M7lp/PR93wP/o9RawMajB1uSqj8=
t73f.de/r/webs v0.0.0-20240617100047-8730e9917915 h1:rwUaPBIH3shrUIkmw51f4RyCplsCU+ISZHailsLiHTE=
t73f.de/r/webs v0.0.0-20240617100047-8730e9917915/go.mod h1:UGAAtul0TK5ACeZ6zTS3SX6GqwMFXxlUpHiV8oqNq5w=
|
|
|
|
|
|
1
2
3
4
5
6
t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5 h1:ug4hohM6pK28M8Uo0o3+XvjBure2wfEtuCnHVIdqBZY=
t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5/go.mod h1:VRvsWoBErPKvMieDMMk1hsh1tb9sA4ijEQWGw/TbtQ0=
t73f.de/r/sxwebs v0.0.0-20240814085618-5b4b5c496c94 h1:gLneaEyYotvcY/dDznzdcSXK1RqsJVi2AfeYDc1iVwM=
t73f.de/r/sxwebs v0.0.0-20240814085618-5b4b5c496c94/go.mod h1:83W3QFkmrniIKv6R+Xq+imvbSolhoutTnNhW0ErJoco=
t73f.de/r/webs v0.0.0-20240814085020-19dac746d568 h1:Pa+vO2r++qhcShv0p7t/gIrJ1DHPMn4gopEXLxDmoRg=
t73f.de/r/webs v0.0.0-20240814085020-19dac746d568/go.mod h1:NSoOON8be62MfQZzlCApK27Jt2zhIa6Vrmo9RJ4tOnQ=

Changes to input/input.go.

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
		inp.Next()
		return true
	}
	return false
}

// IsEOLEOS returns true if char is either EOS or EOL.
func IsEOLEOS(ch rune) bool {
	switch ch {
	case EOS, '\n', '\r':
		return true
	}
	return false
}

// EatEOL transforms both "\r" and "\r\n" into "\n".
func (inp *Input) EatEOL() {
	switch inp.Ch {
	case '\r':
		if inp.Peek() == '\n' {
			inp.Next()







|
<
<
<
<
<
<







93
94
95
96
97
98
99
100






101
102
103
104
105
106
107
		inp.Next()
		return true
	}
	return false
}

// IsEOLEOS returns true if char is either EOS or EOL.
func IsEOLEOS(ch rune) bool { return ch == EOS || ch == '\n' || ch == '\r' }







// EatEOL transforms both "\r" and "\r\n" into "\n".
func (inp *Input) EatEOL() {
	switch inp.Ch {
	case '\r':
		if inp.Peek() == '\n' {
			inp.Next()
122
123
124
125
126
127
128







129
130
131
132
133
134
135
// SetPos allows to reset the read position.
func (inp *Input) SetPos(pos int) {
	if inp.Pos != pos {
		inp.readPos = pos
		inp.Next()
	}
}








// SkipToEOL reads until the next end-of-line.
func (inp *Input) SkipToEOL() {
	for {
		switch inp.Ch {
		case EOS, '\n', '\r':
			return







>
>
>
>
>
>
>







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SetPos allows to reset the read position.
func (inp *Input) SetPos(pos int) {
	if inp.Pos != pos {
		inp.readPos = pos
		inp.Next()
	}
}

// SkipSpace reads while the current character is not a space character.
func (inp *Input) SkipSpace() {
	for ch := inp.Ch; IsSpace(ch); {
		ch = inp.Next()
	}
}

// SkipToEOL reads until the next end-of-line.
func (inp *Input) SkipToEOL() {
	for {
		switch inp.Ch {
		case EOS, '\n', '\r':
			return

Changes to input/runes.go.

21
22
23
24
25
26
27



	case ' ', '\t':
		return true
	case '\n', '\r', EOS:
		return false
	}
	return unicode.IsSpace(ch)
}










>
>
>
21
22
23
24
25
26
27
28
29
30
	case ' ', '\t':
		return true
	case '\n', '\r', EOS:
		return false
	}
	return unicode.IsSpace(ch)
}

// IsSpace returns true if current character is a whitespace.
func (inp *Input) IsSpace() bool { return IsSpace(inp.Ch) }

Changes to shtml/const.go.

57
58
59
60
61
62
63

64
65
66
67
68
69
70
	SymSTRONG     = sx.MakeSymbol("strong")
	symSUB        = sx.MakeSymbol("sub")
	symSUP        = sx.MakeSymbol("sup")
	symTABLE      = sx.MakeSymbol("table")
	symTBODY      = sx.MakeSymbol("tbody")
	symTHEAD      = sx.MakeSymbol("thead")
	symTD         = sx.MakeSymbol("td")

	symTR         = sx.MakeSymbol("tr")
	SymUL         = sx.MakeSymbol("ul")
)

// Symbols for HTML attribute keys
var (
	symAttrAlt    = sx.MakeSymbol("alt")







>







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	SymSTRONG     = sx.MakeSymbol("strong")
	symSUB        = sx.MakeSymbol("sub")
	symSUP        = sx.MakeSymbol("sup")
	symTABLE      = sx.MakeSymbol("table")
	symTBODY      = sx.MakeSymbol("tbody")
	symTHEAD      = sx.MakeSymbol("thead")
	symTD         = sx.MakeSymbol("td")
	symTH         = sx.MakeSymbol("th")
	symTR         = sx.MakeSymbol("tr")
	SymUL         = sx.MakeSymbol("ul")
)

// Symbols for HTML attribute keys
var (
	symAttrAlt    = sx.MakeSymbol("alt")

Added shtml/lang.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
//-----------------------------------------------------------------------------
// 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2023-present Detlef Stern
//-----------------------------------------------------------------------------

package shtml

import (
	"strings"

	"t73f.de/r/zsc/api"
)

// LangStack is a stack to store the nesting of "lang" attribute values.
// It is used to generate typographically correct quotes.
type LangStack []string

// NewLangStack creates a new language stack.
func NewLangStack(lang string) LangStack {
	ls := make([]string, 1, 16)
	ls[0] = lang
	return ls
}

// Reset restores the language stack to its initial value.
func (ls *LangStack) Reset() {
	*ls = (*ls)[0:1]
}

// Push adds a new language value.
func (ls *LangStack) Push(lang string) {
	*ls = append(*ls, lang)
}

// Pop removes the topmost language value.
func (ls *LangStack) Pop() {
	*ls = (*ls)[0 : len(*ls)-1]
}

// Top returns the topmost language value.
func (ls *LangStack) Top() string {
	return (*ls)[len(*ls)-1]
}

// Dup duplicates the topmost language value.
func (ls *LangStack) Dup() {
	*ls = append(*ls, (*ls)[len(*ls)-1])
}

// QuoteInfo contains language specific data about quotes.
type QuoteInfo struct {
	primLeft, primRight string
	secLeft, secRight   string
	nbsp                bool
}

// GetPrimary returns the primary left and right quote entity.
func (qi *QuoteInfo) GetPrimary() (string, string) {
	return qi.primLeft, qi.primRight
}

// GetSecondary returns the secondary left and right quote entity.
func (qi *QuoteInfo) GetSecondary() (string, string) {
	return qi.secLeft, qi.secRight
}

// GetQuotes returns quotes based on a nesting level.
func (qi *QuoteInfo) GetQuotes(level uint) (string, string) {
	if level%2 == 0 {
		return qi.GetPrimary()
	}
	return qi.GetSecondary()
}

// GetNBSp returns true, if there must be a non-breaking space between the
// quote entities and the quoted text.
func (qi *QuoteInfo) GetNBSp() bool { return qi.nbsp }

var langQuotes = map[string]*QuoteInfo{
	"":              {"&quot;", "&quot;", "&quot;", "&quot;", false},
	api.ValueLangEN: {"&ldquo;", "&rdquo;", "&lsquo;", "&rsquo;", false},
	"de":            {"&bdquo;", "&ldquo;", "&sbquo;", "&lsquo;", false},
	"fr":            {"&laquo;", "&raquo;", "&lsaquo;", "&rsaquo;", true},
}

// GetQuoteInfo returns language specific data about quotes.
func GetQuoteInfo(lang string) *QuoteInfo {
	langFields := strings.FieldsFunc(lang, func(r rune) bool { return r == '-' || r == '_' })
	for len(langFields) > 0 {
		langSup := strings.Join(langFields, "-")
		quotes, ok := langQuotes[langSup]
		if ok {
			return quotes
		}
		langFields = langFields[0 : len(langFields)-1]
	}
	return langQuotes[""]
}

Changes to shtml/shtml.go.

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
	ev.bindMetadata()
	ev.bindBlocks()
	ev.bindInlines()
	return ev
}

// SetUnique sets a prefix to make several HTML ids unique.
func (tr *Evaluator) SetUnique(s string) { tr.unique = s }

// IsValidName returns true, if name is a valid symbol name.
func (tr *Evaluator) IsValidName(s string) bool { return s != "" }

// EvaluateAttrbute transforms the given attributes into a HTML s-expression.
func (tr *Evaluator) EvaluateAttrbute(a attrs.Attributes) *sx.Pair {
	if len(a) == 0 {
		return nil
	}
	plist := sx.Nil()
	keys := a.Keys()
	for i := len(keys) - 1; i >= 0; i-- {
		key := keys[i]
		if key != attrs.DefaultAttribute && tr.IsValidName(key) {
			plist = plist.Cons(sx.Cons(sx.MakeSymbol(key), sx.MakeString(a[key])))
		}
	}
	if plist == nil {
		return nil
	}
	return plist.Cons(sxhtml.SymAttr)







|


|


|







|







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
	ev.bindMetadata()
	ev.bindBlocks()
	ev.bindInlines()
	return ev
}

// SetUnique sets a prefix to make several HTML ids unique.
func (ev *Evaluator) SetUnique(s string) { ev.unique = s }

// IsValidName returns true, if name is a valid symbol name.
func isValidName(s string) bool { return s != "" }

// EvaluateAttrbute transforms the given attributes into a HTML s-expression.
func EvaluateAttrbute(a attrs.Attributes) *sx.Pair {
	if len(a) == 0 {
		return nil
	}
	plist := sx.Nil()
	keys := a.Keys()
	for i := len(keys) - 1; i >= 0; i-- {
		key := keys[i]
		if key != attrs.DefaultAttribute && isValidName(key) {
			plist = plist.Cons(sx.Cons(sx.MakeSymbol(key), sx.MakeString(a[key])))
		}
	}
	if plist == nil {
		return nil
	}
	return plist.Cons(sxhtml.SymAttr)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	if err := env.err; err != nil {
		return nil, err
	}
	return result.List(), nil
}

// Endnotes returns a SHTML object with all collected endnotes.
func (ev *Evaluator) Endnotes(env *Environment) *sx.Pair {
	if env.err != nil || len(env.endnotes) == 0 {
		return nil
	}

	var result sx.ListBuilder
	result.Add(SymOL)
	result.Add(sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))).Cons(sxhtml.SymAttr))







|







113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	if err := env.err; err != nil {
		return nil, err
	}
	return result.List(), nil
}

// Endnotes returns a SHTML object with all collected endnotes.
func Endnotes(env *Environment) *sx.Pair {
	if env.err != nil || len(env.endnotes) == 0 {
		return nil
	}

	var result sx.ListBuilder
	result.Add(SymOL)
	result.Add(sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))).Cons(sxhtml.SymAttr))
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
	}
	return result.List()
}

// Environment where sz objects are evaluated to shtml objects
type Environment struct {
	err          error
	langStack    []string
	endnotes     []endnoteInfo
	quoteNesting uint
}
type endnoteInfo struct {
	noteID  string    // link id
	noteAST sx.Vector // Endnote as list of AST inline elements
	attrs   *sx.Pair  // attrs a-list
	noteHx  *sx.Pair  // Endnote as SxHTML
}

// MakeEnvironment builds a new evaluation environment.
func MakeEnvironment(lang string) Environment {
	langStack := make([]string, 1, 16)
	langStack[0] = lang
	return Environment{
		err:          nil,
		langStack:    langStack,
		endnotes:     nil,
		quoteNesting: 0,
	}
}

// GetError returns the last error found.
func (env *Environment) GetError() error { return env.err }

// Reset the environment.
func (env *Environment) Reset() {
	env.langStack = env.langStack[0:1]
	env.endnotes = nil
	env.quoteNesting = 0
}

// PushAttribute adds the current attributes to the environment.
func (env *Environment) pushAttributes(a attrs.Attributes) {
	if value, ok := a.Get("lang"); ok {
		env.langStack = append(env.langStack, value)
	} else {
		env.langStack = append(env.langStack, env.getLanguage())
	}
}

// popAttributes removes the current attributes from the envrionment
func (env *Environment) popAttributes() {
	env.langStack = env.langStack[0 : len(env.langStack)-1]
}

// getLanguage returns the current language
func (env *Environment) getLanguage() string {
	return env.langStack[len(env.langStack)-1]




}

// EvalFn is a function to be called for evaluation.
type EvalFn func(sx.Vector, *Environment) sx.Object

func (ev *Evaluator) bind(sym *sx.Symbol, minArgs int, fn EvalFn) {
	symVal := sym.GetValue()







|












<
<


|










|




|


|

|



|
|
<
|
<
|
|
|
>
>
>
>







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
	}
	return result.List()
}

// Environment where sz objects are evaluated to shtml objects
type Environment struct {
	err          error
	langStack    LangStack
	endnotes     []endnoteInfo
	quoteNesting uint
}
type endnoteInfo struct {
	noteID  string    // link id
	noteAST sx.Vector // Endnote as list of AST inline elements
	attrs   *sx.Pair  // attrs a-list
	noteHx  *sx.Pair  // Endnote as SxHTML
}

// MakeEnvironment builds a new evaluation environment.
func MakeEnvironment(lang string) Environment {


	return Environment{
		err:          nil,
		langStack:    NewLangStack(lang),
		endnotes:     nil,
		quoteNesting: 0,
	}
}

// GetError returns the last error found.
func (env *Environment) GetError() error { return env.err }

// Reset the environment.
func (env *Environment) Reset() {
	env.langStack.Reset()
	env.endnotes = nil
	env.quoteNesting = 0
}

// pushAttribute adds the current attributes to the environment.
func (env *Environment) pushAttributes(a attrs.Attributes) {
	if value, ok := a.Get("lang"); ok {
		env.langStack.Push(value)
	} else {
		env.langStack.Dup()
	}
}

// popAttributes removes the current attributes from the envrionment.
func (env *Environment) popAttributes() { env.langStack.Pop() }



// getLanguage returns the current language.
func (env *Environment) getLanguage() string { return env.langStack.Top() }

func (env *Environment) getQuotes() (string, string, bool) {
	qi := GetQuoteInfo(env.getLanguage())
	leftQ, rightQ := qi.GetQuotes(env.quoteNesting)
	return leftQ, rightQ, qi.GetNBSp()
}

// EvalFn is a function to be called for evaluation.
type EvalFn func(sx.Vector, *Environment) sx.Object

func (ev *Evaluator) bind(sym *sx.Symbol, minArgs int, fn EvalFn) {
	symVal := sym.GetValue()
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	ev.fns[symVal] = fn
}

func (ev *Evaluator) bindMetadata() {
	ev.bind(sz.SymMeta, 0, ev.evalList)
	evalMetaString := func(args sx.Vector, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(args[0], env).GetValue()).
			Set("content", getString(args[1], env).GetValue())
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.SymTypeCredential, 2, evalMetaString)
	ev.bind(sz.SymTypeEmpty, 2, evalMetaString)
	ev.bind(sz.SymTypeID, 2, evalMetaString)
	ev.bind(sz.SymTypeNumber, 2, evalMetaString)







|







235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	ev.fns[symVal] = fn
}

func (ev *Evaluator) bindMetadata() {
	ev.bind(sz.SymMeta, 0, ev.evalList)
	evalMetaString := func(args sx.Vector, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", getSymbol(args[0], env).GetValue()).
			Set("content", getString(args[1], env).GetValue())
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.SymTypeCredential, 2, evalMetaString)
	ev.bind(sz.SymTypeEmpty, 2, evalMetaString)
	ev.bind(sz.SymTypeID, 2, evalMetaString)
	ev.bind(sz.SymTypeNumber, 2, evalMetaString)
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
			sb.WriteString(getString(elem.Car(), env).GetValue())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(args[0], env).GetValue()).
			Set("content", s)
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.SymTypeIDSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeTagSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeZettelmarkup, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(args[0], env).GetValue()).
			Set("content", text.EvaluateInlineString(getList(args[1], env)))
		return ev.EvaluateMeta(a)
	})
}

// EvaluateMeta returns HTML meta object for an attribute.
func (ev *Evaluator) EvaluateMeta(a attrs.Attributes) *sx.Pair {
	return sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(SymMeta)
}

func (ev *Evaluator) bindBlocks() {
	ev.bind(sz.SymBlock, 0, ev.evalList)
	ev.bind(sz.SymPara, 0, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalSlice(args, env).Cons(SymP)
	})
	ev.bind(sz.SymHeading, 5, func(args sx.Vector, env *Environment) sx.Object {
		nLevel := getInt64(args[0], env)
		if nLevel <= 0 {
			env.err = fmt.Errorf("%v is a negative level", nLevel)
			return sx.Nil()
		}
		level := strconv.FormatInt(nLevel+ev.headingOffset, 10)
		headingSymbol := sx.MakeSymbol("h" + level)

		a := ev.GetAttributes(args[1], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if fragment := getString(args[3], env).GetValue(); fragment != "" {
			a = a.Set("id", ev.unique+fragment)
		}

		if result, _ := ev.EvaluateList(args[4:], env); result != nil {
			if len(a) > 0 {
				result = result.Cons(ev.EvaluateAttrbute(a))
			}
			return result.Cons(headingSymbol)
		}
		return sx.MakeList(headingSymbol, sx.MakeString("<MISSING TEXT>"))
	})
	ev.bind(sz.SymThematic, 0, func(args sx.Vector, env *Environment) sx.Object {
		result := sx.Nil()
		if len(args) > 0 {
			if attrList := getList(args[0], env); attrList != nil {
				result = result.Cons(ev.EvaluateAttrbute(sz.GetAttributes(attrList)))
			}
		}
		return result.Cons(SymHR)
	})

	ev.bind(sz.SymListOrdered, 0, ev.makeListFn(SymOL))
	ev.bind(sz.SymListUnordered, 0, ev.makeListFn(SymUL))







|







|







|










|





|








|









|







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
			sb.WriteString(getString(elem.Car(), env).GetValue())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", getSymbol(args[0], env).GetValue()).
			Set("content", s)
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.SymTypeIDSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeTagSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeZettelmarkup, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", getSymbol(args[0], env).GetValue()).
			Set("content", text.EvaluateInlineString(getList(args[1], env)))
		return ev.EvaluateMeta(a)
	})
}

// EvaluateMeta returns HTML meta object for an attribute.
func (ev *Evaluator) EvaluateMeta(a attrs.Attributes) *sx.Pair {
	return sx.Nil().Cons(EvaluateAttrbute(a)).Cons(SymMeta)
}

func (ev *Evaluator) bindBlocks() {
	ev.bind(sz.SymBlock, 0, ev.evalList)
	ev.bind(sz.SymPara, 0, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalSlice(args, env).Cons(SymP)
	})
	ev.bind(sz.SymHeading, 5, func(args sx.Vector, env *Environment) sx.Object {
		nLevel := getInt64(args[0], env)
		if nLevel <= 0 {
			env.err = fmt.Errorf("%v is a negative heading level", nLevel)
			return sx.Nil()
		}
		level := strconv.FormatInt(nLevel+ev.headingOffset, 10)
		headingSymbol := sx.MakeSymbol("h" + level)

		a := GetAttributes(args[1], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if fragment := getString(args[3], env).GetValue(); fragment != "" {
			a = a.Set("id", ev.unique+fragment)
		}

		if result, _ := ev.EvaluateList(args[4:], env); result != nil {
			if len(a) > 0 {
				result = result.Cons(EvaluateAttrbute(a))
			}
			return result.Cons(headingSymbol)
		}
		return sx.MakeList(headingSymbol, sx.MakeString("<MISSING TEXT>"))
	})
	ev.bind(sz.SymThematic, 0, func(args sx.Vector, env *Environment) sx.Object {
		result := sx.Nil()
		if len(args) > 0 {
			if attrList := getList(args[0], env); attrList != nil {
				result = result.Cons(EvaluateAttrbute(sz.GetAttributes(attrList)))
			}
		}
		return result.Cons(SymHR)
	})

	ev.bind(sz.SymListOrdered, 0, ev.makeListFn(SymOL))
	ev.bind(sz.SymListUnordered, 0, ev.makeListFn(SymUL))
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
		}
		return result.List()
	})

	ev.bind(sz.SymTable, 1, func(args sx.Vector, env *Environment) sx.Object {
		thead := sx.Nil()
		if header := getList(args[0], env); !sx.IsNil(header) {
			thead = sx.Nil().Cons(ev.evalTableRow(header, env)).Cons(symTHEAD)
		}

		var tbody sx.ListBuilder
		if len(args) > 1 {
			tbody.Add(symTBODY)
			for _, row := range args[1:] {
				tbody.Add(ev.evalTableRow(getList(row, env), env))
			}
		}

		table := sx.Nil()
		if !tbody.IsEmpty() {
			table = table.Cons(tbody.List())
		}







|






|







360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
		}
		return result.List()
	})

	ev.bind(sz.SymTable, 1, func(args sx.Vector, env *Environment) sx.Object {
		thead := sx.Nil()
		if header := getList(args[0], env); !sx.IsNil(header) {
			thead = sx.Nil().Cons(ev.evalTableRow(symTH, header, env)).Cons(symTHEAD)
		}

		var tbody sx.ListBuilder
		if len(args) > 1 {
			tbody.Add(symTBODY)
			for _, row := range args[1:] {
				tbody.Add(ev.evalTableRow(symTD, getList(row, env), env))
			}
		}

		table := sx.Nil()
		if !tbody.IsEmpty() {
			table = table.Cons(tbody.List())
		}
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
	ev.bind(sz.SymCellRight, 0, ev.makeCellFn("right"))

	ev.bind(sz.SymRegionBlock, 2, ev.makeRegionFn(SymDIV, true))
	ev.bind(sz.SymRegionQuote, 2, ev.makeRegionFn(symBLOCKQUOTE, false))
	ev.bind(sz.SymRegionVerse, 2, ev.makeRegionFn(SymDIV, false))

	ev.bind(sz.SymVerbatimComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if ev.GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(args[1], env); s.GetValue() != "" {
					return sx.Nil().Cons(s).Cons(sxhtml.SymBlockComment)
				}
			}
		}
		return nil
	})
	ev.bind(sz.SymVerbatimEval, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.GetAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimHTML, 2, ev.evalHTML)
	ev.bind(sz.SymVerbatimMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.GetAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		content := getString(args[1], env)
		if a.HasDefault() {
			content = sx.MakeString(visibleReplacer.Replace(content.GetValue()))
		}
		return ev.evalVerbatim(a, content)
	})
	ev.bind(sz.SymVerbatimZettel, 0, nilFn)
	ev.bind(sz.SymBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env))
	})
	ev.bind(sz.SymTransclude, 2, func(args sx.Vector, env *Environment) sx.Object {
		ref, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.Nil()
		}
		refKind := ref.Car()
		if sx.IsNil(refKind) {
			return sx.Nil()
		}
		if refValue := getString(ref.Tail().Car(), env); refValue.GetValue() != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.IsEqual(sz.SymRefStateExternal) {
				a := ev.GetAttributes(args[0], env).Set("src", refValue.GetValue()).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(SymIMG)).Cons(SymP)
			}
			return sx.MakeList(
				sxhtml.SymInlineComment,
				sx.MakeString("transclude"),
				refKind,
				sx.MakeString("->"),
				refValue,







|









|



|


|




|



|











|
|
|







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
	ev.bind(sz.SymCellRight, 0, ev.makeCellFn("right"))

	ev.bind(sz.SymRegionBlock, 2, ev.makeRegionFn(SymDIV, true))
	ev.bind(sz.SymRegionQuote, 2, ev.makeRegionFn(symBLOCKQUOTE, false))
	ev.bind(sz.SymRegionVerse, 2, ev.makeRegionFn(SymDIV, false))

	ev.bind(sz.SymVerbatimComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(args[1], env); s.GetValue() != "" {
					return sx.Nil().Cons(s).Cons(sxhtml.SymBlockComment)
				}
			}
		}
		return nil
	})
	ev.bind(sz.SymVerbatimEval, 2, func(args sx.Vector, env *Environment) sx.Object {
		return evalVerbatim(GetAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimHTML, 2, ev.evalHTML)
	ev.bind(sz.SymVerbatimMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		return evalVerbatim(GetAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		content := getString(args[1], env)
		if a.HasDefault() {
			content = sx.MakeString(visibleReplacer.Replace(content.GetValue()))
		}
		return evalVerbatim(a, content)
	})
	ev.bind(sz.SymVerbatimZettel, 0, nilFn)
	ev.bind(sz.SymBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {
		return evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env))
	})
	ev.bind(sz.SymTransclude, 2, func(args sx.Vector, env *Environment) sx.Object {
		ref, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.Nil()
		}
		refKind := ref.Car()
		if sx.IsNil(refKind) {
			return sx.Nil()
		}
		if refValue := getString(ref.Tail().Car(), env); refValue.GetValue() != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.IsEqualSymbol(sz.SymRefStateExternal) {
				a := GetAttributes(args[0], env).Set("src", refValue.GetValue()).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(EvaluateAttrbute(a)).Cons(SymIMG)).Cons(SymP)
			}
			return sx.MakeList(
				sxhtml.SymInlineComment,
				sx.MakeString("transclude"),
				refKind,
				sx.MakeString("->"),
				refValue,
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
	for node := term; node != nil; node = node.Tail() {
		elem := ev.Eval(node.Car(), env)
		result.Add(elem)
	}
	return result.List()
}

func (ev *Evaluator) evalTableRow(pairs *sx.Pair, env *Environment) *sx.Pair {
	if pairs == nil {
		return nil
	}
	var row sx.ListBuilder
	row.Add(symTR)
	for pair := pairs; pair != nil; pair = pair.Tail() {
		row.Add(ev.Eval(pair.Car(), env))
	}
	return row.List()
}
func (ev *Evaluator) makeCellFn(align string) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		tdata := ev.evalSlice(args, env)
		if align != "" {
			tdata = tdata.Cons(ev.EvaluateAttrbute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(symTD)
	}
}

func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		var result sx.ListBuilder
		result.Add(sym)
		if len(a) > 0 {
			result.Add(ev.EvaluateAttrbute(a))
		}
		if region, isPair := sx.GetPair(args[1]); isPair {
			if evalRegion := ev.EvalPairList(region, env); evalRegion != nil {
				result.ExtendBang(evalRegion)
			}
		}
		if len(args) > 2 {
			if cite, _ := ev.EvaluateList(args[2:], env); cite != nil {
				result.Add(cite.Cons(symCITE))
			}
		}
		return result.List()
	}
}

func (ev *Evaluator) evalVerbatim(a attrs.Attributes, s sx.String) sx.Object {
	a = setProgLang(a)
	code := sx.Nil().Cons(s)
	if al := ev.EvaluateAttrbute(a); al != nil {
		code = code.Cons(al)
	}
	code = code.Cons(symCODE)
	return sx.Nil().Cons(code).Cons(symPRE)
}

func (ev *Evaluator) bindInlines() {
	ev.bind(sz.SymInline, 0, ev.evalList)
	ev.bind(sz.SymText, 1, func(args sx.Vector, env *Environment) sx.Object { return getString(args[0], env) })
	ev.bind(sz.SymSoft, 0, func(sx.Vector, *Environment) sx.Object { return sx.MakeString(" ") })
	ev.bind(sz.SymHard, 0, func(sx.Vector, *Environment) sx.Object { return sx.Nil().Cons(symBR) })

	ev.bind(sz.SymLinkInvalid, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		var inline *sx.Pair
		if len(args) > 2 {
			inline = ev.evalSlice(args[2:], env)
		}
		if inline == nil {
			inline = sx.Nil().Cons(ev.Eval(args[1], env))
		}
		return inline.Cons(SymSPAN)
	})
	evalHREF := func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.GetValue()), refValue, args[2:], env)
	}
	ev.bind(sz.SymLinkZettel, 2, evalHREF)
	ev.bind(sz.SymLinkSelf, 2, evalHREF)
	ev.bind(sz.SymLinkFound, 2, evalHREF)
	ev.bind(sz.SymLinkBroken, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.AddClass("broken"), refValue, args[2:], env)
	})
	ev.bind(sz.SymLinkHosted, 2, evalHREF)
	ev.bind(sz.SymLinkBased, 2, evalHREF)
	ev.bind(sz.SymLinkQuery, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.GetValue())
		return ev.evalLink(a.Set("href", query), refValue, args[2:], env)
	})
	ev.bind(sz.SymLinkExternal, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.GetValue()).AddClass("external"), refValue, args[2:], env)
	})

	ev.bind(sz.SymEmbed, 3, func(args sx.Vector, env *Environment) sx.Object {
		ref := getList(args[1], env)
		syntax := getString(args[2], env).GetValue()
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				sxhtml.SymAttr,
				sx.Cons(SymAttrType, sx.MakeString("image/svg+xml")),
				sx.Cons(SymAttrSrc, sx.MakeString("/"+getString(ref.Tail(), env).GetValue()+".svg")),
			)
			return sx.MakeList(
				SymFIGURE,
				sx.MakeList(
					SymEMBED,
					embedAttr,
				),
			)
		}
		a := ev.GetAttributes(args[0], env)
		a = a.Set("src", getString(ref.Tail().Car(), env).GetValue())
		if len(args) > 3 {
			var sb strings.Builder
			flattenText(&sb, sx.MakeList(args[3:]...))
			if d := sb.String(); d != "" {
				a = a.Set("alt", d)
			}
		}
		return sx.MakeList(SymIMG, ev.EvaluateAttrbute(a))
	})
	ev.bind(sz.SymEmbedBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {
		a, syntax, data := ev.GetAttributes(args[0], env), getString(args[1], env), getString(args[2], env)
		summary, hasSummary := a.Get(api.KeySummary)
		if !hasSummary {
			summary = ""
		}
		return ev.evalBLOB(
			sx.MakeList(sxhtml.SymListSplice, sx.MakeString(summary)),
			syntax,
			data,
		)
	})

	ev.bind(sz.SymCite, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		result := sx.Nil()
		if key := getString(args[1], env); key.GetValue() != "" {
			if len(args) > 2 {
				result = ev.evalSlice(args[2:], env).Cons(sx.MakeString(", "))
			}
			result = result.Cons(key)
		}
		if len(a) > 0 {
			result = result.Cons(ev.EvaluateAttrbute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(SymSPAN)
	})
	ev.bind(sz.SymMark, 3, func(args sx.Vector, env *Environment) sx.Object {
		result := ev.evalSlice(args[3:], env)
		if !ev.noLinks {
			if fragment := getString(args[2], env).GetValue(); fragment != "" {
				a := attrs.Attributes{"id": fragment + ev.unique}
				return result.Cons(ev.EvaluateAttrbute(a)).Cons(SymA)
			}
		}
		return result.Cons(SymSPAN)
	})
	ev.bind(sz.SymEndnote, 1, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		attrPlist := sx.Nil()
		if len(a) > 0 {
			if attrs := ev.EvaluateAttrbute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		noteNum := strconv.Itoa(len(env.endnotes) + 1)
		noteID := ev.unique + noteNum
		env.endnotes = append(env.endnotes, endnoteInfo{







|






|







|

|





|










|















|


|













|












|









|








|







|



|




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|








|


|




|







|










|











|





|




|







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
	for node := term; node != nil; node = node.Tail() {
		elem := ev.Eval(node.Car(), env)
		result.Add(elem)
	}
	return result.List()
}

func (ev *Evaluator) evalTableRow(sym *sx.Symbol, pairs *sx.Pair, env *Environment) *sx.Pair {
	if pairs == nil {
		return nil
	}
	var row sx.ListBuilder
	row.Add(symTR)
	for pair := pairs; pair != nil; pair = pair.Tail() {
		row.Add(sx.Cons(sym, ev.Eval(pair.Car(), env)))
	}
	return row.List()
}
func (ev *Evaluator) makeCellFn(align string) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		tdata := ev.evalSlice(args, env)
		if align != "" {
			tdata = tdata.Cons(EvaluateAttrbute(attrs.Attributes{"class": align}))
		}
		return tdata
	}
}

func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		var result sx.ListBuilder
		result.Add(sym)
		if len(a) > 0 {
			result.Add(EvaluateAttrbute(a))
		}
		if region, isPair := sx.GetPair(args[1]); isPair {
			if evalRegion := ev.EvalPairList(region, env); evalRegion != nil {
				result.ExtendBang(evalRegion)
			}
		}
		if len(args) > 2 {
			if cite, _ := ev.EvaluateList(args[2:], env); cite != nil {
				result.Add(cite.Cons(symCITE))
			}
		}
		return result.List()
	}
}

func evalVerbatim(a attrs.Attributes, s sx.String) sx.Object {
	a = setProgLang(a)
	code := sx.Nil().Cons(s)
	if al := EvaluateAttrbute(a); al != nil {
		code = code.Cons(al)
	}
	code = code.Cons(symCODE)
	return sx.Nil().Cons(code).Cons(symPRE)
}

func (ev *Evaluator) bindInlines() {
	ev.bind(sz.SymInline, 0, ev.evalList)
	ev.bind(sz.SymText, 1, func(args sx.Vector, env *Environment) sx.Object { return getString(args[0], env) })
	ev.bind(sz.SymSoft, 0, func(sx.Vector, *Environment) sx.Object { return sx.MakeString(" ") })
	ev.bind(sz.SymHard, 0, func(sx.Vector, *Environment) sx.Object { return sx.Nil().Cons(symBR) })

	ev.bind(sz.SymLinkInvalid, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		var inline *sx.Pair
		if len(args) > 2 {
			inline = ev.evalSlice(args[2:], env)
		}
		if inline == nil {
			inline = sx.Nil().Cons(ev.Eval(args[1], env))
		}
		return inline.Cons(SymSPAN)
	})
	evalHREF := func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.GetValue()), refValue, args[2:], env)
	}
	ev.bind(sz.SymLinkZettel, 2, evalHREF)
	ev.bind(sz.SymLinkSelf, 2, evalHREF)
	ev.bind(sz.SymLinkFound, 2, evalHREF)
	ev.bind(sz.SymLinkBroken, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.AddClass("broken"), refValue, args[2:], env)
	})
	ev.bind(sz.SymLinkHosted, 2, evalHREF)
	ev.bind(sz.SymLinkBased, 2, evalHREF)
	ev.bind(sz.SymLinkQuery, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.GetValue())
		return ev.evalLink(a.Set("href", query), refValue, args[2:], env)
	})
	ev.bind(sz.SymLinkExternal, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.GetValue()).Add("rel", "external"), refValue, args[2:], env)
	})

	ev.bind(sz.SymEmbed, 3, func(args sx.Vector, env *Environment) sx.Object {
		ref := getList(args[1], env)















		a := GetAttributes(args[0], env)
		a = a.Set("src", getString(ref.Tail().Car(), env).GetValue())
		if len(args) > 3 {
			var sb strings.Builder
			flattenText(&sb, sx.MakeList(args[3:]...))
			if d := sb.String(); d != "" {
				a = a.Set("alt", d)
			}
		}
		return sx.MakeList(SymIMG, EvaluateAttrbute(a))
	})
	ev.bind(sz.SymEmbedBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {
		a, syntax, data := GetAttributes(args[0], env), getString(args[1], env), getString(args[2], env)
		summary, hasSummary := a.Get(api.KeySummary)
		if !hasSummary {
			summary = ""
		}
		return evalBLOB(
			sx.MakeList(sxhtml.SymListSplice, sx.MakeString(summary)),
			syntax,
			data,
		)
	})

	ev.bind(sz.SymCite, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		result := sx.Nil()
		if key := getString(args[1], env); key.GetValue() != "" {
			if len(args) > 2 {
				result = ev.evalSlice(args[2:], env).Cons(sx.MakeString(", "))
			}
			result = result.Cons(key)
		}
		if len(a) > 0 {
			result = result.Cons(EvaluateAttrbute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(SymSPAN)
	})
	ev.bind(sz.SymMark, 3, func(args sx.Vector, env *Environment) sx.Object {
		result := ev.evalSlice(args[3:], env)
		if !ev.noLinks {
			if fragment := getString(args[2], env).GetValue(); fragment != "" {
				a := attrs.Attributes{"id": fragment + ev.unique}
				return result.Cons(EvaluateAttrbute(a)).Cons(SymA)
			}
		}
		return result.Cons(SymSPAN)
	})
	ev.bind(sz.SymEndnote, 1, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		attrPlist := sx.Nil()
		if len(a) > 0 {
			if attrs := EvaluateAttrbute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		noteNum := strconv.Itoa(len(env.endnotes) + 1)
		noteID := ev.unique + noteNum
		env.endnotes = append(env.endnotes, endnoteInfo{
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
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
	ev.bind(sz.SymFormatQuote, 1, ev.evalQuote)
	ev.bind(sz.SymFormatSpan, 1, ev.makeFormatFn(SymSPAN))
	ev.bind(sz.SymFormatStrong, 1, ev.makeFormatFn(SymSTRONG))
	ev.bind(sz.SymFormatSub, 1, ev.makeFormatFn(symSUB))
	ev.bind(sz.SymFormatSuper, 1, ev.makeFormatFn(symSUP))

	ev.bind(sz.SymLiteralComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if ev.GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" {
					return sx.Nil().Cons(s).Cons(sxhtml.SymInlineComment)
				}
			}
		}
		return sx.Nil()
	})
	ev.bind(sz.SymLiteralHTML, 2, ev.evalHTML)
	ev.bind(sz.SymLiteralInput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symKBD, env)
	})
	ev.bind(sz.SymLiteralMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env).AddClass("zs-math")
		return ev.evalLiteral(args, a, symCODE, env)
	})
	ev.bind(sz.SymLiteralOutput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symSAMP, env)
	})
	ev.bind(sz.SymLiteralProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symCODE, env)
	})

	ev.bind(sz.SymLiteralZettel, 0, nilFn)
}

func (ev *Evaluator) makeFormatFn(sym *sx.Symbol) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if val, hasClass := a.Get(""); hasClass {
			a = a.Remove("").AddClass(val)
		}
		res := ev.evalSlice(args[1:], env)
		if len(a) > 0 {
			res = res.Cons(ev.EvaluateAttrbute(a))
		}
		return res.Cons(sym)
	}
}

type quoteData struct {
	primLeft, primRight string
	secLeft, secRight   string
	nbsp                bool
}

var langQuotes = map[string]quoteData{
	"":              {"&quot;", "&quot;", "&quot;", "&quot;", false},
	api.ValueLangEN: {"&ldquo;", "&rdquo;", "&lsquo;", "&rsquo;", false},
	"de":            {"&bdquo;", "&ldquo;", "&sbquo;", "&lsquo;", false},
	"fr":            {"&laquo;", "&raquo;", "&lsaquo;", "&rsaquo;", true},
}

func getQuoteData(lang string) quoteData {
	langFields := strings.FieldsFunc(lang, func(r rune) bool { return r == '-' || r == '_' })
	for len(langFields) > 0 {
		langSup := strings.Join(langFields, "-")
		quotes, ok := langQuotes[langSup]
		if ok {
			return quotes
		}
		langFields = langFields[0 : len(langFields)-1]
	}
	return langQuotes[""]
}

func getQuotes(data *quoteData, env *Environment) (string, string) {
	if env.quoteNesting%2 == 0 {
		return data.primLeft, data.primRight
	}
	return data.secLeft, data.secRight
}

func (ev *Evaluator) evalQuote(args sx.Vector, env *Environment) sx.Object {
	a := ev.GetAttributes(args[0], env)
	env.pushAttributes(a)
	defer env.popAttributes()

	if val, hasClass := a.Get(""); hasClass {
		a = a.Remove("").AddClass(val)
	}
	quotes := getQuoteData(env.getLanguage())
	leftQ, rightQ := getQuotes(&quotes, env)

	env.quoteNesting++
	res := ev.evalSlice(args[1:], env)
	env.quoteNesting--

	lastPair := res.LastPair()
	if lastPair.IsNil() {
		res = sx.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString(rightQ)), sx.Nil())
	} else {
		if quotes.nbsp {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString("&nbsp;"), sx.MakeString(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString("&nbsp;")))
		} else {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ)))
		}
	}
	if len(a) > 0 {
		res = res.Cons(ev.EvaluateAttrbute(a))
		return res.Cons(SymSPAN)
	}
	return res.Cons(sxhtml.SymListSplice)
}

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

func (ev *Evaluator) evalLiteral(args sx.Vector, a attrs.Attributes, sym *sx.Symbol, env *Environment) sx.Object {
	if a == nil {
		a = ev.GetAttributes(args[0], env)
	}
	a = setProgLang(a)
	literal := getString(args[1], env).GetValue()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sx.Nil().Cons(sx.MakeString(literal))
	if len(a) > 0 {
		res = res.Cons(ev.EvaluateAttrbute(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 (ev *Evaluator) evalHTML(args sx.Vector, env *Environment) sx.Object {
	if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" && IsSafe(s.GetValue()) {
		return sx.Nil().Cons(s).Cons(sxhtml.SymNoEscape)
	}
	return nil
}

func (ev *Evaluator) evalBLOB(description *sx.Pair, syntax, data sx.String) sx.Object {
	if data.GetValue() == "" {
		return sx.Nil()
	}
	switch syntax.GetValue() {
	case "":
		return sx.Nil()
	case api.ValueSyntaxSVG:







|










|


|
|


|


|







|







|





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

|






<
|









|








|







|

|









|

















|







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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
	ev.bind(sz.SymFormatQuote, 1, ev.evalQuote)
	ev.bind(sz.SymFormatSpan, 1, ev.makeFormatFn(SymSPAN))
	ev.bind(sz.SymFormatStrong, 1, ev.makeFormatFn(SymSTRONG))
	ev.bind(sz.SymFormatSub, 1, ev.makeFormatFn(symSUB))
	ev.bind(sz.SymFormatSuper, 1, ev.makeFormatFn(symSUP))

	ev.bind(sz.SymLiteralComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" {
					return sx.Nil().Cons(s).Cons(sxhtml.SymInlineComment)
				}
			}
		}
		return sx.Nil()
	})
	ev.bind(sz.SymLiteralHTML, 2, ev.evalHTML)
	ev.bind(sz.SymLiteralInput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return evalLiteral(args, nil, symKBD, env)
	})
	ev.bind(sz.SymLiteralMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env).AddClass("zs-math")
		return evalLiteral(args, a, symCODE, env)
	})
	ev.bind(sz.SymLiteralOutput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return evalLiteral(args, nil, symSAMP, env)
	})
	ev.bind(sz.SymLiteralProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		return evalLiteral(args, nil, symCODE, env)
	})

	ev.bind(sz.SymLiteralZettel, 0, nilFn)
}

func (ev *Evaluator) makeFormatFn(sym *sx.Symbol) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		a := GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if val, hasClass := a.Get(""); hasClass {
			a = a.Remove("").AddClass(val)
		}
		res := ev.evalSlice(args[1:], env)
		if len(a) > 0 {
			res = res.Cons(EvaluateAttrbute(a))
		}
		return res.Cons(sym)
	}
}


































func (ev *Evaluator) evalQuote(args sx.Vector, env *Environment) sx.Object {
	a := GetAttributes(args[0], env)
	env.pushAttributes(a)
	defer env.popAttributes()

	if val, hasClass := a.Get(""); hasClass {
		a = a.Remove("").AddClass(val)
	}

	leftQ, rightQ, withNbsp := env.getQuotes()

	env.quoteNesting++
	res := ev.evalSlice(args[1:], env)
	env.quoteNesting--

	lastPair := res.LastPair()
	if lastPair.IsNil() {
		res = sx.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString(rightQ)), sx.Nil())
	} else {
		if withNbsp {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString("&nbsp;"), sx.MakeString(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString("&nbsp;")))
		} else {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ)))
		}
	}
	if len(a) > 0 {
		res = res.Cons(EvaluateAttrbute(a))
		return res.Cons(SymSPAN)
	}
	return res.Cons(sxhtml.SymListSplice)
}

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

func evalLiteral(args sx.Vector, a attrs.Attributes, sym *sx.Symbol, env *Environment) sx.Object {
	if a == nil {
		a = GetAttributes(args[0], env)
	}
	a = setProgLang(a)
	literal := getString(args[1], env).GetValue()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sx.Nil().Cons(sx.MakeString(literal))
	if len(a) > 0 {
		res = res.Cons(EvaluateAttrbute(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 (ev *Evaluator) evalHTML(args sx.Vector, env *Environment) sx.Object {
	if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" && IsSafe(s.GetValue()) {
		return sx.Nil().Cons(s).Cons(sxhtml.SymNoEscape)
	}
	return nil
}

func evalBLOB(description *sx.Pair, syntax, data sx.String) sx.Object {
	if data.GetValue() == "" {
		return sx.Nil()
	}
	switch syntax.GetValue() {
	case "":
		return sx.Nil()
	case api.ValueSyntaxSVG:
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
	result := ev.evalSlice(inline, env)
	if len(inline) == 0 {
		result = sx.Nil().Cons(refValue)
	}
	if ev.noLinks {
		return result.Cons(SymSPAN)
	}
	return result.Cons(ev.EvaluateAttrbute(a)).Cons(SymA)
}

func (ev *Evaluator) getSymbol(obj sx.Object, env *Environment) *sx.Symbol {
	if env.err == nil {
		if sym, ok := sx.GetSymbol(obj); ok {
			return sym
		}
		env.err = fmt.Errorf("%v/%T is not a symbol", obj, obj)
	}
	return sx.MakeSymbol("???")







|


|







897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
	result := ev.evalSlice(inline, env)
	if len(inline) == 0 {
		result = sx.Nil().Cons(refValue)
	}
	if ev.noLinks {
		return result.Cons(SymSPAN)
	}
	return result.Cons(EvaluateAttrbute(a)).Cons(SymA)
}

func getSymbol(obj sx.Object, env *Environment) *sx.Symbol {
	if env.err == nil {
		if sym, ok := sx.GetSymbol(obj); ok {
			return sym
		}
		env.err = fmt.Errorf("%v/%T is not a symbol", obj, obj)
	}
	return sx.MakeSymbol("???")
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
	}
	env.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}

// GetAttributes evaluates the given arg in the given environment and returns
// the contained attributes.
func (ev *Evaluator) GetAttributes(arg sx.Object, env *Environment) attrs.Attributes {
	return sz.GetAttributes(getList(arg, env))
}

var unsafeSnippets = []string{
	"<script", "</script",
	"<iframe", "</iframe",
}







|







941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
	}
	env.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}

// GetAttributes evaluates the given arg in the given environment and returns
// the contained attributes.
func GetAttributes(arg sx.Object, env *Environment) attrs.Attributes {
	return sz.GetAttributes(getList(arg, env))
}

var unsafeSnippets = []string{
	"<script", "</script",
	"<iframe", "</iframe",
}

Changes to sz/walk.go.

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		SymLinkExternal: walkChildrenInlines4,
		SymLinkFound:    walkChildrenInlines4,
		SymLinkHosted:   walkChildrenInlines4,
		SymLinkInvalid:  walkChildrenInlines4,
		SymLinkQuery:    walkChildrenInlines4,
		SymLinkSelf:     walkChildrenInlines4,
		SymLinkZettel:   walkChildrenInlines4,
		SymEmbed:        walkChildrenInlines4,
		SymCite:         walkChildrenInlines4,
		SymFormatDelete: walkChildrenInlines3,
		SymFormatEmph:   walkChildrenInlines3,
		SymFormatInsert: walkChildrenInlines3,
		SymFormatMark:   walkChildrenInlines3,
		SymFormatQuote:  walkChildrenInlines3,
		SymFormatStrong: walkChildrenInlines3,







|







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		SymLinkExternal: walkChildrenInlines4,
		SymLinkFound:    walkChildrenInlines4,
		SymLinkHosted:   walkChildrenInlines4,
		SymLinkInvalid:  walkChildrenInlines4,
		SymLinkQuery:    walkChildrenInlines4,
		SymLinkSelf:     walkChildrenInlines4,
		SymLinkZettel:   walkChildrenInlines4,
		SymEmbed:        walkChildrenEmbed,
		SymCite:         walkChildrenInlines4,
		SymFormatDelete: walkChildrenInlines3,
		SymFormatEmph:   walkChildrenInlines3,
		SymFormatInsert: walkChildrenInlines3,
		SymFormatMark:   walkChildrenInlines3,
		SymFormatQuote:  walkChildrenInlines3,
		SymFormatStrong: walkChildrenInlines3,
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
		}
		n.SetCar(Walk(v, n.Head(), env))
	}
	return dn
}

func walkChildrenTable(v Visitor, tn *sx.Pair, env *sx.Pair) *sx.Pair {
	header := tn.Tail()
	header.SetCar(walkChildrenList(v, header.Tail(), env))
	for row := header.Tail(); row != nil; row = row.Tail() {
		row.SetCar(walkChildrenList(v, row.Head(), env))
	}
	return tn
}

func walkChildrenMark(v Visitor, mn *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := mn.Car()
	next := mn.Tail()
	// mark := next.Car()
	next = next.Tail()
	// slug := next.Car()
	next = next.Tail()
	// fragment := next.Car()
	next.SetCdr(walkChildrenList(v, next.Tail(), env))
	return mn
}

















func walkChildrenInlines4(v Visitor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := ln.Car()
	next := ln.Tail()
	// attrs := next.Car()
	next = next.Tail()
	// val3 := next.Car()







<
<
|
















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
		}
		n.SetCar(Walk(v, n.Head(), env))
	}
	return dn
}

func walkChildrenTable(v Visitor, tn *sx.Pair, env *sx.Pair) *sx.Pair {


	for row := tn.Tail(); row != nil; row = row.Tail() {
		row.SetCar(walkChildrenList(v, row.Head(), env))
	}
	return tn
}

func walkChildrenMark(v Visitor, mn *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := mn.Car()
	next := mn.Tail()
	// mark := next.Car()
	next = next.Tail()
	// slug := next.Car()
	next = next.Tail()
	// fragment := next.Car()
	next.SetCdr(walkChildrenList(v, next.Tail(), env))
	return mn
}

func walkChildrenEmbed(v Visitor, en *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := en.Car()
	next := en.Tail()
	// attr := next.Car()
	next = next.Tail()
	// ref := next.Car()
	next = next.Tail()
	// syntax := next.Car()
	next = next.Tail()
	if next != nil {
		// text := next.Car()
		next.SetCar(Walk(v, next.Head(), env))
	}
	return en
}

func walkChildrenInlines4(v Visitor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := ln.Car()
	next := ln.Tail()
	// attrs := next.Car()
	next = next.Tail()
	// val3 := next.Car()

Changes to sz/zmk/block.go.

270
271
272
273
274
275
276

277
278
279
280
281
282
283
284
285
286
287
288
			lastPara = bn
		}
	}
}

// parseRegionLastLine parses the last line of a region and returns its inline text.
func (cp *zmkP) parseRegionLastLine() *sx.Pair {

	cp.clearStacked() // remove any lists defined in the region
	cp.skipSpace()
	var region sx.ListBuilder
	for {
		switch cp.inp.Ch {
		case input.EOS, '\n', '\r':
			return region.List()
		}
		in := cp.parseInline()
		if in == nil {
			return region.List()
		}







>

|


|







270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
			lastPara = bn
		}
	}
}

// parseRegionLastLine parses the last line of a region and returns its inline text.
func (cp *zmkP) parseRegionLastLine() *sx.Pair {
	inp := cp.inp
	cp.clearStacked() // remove any lists defined in the region
	inp.SkipSpace()
	var region sx.ListBuilder
	for {
		switch inp.Ch {
		case input.EOS, '\n', '\r':
			return region.List()
		}
		in := cp.parseInline()
		if in == nil {
			return region.List()
		}
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
	if delims < 3 {
		return nil, false
	}
	if inp.Ch != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	if delims > 7 {
		delims = 7
	}
	level := int64(delims - 2)
	var attrs *sx.Pair
	var text sx.ListBuilder
	for {







|







298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
	if delims < 3 {
		return nil, false
	}
	if inp.Ch != ' ' {
		return nil, false
	}
	inp.Next()
	inp.SkipSpace()
	if delims > 7 {
		delims = 7
	}
	level := int64(delims - 2)
	var attrs *sx.Pair
	var text sx.ListBuilder
	for {
347
348
349
350
351
352
353

354
355
356
357
358
359
360
361
362

// parseNestedList parses a list.
func (cp *zmkP) parseNestedList() (res *sx.Pair, success bool) {
	kinds := cp.parseNestedListKinds()
	if len(kinds) == 0 {
		return nil, false
	}

	cp.skipSpace()
	if !kinds[len(kinds)-1].IsEqual(sz.SymListQuote) && input.IsEOLEOS(cp.inp.Ch) {
		return nil, false
	}

	if len(kinds) < len(cp.lists) {
		cp.lists = cp.lists[:len(kinds)]
	}
	ln, newLnCount := cp.buildNestedList(kinds)







>
|
|







348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

// parseNestedList parses a list.
func (cp *zmkP) parseNestedList() (res *sx.Pair, success bool) {
	kinds := cp.parseNestedListKinds()
	if len(kinds) == 0 {
		return nil, false
	}
	inp := cp.inp
	inp.SkipSpace()
	if !kinds[len(kinds)-1].IsEqual(sz.SymListQuote) && input.IsEOLEOS(inp.Ch) {
		return nil, false
	}

	if len(kinds) < len(cp.lists) {
		cp.lists = cp.lists[:len(kinds)]
	}
	ln, newLnCount := cp.buildNestedList(kinds)
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// parseDefTerm parses a term of a definition list.
func (cp *zmkP) parseDefTerm() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	descrl := cp.descrl
	if descrl == nil {
		descrl = sx.Cons(sz.SymDescription, nil)
		cp.descrl = descrl
		res = descrl
	}
	lastPair, pos := lastPairPos(descrl)







|







445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// parseDefTerm parses a term of a definition list.
func (cp *zmkP) parseDefTerm() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	inp.SkipSpace()
	descrl := cp.descrl
	if descrl == nil {
		descrl = sx.Cons(sz.SymDescription, nil)
		cp.descrl = descrl
		res = descrl
	}
	lastPair, pos := lastPairPos(descrl)
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// parseDefDescr parses a description of a definition list.
func (cp *zmkP) parseDefDescr() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	descrl := cp.descrl
	lastPair, pos := lastPairPos(descrl)
	if descrl == nil || pos <= 0 {
		// No term given
		return nil, false
	}








|







484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// parseDefDescr parses a description of a definition list.
func (cp *zmkP) parseDefDescr() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	inp.SkipSpace()
	descrl := cp.descrl
	lastPair, pos := lastPairPos(descrl)
	if descrl == nil || pos <= 0 {
		// No term given
		return nil, false
	}

Changes to sz/zmk/inline.go.

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
func hasQueryPrefix(src []byte) bool {
	return len(src) > len(api.QueryPrefix) && string(src[:len(api.QueryPrefix)]) == api.QueryPrefix
}

func (cp *zmkP) parseReference(openCh, closeCh rune) (ref string, text *sx.Pair, _ bool) {
	inp := cp.inp
	inp.Next()
	cp.skipSpace()
	if inp.Ch == openCh {
		// Additional opening chars result in a fail
		return "", nil, false
	}
	var is sx.Vector
	pos := inp.Pos
	if !hasQueryPrefix(inp.Src[pos:]) {







|







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
func hasQueryPrefix(src []byte) bool {
	return len(src) > len(api.QueryPrefix) && string(src[:len(api.QueryPrefix)]) == api.QueryPrefix
}

func (cp *zmkP) parseReference(openCh, closeCh rune) (ref string, text *sx.Pair, _ bool) {
	inp := cp.inp
	inp.Next()
	inp.SkipSpace()
	if inp.Ch == openCh {
		// Additional opening chars result in a fail
		return "", nil, false
	}
	var is sx.Vector
	pos := inp.Pos
	if !hasQueryPrefix(inp.Src[pos:]) {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
			if hasSpace {
				return "", nil, false
			}
			inp.SetPos(pos)
		}
	}

	cp.skipSpace()
	pos = inp.Pos
	if !cp.readReferenceToClose(closeCh) {
		return "", nil, false
	}
	ref = strings.TrimSpace(string(inp.Src[pos:inp.Pos]))
	if inp.Next() != closeCh {
		return "", nil, false







|







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
			if hasSpace {
				return "", nil, false
			}
			inp.SetPos(pos)
		}
	}

	inp.SkipSpace()
	pos = inp.Pos
	if !cp.readReferenceToClose(closeCh) {
		return "", nil, false
	}
	ref = strings.TrimSpace(string(inp.Src[pos:inp.Pos]))
	if inp.Next() != closeCh {
		return "", nil, false
326
327
328
329
330
331
332
333
334
335

336
337
338
339
340
341
342
		Cons(sz.SymMark)
	return mn, true
	// Problematisch ist, dass hier noch nicht mn.Fragment und mn.Slug gesetzt werden.
	// Evtl. muss es ein PreMark-Symbol geben
}

func (cp *zmkP) parseLinkLikeRest() (*sx.Pair, bool) {
	cp.skipSpace()
	var ins sx.Vector
	inp := cp.inp

	for inp.Ch != ']' {
		in := cp.parseInline()
		if in == nil {
			return nil, false
		}
		ins = append(ins, in)
		if input.IsEOLEOS(inp.Ch) && sz.IsBreakSym(in.Car()) {







<


>







326
327
328
329
330
331
332

333
334
335
336
337
338
339
340
341
342
		Cons(sz.SymMark)
	return mn, true
	// Problematisch ist, dass hier noch nicht mn.Fragment und mn.Slug gesetzt werden.
	// Evtl. muss es ein PreMark-Symbol geben
}

func (cp *zmkP) parseLinkLikeRest() (*sx.Pair, bool) {

	var ins sx.Vector
	inp := cp.inp
	inp.SkipSpace()
	for inp.Ch != ']' {
		in := cp.parseInline()
		if in == nil {
			return nil, false
		}
		ins = append(ins, in)
		if input.IsEOLEOS(inp.Ch) && sz.IsBreakSym(in.Car()) {
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
	if inp.Next() != '%' {
		return nil, false
	}
	for inp.Ch == '%' {
		inp.Next()
	}
	attrs := cp.parseInlineAttributes()
	cp.skipSpace()
	pos := inp.Pos
	for {
		if input.IsEOLEOS(inp.Ch) {
			return sx.MakeList(
				sz.SymLiteralComment,
				attrs,
				sx.MakeString(string(inp.Src[pos:inp.Pos])),







|







355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
	if inp.Next() != '%' {
		return nil, false
	}
	for inp.Ch == '%' {
		inp.Next()
	}
	attrs := cp.parseInlineAttributes()
	inp.SkipSpace()
	pos := inp.Pos
	for {
		if input.IsEOLEOS(inp.Ch) {
			return sx.MakeList(
				sz.SymLiteralComment,
				attrs,
				sx.MakeString(string(inp.Src[pos:inp.Pos])),
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
			s := cp.parseString()
			sb.WriteString(s.GetValue())
		}
	}
}

func createLiteralNode(sym *sx.Symbol, attrs *sx.Pair, content string) *sx.Pair {
	if sym.IsEqual(sz.SymLiteralZettel) {
		if p := attrs.Assoc(sx.MakeString("")); p != nil {
			if val, isString := sx.GetString(p.Cdr()); isString && val.GetValue() == api.ValueSyntaxHTML {
				sym = sz.SymLiteralHTML
				attrs = attrs.RemoveAssoc(sx.MakeString(""))
			}
		}
	}







|







457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
			s := cp.parseString()
			sb.WriteString(s.GetValue())
		}
	}
}

func createLiteralNode(sym *sx.Symbol, attrs *sx.Pair, content string) *sx.Pair {
	if sym.IsEqualSymbol(sz.SymLiteralZettel) {
		if p := attrs.Assoc(sx.MakeString("")); p != nil {
			if val, isString := sx.GetString(p.Cdr()); isString && val.GetValue() == api.ValueSyntaxHTML {
				sym = sz.SymLiteralHTML
				attrs = attrs.RemoveAssoc(sx.MakeString(""))
			}
		}
	}

Changes to sz/zmk/post-processor.go.

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
		}

		elem = cellTail.Head()
		if elem.Car().IsEqual(sz.SymText) {
			if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
				str := s.GetValue()
				cellAlign := getCellAlignment(str[len(str)-1])
				if !cellAlign.IsEqual(sz.SymCell) {
					elem.SetCdr(sx.Cons(sx.MakeString(str[0:len(str)-1]), nil))
				}
				align[cellCount-1] = cellAlign
				cell.SetCar(cellAlign)
			}
		}
	}







|







320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
		}

		elem = cellTail.Head()
		if elem.Car().IsEqual(sz.SymText) {
			if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
				str := s.GetValue()
				cellAlign := getCellAlignment(str[len(str)-1])
				if !cellAlign.IsEqualSymbol(sz.SymCell) {
					elem.SetCdr(sx.Cons(sx.MakeString(str[0:len(str)-1]), nil))
				}
				align[cellCount-1] = cellAlign
				cell.SetCar(cellAlign)
			}
		}
	}
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

		// elem is first cell inline element
		elem := cellTail.Head()
		if elem.Car().IsEqual(sz.SymText) {
			if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
				str := s.GetValue()
				cellAlign := getCellAlignment(str[0])
				if !cellAlign.IsEqual(sz.SymCell) {
					elem.SetCdr(sx.Cons(sx.MakeString(str[1:]), nil))
					cell.SetCar(cellAlign)
				}
			}
		}
	}








|







366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

		// elem is first cell inline element
		elem := cellTail.Head()
		if elem.Car().IsEqual(sz.SymText) {
			if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" {
				str := s.GetValue()
				cellAlign := getCellAlignment(str[0])
				if !cellAlign.IsEqualSymbol(sz.SymCell) {
					elem.SetCdr(sx.Cons(sx.MakeString(str[1:]), nil))
					cell.SetCar(cellAlign)
				}
			}
		}
	}

Changes to sz/zmk/ref.go.

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	if invalidReference(s) {
		return makePairRef(sz.SymRefStateInvalid, s)
	}
	if strings.HasPrefix(s, api.QueryPrefix) {
		return makePairRef(sz.SymRefStateQuery, s[len(api.QueryPrefix):])
	}
	if state, ok := localState(s); ok {
		if state.IsEqual(sz.SymRefStateBased) {
			s = s[1:]
		}
		_, err := url.Parse(s)
		if err == nil {
			return makePairRef(state, s)
		}
	}







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	if invalidReference(s) {
		return makePairRef(sz.SymRefStateInvalid, s)
	}
	if strings.HasPrefix(s, api.QueryPrefix) {
		return makePairRef(sz.SymRefStateQuery, s[len(api.QueryPrefix):])
	}
	if state, ok := localState(s); ok {
		if state.IsEqualSymbol(sz.SymRefStateBased) {
			s = s[1:]
		}
		_, err := url.Parse(s)
		if err == nil {
			return makePairRef(state, s)
		}
	}

Changes to sz/zmk/zmk.go.

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		inp.Next()
	}
	if pos < inp.Pos {
		return attrMap{"": string(inp.Src[pos:inp.Pos])}.asPairAssoc()
	}

	// No immediate name: skip spaces
	cp.skipSpace()
	return cp.parseInlineAttributes()
}

func (cp *zmkP) parseInlineAttributes() *sx.Pair {
	inp := cp.inp
	pos := inp.Pos
	if attrs, success := cp.doParseAttributes(); success {







|







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		inp.Next()
	}
	if pos < inp.Pos {
		return attrMap{"": string(inp.Src[pos:inp.Pos])}.asPairAssoc()
	}

	// No immediate name: skip spaces
	inp.SkipSpace()
	return cp.parseInlineAttributes()
}

func (cp *zmkP) parseInlineAttributes() *sx.Pair {
	inp := cp.inp
	pos := inp.Pos
	if attrs, success := cp.doParseAttributes(); success {
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
			inp.EatEOL()
		default:
			return
		}
	}
}

func (cp *zmkP) skipSpace() {
	for inp := cp.inp; inp.Ch == ' '; {
		inp.Next()
	}
}

func isNameRune(ch rune) bool {
	return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '-' || ch == '_'
}







<
<
<
<
<
<



271
272
273
274
275
276
277






278
279
280
			inp.EatEOL()
		default:
			return
		}
	}
}







func isNameRune(ch rune) bool {
	return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '-' || ch == '_'
}

Changes to sz/zmk/zmk_test.go.

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
func checkTcs(t *testing.T, isBlock bool, tcs TestCases) {
	t.Helper()

	for tcn, tc := range tcs {
		t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.source), func(st *testing.T) {
			st.Helper()
			ast := parseInput(tc.source, isBlock)

			got := ast.String()
			if tc.want != got {
				st.Errorf("\nwant=%q\n got=%q", tc.want, got)
			}
		})
	}
}
func parseInput(src string, asBlock bool) sx.Sequence {
	inp := input.NewInput([]byte(src))
	if asBlock {
		bl := zmk.ParseBlocks(inp)
		return bl
	}
	il := zmk.ParseInlines(inp)
	return il
}





func TestEOL(t *testing.T) {
	t.Parallel()
	for _, isBlock := range []bool{true, false} {
		checkTcs(t, isBlock, TestCases{
			{"", "()"},
			{"\n", "()"},
			{"\r", "()"},







>







|









>
>
>
>







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
func checkTcs(t *testing.T, isBlock bool, tcs TestCases) {
	t.Helper()

	for tcn, tc := range tcs {
		t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.source), func(st *testing.T) {
			st.Helper()
			ast := parseInput(tc.source, isBlock)
			sz.Walk(astWalker{}, ast, nil)
			got := ast.String()
			if tc.want != got {
				st.Errorf("\nwant=%q\n got=%q", tc.want, got)
			}
		})
	}
}
func parseInput(src string, asBlock bool) *sx.Pair {
	inp := input.NewInput([]byte(src))
	if asBlock {
		bl := zmk.ParseBlocks(inp)
		return bl
	}
	il := zmk.ParseInlines(inp)
	return il
}

type astWalker struct{}

func (astWalker) Visit(node *sx.Pair, env *sx.Pair) sx.Object { return sx.MakeBoolean(true) }

func TestEOL(t *testing.T) {
	t.Parallel()
	for _, isBlock := range []bool{true, false} {
		checkTcs(t, isBlock, TestCases{
			{"", "()"},
			{"\n", "()"},
			{"\r", "()"},

Changes to www/changes.wiki.

1
2
3
4



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

<a name="0_19"></a>
<h2>Changes for Version 0.19.0 (pending)</h2>




<a name="0_18"></a>
<h2>Changes for Version 0.18.0 (2024-07-11)</h2>
  *  Add client method <code>GetApplicationZid</code> to retrieve the zettel
     identifier of an configuration zettel for a specific application.
  *  Rename to be package <code>t73f.de/r/zsc</code>
  *  Reserve some zettel identifier for future use




>
>
>







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

<a name="0_19"></a>
<h2>Changes for Version 0.19.0 (pending)</h2>
  *  Remove support for rename operation; removed all associated constants
  *  Make quote handling in shtml public, to be used by other encodeers
  *  shtml generates external links with rel attribute

<a name="0_18"></a>
<h2>Changes for Version 0.18.0 (2024-07-11)</h2>
  *  Add client method <code>GetApplicationZid</code> to retrieve the zettel
     identifier of an configuration zettel for a specific application.
  *  Rename to be package <code>t73f.de/r/zsc</code>
  *  Reserve some zettel identifier for future use