Zettelstore Client

Check-in Differences
Login

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

Difference From trunk To v0.11.0

2023-11-30
15:15
Version 0.16.0 ... (Leaf check-in: 751754d40c user: stern tags: trunk, release, v0.16.0)
2023-11-29
16:50
Add zettel identifier for predefined role zettel ... (check-in: d47ca9287a user: stern tags: trunk)
2023-04-02
16:14
Add metadata key 'expire' ... (check-in: e11f223228 user: stern tags: trunk)
2023-03-27
14:37
Version 0.11.0 ... (check-in: 331cad470c user: stern tags: trunk, release, v0.11.0)
14:34
Version 0.11.0 ... (check-in: 712c21afc8 user: stern tags: trunk, release, v0.10.0)

Changes to api/api.go.

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
















41
42
43
44
45
46
47
48







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73





74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89






90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127







+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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
	Rights ZettelRights

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

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

// MetaJSON contains the metadata of a zettel.
type MetaJSON struct {
	Meta   ZettelMeta   `json:"meta"`
	Rights ZettelRights `json:"rights"`
}

// ZidMetaJSON contains the identifier and the metadata of a zettel.
type ZidMetaJSON struct {
	ID     ZettelID     `json:"id"`
	Meta   ZettelMeta   `json:"meta"`
	Rights ZettelRights `json:"rights"`
}
// ZidMetaRights contains the identifier, the metadata of a zettel, and its rights.
type ZidMetaRights struct {
	ID     ZettelID
	Meta   ZettelMeta
	Rights ZettelRights

// ZidMetaRelatedList contains identifier/metadata of a zettel and the same for related zettel
type ZidMetaRelatedList struct {
	ID     ZettelID      `json:"id"`
	Meta   ZettelMeta    `json:"meta"`
	Rights ZettelRights  `json:"rights"`
	List   []ZidMetaJSON `json:"list"`
}

// ZettelDataJSON contains all data for a zettel.
type ZettelDataJSON struct {
	Meta     ZettelMeta `json:"meta"`
	Encoding string     `json:"encoding"`
	Content  string     `json:"content"`
}

// ZettelData contains all data for a zettel.
type ZettelData struct {
	Meta     ZettelMeta
	Rights   ZettelRights
	Encoding string
	Content  string
// ZettelJSON contains all data for a zettel, the identifier, the metadata, and the content.
type ZettelJSON struct {
	ID       ZettelID     `json:"id"`
	Meta     ZettelMeta   `json:"meta"`
	Encoding string       `json:"encoding"`
	Content  string       `json:"content"`
	Rights   ZettelRights `json:"rights"`
}

// ZettelContentJSON contains all elements to transfer the content of a zettel.
type ZettelContentJSON struct {
	Encoding string `json:"encoding"`
	Content  string `json:"content"`
}

// ZettelListJSON contains data for a zettel list.
type ZettelListJSON struct {
	Query string        `json:"query"`
	Human string        `json:"human"`
	List  []ZidMetaJSON `json:"list"`
}

// Aggregate maps metadata keys to list of zettel identifier.
type Aggregate map[string][]ZettelID
// MapMeta maps metadata keys to list of metadata.
type MapMeta map[string][]ZettelID

// MapListJSON specifies the map of metadata key to list of metadata that contains the key.
type MapListJSON struct {
	Map MapMeta `json:"map"`
}

// VersionJSON contains version information.
type VersionJSON struct {
	Major int    `json:"major"`
	Minor int    `json:"minor"`
	Patch int    `json:"patch"`
	Info  string `json:"info"`
	Hash  string `json:"hash"`
}

Changes to api/const.go.

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







39
40

41
42
43
44


45
46
47
48



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
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







-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
-
-
-
-
+
+


-
-
+
+
+






-
-
-
-
-
-
-
-
-



-
-







	ZidBoxManager           = ZettelID("00000000000020")
	ZidMetadataKey          = ZettelID("00000000000090")
	ZidParser               = ZettelID("00000000000092")
	ZidStartupConfiguration = ZettelID("00000000000096")
	ZidConfiguration        = ZettelID("00000000000100")

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

	// WebUI sxn code zettel are in the range 19000..19999
	ZidSxnStart = ZettelID("00000000019000")
	ZidSxnBase  = ZettelID("00000000019990")
	ZidContextTemplate = ZettelID("00000000010406")
	ZidErrorTemplate   = ZettelID("00000000010700")

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

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

	// WebUI image zettel are in the range 40000..49999
	ZidEmoji = ZettelID("00000000040001")

	// Other sxn code zettel are in the range 50000..59999
	ZidSxnPrelude = ZettelID("00000000059900")

	// Predefined Zettelmarkup zettel are in the range 60000..69999
	ZidRoleZettelZettel        = ZettelID("00000000060010")
	ZidRoleConfigurationZettel = ZettelID("00000000060020")
	ZidRoleRoleZettel          = ZettelID("00000000060030")
	ZidRoleTagZettel           = ZettelID("00000000060040")

	// Range 90000...99999 is reserved for zettel templates
	ZidTOCNewTemplate    = ZettelID("00000000090000")
	ZidTemplateNewZettel = ZettelID("00000000090001")
	ZidTemplateNewRole   = ZettelID("00000000090004")
	ZidTemplateNewTag    = ZettelID("00000000090003")
	ZidTemplateNewUser   = ZettelID("00000000090002")

	ZidDefaultHome = ZettelID("00010000000000")
)

// LengthZid factors the constant length of a zettel identifier
const LengthZid = len(ZidDefaultHome)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

146
147
148
149
150
151
152
153
154
155
88
89
90
91
92
93
94

95

96
97
98
99
100
101
102
103
104

105

106
107
108
109
110
111
112
113
114
115
116
117
118
119


120
121
122
123
124
125
126
127
128
129

130
131
132
133
134
135
136







-

-









-

-














-
-







+


-







	KeyBack         = "back"
	KeyBackward     = "backward"
	KeyBoxNumber    = "box-number"
	KeyCopyright    = "copyright"
	KeyCreated      = "created"
	KeyCredential   = "credential"
	KeyDead         = "dead"
	KeyExpire       = "expire"
	KeyFolge        = "folge"
	KeyFolgeRole    = "folge-role"
	KeyForward      = "forward"
	KeyLang         = "lang"
	KeyLicense      = "license"
	KeyModified     = "modified"
	KeyPrecursor    = "precursor"
	KeyPredecessor  = "predecessor"
	KeyPublished    = "published"
	KeyQuery        = "query"
	KeyReadOnly     = "read-only"
	KeySubordinates = "subordinates"
	KeySuccessors   = "successors"
	KeySuperior     = "superior"
	KeySummary      = "summary"
	KeyURL          = "url"
	KeyUselessFiles = "useless-files"
	KeyUserID       = "user-id"
	KeyUserRole     = "user-role"
	KeyVisibility   = "visibility"
)

// Predefined Metadata values
const (
	ValueFalse             = "false"
	ValueTrue              = "true"
	ValueLangEN            = "en"
	ValueRoleConfiguration = "configuration"
	ValueRoleTag           = "tag"
	ValueRoleRole          = "role"
	ValueRoleZettel        = "zettel"
	ValueSyntaxCSS         = "css"
	ValueSyntaxDraw        = "draw"
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"
	ValueSyntaxMustache    = "mustache"
	ValueSyntaxNone        = "none"
	ValueSyntaxSVG         = "svg"
	ValueSyntaxSxn         = "sxn"
	ValueSyntaxText        = "text"
	ValueSyntaxZmk         = "zmk"
	ValueUserRoleCreator   = "creator"
	ValueUserRoleOwner     = "owner"
	ValueUserRoleReader    = "reader"
	ValueUserRoleWriter    = "writer"
	ValueVisibilityCreator = "creator"
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164
165

166
167
168
169
170
171
172
173
174
175
176
177
178

179
180
181
182

183
184
185
186
187
188
189
190

191
192
193
194

195
196
197
198
199
200
201
202







+
+


+



-

+
-
+
+
+
+
+






+

-




-
+





+

-




-
+







	HeaderDestination = "Destination"
	HeaderLocation    = "Location"
)

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

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

// Supported encoding values.
const (
	EncodingHTML  = "html"
	EncodingMD    = "md"
	EncodingSexpr = "sexpr"
	EncodingSHTML = "shtml"
	EncodingSz    = "sz"
	EncodingText  = "text"
	EncodingZMK   = "zmk"

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

var mapEncodingEnum = map[string]EncodingEnum{
	EncodingHTML:  EncoderHTML,
	EncodingMD:    EncoderMD,
	EncodingSexpr: EncoderSexpr,
	EncodingSHTML: EncoderSHTML,
	EncodingSz:    EncoderSz,
	EncodingText:  EncoderText,
	EncodingZMK:   EncoderZmk,

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

func init() {
	for k, v := range mapEncodingEnum {
		mapEnumEncoding[v] = k
	}
226
227
228
229
230
231
232
233
234


235
236
237
238
239

240
241
242
243
244
245
246
214
215
216
217
218
219
220


221
222
223
224
225
226

227
228
229
230
231
232
233
234







-
-
+
+




-
+







type EncodingEnum uint8

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

	EncoderPlain
	EncoderData
	EncoderJson
)

// String representation of an encoder key.
func (e EncodingEnum) String() string {
	if f, ok := mapEnumEncoding[e]; ok {
		return f
	}
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
249
250
251
252
253
254
255


















256



257
258


259










260
261
262
263
264
265
266
267




268







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-

const (
	CommandAuthenticated = Command("authenticated")
	CommandRefresh       = Command("refresh")
)

// Supported search operator representations
const (
	BackwardDirective = "BACKWARD"
	ContextDirective  = "CONTEXT"
	CostDirective     = "COST"
	ForwardDirective  = "FORWARD"
	IdentDirective    = "IDENT"
	ItemsDirective    = "ITEMS"
	MaxDirective      = "MAX"
	LimitDirective    = "LIMIT"
	OffsetDirective   = "OFFSET"
	OrDirective       = "OR"
	OrderDirective    = "ORDER"
	PhraseDirective   = "PHRASE"
	PickDirective     = "PICK"
	RandomDirective   = "RANDOM"
	ReverseDirective  = "REVERSE"
	UnlinkedDirective = "UNLINKED"

	ActionSeparator = "|"
	ActionSeparator        = "|"

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

	SearchOperatorNot        = "!"
	SearchOperatorNot      = "!"
	SearchOperatorEqual      = "="
	SearchOperatorNotEqual   = "!="
	SearchOperatorHas        = ":"
	SearchOperatorHasNot     = "!:"
	SearchOperatorPrefix     = "["
	SearchOperatorNoPrefix   = "!["
	SearchOperatorSuffix     = "]"
	SearchOperatorNoSuffix   = "!]"
	SearchOperatorMatch      = "~"
	SearchOperatorNoMatch    = "!~"
	SearchOperatorHas      = ":"
	SearchOperatorHasNot   = "!:"
	SearchOperatorPrefix   = ">"
	SearchOperatorNoPrefix = "!>"
	SearchOperatorSuffix   = "<"
	SearchOperatorNoSuffix = "!<"
	SearchOperatorMatch    = "~"
	SearchOperatorNoMatch  = "!~"
	SearchOperatorLess       = "<"
	SearchOperatorNotLess    = "!<"
	SearchOperatorGreater    = ">"
	SearchOperatorNotGreater = "!>"
)

Changes to api/urlbuilder.go.

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
87
88
89
90
91
92
93



94
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131







-
-
-
+
+
-




















+
+
+
+
+
+
+
+
+







	ub.rawLocal = ""
	ub.query = append(ub.query, urlQuery{key, value})
	return ub
}

// AppendQuery adds a new query
func (ub *URLBuilder) AppendQuery(value string) *URLBuilder {
	if value != "" {
		ub.rawLocal = ""
		ub.query = append(ub.query, urlQuery{QueryKeyQuery, value})
	ub.rawLocal = ""
	ub.query = append(ub.query, urlQuery{QueryKeyQuery, value})
	}
	return ub
}

// ClearQuery removes all query parameters.
func (ub *URLBuilder) ClearQuery() *URLBuilder {
	ub.rawLocal = ""
	ub.query = nil
	ub.fragment = ""
	return ub
}

// SetFragment stores the fragment
func (ub *URLBuilder) SetFragment(s string) *URLBuilder {
	ub.rawLocal = ""
	ub.fragment = s
	return ub
}

// String produces a string value.
func (ub *URLBuilder) String() string {
	return ub.asString("&")
}

// AttrString returns the string value of the URL suitable to be placed in a HTML attribute.
func (ub *URLBuilder) AttrString() string {
	return ub.asString("&amp;")
}

func (ub *URLBuilder) asString(qsep string) string {
	var sb strings.Builder

	sb.WriteString(ub.prefix)
	if ub.key != '/' {
		sb.WriteByte(ub.key)
	}
	if ub.rawLocal != "" {
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
142
143
144
145
146
147
148

149
150
151
152
153
154
155
156
157
158







-
+









		sb.WriteByte('#')
		sb.WriteString(ub.fragment)
	}
	for i, q := range ub.query {
		if i == 0 {
			sb.WriteByte('?')
		} else {
			sb.WriteByte('&')
			sb.WriteString(qsep)
		}
		sb.WriteString(q.key)
		if val := q.val; val != "" {
			sb.WriteByte('=')
			sb.WriteString(url.QueryEscape(val))
		}
	}
	return sb.String()
}

Changes to attrs/attrs.go.

10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24







-
+








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

import (
	"strings"

	"zettelstore.de/client.fossil/maps"
	"zettelstore.de/c/maps"
)

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

// IsEmpty returns true if there are no attributes.
func (a Attributes) IsEmpty() bool { return len(a) == 0 }

Changes to attrs/attrs_test.go.

9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23







-
+







//-----------------------------------------------------------------------------

package attrs_test

import (
	"testing"

	"zettelstore.de/client.fossil/attrs"
	"zettelstore.de/c/attrs"
)

func TestHasDefault(t *testing.T) {
	t.Parallel()
	attr := attrs.Attributes{}
	if attr.HasDefault() {
		t.Error("Should not have default attr")

Changes to client/client.go.

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
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







-
+








-
+
-
-
-
+
+







// Package client provides a client for accessing the Zettelstore via its API.
package client

import (
	"bufio"
	"bytes"
	"context"
	"fmt"
	"encoding/json"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"zettelstore.de/client.fossil/api"
	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/client.fossil/sexp"
	"zettelstore.de/sx.fossil"
	"zettelstore.de/sx.fossil/sxreader"
	"codeberg.org/t73fde/sxpf/reader"
	"zettelstore.de/c/api"
)

// Client contains all data to execute requests.
type Client struct {
	base      string
	username  string
	password  string
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
66
67
68
69
70
71
72













73
74
75
76
77
78
79







-
-
-
-
-
-
-
-
-
-
-
-
-







				TLSHandshakeTimeout: 5 * time.Second,
			},
		},
	}
	return &c
}

// AllowRedirect will modify the client to not follow redirect status code when
// using the Zettelstore. The original behaviour can be restored by settinh
// "allow" to false.
func (c *Client) AllowRedirect(allow bool) {
	if allow {
		c.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		}
	} else {
		c.client.CheckRedirect = nil
	}
}

// Error encapsulates the possible client call errors.
type Error struct {
	StatusCode int
	Message    string
	Body       []byte
}

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
162
163
164
165
166
167
168



169
170
171



172
173
174







175
176
177
178
179
180
181
182
183
184







-
-
-
+
+
+
-
-
-



-
-
-
-
-
-
-
+
+
+







	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return statusToError(resp)
	}
	rd := sxreader.MakeReader(resp.Body)
	obj, err := rd.Read()
	if err != nil {
	dec := json.NewDecoder(resp.Body)
	var tinfo api.AuthJSON
	err = dec.Decode(&tinfo)
		return err
	}
	vals, err := sexp.ParseList(obj, "ssi")
	if err != nil {
		return err
	}
	token := vals[1].(sx.String).String()
	if len(token) < 4 {
		return fmt.Errorf("no valid token found: %q", token)
	}
	c.token = token
	c.tokenType = vals[0].(sx.String).String()
	c.expires = time.Now().Add(time.Duration(vals[2].(sx.Int64)*9/10) * time.Second)
	c.token = tinfo.Token
	c.tokenType = tinfo.Type
	c.expires = time.Now().Add(time.Duration(tinfo.Expires*10/9) * time.Second)
	return nil
}

func (c *Client) updateToken(ctx context.Context) error {
	if c.username == "" {
		return nil
	}
246
247
248
249
250
251
252
253
254


255
256

257
258
259

260
261
262
263
264
265
266
267
268
269



270
271
272

273









274
275
276
277
278
279


280
281
282
283
284
285
286
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301
302
303
304
305
306



307
308
309
310
311
312
313
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

403
404
405
406
407
408

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

466
467
468
469
470
471


472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

497
498
499
500


501
502

503
504
505
506
507
508
509
510
511
512
513
514













515
516

517
518
519
520
521
522
523
225
226
227
228
229
230
231


232
233
234

235
236
237

238
239
240
241
242
243


244
245
246
247
248
249
250
251
252
253

254
255
256
257
258
259
260
261
262
263
264
265
266


267
268
269
270
271
272
273
274

275
276
277

278
279
280
281
282
283
284
285
286
287
288
289
290
291



292
293
294
295
296
297
298
299



300




301
302



















303































































304






305





306



















































307






308
309



310
311
312
313
314
315
316
317
318
319
320
321
322

323
324
325

326
327
328

329
330
331


332
333
334

335
336
337










338
339
340
341
342
343
344
345
346
347
348
349
350


351
352
353
354
355
356
357
358







-
-
+
+

-
+


-
+





-
-



+
+
+



+
-
+
+
+
+
+
+
+
+
+




-
-
+
+






-



-



+










-
-
-
+
+
+





-
-
-
+
-
-
-
-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
-













-



-



-
+


-
-
+
+

-
+


-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+







	}
	if zid := api.ZettelID(b); zid.IsValid() {
		return zid, nil
	}
	return api.InvalidZID, err
}

// CreateZettelData creates a new zettel and returns its URL.
func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (api.ZettelID, error) {
// CreateZettelJSON creates a new zettel and returns its URL.
func (c *Client) CreateZettelJSON(ctx context.Context, data *api.ZettelDataJSON) (api.ZettelID, error) {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
	if err := encodeZettelData(&buf, data); err != nil {
		return api.InvalidZID, err
	}
	ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson)
	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)
	}
	dec := json.NewDecoder(resp.Body)
	var newZid api.ZidJSON
	err = dec.Decode(&newZid)
	if err != nil {
		return api.InvalidZID, err
	}
	if zid := newZid.ID; zid.IsValid() {
	return makeZettelID(obj)
		return zid, nil
	}
	return api.InvalidZID, err
}

func encodeZettelData(buf *bytes.Buffer, data *api.ZettelDataJSON) error {
	enc := json.NewEncoder(buf)
	enc.SetEscapeHTML(false)
	return enc.Encode(&data)
}

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

// QueryZettel returns a list of all Zettel.
func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) {
// ListZettel returns a list of all Zettel.
func (c *Client) ListZettel(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:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	lines := bytes.Split(data, bsLF)
	if len(lines[len(lines)-1]) == 0 {
		lines = lines[:len(lines)-1]
	}
	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)
// ListZettelJSON returns a list of zettel.
func (c *Client) ListZettelJSON(ctx context.Context, query string) (string, string, []api.ZidMetaJSON, error) {
	ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson).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 {
	if resp.StatusCode != http.StatusOK {
	case http.StatusOK:
	case http.StatusNoContent:
		return "", "", nil, nil
	default:
		return "", "", nil, statusToError(resp)
	}
	if err != nil {
		return "", "", nil, err
	}
	vals, err := sexp.ParseList(obj, "yppp")
	if err != nil {
		return "", "", nil, err
	}
	qVals, err := sexp.ParseList(vals[1], "ys")
	if err != nil {
		return "", "", nil, err
	}
	hVals, err := sexp.ParseList(vals[2], "ys")
	if err != nil {
		return "", "", nil, err
	}
	metaList, err := parseMetaList(vals[3].(*sx.Pair))
	return qVals[1].String(), hVals[1].String(), metaList, err
}

	dec := json.NewDecoder(resp.Body)
func parseMetaList(metaPair *sx.Pair) ([]api.ZidMetaRights, error) {
	if metaPair == nil {
		return nil, fmt.Errorf("no zettel list")
	}
	if errSym := sexp.CheckSymbol(metaPair.Car(), "list"); errSym != nil {
		return nil, errSym
	}
	var result []api.ZidMetaRights
	for node := metaPair.Cdr(); !sx.IsNil(node); {
		elem, isPair := sx.GetPair(node)
		if !isPair {
			return nil, fmt.Errorf("meta-list not a proper list: %v", metaPair.String())
		}
		node = elem.Cdr()
		vals, err := sexp.ParseList(elem.Car(), "yppp")
		if err != nil {
			return nil, err
		}

		if errSym := sexp.CheckSymbol(vals[0], "zettel"); errSym != nil {
			return nil, errSym
		}

		idVals, err := sexp.ParseList(vals[1], "yi")
		if err != nil {
			return nil, err
		}
		if errSym := sexp.CheckSymbol(idVals[0], "id"); errSym != nil {
			return nil, errSym
		}
		zid, err := makeZettelID(idVals[1])
		if err != nil {
			return nil, err
		}

		meta, err := sexp.ParseMeta(vals[2].(*sx.Pair))
		if err != nil {
			return nil, err
		}

		rights, err := sexp.ParseRights(vals[3])
		if err != nil {
			return nil, err
		}

		result = append(result, api.ZidMetaRights{
			ID:     zid,
			Meta:   meta,
			Rights: rights,
		})
	}
	return result, nil
}
func makeZettelID(obj sx.Object) (api.ZettelID, error) {
	val, isInt64 := obj.(sx.Int64)
	if !isInt64 || val <= 0 {
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	sVal := strconv.FormatInt(int64(val), 10)
	if len(sVal) < 14 {
		sVal = "00000000000000"[0:14-len(sVal)] + sVal
	}
	zid := api.ZettelID(sVal)
	var zl api.ZettelListJSON
	if !zid.IsValid() {
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	return zid, nil
}

	err = dec.Decode(&zl)
// QueryAggregate returns a aggregate as a result of a query.
// It is most often used in a query with an action, where the action is either
// a metadata key of type Word, WordSet, or TagSet.
func (c *Client) QueryAggregate(ctx context.Context, query string) (api.Aggregate, error) {
	lines, err := c.QueryZettel(ctx, query)
	if err != nil {
		return nil, err
	}
	if len(lines) == 0 {
		return nil, nil
	}
	agg := make(api.Aggregate, len(lines))
	for _, line := range lines {
		if fields := bytes.Fields(line); len(fields) > 1 {
			key := string(fields[0])
			for _, field := range fields[1:] {
				if zid := api.ZettelID(string(field)); zid.IsValid() {
					agg[key] = append(agg[key], zid)
				}
			}
		}
	}
	return agg, nil
}

// TagZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) TagZettel(ctx context.Context, tag string) (api.ZettelID, error) {
	return c.fetchTagOrRoleZettel(ctx, api.QueryKeyTag, tag)
}

// RoleZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) RoleZettel(ctx context.Context, role string) (api.ZettelID, error) {
	return c.fetchTagOrRoleZettel(ctx, api.QueryKeyRole, role)
}

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
	}

	switch resp.StatusCode {
	case http.StatusNotFound:
		return "", nil
		return "", "", nil, err
	case http.StatusFound:
		zid := api.ZettelID(data)
		if zid.IsValid() {
			return zid, nil
		}
		return api.InvalidZID, nil
	}
	return zl.Query, zl.Human, zl.List, nil
	default:
		return api.InvalidZID, statusToError(resp)
	}
}

// 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
	return io.ReadAll(resp.Body)
}

// GetZettelData returns a zettel as a struct of its parts.
func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) {
// GetZettelJSON returns a zettel as a JSON struct.
func (c *Client) GetZettelJSON(ctx context.Context, zid api.ZettelID) (*api.ZettelDataJSON, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson)
	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()
		if err2 == nil {
			return sexp.ParseZettel(obj)
		}
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.ZettelDataJSON
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
	}
	}
	return api.ZettelData{}, err
	return &out, nil
}

// GetParsedZettel return a parsed zettel in a defined encoding.
func (c *Client) GetParsedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) {
	return c.getZettelString(ctx, zid, enc, true)
}

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
378
379
380
381
382
383
384



385
386
387
388
389



390
391
392
393
394

395
396

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

412
413
414


415
416
417

418
419
420
421

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


493

494
495
496
497
498
499

500
501




502
503





504
505
506
507
508
509
510

511
512
513
514
515


516
517
518
519
520

521
522





523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539


540
541
542

543
544
545

546
547
548
549
550
551
552
553







-
-
-
+
+
+


-
-
-
+
+
+


-
+

-
+














-
+


-
-
+
+

-
+



-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-

-
+

+
+
+

-
+

-
-
-
-
+
+
-
-
-
-
-
+
+
+
+
+
+

-
+

+
+
+
-
-
+
+
+
+

-
+

-
-
-
-
-
+
















-
-
+
+

-
+


-
+







	case http.StatusNoContent:
	default:
		return nil, statusToError(resp)
	}
	return io.ReadAll(resp.Body)
}

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

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

func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sx.SymbolFactory) (sx.Object, error) {
func (c *Client) getSexpr(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sxpf.SymbolFactory) (sxpf.Object, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSz)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSexpr)
	if part != "" {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	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), sxreader.WithSymbolFactory(sf)).Read()
	return reader.MakeReader(bufio.NewReaderSize(resp.Body, 8), reader.WithSymbolFactory(sf)).Read()
}

// GetMetaData returns the metadata of a zettel.
func (c *Client) GetMetaData(ctx context.Context, zid api.ZettelID) (api.MetaRights, error) {
// GetMeta returns the metadata of a zettel.
func (c *Client) GetMeta(ctx context.Context, zid api.ZettelID) (api.ZettelMeta, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartMeta)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.MetaRights{}, err
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.MetaJSON
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
	}
	return out.Meta, nil
}

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

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

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

// GetZettelContext returns metadata of the given zettel and, more important,
// metadata of zettel that for the context of the first zettel.
func (c *Client) GetZettelContext(
	ctx context.Context, zid api.ZettelID, dir ContextDirection, cost, limit int) (
	*api.ZidMetaRelatedList, error,
) {
	ub := c.newURLBuilder('x').SetZid(zid)
	switch dir {
	case DirBackward:
		ub.AppendKVQuery(api.QueryKeyDir, api.DirBackward)
	case DirForward:
		ub.AppendKVQuery(api.QueryKeyDir, api.DirForward)
	}
	if cost > 0 {
		ub.AppendKVQuery(api.QueryKeyCost, strconv.Itoa(cost))
	}
	if limit > 0 {
		ub.AppendKVQuery(api.QueryKeyLimit, strconv.Itoa(limit))
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if resp.StatusCode != http.StatusOK {
		return api.MetaRights{}, statusToError(resp)
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.ZidMetaRelatedList
	err = dec.Decode(&out)
	if err != nil {
		return api.MetaRights{}, err
		return nil, err
	}
	vals, err := sexp.ParseList(obj, "ypp")
	if err != nil {
		return api.MetaRights{}, err
	}
	return &out, nil
}
	if errSym := sexp.CheckSymbol(vals[0], "list"); errSym != nil {
		return api.MetaRights{}, err
	}

	meta, err := sexp.ParseMeta(vals[1].(*sx.Pair))

// GetUnlinkedReferences returns connections to other zettel, embedded material, externals URLs.
func (c *Client) GetUnlinkedReferences(
	ctx context.Context, zid api.ZettelID, query url.Values) (*api.ZidMetaRelatedList, error) {
	ub := c.newQueryURLBuilder('u', query).SetZid(zid)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.MetaRights{}, err
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)

	rights, err := sexp.ParseRights(vals[2])
	}
	dec := json.NewDecoder(resp.Body)
	var out api.ZidMetaRelatedList
	err = dec.Decode(&out)
	if err != nil {
		return api.MetaRights{}, err
		return nil, err
	}

	return api.MetaRights{
		Meta:   meta,
		Rights: rights,
	}, nil
	return &out, 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 {
// UpdateZettelJSON updates an existing zettel.
func (c *Client) UpdateZettelJSON(ctx context.Context, zid api.ZettelID, data *api.ZettelDataJSON) error {
	var buf bytes.Buffer
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
	if err := encodeZettelData(&buf, data); err != nil {
		return err
	}
	ub := c.newURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub := c.newURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson)
	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)
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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+


-
+



-
+

-
-
-
+
+
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-

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

func (c *Client) newQueryURLBuilder(key byte, query url.Values) *api.URLBuilder {
	ub := c.newURLBuilder(key)
	for key, values := range query {
		if key == api.QueryKeyEncoding {
			continue
		}
		for _, val := range values {
			ub.AppendKVQuery(key, val)
		}
	}
	return ub
}

// QueryMapMeta returns a map of all metadata values with the given query action to the
// list of zettel IDs containing this value.
func (c *Client) QueryMapMeta(ctx context.Context, query string) (api.MapMeta, error) {
	err := c.updateToken(ctx)
	if err != nil {
		return nil, err
	}
	req, err := c.newRequest(ctx, http.MethodGet, c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingJson).AppendQuery(query), nil)
	if err != nil {
		return nil, err
	}
	resp, err := c.executeRequest(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var mlj api.MapListJSON
	err = dec.Decode(&mlj)
	if err != nil {
		return nil, err
	}
	return mlj.Map, nil
}

// GetVersionInfo returns version information..
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
// GetVersionJSON returns version information..
func (c *Client) GetVersionJSON(ctx context.Context) (api.VersionJSON, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.newURLBuilder('x'), nil, nil)
	if err != nil {
		return VersionInfo{}, err
		return api.VersionJSON{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return VersionInfo{}, statusToError(resp)
		return api.VersionJSON{}, statusToError(resp)
	}
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if err == nil {
	dec := json.NewDecoder(resp.Body)
	var version api.VersionJSON
	err = dec.Decode(&version)
		if vals, errVals := sexp.ParseList(obj, "iiiss"); errVals == nil {
			return VersionInfo{
				Major: int(vals[0].(sx.Int64)),
				Minor: int(vals[1].(sx.Int64)),
				Patch: int(vals[2].(sx.Int64)),
				Info:  vals[3].(sx.String).String(),
				Hash:  vals[4].(sx.String).String(),
			}, nil
	if err != nil {
		}
	}
	return VersionInfo{}, err
}

		return api.VersionJSON{}, err
	}
	return version, nil
// VersionInfo contains version information.
type VersionInfo struct {
	Major int
	Minor int
	Patch int
	Info  string
	Hash  string
}

Changes to client/client_test.go.

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
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







-
-
-
-
+
+
+
+




-
+



















-
+

-
-
+
+

-
+







import (
	"context"
	"flag"
	"net/http"
	"net/url"
	"testing"

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

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

func TestGetProtectedZettel(t *testing.T) {
	c := getClient()
	_, err := c.GetZettel(context.Background(), api.ZidStartupConfiguration, api.PartZettel)
	if err != nil {
		if cErr, ok := err.(*client.Error); ok && cErr.StatusCode == http.StatusForbidden {
			return
		} else {
			t.Error(err)
		}
		return
	}
}

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

Changes to go.mod.

1

2
3

4
5





1
2

3
4

5
6
7
8
-
+

-
+

-
+
+
+
+
module zettelstore.de/client.fossil
module zettelstore.de/c

go 1.21
go 1.20

require zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207
require (
	codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197
	codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475
)

Changes to go.sum.

1
2






1
2
3
4
-
-
+
+
+
+
zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207 h1:8ch54z0w53bps6a00NDofEqo3AJ1l7ITXyC3XyLmlY4=
zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207/go.mod h1:Uw3OLM1ufOM4Xe0G51mvkTDUv2okd+HyDBMx+0ZG7ME=
codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197 h1:6kX7TY25agLFlHNvByO1Jc3GrBA7mu7aOa8tCOniUew=
codeberg.org/t73fde/sxhtml v0.0.0-20230317170051-24321195e197/go.mod h1:Dp3EwBSsE3TvdPw9QZ4Wm25ZragluVT2OayRFRiq6jk=
codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475 h1:0OTzV3FYY/Y7YsaVaSzF4Wd17pXzdH6DaSvMeqteJc4=
codeberg.org/t73fde/sxpf v0.0.0-20230319111333-7de220f3b475/go.mod h1:iSbMygOmtRQYp8pryNKYzRuMibYDSR80smU2b6qm1bc=

Changes to maps/maps_test.go.

9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23







-
+







//-----------------------------------------------------------------------------

package maps_test

import (
	"testing"

	"zettelstore.de/client.fossil/maps"
	"zettelstore.de/c/maps"
)

func isSorted(seq []string) bool {
	for i := 1; i < len(seq); i++ {
		if seq[i] < seq[i-1] {
			return false
		}

Deleted sexp/sexp.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203











































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

// Package sexp contains helper function to work with s-expression in an alien
// environment.
package sexp

import (
	"errors"
	"fmt"
	"sort"

	"zettelstore.de/client.fossil/api"
	"zettelstore.de/sx.fossil"
)

// EncodeZettel transforms zettel data into a sx object.
func EncodeZettel(zettel api.ZettelData) sx.Object {
	sf := sx.MakeMappedFactory(1024)
	return sx.MakeList(
		sf.MustMake("zettel"),
		meta2sz(zettel.Meta, sf),
		sx.MakeList(sf.MustMake("rights"), sx.Int64(int64(zettel.Rights))),
		sx.MakeList(sf.MustMake("encoding"), sx.String(zettel.Encoding)),
		sx.MakeList(sf.MustMake("content"), sx.String(zettel.Content)),
	)
}

func ParseZettel(obj sx.Object) (api.ZettelData, error) {
	vals, err := ParseList(obj, "ypppp")
	if err != nil {
		return api.ZettelData{}, err
	}
	if errSym := CheckSymbol(vals[0], "zettel"); errSym != nil {
		return api.ZettelData{}, errSym
	}

	meta, err := ParseMeta(vals[1].(*sx.Pair))
	if err != nil {
		return api.ZettelData{}, err
	}

	rights, err := ParseRights(vals[2])
	if err != nil {
		return api.ZettelData{}, err
	}

	encVals, err := ParseList(vals[3], "ys")
	if err != nil {
		return api.ZettelData{}, err
	}
	if errSym := CheckSymbol(encVals[0], "encoding"); errSym != nil {
		return api.ZettelData{}, errSym
	}

	contentVals, err := ParseList(vals[4], "ys")
	if err != nil {
		return api.ZettelData{}, err
	}
	if errSym := CheckSymbol(contentVals[0], "content"); errSym != nil {
		return api.ZettelData{}, errSym
	}

	return api.ZettelData{
		Meta:     meta,
		Rights:   rights,
		Encoding: encVals[1].(sx.String).String(),
		Content:  contentVals[1].(sx.String).String(),
	}, nil
}

// EncodeMetaRights translates metadata/rights into a sx object.
func EncodeMetaRights(mr api.MetaRights) *sx.Pair {
	sf := sx.MakeMappedFactory(1024)
	return sx.MakeList(
		sf.MustMake("list"),
		meta2sz(mr.Meta, sf),
		sx.MakeList(sf.MustMake("rights"), sx.Int64(int64(mr.Rights))),
	)
}

func meta2sz(m api.ZettelMeta, sf sx.SymbolFactory) sx.Object {
	result := sx.Nil().Cons(sf.MustMake("meta"))
	curr := result
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		val := sx.MakeList(sf.MustMake(k), sx.String(m[k]))
		curr = curr.AppendBang(val)
	}
	return result
}

// ParseMeta translates the given list to metadata.
func ParseMeta(pair *sx.Pair) (api.ZettelMeta, error) {
	if err := CheckSymbol(pair.Car(), "meta"); err != nil {
		return nil, err
	}
	res := api.ZettelMeta{}
	for node := pair.Tail(); node != nil; node = node.Tail() {
		mVals, err := ParseList(node.Car(), "ys")
		if err != nil {
			return nil, err
		}
		res[mVals[0].(*sx.Symbol).Name()] = mVals[1].(sx.String).String()
	}
	return res, nil
}

// ParseRights returns the rights values of the given object.
func ParseRights(obj sx.Object) (api.ZettelRights, error) {
	rVals, err := ParseList(obj, "yi")
	if err != nil {
		return api.ZettelMaxRight, err
	}
	if errSym := CheckSymbol(rVals[0], "rights"); errSym != nil {
		return api.ZettelMaxRight, errSym
	}
	i64 := int64(rVals[1].(sx.Int64))
	if i64 < 0 && i64 >= int64(api.ZettelMaxRight) {
		return api.ZettelMaxRight, fmt.Errorf("invalid zettel right value: %v", i64)
	}
	return api.ZettelRights(i64), nil
}

// ParseList parses the given object as a proper list, based on a type specification.
func ParseList(obj sx.Object, spec string) ([]sx.Object, error) {
	pair, isPair := sx.GetPair(obj)
	if !isPair {
		return nil, fmt.Errorf("not a list: %T/%v", obj, obj)
	}
	if pair == nil {
		if spec == "" {
			return nil, nil
		}
		return nil, ErrElementsMissing
	}

	result := make([]sx.Object, 0, len(spec))
	node, i := pair, 0
	for ; node != nil; i++ {
		if i >= len(spec) {
			return nil, ErrNoSpec
		}
		var val sx.Object
		var ok bool
		car := node.Car()
		switch spec[i] {
		case 'b':
			val, ok = sx.MakeBoolean(!sx.IsNil(car)), true
		case 'i':
			val, ok = car.(sx.Int64)
		case 'o':
			val, ok = car, true
		case 'p':
			val, ok = sx.GetPair(car)
		case 's':
			val, ok = sx.GetString(car)
		case 'y':
			val, ok = sx.GetSymbol(car)
		default:
			return nil, fmt.Errorf("unknown spec '%c'", spec[i])
		}
		if !ok {
			return nil, fmt.Errorf("does not match spec '%v': %v", spec[i], car)
		}
		result = append(result, val)
		next, isNextPair := sx.GetPair(node.Cdr())
		if !isNextPair {
			return nil, sx.ErrImproper{Pair: pair}
		}
		node = next
	}
	if i < len(spec) {
		return nil, ErrElementsMissing
	}
	return result, nil
}

var ErrElementsMissing = errors.New("spec contains more data")
var ErrNoSpec = errors.New("no spec for elements")

// CheckSymbol ensures that the given object is a symbol with the given name.
func CheckSymbol(obj sx.Object, name string) error {
	sym, isSymbol := sx.GetSymbol(obj)
	if !isSymbol {
		return fmt.Errorf("object %v/%T is not a symbol", obj, obj)
	}
	if got := sym.Name(); got != name {
		return fmt.Errorf("symbol %q expected, but got: %q", name, got)
	}
	return nil
}

Deleted sexp/sexp_test.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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






















































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sexp_test

import (
	"testing"

	"zettelstore.de/client.fossil/sexp"
	"zettelstore.de/sx.fossil"
)

func TestParseObject(t *testing.T) {
	if elems, err := sexp.ParseList(sx.String("a"), "s"); err == nil {
		t.Error("expected an error, but got: ", elems)
	}
	if elems, err := sexp.ParseList(sx.Nil(), ""); err != nil {
		t.Error(err)
	} else if len(elems) != 0 {
		t.Error("Must be empty, but got:", elems)
	}
	if elems, err := sexp.ParseList(sx.Nil(), "b"); err == nil {
		t.Error("expected error, but got: ", elems)
	}

	if elems, err := sexp.ParseList(sx.MakeList(sx.String("a")), "ss"); err == nil {
		t.Error("expected error, but got: ", elems)
	}
	if elems, err := sexp.ParseList(sx.MakeList(sx.String("a")), ""); err == nil {
		t.Error("expected error, but got: ", elems)
	}
	if _, err := sexp.ParseList(sx.MakeList(sx.String("a")), "b"); err != nil {
		t.Error("expected [1], but got error: ", err)
	}
	if elems, err := sexp.ParseList(sx.Cons(sx.Nil(), sx.String("a")), "ps"); err == nil {
		t.Error("expected error, but got: ", elems)
	}

	if elems, err := sexp.ParseList(sx.MakeList(sx.String("a")), "s"); err != nil {
		t.Error(err)
	} else if len(elems) != 1 {
		t.Error("length == 1, but got: ", elems)
	} else {
		_ = elems[0].(sx.String)
	}

}

Added sexpr/const.go.










































































































































































































































































































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

package sexpr

import "codeberg.org/t73fde/sxpf"

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

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

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

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

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

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

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

	// Constant symbols for reference states.

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

	// Symbols for metadata types

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

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

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

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

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

Added sexpr/const_test.go.



























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

package sexpr_test

import (
	"testing"

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

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

Added sexpr/sexpr.go.































































































































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

package sexpr

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

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

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

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

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

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

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

Changes to shtml/shtml.go.

13
14
15
16
17
18
19
20
21
22
23
24
25








26
27
28

29
30
31



32
33

34
35
36

37
38
39
40


41
42
43

44
45
46
47
48







49
50
51
52


53
54

55
56

57

58
59
60
61
62
63
64
65
66
67
68
69

70
71
72

73
74
75
76
77
78



79
80
81

82
83
84
85
86
87

88
89
90







91
92
93


94
95

96
97

98
99
100
101
102

103
104
105
106

107
108
109
110





111
112
113
114
115



















116




117
118
119




120



121
122


123
124

125
126
127
128
129
130
131


132
133
134
135



136
137

138

139
140
141
142
143
144
145


146
147
148

149
150
151

152
153

154


155
156
157
158
159





160
161
162
163
164
165
166
167







168
169

170
171
172

173
174

175
176
177
178
179
180






181
182

183
184
185

186
187
188

189
190
191

192
193
194
195
196
197
198
199



200
201
202
203



204
205
206

207
208
209
210
211



212
213

214
215
216
217
218
219
220
221

222
223
224
225
226
227

228
229
230
231
232

233
234
235

236
237
238

239
240
241
242



243
244
245
246
247

248
249
250

251
252
253
254




255
256
257
258

259
260
261
262


263
264
265
266

267
268
269
270



271
272
273
274
275
276
277
278
279








280
281

282
283
284

285
286

287
288
289
290
291
292
293

294
295

296
297
298
299



300
301
302
303
304
305
306
307
308
309

310
311
312








313
314
315
316

317
318
319


320
321
322


323
324

325
326
327
328
329
330





331
332
333

334
335

336
337

338
339

340
341
342
343
344
345





346
347
348

349
350
351
352
353
354
355





356
357

358
359
360
361



362
363


364
365
366

367
368

369
370
371
372


373
374
375
376

377

378
379

380
381

382
383
384
385



386
387
388
389
390
391
392
393
394




395
396
397
398
399



400
401
402


403
404
405
406

407
408
409
410
411
412
413
414

415
416

417
418
419
420
421




422
423
424
425
426



427
428
429


430
431
432
433
434
435





436
437
438

439
440


441
442
443
444



445
446
447
448



449
450

451
452

453
454
455
456





457

458
459
460
461




462
463
464
465


466
467
468
469
470




471
472
473
474



475
476

477
478
479
480

481
482
483
484
485
486
487




488
489
490
491



492
493
494
495
496
497
498
499

500
501
502
503



504
505
506
507


508
509
510

511
512
513



514
515

516
517

518
519
520
521
522
523



524
525
526
527
528
529
530
531

532
533

534
535
536
537



538
539
540
541
542



543
544
545
546
547
548
549

550
551
552


553
554
555
556


557
558
559
560
561
562
563
564






565
566

567
568
569
570



571
572
573


574
575
576
577
578


579
580
581

582
583

584
585
586


587
588
589
590


591
592
593
594
595
596





597
598
599
600


601
602
603
604
605




606
607
608

609
610

611
612
613


614
615
616
617


618
619
620
621
622




623
624
625
626
627




628
629
630
631
632




633
634
635
636
637
638


639
640
641
642
643
644





645
646

647
648
649
650




651
652
653
654
655


656
657
658
659
660
661

662
663
664
665
666
667
668





669
670
671
672
673


674
675
676
677
678

679
680
681
682
683
684
685
686
687
688











689

690

691
692
693
694
695
696



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715















716
717
718
719
720



721
722
723
724
725
726





727
728
729


730
731
732
733
734




735
736

737
738
739


740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762


763
764
765
766
767
768


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
847

848
849
850

851
852
853
854
855

856
857

858
859
860

861
862
863
864
865
866
867
868
869
870



871
872
873
874
875

876
877

878
879
880
881

882
883

884
885

886
887

888
889

890
891

892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910











911
912
913

914
915

916
917
918
919

920
921

922
923


924
925
926
927



928
929
930
931
932
933







934
935
936

937
938
939

940
941

942
943
944
945

946
947
948


949
950
951
952
953
954
955








956


957
958
959



960
961
962
963
964
965
966
967
968




969
970
971
972



973
974

975
976
977
978
979

980

981

982
983

984
985
986
987




988
989

990
991

992
993
994
995
996



997
998
999

1000
1001
1002





1003
1004

1005
1006
1007


1008
1009


1010
1011

1012
1013
1014
1015



1016
1017
1018

1019
1020


1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
13
14
15
16
17
18
19






20
21
22
23
24
25
26
27
28
29

30
31


32
33
34
35
36
37
38


39




40
41



42





43
44
45
46
47
48
49
50
51


52
53
54

55
56

57
58
59
60
61


62
63


64

65

66



67






68
69
70
71
72

73
74



75

76
77
78

79
80
81
82
83
84
85
86


87
88
89

90
91

92
93
94
95
96

97
98
99
100

101
102
103
104
105
106
107
108
109
110





111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134



135
136
137
138
139
140
141
142


143
144
145

146







147
148
149



150
151
152
153

154
155
156
157

158
159
160


161
162
163
164

165



166


167
168
169
170





171
172
173
174
175
176







177
178
179
180
181
182
183
184

185
186
187

188
189
190
191
192
193
194



195
196
197
198
199
200


201



202



203



204








205
206
207




208
209
210



211





212
213
214


215








216
217





218





219



220



221




222
223
224





225



226




227
228
229
230




231




232
233




234
235



236
237
238
239








240
241
242
243
244
245
246
247


248
249


250
251

252
253
254
255
256
257
258

259
260

261
262



263
264
265






266
267


268



269
270
271
272
273
274
275
276




277
278


279
280
281


282
283
284

285
286





287
288
289
290
291
292
293

294
295

296
297

298
299

300
301





302
303
304
305
306
307
308

309
310






311
312
313
314
315
316

317
318



319
320
321


322
323
324
325

326
327

328
329
330


331
332
333
334
335
336
337

338
339

340
341

342
343



344
345
346
347
348
349
350
351




352
353
354
355
356
357



358
359
360
361


362
363
364
365
366

367
368
369
370
371
372
373
374

375
376

377
378




379
380
381
382
383




384
385
386
387


388
389






390
391
392
393
394

395
396
397


398
399
400



401
402
403
404



405
406
407
408

409
410

411
412



413
414
415
416
417
418
419




420
421
422
423
424
425


426
427
428




429
430
431
432
433



434
435
436
437

438
439
440
441

442
443
444
445




446
447
448
449
450



451
452
453
454
455
456
457
458
459
460

461




462
463
464
465
466


467
468
469
470
471
472



473
474
475
476

477
478

479
480
481
482



483
484
485


486
487
488
489
490

491
492

493
494
495


496
497
498
499
500



501
502
503
504
505
506
507
508
509

510
511


512
513
514
515


516
517
518
519






520
521
522
523
524
525
526

527
528



529
530
531
532


533
534





535
536

537

538
539

540
541


542
543




544
545
546





547
548
549
550
551




552
553
554




555
556
557
558



559
560

561
562


563
564




565
566
567
568



569
570
571
572
573




574
575
576
577
578




579
580
581
582
583
584
585
586


587
588






589
590
591
592
593


594
595



596
597
598
599





600
601
602
603
604
605
606

607







608
609
610
611
612
613
614
615


616
617
618
619
620
621

622
623









624
625
626
627
628
629
630
631
632
633
634
635
636

637






638
639
640
641
642
643
















644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660



661
662
663






664
665
666
667
668
669


670
671





672
673
674
675


676
677


678
679























680
681






682
683






684
685
686
687
688
689
690
691
692
693
694
695


696


697








698
699





700


701
702
703
704
705
706

707




708













709
710
711
712
713
714
715
716
717
718
719
720
721
722



723
















724
725
726


727
728
729
730

731
732
733
734
735

736
737

738
739
740

741
742
743
744
745

746
747

748
749
750
751
752
753
754
755
756
757
758
759



760
761
762
763
764
765
766

767
768

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

847
848

849




850
851
852
853
854

855
856

857
858




859
860
861



862



863
864
865
866
867


868



869
870


871
872


873




874
875
876



877


878
879



880
881
882
883
884
885
886







-
-
-
-
-
-
+
+
+
+
+
+
+
+


-
+

-
-
+
+
+


+

-
-
+
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
+
+
+
+
+
+
+


-
-
+
+

-
+

-
+

+


-
-


-
-

-

-
+
-
-
-
+
-
-
-
-
-
-
+
+
+


-
+

-
-
-

-
+


-
+
+
+
+
+
+
+

-
-
+
+

-
+

-
+




-
+



-
+




+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
-
-
-
+
+
+
+

+
+
+
-
-
+
+

-
+
-
-
-
-
-
-
-
+
+

-
-
-
+
+
+

-
+

+

-



-
-
+
+


-
+
-
-
-
+
-
-
+

+
+
-
-
-
-
-
+
+
+
+
+

-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
+


-
+


+



-
-
-
+
+
+
+
+
+
-
-
+
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
+
+
+
-
-
-
+
-
-
-
-
-
+
+
+
-
-
+
-
-
-
-
-
-
-
-
+

-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
+
-
-
-
+
-
-
-
-
+
+
+
+
-
-
-
-
+
-
-
-
-
+
+
-
-
-
-
+

-
-
-
+
+
+

-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+

-
-
+

-
+






-
+

-
+

-
-
-
+
+
+
-
-
-
-
-
-


-
-
+
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+

-
-
+
+

-
-
+
+

-
+

-
-
-
-
-
+
+
+
+
+


-
+

-
+

-
+

-
+

-
-
-
-
-
+
+
+
+
+


-
+

-
-
-
-
-
-
+
+
+
+
+

-
+

-
-
-
+
+
+
-
-
+
+


-
+

-
+


-
-
+
+




+
-
+

-
+

-
+

-
-
-
+
+
+





-
-
-
-
+
+
+
+


-
-
-
+
+
+

-
-
+
+



-
+







-
+

-
+

-
-
-
-
+
+
+
+

-
-
-
-
+
+
+

-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-


+
-
-
+
+

-
-
-
+
+
+

-
-
-
+
+
+

-
+

-
+

-
-
-
+
+
+
+
+

+
-
-
-
-
+
+
+
+


-
-
+
+

-
-
-
-
+
+
+
+

-
-
-
+
+
+

-
+



-
+



-
-
-
-
+
+
+
+

-
-
-
+
+
+







-
+
-
-
-
-
+
+
+


-
-
+
+



+
-
-
-
+
+
+

-
+

-
+



-
-
-
+
+
+
-
-





-
+

-
+


-
-
+
+
+


-
-
-
+
+
+






-
+

-
-
+
+


-
-
+
+


-
-
-
-
-
-
+
+
+
+
+
+

-
+

-
-
-
+
+
+

-
-
+
+
-
-
-
-
-
+
+
-

-
+

-
+

-
-
+
+
-
-
-
-
+
+

-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+

-
-
-
-
+
+
+
+
-
-
-
+

-
+

-
-
+
+
-
-
-
-
+
+


-
-
-
+
+
+
+

-
-
-
-
+
+
+
+

-
-
-
-
+
+
+
+




-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
+

-
-
-
+
+
+
+
-
-
-
-
-
+
+





-
+
-
-
-
-
-
-
-
+
+
+
+
+



-
-
+
+




-
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

+
-
+
-
-
-
-
-
-
+
+
+



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+

-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
+

-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

-
-
+
-
-

-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
+
-
-
+
+
+
+

+
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+


-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

+
+
-
-
+
+
+

-
+




-
+

-
+


-
+




-
+

-
+



+







-
-
-
+
+
+




-
+

-
+



-
+

-
+

-
+

-
+

-
+

-
+


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
-
-
+
-
-
-
-
+
-
-
+
-
-
+
+
-
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+

-
-
+

-
-
+
-
-
+
-
-
-
-
+

-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+

+
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
-
-
+
-
-
-
-
-
+

+
-
+

-
+
-
-
-
-
+
+
+
+

-
+

-
+

-
-
-
-
+
+
+
-
-
-
+
-
-
-
+
+
+
+
+
-
-
+
-
-
-
+
+
-
-
+
+
-
-
+
-
-
-
-
+
+
+
-
-
-
+
-
-
+
+
-
-
-








import (
	"fmt"
	"net/url"
	"strconv"
	"strings"

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

// Evaluator will transform a s-expression that encodes the zettel AST into an s-expression
// Transformer will transform a s-expression that encodes the zettel AST into an s-expression
// that represents HTML.
type Evaluator struct {
	sf            sx.SymbolFactory
type Transformer struct {
	sf            sxpf.SymbolFactory
	rebinder      RebindProc
	headingOffset int64
	unique        string
	endnotes      []endnoteInfo
	noLinks       bool // true iff output must not include links

	symAttr     *sx.Symbol
	symAttr       *sxpf.Symbol
	symList     *sx.Symbol
	symNoEscape *sx.Symbol
	symClass    *sx.Symbol
	symMeta     *sx.Symbol
	symClass      *sxpf.Symbol
	symMeta       *sxpf.Symbol
	symP        *sx.Symbol
	symLI       *sx.Symbol
	symA        *sx.Symbol
	symA          *sxpf.Symbol
	symHREF     *sx.Symbol
	symSpan     *sx.Symbol

	fns     map[string]EvalFn
	minArgs map[string]int
	symSpan       *sxpf.Symbol
}

type endnoteInfo struct {
	noteAST *sxpf.List // Endnote as AST
	noteHx  *sxpf.List // Endnote as SxHTML
	attrs   *sxpf.List // attrs a-list
}

// NewEvaluator creates a new Evaluator object.
func NewEvaluator(headingOffset int, sf sx.SymbolFactory) *Evaluator {
// NewTransformer creates a new transformer object.
func NewTransformer(headingOffset int, sf sxpf.SymbolFactory) *Transformer {
	if sf == nil {
		sf = sx.MakeMappedFactory(128)
		sf = sxpf.MakeMappedFactory()
	}
	ev := &Evaluator{
	return &Transformer{
		sf:            sf,
		rebinder:      nil,
		headingOffset: int64(headingOffset),
		symAttr:       sf.MustMake(sxhtml.NameSymAttr),
		symList:       sf.MustMake(sxhtml.NameSymList),
		symNoEscape:   sf.MustMake(sxhtml.NameSymNoEscape),
		symClass:      sf.MustMake("class"),
		symMeta:       sf.MustMake("meta"),
		symP:          sf.MustMake("p"),
		symLI:         sf.MustMake("li"),
		symA:          sf.MustMake("a"),
		symHREF:       sf.MustMake("href"),
		symSpan:       sf.MustMake("span"),

	}
		fns:     make(map[string]EvalFn, 128),
		minArgs: make(map[string]int, 128),
	}
}
	ev.bindCommon()
	ev.bindMetadata()
	ev.bindBlocks()
	ev.bindInlines()
	return ev
}

// SymbolFactory returns the symbol factory to create HTML symbols.
func (tr *Transformer) SymbolFactory() sxpf.SymbolFactory { return tr.sf }

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

// SymbolFactory returns the symbol factory of this evaluator.
func (ev *Evaluator) SymbolFactory() sx.SymbolFactory { return ev.sf }

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

// Make a new HTML symbol.
func (tr *Evaluator) Make(s string) *sx.Symbol { return tr.sf.MustMake(s) }
func (tr *Transformer) Make(s string) *sxpf.Symbol { return tr.sf.MustMake(s) }

// RebindProc is a procedure which is called every time before a tranformation takes place.
type RebindProc func(*TransformEnv)

// SetRebinder sets the rebinder procedure.
func (tr *Transformer) SetRebinder(rb RebindProc) { tr.rebinder = rb }

// EvaluateAttrbute transforms the given attributes into a HTML s-expression.
func (tr *Evaluator) EvaluateAttrbute(a attrs.Attributes) *sx.Pair {
// TransformAttrbute transforms the given attributes into a HTML s-expression.
func (tr *Transformer) TransformAttrbute(a attrs.Attributes) *sxpf.List {
	if len(a) == 0 {
		return nil
		return sxpf.Nil()
	}
	plist := sx.Nil()
	plist := sxpf.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(tr.Make(key), sx.String(a[key])))
			plist = plist.Cons(sxpf.Cons(tr.Make(key), sxpf.MakeString(a[key])))
		}
	}
	if plist == nil {
		return nil
		return sxpf.Nil()
	}
	return plist.Cons(tr.symAttr)
}

// TransformMeta creates a HTML meta s-expression
func (tr *Transformer) TransformMeta(a attrs.Attributes) *sxpf.List {
	return sxpf.Nil().Cons(tr.TransformAttrbute(a)).Cons(tr.symMeta)
}

// Evaluate a metadata s-expression into a list of HTML s-expressions.
func (ev *Evaluator) Evaluate(lst *sx.Pair, env *Environment) (*sx.Pair, error) {
	result := ev.eval(lst, env)
	if err := env.err; err != nil {
		return nil, err
// Transform an AST s-expression into a list of HTML s-expressions.
func (tr *Transformer) Transform(lst *sxpf.List) (*sxpf.List, error) {
	astSF := sxpf.FindSymbolFactory(lst)
	if astSF != nil {
		if astSF == tr.sf {
			panic("Invalid AST SymbolFactory")
		}
	} else {
		astSF = sxpf.MakeMappedFactory()
	}
	astEnv := sxpf.MakeRootEnvironment()
	engine := eval.MakeEngine(astSF, astEnv, eval.MakeDefaultParser(), eval.MakeSimpleExecutor())
	quote.InstallQuote(engine, sexpr.NameSymQuote, nil, 0)
	te := TransformEnv{
		tr:      tr,
		astSF:   astSF,
		astEnv:  astEnv,
		err:     nil,
		textEnc: text.NewEncoder(astSF),
	}
	te.initialize()
	if rb := tr.rebinder; rb != nil {
		rb(&te)
	}
	pair, isPair := sx.GetPair(result)
	if !isPair {
		return nil, fmt.Errorf("evaluation does not result in a pair, but %T/%v", result, result)

	val, err := engine.Eval(te.astEnv, lst)
	if err != nil {
		return sxpf.Nil(), err
	}
	res, ok := val.(*sxpf.List)
	if !ok {
		panic("Result is not a list")

	for i := 0; i < len(env.endnotes); i++ {
	}
	for i := 0; i < len(tr.endnotes); i++ {
		// May extend tr.endnotes

		val, err = engine.Eval(te.astEnv, tr.endnotes[i].noteAST)
		if env.endnotes[i].noteHx != nil {
			continue
		}

		objHx := ev.eval(env.endnotes[i].noteAST, env)
		if env.err != nil {
			break
		if err != nil {
			return res, err
		}
		noteHx, isHx := sx.GetPair(objHx)
		if !isHx {
			return nil, fmt.Errorf("endnote evaluation does not result in pair, but %T/%v", objHx, objHx)
		en, ok2 := val.(*sxpf.List)
		if !ok2 {
			panic("Endnote is not a list")
		}
		env.endnotes[i].noteHx = noteHx
		tr.endnotes[i].noteHx = en
	}
	return res, err

	return pair, 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 {
func (tr *Transformer) Endnotes() *sxpf.List {
	if len(tr.endnotes) == 0 {
		return nil
	}

	result := sxpf.Nil().Cons(tr.Make("ol"))
	result := sx.Nil().Cons(ev.Make("ol"))
	symValue, symId, symRole := ev.Make("value"), ev.Make("id"), ev.Make("role")

	currResult := result.AppendBang(sxpf.Nil().Cons(sxpf.Cons(tr.symClass, sxpf.MakeString("zs-endnotes"))).Cons(tr.symAttr))
	currResult := result.AppendBang(sx.Nil().Cons(sx.Cons(ev.symClass, sx.String("zs-endnotes"))).Cons(ev.symAttr))
	for i, fni := range env.endnotes {
	for i, fni := range tr.endnotes {
		noteNum := strconv.Itoa(i + 1)
		noteID := tr.unique + noteNum

		attrs := fni.attrs.Cons(sx.Cons(ev.symClass, sx.String("zs-endnote"))).
			Cons(sx.Cons(symValue, sx.String(noteNum))).
			Cons(sx.Cons(symId, sx.String("fn:"+fni.noteID))).
			Cons(sx.Cons(symRole, sx.String("doc-endnote"))).
			Cons(ev.symAttr)
		attrs := fni.attrs.Cons(sxpf.Cons(tr.symClass, sxpf.MakeString("zs-endnote"))).
			Cons(sxpf.Cons(tr.Make("value"), sxpf.MakeString(noteNum))).
			Cons(sxpf.Cons(tr.Make("id"), sxpf.MakeString("fn:"+noteID))).
			Cons(sxpf.Cons(tr.Make("role"), sxpf.MakeString("doc-endnote"))).
			Cons(tr.symAttr)

		backref := sx.Nil().Cons(sx.String("\u21a9\ufe0e")).
			Cons(sx.Nil().
				Cons(sx.Cons(ev.symClass, sx.String("zs-endnote-backref"))).
				Cons(sx.Cons(ev.symHREF, sx.String("#fnref:"+fni.noteID))).
				Cons(sx.Cons(symRole, sx.String("doc-backlink"))).
				Cons(ev.symAttr)).
			Cons(ev.symA)
		backref := sxpf.Nil().Cons(sxpf.MakeString("\u21a9\ufe0e")).
			Cons(sxpf.Nil().
				Cons(sxpf.Cons(tr.symClass, sxpf.MakeString("zs-endnote-backref"))).
				Cons(sxpf.Cons(tr.Make("href"), sxpf.MakeString("#fnref:"+noteID))).
				Cons(sxpf.Cons(tr.Make("role"), sxpf.MakeString("doc-backlink"))).
				Cons(tr.symAttr)).
			Cons(tr.symA)

		li := sx.Nil().Cons(ev.symLI)
		li := sxpf.Nil().Cons(tr.Make("li"))
		li.AppendBang(attrs).
			ExtendBang(fni.noteHx).
			AppendBang(sx.String(" ")).AppendBang(backref)
			AppendBang(sxpf.MakeString(" ")).AppendBang(backref)
		currResult = currResult.AppendBang(li)
	}
	tr.endnotes = nil
	return result
}

// Environment where sz objects are evaluated to shtml objects
type Environment struct {
	err          error
// TransformEnv is the environment where the actual transformation takes places.
type TransformEnv struct {
	tr          *Transformer
	astSF       sxpf.SymbolFactory
	astEnv      sxpf.Environment
	err         error
	langStack    []string
	endnotes     []endnoteInfo
	textEnc     *text.Encoder
	quoteNesting uint
}
type endnoteInfo struct {
	symNoEscape *sxpf.Symbol
	noteID  string    // link id
	noteAST sx.Object // Endnote as AST
	attrs   *sx.Pair  // attrs a-list
	symAttr     *sxpf.Symbol
	noteHx  *sx.Pair  // Endnote as SxHTML
}

	symMeta     *sxpf.Symbol
// 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,
	symA        *sxpf.Symbol
	symSpan     *sxpf.Symbol
	symP        *sxpf.Symbol
		quoteNesting: 0,
	}
}

}

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

	te.symNoEscape = te.Make(sxhtml.NameSymNoEscape)
// Reset the environment.
func (env *Environment) Reset() {
	env.langStack = env.langStack[0:1]
	env.endnotes = nil
	env.quoteNesting = 0
	te.symAttr = te.tr.symAttr
	te.symMeta = te.tr.symMeta
	te.symA = te.tr.symA
}

	te.symSpan = te.tr.symSpan
// 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())
	}
}
	te.symP = te.Make("p")

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

	te.bind(sexpr.NameSymList, 0, listArgs)
// getLanguage returns the current language
func (env *Environment) getLanguage() string {
	return env.langStack[len(env.langStack)-1]
}

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

	te.bindBlocks()
func (ev *Evaluator) bind(name string, minArgs int, fn EvalFn) {
	ev.fns[name] = fn
	if minArgs > 0 {
	te.bindInlines()
		ev.minArgs[name] = minArgs
	}
}

}

func listArgs(args *sxpf.List) sxpf.Object { return args }
// ResolveBinding returns the function bound to the given name.
func (ev *Evaluator) ResolveBinding(name string) EvalFn {
	if fn, found := ev.fns[name]; found {
		return fn
	}

	return nil
}

func (te *TransformEnv) bindMetadata() {
// Rebind overwrites a binding, but leaves the minimum number of arguments intact.
func (ev *Evaluator) Rebind(name string, fn EvalFn) {
	if _, found := ev.fns[name]; !found {
		panic(name)
	te.bind(sexpr.NameSymMeta, 0, listArgs)
	te.bind(sexpr.NameSymTypeZettelmarkup, 2, func(args *sxpf.List) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getString(args).String()).
	}
	ev.fns[name] = fn
}

			Set("content", te.textEnc.Encode(te.getList(args.Tail())))
func (ev *Evaluator) bindCommon() {
	ev.bind(sx.ListName, 0, ev.evalList)
	ev.bind("quote", 1, func(args []sx.Object, _ *Environment) sx.Object { return args[0] })
}
		return te.transformMeta(a)
	})

func (ev *Evaluator) bindMetadata() {
	ev.bind(sz.NameSymMeta, 0, ev.evalList)
	evalMetaString := func(args []sx.Object, env *Environment) sx.Object {
	metaString := func(args *sxpf.List) sxpf.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).Name()).
			Set("content", getString(args[1], env).String())
		return ev.EvaluateMeta(a)
			Set("name", te.getString(args).String()).
			Set("content", te.getString(args.Tail()).String())
		return te.transformMeta(a)
	}
	ev.bind(sz.NameSymTypeCredential, 2, evalMetaString)
	ev.bind(sz.NameSymTypeEmpty, 2, evalMetaString)
	ev.bind(sz.NameSymTypeID, 2, evalMetaString)
	ev.bind(sz.NameSymTypeNumber, 2, evalMetaString)
	ev.bind(sz.NameSymTypeString, 2, evalMetaString)
	ev.bind(sz.NameSymTypeTimestamp, 2, evalMetaString)
	ev.bind(sz.NameSymTypeURL, 2, evalMetaString)
	ev.bind(sz.NameSymTypeWord, 2, evalMetaString)
	te.bind(sexpr.NameSymTypeCredential, 2, metaString)
	te.bind(sexpr.NameSymTypeEmpty, 2, metaString)
	te.bind(sexpr.NameSymTypeID, 2, metaString)
	te.bind(sexpr.NameSymTypeNumber, 2, metaString)
	te.bind(sexpr.NameSymTypeString, 2, metaString)
	te.bind(sexpr.NameSymTypeTimestamp, 2, metaString)
	te.bind(sexpr.NameSymTypeURL, 2, metaString)
	te.bind(sexpr.NameSymTypeWord, 2, metaString)

	evalMetaSet := func(args []sx.Object, env *Environment) sx.Object {
	metaSet := func(args *sxpf.List) sxpf.Object {
		var sb strings.Builder
		lst := ev.eval(args[1], env)
		for elem := getList(lst, env); elem != nil; elem = elem.Tail() {
		for elem := te.getList(args.Tail()); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(getString(elem.Car(), env).String())
			sb.WriteString(te.getString(elem).String())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).Name()).
			Set("name", te.getString(args).String()).
			Set("content", s)
		return ev.EvaluateMeta(a)
		return te.transformMeta(a)
	}
	ev.bind(sz.NameSymTypeIDSet, 2, evalMetaSet)
	ev.bind(sz.NameSymTypeTagSet, 2, evalMetaSet)
	ev.bind(sz.NameSymTypeWordSet, 2, evalMetaSet)
	te.bind(sexpr.NameSymTypeIDSet, 2, metaSet)
	te.bind(sexpr.NameSymTypeTagSet, 2, metaSet)
	te.bind(sexpr.NameSymTypeWordSet, 2, metaSet)
	ev.bind(sz.NameSymTypeZettelmarkup, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).String()).
			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 {
func (te *TransformEnv) bindBlocks() {
	return sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(ev.symMeta)
}

	te.bind(sexpr.NameSymBlock, 0, listArgs)
	te.bind(sexpr.NameSymPara, 0, func(args *sxpf.List) sxpf.Object {
		for ; args != nil; args = args.Tail() {
			lst, ok := sxpf.GetList(args.Car())
			if !ok || lst != nil {
				break
			}
		}
func (ev *Evaluator) bindBlocks() {
	ev.bind(sz.NameSymBlock, 0, ev.evalList)
	ev.bind(sz.NameSymPara, 0, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalSlice(args, env).Cons(ev.symP)
		return args.Cons(te.symP)
	})
	ev.bind(sz.NameSymHeading, 5, func(args []sx.Object, env *Environment) sx.Object {
		nLevel := getInt64(args[0], env)
	te.bind(sexpr.NameSymHeading, 5, func(args *sxpf.List) sxpf.Object {
		nLevel := te.getInt64(args)
		if nLevel <= 0 {
			env.err = fmt.Errorf("%v is a negative level", nLevel)
			return sx.Nil()
			te.err = fmt.Errorf("%v is a negative level", nLevel)
			return sxpf.Nil()
		}
		level := strconv.FormatInt(nLevel+ev.headingOffset, 10)
		level := strconv.FormatInt(nLevel+te.tr.headingOffset, 10)

		a := ev.getAttributes(args[1], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if fragment := getString(args[3], env).String(); fragment != "" {
			a = a.Set("id", ev.unique+fragment)
		argAttr := args.Tail()
		a := te.getAttributes(argAttr)
		argFragment := argAttr.Tail().Tail()
		if fragment := te.getString(argFragment).String(); fragment != "" {
			a = a.Set("id", te.tr.unique+fragment)
		}

		if result, isPair := sx.GetPair(ev.eval(args[4], env)); isPair && result != nil {
		if result, ok := sxpf.GetList(argFragment.Tail().Car()); ok && result != nil {
			if len(a) > 0 {
				result = result.Cons(ev.EvaluateAttrbute(a))
				result = result.Cons(te.transformAttribute(a))
			}
			return result.Cons(ev.Make("h" + level))
			return result.Cons(te.Make("h" + level))
		}
		return sx.MakeList(ev.Make("h"+level), sx.String("<MISSING TEXT>"))
		return sxpf.MakeList(te.Make("h"+level), sxpf.MakeString("<MISSING TEXT>"))
	})
	ev.bind(sz.NameSymThematic, 0, func(args []sx.Object, env *Environment) sx.Object {
		result := sx.Nil()
		if len(args) > 0 {
			if attrList := getList(ev.eval(args[0], env), env); attrList != nil {
				result = result.Cons(ev.EvaluateAttrbute(sz.GetAttributes(attrList)))
	te.bind(sexpr.NameSymThematic, 0, func(args *sxpf.List) sxpf.Object {
		result := sxpf.Nil()
		if args != nil {
			if attrList := te.getList(args); attrList != nil {
				result = result.Cons(te.transformAttribute(sexpr.GetAttributes(attrList)))
			}
		}
		return result.Cons(ev.Make("hr"))
		return result.Cons(te.Make("hr"))
	})

	ev.bind(sz.NameSymListOrdered, 0, ev.makeListFn("ol"))
	ev.bind(sz.NameSymListUnordered, 0, ev.makeListFn("ul"))
	ev.bind(sz.NameSymDescription, 0, func(args []sx.Object, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.Nil()
	te.bind(sexpr.NameSymListOrdered, 0, te.makeListFn("ol"))
	te.bind(sexpr.NameSymListUnordered, 0, te.makeListFn("ul"))
	te.bind(sexpr.NameSymDescription, 0, func(args *sxpf.List) sxpf.Object {
		if args == nil {
			return sxpf.Nil()
		}
		items := sx.Nil().Cons(ev.Make("dl"))
		items := sxpf.Nil().Cons(te.Make("dl"))
		curItem := items
		for pos := 0; pos < len(args); pos++ {
			term := getList(ev.eval(args[pos], env), env)
			curItem = curItem.AppendBang(term.Cons(ev.Make("dt")))
		for elem := args; elem != nil; elem = elem.Tail() {
			term := te.getList(elem)
			curItem = curItem.AppendBang(term.Cons(te.Make("dt")))
			pos++
			if pos >= len(args) {
			elem = elem.Tail()
			if elem == nil {
				break
			}
			ddBlock := getList(ev.eval(args[pos], env), env)
			ddBlock := te.getList(elem)
			if ddBlock == nil {
				continue
				break
			}
			for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() {
				dditem := getList(ddlst.Car(), env)
				curItem = curItem.AppendBang(dditem.Cons(ev.Make("dd")))
				dditem := te.getList(ddlst)
				curItem = curItem.AppendBang(dditem.Cons(te.Make("dd")))
			}
		}
		return items
	})

	ev.bind(sz.NameSymListQuote, 0, func(args []sx.Object, env *Environment) sx.Object {
	te.bind(sexpr.NameSymListQuote, 0, func(args *sxpf.List) sxpf.Object {
		if args == nil {
			return sx.Nil()
			return sxpf.Nil()
		}
		result := sx.Nil().Cons(ev.Make("blockquote"))
		result := sxpf.Nil().Cons(te.Make("blockquote"))
		currResult := result
		for _, elem := range args {
			if quote, isPair := sx.GetPair(ev.eval(elem, env)); isPair {
				currResult = currResult.AppendBang(quote.Cons(ev.symP))
		for elem := args; elem != nil; elem = elem.Tail() {
			if quote, ok := elem.Car().(*sxpf.List); ok {
				currResult = currResult.AppendBang(quote.Cons(te.symP))
			}
		}
		return result
	})

	ev.bind(sz.NameSymTable, 1, func(args []sx.Object, env *Environment) sx.Object {
		thead := sx.Nil()
		if header := getList(ev.eval(args[0], env), env); !sx.IsNil(header) {
			thead = sx.Nil().Cons(ev.evalTableRow(header)).Cons(ev.Make("thead"))
	te.bind(sexpr.NameSymTable, 1, func(args *sxpf.List) sxpf.Object {
		thead := sxpf.Nil()
		if header := te.getList(args); header != nil {
			thead = sxpf.Nil().Cons(te.transformTableRow(header)).Cons(te.Make("thead"))
		}

		tbody := sx.Nil()
		if len(args) > 1 {
			tbody = sx.Nil().Cons(ev.Make("tbody"))
		tbody := sxpf.Nil()
		if argBody := args.Tail(); argBody != nil {
			tbody = sxpf.Nil().Cons(te.Make("tbody"))
			curBody := tbody
			for _, row := range args[1:] {
				curBody = curBody.AppendBang(ev.evalTableRow(getList(ev.eval(row, env), env)))
			for row := argBody; row != nil; row = row.Tail() {
				curBody = curBody.AppendBang(te.transformTableRow(te.getList(row)))
			}
		}

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

	symDiv := ev.Make("div")
	ev.bind(sz.NameSymRegionBlock, 2, ev.makeRegionFn(symDiv, true))
	ev.bind(sz.NameSymRegionQuote, 2, ev.makeRegionFn(ev.Make("blockquote"), false))
	ev.bind(sz.NameSymRegionVerse, 2, ev.makeRegionFn(symDiv, false))
	te.bind(sexpr.NameSymRegionBlock, 2, te.makeRegionFn(te.Make("div"), true))
	te.bind(sexpr.NameSymRegionQuote, 2, te.makeRegionFn(te.Make("blockquote"), false))
	te.bind(sexpr.NameSymRegionVerse, 2, te.makeRegionFn(te.Make("div"), false))

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

	ev.bind(sz.NameSymVerbatimEval, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.getAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env))
	te.bind(sexpr.NameSymVerbatimEval, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args).AddClass("zs-eval"), te.getString(args.Tail()))
	})
	ev.bind(sz.NameSymVerbatimHTML, 2, ev.evalHTML)
	ev.bind(sz.NameSymVerbatimMath, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.getAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env))
	te.bind(sexpr.NameSymVerbatimHTML, 2, te.transformHTML)
	te.bind(sexpr.NameSymVerbatimMath, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformVerbatim(te.getAttributes(args).AddClass("zs-math"), te.getString(args.Tail()))
	})
	ev.bind(sz.NameSymVerbatimProg, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		content := getString(args[1], env)
	te.bind(sexpr.NameSymVerbatimProg, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		content := te.getString(args.Tail())
		if a.HasDefault() {
			content = sx.String(visibleReplacer.Replace(content.String()))
			content = sxpf.MakeString(visibleReplacer.Replace(content.String()))
		}
		return ev.evalVerbatim(a, content)
		return te.transformVerbatim(a, content)
	})
	ev.bind(sz.NameSymVerbatimZettel, 0, nilFn)
	ev.bind(sz.NameSymBLOB, 3, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env))
	te.bind(sexpr.NameSymVerbatimZettel, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil() })

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

	ev.bind(sz.NameSymTransclude, 2, func(args []sx.Object, env *Environment) sx.Object {
		ref, isPair := sx.GetPair(ev.eval(args[1], env))
		if !isPair {
			return sx.Nil()
	te.bind(sexpr.NameSymTransclude, 2, func(args *sxpf.List) sxpf.Object {
		ref, ok := args.Tail().Car().(*sxpf.List)
		if !ok {
			return sxpf.Nil()
		}
		refKind := ref.Car()
		if sx.IsNil(refKind) {
			return sx.Nil()
		if sxpf.IsNil(refKind) {
			return sxpf.Nil()
		}
		if refValue := getString(ref.Tail().Car(), env); refValue != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.Name() == sz.NameSymRefStateExternal {
				a := ev.getAttributes(args[0], env).Set("src", refValue.String()).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(ev.Make("img"))).Cons(ev.symP)
		if refValue := te.getString(ref.Tail()); refValue != "" {
			if te.astSF.MustMake(sexpr.NameSymRefStateExternal).IsEqual(refKind) {
				a := te.getAttributes(args).Set("src", refValue.String()).AddClass("external")
				return sxpf.Nil().Cons(sxpf.Nil().Cons(te.transformAttribute(a)).Cons(te.Make("img"))).Cons(te.symP)
			}
			return sx.MakeList(
				ev.Make(sxhtml.NameSymInlineComment),
				sx.String("transclude"),
			return sxpf.MakeList(
				te.Make(sxhtml.NameSymInlineComment),
				sxpf.MakeString("transclude"),
				refKind,
				sx.String("->"),
				sxpf.MakeString("->"),
				refValue,
			)
		}
		return ev.evalSlice(args, env)
		return args
	})
}

func (ev *Evaluator) makeListFn(tag string) EvalFn {
	sym := ev.Make(tag)
	return func(args []sx.Object, env *Environment) sx.Object {
		result := sx.Nil().Cons(sym)
func (te *TransformEnv) makeListFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args *sxpf.List) sxpf.Object {
		result := sxpf.Nil().Cons(sym)
		last := result
		for _, elem := range args {
			item := sx.Nil().Cons(ev.symLI)
			if res, isPair := sx.GetPair(ev.eval(elem, env)); isPair {
		for elem := args; elem != nil; elem = elem.Tail() {
			item := sxpf.Nil().Cons(te.Make("li"))
			if res, ok := elem.Car().(*sxpf.List); ok {
				item.ExtendBang(res)
			}
			last = last.AppendBang(item)
		}
		return result
	}
}

func (te *TransformEnv) transformTableRow(cells *sxpf.List) *sxpf.List {
func (ev *Evaluator) evalTableRow(pairs *sx.Pair) *sx.Pair {
	row := sx.Nil().Cons(ev.Make("tr"))
	if pairs == nil {
		return nil
	row := sxpf.Nil().Cons(te.Make("tr"))
	if cells == nil {
		return sxpf.Nil()
	}
	curRow := row
	for pair := pairs; pair != nil; pair = pair.Tail() {
		curRow = curRow.AppendBang(pair.Car())
	for cell := cells; cell != nil; cell = cell.Tail() {
		curRow = curRow.AppendBang(cell.Car())
	}
	return row
}

func (ev *Evaluator) makeCellFn(align string) EvalFn {
	return func(args []sx.Object, env *Environment) sx.Object {
		tdata := ev.evalSlice(args, env)
func (te *TransformEnv) makeCellFn(align string) transformFn {
	return func(args *sxpf.List) sxpf.Object {
		tdata := args
		if align != "" {
			tdata = tdata.Cons(ev.EvaluateAttrbute(attrs.Attributes{"class": align}))
			tdata = tdata.Cons(te.transformAttribute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(ev.Make("td"))
		return tdata.Cons(te.Make("td"))
	}
}

func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn {
	return func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
func (te *TransformEnv) makeRegionFn(sym *sxpf.Symbol, genericToClass bool) transformFn {
	return func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		result := sx.Nil()
		result := sxpf.Nil()
		if len(a) > 0 {
			result = result.Cons(ev.EvaluateAttrbute(a))
			result = result.Cons(te.transformAttribute(a))
		}
		result = result.Cons(sym)
		currResult := result.LastPair()
		if region, isPair := sx.GetPair(ev.eval(args[1], env)); isPair {
		currResult := result.Last()
		blockArg := args.Tail()
		if region, ok := blockArg.Car().(*sxpf.List); ok {
			currResult = currResult.ExtendBang(region)
		}
		if len(args) > 2 {
			if cite, isPair := sx.GetPair(ev.eval(args[2], env)); isPair && cite != nil {
				currResult.AppendBang(cite.Cons(ev.Make("cite")))
		if citeArg := blockArg.Tail(); citeArg != nil {
			if cite, ok := citeArg.Car().(*sxpf.List); ok && cite != nil {
				currResult.AppendBang(cite.Cons(te.Make("cite")))
			}
		}
		return result
	}
}

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

func (ev *Evaluator) bindInlines() {
	ev.bind(sz.NameSymInline, 0, ev.evalList)
	ev.bind(sz.NameSymText, 1, func(args []sx.Object, env *Environment) sx.Object { return getString(args[0], env) })
	ev.bind(sz.NameSymSpace, 0, func(args []sx.Object, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.String(" ")
func (te *TransformEnv) bindInlines() {
	te.bind(sexpr.NameSymInline, 0, listArgs)
	te.bind(sexpr.NameSymText, 1, func(args *sxpf.List) sxpf.Object { return te.getString(args) })
	te.bind(sexpr.NameSymSpace, 0, func(args *sxpf.List) sxpf.Object {
		if args.IsNil() {
			return sxpf.MakeString(" ")
		}
		return getString(args[0], env)
		return te.getString(args)
	})
	ev.bind(sz.NameSymSoft, 0, func([]sx.Object, *Environment) sx.Object { return sx.String(" ") })
	symBR := ev.Make("br")
	ev.bind(sz.NameSymHard, 0, func([]sx.Object, *Environment) sx.Object { return sx.Nil().Cons(symBR) })
	te.bind(sexpr.NameSymSoft, 0, func(*sxpf.List) sxpf.Object { return sxpf.MakeString(" ") })
	brSym := te.Make("br")
	te.bind(sexpr.NameSymHard, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil().Cons(brSym) })

	ev.bind(sz.NameSymLinkInvalid, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	te.bind(sexpr.NameSymLinkInvalid, 2, func(args *sxpf.List) sxpf.Object {
		// a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		var inline *sx.Pair
		if len(args) > 2 {
			inline = ev.evalSlice(args[2:], env)
		refArg := args.Tail()
		inline := refArg.Tail()
		}
		if inline == nil {
			inline = sx.Nil().Cons(ev.eval(args[1], env))
			inline = sxpf.Nil().Cons(refArg.Car())
		}
		return inline.Cons(ev.symSpan)
		return inline.Cons(te.symSpan)
	})
	evalHREF := func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	transformHREF := func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.String()), refValue, args[2:], env)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.Set("href", refValue.String()), refValue, args.Tail().Tail())
	}
	ev.bind(sz.NameSymLinkZettel, 2, evalHREF)
	ev.bind(sz.NameSymLinkSelf, 2, evalHREF)
	ev.bind(sz.NameSymLinkFound, 2, evalHREF)
	ev.bind(sz.NameSymLinkBroken, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	te.bind(sexpr.NameSymLinkZettel, 2, transformHREF)
	te.bind(sexpr.NameSymLinkSelf, 2, transformHREF)
	te.bind(sexpr.NameSymLinkFound, 2, transformHREF)
	te.bind(sexpr.NameSymLinkBroken, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.AddClass("broken"), refValue, args[2:], env)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.AddClass("broken"), refValue, args.Tail().Tail())
	})
	ev.bind(sz.NameSymLinkHosted, 2, evalHREF)
	ev.bind(sz.NameSymLinkBased, 2, evalHREF)
	ev.bind(sz.NameSymLinkQuery, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	te.bind(sexpr.NameSymLinkHosted, 2, transformHREF)
	te.bind(sexpr.NameSymLinkBased, 2, transformHREF)
	te.bind(sexpr.NameSymLinkQuery, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		refValue := te.getString(args.Tail())
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String())
		return ev.evalLink(a.Set("href", query), refValue, args[2:], env)
		return te.transformLink(a.Set("href", query), refValue, args.Tail().Tail())
	})
	ev.bind(sz.NameSymLinkExternal, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	te.bind(sexpr.NameSymLinkExternal, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args[2:], env)
		refValue := te.getString(args.Tail())
		return te.transformLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args.Tail().Tail())
	})

	ev.bind(sz.NameSymEmbed, 3, func(args []sx.Object, env *Environment) sx.Object {
		ref := getList(ev.eval(args[1], env), env)
		syntax := getString(args[2], env)
	te.bind(sexpr.NameSymEmbed, 3, func(args *sxpf.List) sxpf.Object {
		argRef := args.Tail()
		ref := te.getList(argRef)
		syntax := te.getString(argRef.Tail())
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				ev.symAttr,
				sx.Cons(ev.Make("type"), sx.String("image/svg+xml")),
				sx.Cons(ev.Make("src"), sx.String("/"+getString(ref.Tail(), env).String()+".svg")),
			embedAttr := sxpf.MakeList(
				te.symAttr,
				sxpf.Cons(te.Make("type"), sxpf.MakeString("image/svg+xml")),
				sxpf.Cons(te.Make("src"), sxpf.MakeString("/"+te.getString(ref.Tail()).String()+".svg")),
			)
			return sx.MakeList(
				ev.Make("figure"),
				sx.MakeList(
					ev.Make("embed"),
			return sxpf.MakeList(
				te.Make("figure"),
				sxpf.MakeList(
					te.Make("embed"),
					embedAttr,
				),
			)
		}
		a := ev.getAttributes(args[0], env)
		a = a.Set("src", getString(ref.Tail().Car(), env).String())
		a := te.getAttributes(args)
		a = a.Set("src", string(te.getString(ref.Tail())))
		if len(args) > 3 {
			var sb strings.Builder
			flattenText(&sb, sx.MakeList(args[3:]...))
			if d := sb.String(); d != "" {
				a = a.Set("alt", d)
			}
		var sb strings.Builder
		te.flattenText(&sb, ref.Tail().Tail().Tail())
		if d := sb.String(); d != "" {
			a = a.Set("alt", d)
		}
		}
		return sx.MakeList(ev.Make("img"), ev.EvaluateAttrbute(a))
		return sxpf.MakeList(te.Make("img"), te.transformAttribute(a))
	})
	ev.bind(sz.NameSymEmbedBLOB, 3, func(args []sx.Object, 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)
	te.bind(sexpr.NameSymEmbedBLOB, 3, func(args *sxpf.List) sxpf.Object {
		argSyntax := args.Tail()
		a, syntax, data := te.getAttributes(args), te.getString(argSyntax), te.getString(argSyntax.Tail())
		summary, _ := a.Get(api.KeySummary)
		if !hasSummary {
			summary = ""
		}
		return ev.evalBLOB(
			sx.MakeList(ev.Make(sx.ListName), sx.String(summary)),
		return te.transformBLOB(
			sxpf.MakeList(te.astSF.MustMake(sexpr.NameSymInline), sxpf.MakeString(summary)),
			syntax,
			data,
		)
	})

	ev.bind(sz.NameSymCite, 2, func(args []sx.Object, env *Environment) sx.Object {
	te.bind(sexpr.NameSymCite, 2, func(args *sxpf.List) sxpf.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		result := sx.Nil()
		if key := getString(args[1], env); key != "" {
			if len(args) > 2 {
				result = ev.evalSlice(args[2:], env).Cons(sx.String(", "))
		result := sxpf.Nil()
		argKey := args.Tail()
		if key := te.getString(argKey); key != "" {
			if text := argKey.Tail(); text != nil {
				result = text.Cons(sxpf.MakeString(", "))
			}
			result = result.Cons(key)
		}
		if len(a) > 0 {
			result = result.Cons(ev.EvaluateAttrbute(a))
		if a := te.getAttributes(args); len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(ev.symSpan)
		return result.Cons(te.symSpan)
	})
	ev.bind(sz.NameSymMark, 3, func(args []sx.Object, env *Environment) sx.Object {
		result := ev.evalSlice(args[3:], env)
		if !ev.noLinks {
			if fragment := getString(args[2], env); fragment != "" {
				a := attrs.Attributes{"id": fragment.String() + ev.unique}
				return result.Cons(ev.EvaluateAttrbute(a)).Cons(ev.symA)
			}
		}
		return result.Cons(ev.symSpan)

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

	ev.bind(sz.NameSymEndnote, 1, func(args []sx.Object, env *Environment) sx.Object {
	te.bind(sexpr.NameSymEndnote, 1, func(args *sxpf.List) sxpf.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 := sxpf.Nil()
		if a := te.getAttributes(args); len(a) > 0 {
			if attrs := te.transformAttribute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		text, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.Nil()
		}
		noteNum := strconv.Itoa(len(env.endnotes) + 1)
		noteID := ev.unique + noteNum
		env.endnotes = append(env.endnotes, endnoteInfo{
			noteID: noteID, noteAST: ev.eval(text, env), noteHx: nil, attrs: attrPlist})
		hrefAttr := sx.Nil().Cons(sx.Cons(ev.Make("role"), sx.String("doc-noteref"))).
			Cons(sx.Cons(ev.symHREF, sx.String("#fn:"+noteID))).
			Cons(sx.Cons(ev.symClass, sx.String("zs-noteref"))).
			Cons(ev.symAttr)
		href := sx.Nil().Cons(sx.String(noteNum)).Cons(hrefAttr).Cons(ev.symA)
		supAttr := sx.Nil().Cons(sx.Cons(ev.Make("id"), sx.String("fnref:"+noteID))).Cons(ev.symAttr)
		return sx.Nil().Cons(href).Cons(supAttr).Cons(ev.Make("sup"))

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

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

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

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

func (ev *Evaluator) makeFormatFn(tag string) EvalFn {
	sym := ev.Make(tag)
	return func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
	te.bind(sexpr.NameSymLiteralInput, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, kbdSym)
		env.pushAttributes(a)
		defer env.popAttributes()
		if val, hasClass := a.Get(""); hasClass {
			a = a.Remove("").AddClass(val)
		}
		res := ev.evalSlice(args[1:], env)
	})
	codeSym := te.Make("code")
		if len(a) > 0 {
			res = res.Cons(ev.EvaluateAttrbute(a))
		}
		return res.Cons(sym)
	}
}
	te.bind(sexpr.NameSymLiteralMath, 2, func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args).AddClass("zs-math")
		return te.transformLiteral(args, a, codeSym)
	})
	sampSym := te.Make("samp")
	te.bind(sexpr.NameSymLiteralOutput, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, sampSym)
	})
	te.bind(sexpr.NameSymLiteralProg, 2, func(args *sxpf.List) sxpf.Object {
		return te.transformLiteral(args, nil, codeSym)
	})

type quoteData struct {
	primLeft, primRight string
	te.bind(sexpr.NameSymLiteralZettel, 0, func(*sxpf.List) sxpf.Object { return sxpf.Nil() })
	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 (te *TransformEnv) makeFormatFn(tag string) transformFn {
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]
	sym := te.Make(tag)
		if ok {
			return quotes
	return func(args *sxpf.List) sxpf.Object {
		a := te.getAttributes(args)
		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		res := args.Tail()
		langFields = langFields[0 : len(langFields)-1]
		if len(a) > 0 {
	}
	return langQuotes[""]
}

			res = res.Cons(te.transformAttribute(a))
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.Object, env *Environment) sx.Object {
	a := ev.getAttributes(args[0], env)
	env.pushAttributes(a)
	defer env.popAttributes()

	if val, hasClass := a.Get(""); hasClass {
		}
		return res.Cons(sym)
	}
}
func (te *TransformEnv) transformQuote(args *sxpf.List) sxpf.Object {
	const langAttr = "lang"
	a := te.getAttributes(args)
	langVal, found := a.Get(langAttr)
	if found {
		a = a.Remove(langAttr)
	}
	if val, found2 := a.Get(""); found2 {
		a = a.Remove("").AddClass(val)
	}
	quotes := getQuoteData(env.getLanguage())
	leftQ, rightQ := getQuotes(&quotes, env)

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

	lastPair := res.LastPair()
	if lastPair.IsNil() {
		res = sx.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ), sx.String(rightQ)), sx.Nil())
	} else {
		if quotes.nbsp {
			lastPair.AppendBang(sx.MakeList(ev.symNoEscape, sx.String("&nbsp;"), sx.String(rightQ)))
			res = res.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ), sx.String("&nbsp;")))
		} else {
			lastPair.AppendBang(sx.MakeList(ev.symNoEscape, sx.String(rightQ)))
			res = res.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ)))
		}
	}
	if len(a) > 0 {
		res = res.Cons(te.transformAttribute(a))
	}
		res = res.Cons(ev.EvaluateAttrbute(a))
		return res.Cons(ev.symSpan)
	res = res.Cons(te.Make("q"))
	if found {
		res = sxpf.Nil().Cons(res).Cons(te.transformAttribute(attrs.Attributes{}.Set(langAttr, langVal))).Cons(te.symSpan)
	}
	return res.Cons(ev.symList)
	return res
}

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

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

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

func (ev *Evaluator) evalHTML(args []sx.Object, env *Environment) sx.Object {
	if s := getString(ev.eval(args[1], env), env); s != "" && IsSafe(s.String()) {
		return sx.Nil().Cons(s).Cons(ev.symNoEscape)
func (te *TransformEnv) transformHTML(args *sxpf.List) sxpf.Object {
	if s := te.getString(args.Tail()); s != "" && IsSafe(s.String()) {
		return sxpf.Nil().Cons(s).Cons(te.symNoEscape)
	}
	return nil
}

func (ev *Evaluator) evalBLOB(description *sx.Pair, syntax, data sx.String) sx.Object {
func (te *TransformEnv) transformBLOB(description *sxpf.List, syntax, data sxpf.String) sxpf.Object {
	if data == "" {
		return sx.Nil()
		return sxpf.Nil()
	}
	switch syntax {
	case "":
		return sx.Nil()
		return sxpf.Nil()
	case api.ValueSyntaxSVG:
		return sx.Nil().Cons(sx.Nil().Cons(data).Cons(ev.symNoEscape)).Cons(ev.symP)
		return sxpf.Nil().Cons(sxpf.Nil().Cons(data).Cons(te.symNoEscape)).Cons(te.symP)
	default:
		imgAttr := sx.Nil().Cons(sx.Cons(ev.Make("src"), sx.String("data:image/"+syntax.String()+";base64,"+data.String())))
		imgAttr := sxpf.Nil().Cons(sxpf.Cons(te.Make("src"), sxpf.MakeString("data:image/"+syntax.String()+";base64,"+data.String())))
		var sb strings.Builder
		flattenText(&sb, description)
		te.flattenText(&sb, description)
		if d := sb.String(); d != "" {
			imgAttr = imgAttr.Cons(sx.Cons(ev.Make("alt"), sx.String(d)))
			imgAttr = imgAttr.Cons(sxpf.Cons(te.Make("alt"), sxpf.MakeString(d)))
		}
		return sx.Nil().Cons(sx.Nil().Cons(imgAttr.Cons(ev.symAttr)).Cons(ev.Make("img"))).Cons(ev.symP)
		return sxpf.Nil().Cons(sxpf.Nil().Cons(imgAttr.Cons(te.symAttr)).Cons(te.Make("img"))).Cons(te.symP)
	}
}

func flattenText(sb *strings.Builder, lst *sx.Pair) {
	for elem := lst; elem != nil; elem = elem.Tail() {
		switch obj := elem.Car().(type) {
		case sx.String:
			sb.WriteString(obj.String())
		case *sx.Symbol:
			if obj.Name() == sz.NameSymSpace {
				sb.WriteByte(' ')
				break
			}
		case *sx.Pair:
			flattenText(sb, obj)
		}
	}
}


func (te *TransformEnv) flattenText(sb *strings.Builder, lst *sxpf.List) {
	for elem := lst; elem != nil; elem = elem.Tail() {
		switch obj := elem.Car().(type) {
		case sxpf.String:
			sb.WriteString(obj.String())
		case *sxpf.List:
			te.flattenText(sb, obj)
		}
	}
}
func (ev *Evaluator) evalList(args []sx.Object, env *Environment) sx.Object {
	return ev.evalSlice(args, env)
}

func nilFn([]sx.Object, *Environment) sx.Object { return sx.Nil() }

type transformFn func(*sxpf.List) sxpf.Object
func (ev *Evaluator) eval(obj sx.Object, env *Environment) sx.Object {
	if env.err != nil {
		return sx.Nil()
	}

	if sx.IsNil(obj) {
		return obj
func (te *TransformEnv) bind(name string, minArity int, fn transformFn) {
	}
	lst, isLst := sx.GetPair(obj)
	te.astEnv.Bind(te.astSF.MustMake(name), eval.MakeBuiltin(name, func(_ sxpf.Environment, args *sxpf.List) (sxpf.Object, error) {
		if nArgs := args.Length(); nArgs < minArity {
	if !isLst {
		return obj
	}
	sym, found := sx.GetSymbol(lst.Car())
			return sxpf.Nil(), fmt.Errorf("not enough arguments (%d) for form %v (%d)", nArgs, name, minArity)
		}
		res := fn(args)
	if !found {
		env.err = fmt.Errorf("symbol expected, but got %T/%v", lst.Car(), lst.Car())
		return sx.Nil()
	}
	name := sym.Name()
	fn, found := ev.fns[name]
		return res, te.err
	}))
}

func (te *TransformEnv) Rebind(name string, fn func(sxpf.Environment, *sxpf.List, sxpf.Callable) sxpf.Object) {
	sym := te.astSF.MustMake(name)
	obj, found := te.astEnv.Lookup(sym)
	if !found {
		env.err = fmt.Errorf("symbol %q not bound", name)
		return sx.Nil()
		panic(sym.String())
	}
	var args []sx.Object
	for cdr := lst.Cdr(); !sx.IsNil(cdr); {
	preFn, ok := obj.(sxpf.Callable)
		pair, isPair := sx.GetPair(cdr)
		if !isPair {
	if !ok {
			break
		}
		args = append(args, pair.Car())
		cdr = pair.Cdr()
		panic(sym.String())
	}
	if minArgs, hasMinArgs := ev.minArgs[name]; hasMinArgs {
		if minArgs > len(args) {
	te.astEnv.Bind(sym, eval.MakeBuiltin(name, func(env sxpf.Environment, args *sxpf.List) (sxpf.Object, error) {
		res := fn(env, args, preFn)
			env.err = fmt.Errorf("%v needs at least %d arguments, but got only %d", name, minArgs, len(args))
			return sx.Nil()
		}
	}
	result := fn(args, env)
	if env.err != nil {
		return sx.Nil()
		return res, te.err
	}))
}

func (te *TransformEnv) Make(name string) *sxpf.Symbol { return te.tr.Make(name) }
func (te *TransformEnv) getString(lst *sxpf.List) sxpf.String {
	if te.err != nil {
		return ""
	}
	val := lst.Car()
	if s, ok := val.(sxpf.String); ok {
	return result
}

		return s
	}
	te.err = fmt.Errorf("%v/%T is not a string", val, val)
func (ev *Evaluator) evalSlice(args []sx.Object, env *Environment) *sx.Pair {
	result := sx.Cons(sx.Nil(), sx.Nil())
	curr := result
	for _, arg := range args {
		elem := ev.eval(arg, env)
		if env.err != nil {
			return nil
		}
		curr = curr.AppendBang(elem)
	return ""
}
func (te *TransformEnv) getInt64(lst *sxpf.List) int64 {
	if te.err != nil {
	}
	return result.Tail()
}

		return -1017
	}
	val := lst.Car()
func (ev *Evaluator) evalLink(a attrs.Attributes, refValue sx.String, inline []sx.Object, env *Environment) sx.Object {
	result := ev.evalSlice(inline, env)
	if num, ok := val.(*sxpf.Number); ok {
	if len(inline) == 0 {
		result = sx.Nil().Cons(refValue)
	}
	if ev.noLinks {
		return result.Cons(ev.symSpan)
		return num.GetInt64()
	}
	te.err = fmt.Errorf("%v/%T is not a number", val, val)
	return result.Cons(ev.EvaluateAttrbute(a)).Cons(ev.symA)
	return -1017
}

func (te *TransformEnv) getList(lst *sxpf.List) *sxpf.List {
func (ev *Evaluator) getSymbol(val sx.Object, env *Environment) *sx.Symbol {
	if env.err == nil {
		if sym, ok := sx.GetSymbol(val); ok {
			return sym
	if te.err == nil {
		val := lst.Car()
		if res, ok := val.(*sxpf.List); ok {
			return res
		}
		env.err = fmt.Errorf("%v/%T is not a symbol", val, val)
		te.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return ev.Make("???")
	return sxpf.Nil()
}
func getString(val sx.Object, env *Environment) sx.String {
	if env.err != nil {
		return ""
	}
func (te *TransformEnv) getAttributes(args *sxpf.List) attrs.Attributes {
	return sexpr.GetAttributes(te.getList(args))
}
	if s, ok := sx.GetString(val); ok {
		return s
	}

	env.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
func (te *TransformEnv) transformLink(a attrs.Attributes, refValue sxpf.String, inline *sxpf.List) sxpf.Object {
	result := inline
	if inline.IsNil() {
		result = sxpf.Nil().Cons(refValue)
	}
func getList(val sx.Object, env *Environment) *sx.Pair {
	if env.err == nil {
	if te.tr.noLinks {
		if res, isPair := sx.GetPair(val); isPair {
			return res
		}
		return result.Cons(te.symSpan)
	}
		env.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return result.Cons(te.transformAttribute(a)).Cons(te.symA)
}
	return nil
}

func getInt64(val sx.Object, env *Environment) int64 {
	if env.err != nil {
		return -1017
	}
func (te *TransformEnv) transformAttribute(a attrs.Attributes) *sxpf.List {
	return te.tr.TransformAttrbute(a)
}
	if num, ok := sx.GetNumber(val); ok {
		return int64(num.(sx.Int64))
	}

	env.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
func (te *TransformEnv) transformMeta(a attrs.Attributes) *sxpf.List {
	return te.tr.TransformMeta(a)
}
func (ev *Evaluator) getAttributes(arg sx.Object, env *Environment) attrs.Attributes {
	return sz.GetAttributes(getList(ev.eval(arg, env), env))
}

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

Deleted sz/const.go.

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










































































































































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz

import "zettelstore.de/sx.fossil"

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

const (
	// Symbols for Metanodes
	NameSymBlock  = "BLOCK"
	NameSymInline = "INLINE"
	NameSymMeta   = "META"

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

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

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

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

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

	// Constant symbols for reference states.

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

	// Symbols for metadata types

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

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

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

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

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

Deleted sz/const_test.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz_test

import (
	"strconv"
	"testing"

	"zettelstore.de/client.fossil/sz"
	"zettelstore.de/sx.fossil"
)

func BenchmarkInitZttlSyms(b *testing.B) {
	for j := 0; j < 20; j++ {
		b.Run("hint-"+strconv.Itoa(j), func(b *testing.B) {
			sf := sx.MakeMappedFactory(1 << j)
			for i := 0; i < b.N; i++ {
				var zs sz.ZettelSymbols
				zs.InitializeZettelSymbols(sf)
			}
		})
	}
}

Deleted sz/sz.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134






































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//-----------------------------------------------------------------------------

package sz

import (
	"zettelstore.de/client.fossil/attrs"
	"zettelstore.de/sx.fossil"
)

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

// GetMetaContent returns the metadata and the content of a sz encoded zettel.
func GetMetaContent(zettel sx.Object) (Meta, *sx.Pair) {
	if pair, isPair := sx.GetPair(zettel); isPair {
		m := pair.Car()
		if s := pair.Tail(); s != nil {
			if content, isContentPair := sx.GetPair(s.Car()); isContentPair {
				return MakeMeta(m), content
			}
		}
		return MakeMeta(m), nil
	}
	return nil, nil
}

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

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

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

func (m Meta) GetPair(key string) *sx.Pair {
	if mv, found := m[key]; found {
		if pair, isPair := sx.GetPair(mv.Value); isPair {
			return pair
		}
	}
	return nil
}

Changes to text/text.go.

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
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







-
-
+
+




-
+


-
-
-
-
-
+
+
+
+
+


-
+






-
-
-
-
-
+
+
+
+
+




-
+







-
-
+
+





-
+




-
-
-
+
+
+



-
+







-
+











// Package text provides types, constants and function to work with text output.
package text

import (
	"strings"

	"zettelstore.de/client.fossil/sz"
	"zettelstore.de/sx.fossil"
	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/sexpr"
)

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

	symText  *sx.Symbol
	symSpace *sx.Symbol
	symSoft  *sx.Symbol
	symHard  *sx.Symbol
	symQuote *sx.Symbol
	symText  *sxpf.Symbol
	symSpace *sxpf.Symbol
	symSoft  *sxpf.Symbol
	symHard  *sxpf.Symbol
	symQuote *sxpf.Symbol
}

func NewEncoder(sf sx.SymbolFactory) *Encoder {
func NewEncoder(sf sxpf.SymbolFactory) *Encoder {
	if sf == nil {
		return nil
	}
	enc := &Encoder{
		sf:       sf,
		sb:       strings.Builder{},
		symText:  sf.MustMake(sz.NameSymText),
		symSpace: sf.MustMake(sz.NameSymSpace),
		symSoft:  sf.MustMake(sz.NameSymSoft),
		symHard:  sf.MustMake(sz.NameSymHard),
		symQuote: sf.MustMake(sx.QuoteName),
		symText:  sf.MustMake(sexpr.NameSymText),
		symSpace: sf.MustMake(sexpr.NameSymSpace),
		symSoft:  sf.MustMake(sexpr.NameSymSoft),
		symHard:  sf.MustMake(sexpr.NameSymHard),
		symQuote: sf.MustMake(sexpr.NameSymQuote),
	}
	return enc
}

func (enc *Encoder) Encode(lst *sx.Pair) string {
func (enc *Encoder) Encode(lst *sxpf.List) string {
	enc.executeList(lst)
	result := enc.sb.String()
	enc.sb.Reset()
	return result
}

// EvaluateInlineString returns the text content of the given inline list as a string.
func EvaluateInlineString(lst *sx.Pair) string {
	if sf := sx.FindSymbolFactory(lst); sf != nil {
func EvaluateInlineString(lst *sxpf.List) string {
	if sf := sxpf.FindSymbolFactory(lst); sf != nil {
		return NewEncoder(sf).Encode(lst)
	}
	return ""
}

func (enc *Encoder) executeList(lst *sx.Pair) {
func (enc *Encoder) executeList(lst *sxpf.List) {
	for elem := lst; elem != nil; elem = elem.Tail() {
		enc.execute(elem.Car())
	}
}
func (enc *Encoder) execute(obj sx.Object) {
	cmd, isPair := sx.GetPair(obj)
	if !isPair {
func (enc *Encoder) execute(obj sxpf.Object) {
	cmd, ok := obj.(*sxpf.List)
	if !ok {
		return
	}
	sym := cmd.Car()
	if sx.IsNil(sym) {
	if sxpf.IsNil(sym) {
		return
	}
	if sym.IsEqual(enc.symText) {
		args := cmd.Tail()
		if args == nil {
			return
		}
		if val, isString := sx.GetString(args.Car()); isString {
		if val, ok2 := args.Car().(sxpf.String); ok2 {
			enc.sb.WriteString(val.String())
		}
	} else if sym.IsEqual(enc.symSpace) || sym.IsEqual(enc.symSoft) {
		enc.sb.WriteByte(' ')
	} else if sym.IsEqual(enc.symHard) {
		enc.sb.WriteByte('\n')
	} else if !sym.IsEqual(enc.symQuote) {
		enc.executeList(cmd.Tail())
	}
}

Changes to text/text_test.go.

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
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







-
-
-
+
+
+


-
+









-
+




-
-
+
+









package text_test

import (
	"strings"
	"testing"

	"zettelstore.de/client.fossil/text"
	"zettelstore.de/sx.fossil"
	"zettelstore.de/sx.fossil/sxreader"
	"codeberg.org/t73fde/sxpf"
	"codeberg.org/t73fde/sxpf/reader"
	"zettelstore.de/c/text"
)

func TestSzText(t *testing.T) {
func TestSexprText(t *testing.T) {
	testcases := []struct {
		src string
		exp string
	}{
		{"()", ""},
		{`(INLINE (TEXT "a"))`, "a"},
		{`(INLINE (SPACE "a"))`, " "},
	}
	for i, tc := range testcases {
		sval, err := sxreader.MakeReader(strings.NewReader(tc.src)).Read()
		sval, err := reader.MakeReader(strings.NewReader(tc.src)).Read()
		if err != nil {
			t.Error(err)
			continue
		}
		seq, isPair := sx.GetPair(sval)
		if !isPair {
		seq, ok := sval.(*sxpf.List)
		if !ok {
			t.Errorf("%d: not a list: %v", i, sval)
		}
		got := text.EvaluateInlineString(seq)
		if got != tc.exp {
			t.Errorf("%d: EncodeBlock(%q) == %q, but got %q", i, tc.src, tc.exp, got)
		}
	}
}

Changes to www/changes.wiki.

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
1
2

























3

4

5
6
7
8
9
10
11


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

-
+
-







<title>Change Log</title>

<a name="0_17"></a>
<h2>Changes for Version 0.17.0 (pending)</h2>

<a name="0_16"></a>
<h2>Changes for Version 0.16.0 (2023-11-30)</h2>
  *  Refactor shtml transformator to support evaluating the language tree of a
     zettel AST. Its API has been changes, since evaluation is now top-down,
     where previous transformation was bottom.up.
  *  Add API call to retrieve role zettel.
  *  Added constants for role zettel and to mark text within zettelmarkup.

<a name="0_15"></a>
<h2>Changes for Version 0.15.0 (2023-10-26)</h2>
  *  Tag zettel: API, constant values
  *  Refactorings b/c Sx

<a name="0_14"></a>
<h2>Changes for Version 0.14.0 (2023-09-23)</h2>
  *  Remove support for JSON encoding

<a name="0_13"></a>
<h2>Changes for Version 0.13.0 (2023-08-07)</h2>
  *  API uses plain data or sx data, but no JSON encoded data.
  *  Dependency sx is now hosted on Fossil repository, same for this library.

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

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

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

Changes to www/index.wiki.

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
1
2
3
4
5






6
7
8
9
10
11
12





























-
-
-
-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<title>Home</title>

This repository contains Go client software to
access [https://zettelstore.de|Zettelstore] via its API.

<h3>Latest Release: 0.16.0 (2023-11-30)</h3>
  *  [./changes.wiki#0_16|Change summary]
  *  [/timeline?p=v0.16.0&bt=v0.15.0&y=ci|Check-ins for version 0.16.0],
     [/vdiff?to=v0.16.0&from=v0.15.0|content diff]
  *  [/timeline?df=v0.16.0&y=ci|Check-ins derived from the 0.16.0 release],
     [/vdiff?from=v0.16.0&to=trunk|content diff]
<h3>Latest Release: 0.11.0 (2023-03-27)</h3>
  *  [./changes.wiki#0_11|Change summary]
  *  [/timeline?p=v0.11.0&bt=v0.10.0&y=ci|Check-ins for version 0.11.0],
     [/vdiff?to=v0.11.0&from=v0.10.0|content diff]
  *  [/timeline?df=v0.11.0&y=ci|Check-ins derived from the 0.11.0 release],
     [/vdiff?from=v0.11.0&to=trunk|content diff]
  *  [/timeline?t=release|Timeline of all past releases]

<h3>Use instructions</h3>

If you want to import this library into your own [https://go.dev/|Go] software,
you must execute a <code>go get</code> command. Since Go treats non-standard
software and non-standard platforms quite badly, you must use some non-standard
commands.

First, you must install the version control system
[https://fossil-scm.org|Fossil], which is a superior solution compared to Git,
in too many use cases. It is just a single executable, nothing more. Make sure,
it is in your search path for commands.

How you can execute the following Go command to retrieve a given version of
this library:

<code>GOVCS=zettelstore.de:fossil go get -x
zettelstore.de/client.fossil@HASH</code>

where <code>HASH</code> is the hash value of the commit you want to use.

Go currently seems not to support software versions when the software is
managed by Fossil. This explains the need for the hash value. However, this
methods works and you can use the client software to access a Zettelstore.