Zettelstore Client

Check-in Differences
Login

Check-in Differences

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

Difference From v0.13.0 To trunk

2024-05-31
15:48
Initial version of zmk->Sx parser ... (Leaf check-in: 955147c904 user: stern tags: trunk)
15:46
Merge from trunk ... (Leaf check-in: 9aec1d38bb user: stern tags: zmk)
2024-04-22
14:51
Package sxhtml was renamed to sxwebs/sxhtml ... (check-in: 3bbfcfd939 user: stern tags: trunk)
2023-08-07
15:17
Remove support for JSON encoding ... (check-in: 0628873da8 user: stern tags: trunk)
13:44
Version 0.13.0 ... (check-in: 92d8dd7df8 user: stern tags: release, trunk, v0.13.0)
10:22
client: use sx for creating/updating zettel ... (check-in: 4ce083f28b user: stern tags: trunk)

Deleted .github/dependabot.yml.

1
2
3
4
5
6
7
8
9
10
11
12
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
  - package-ecosystem: "gomod" # See documentation for possible values
    directory: "/" # Location of package manifests
    schedule:
      interval: "daily"
    rebase-strategy: "disabled"
<
<
<
<
<
<
<
<
<
<
<
<
























Changes to api/api.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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-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 api contains common definitions used for client and server.
package api

// ZettelID contains the identifier of a zettel. It is a string with 14 digits.
type ZettelID string

// InvalidZID is an invalif zettel identifier
const InvalidZID = ""

// IsValid returns true, if the idenfifier contains 14 digits.
func (zid ZettelID) IsValid() bool {
	if len(zid) != 14 {
		return false
	}
	for i := 0; i < 14; i++ {
		ch := zid[i]
		if ch < '0' || '9' < ch {
			return false
		}
	}
	return true
}








>
>
>
















|







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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------

// Package api contains common definitions used for client and server.
package api

// ZettelID contains the identifier of a zettel. It is a string with 14 digits.
type ZettelID string

// InvalidZID is an invalif zettel identifier
const InvalidZID = ""

// IsValid returns true, if the idenfifier contains 14 digits.
func (zid ZettelID) IsValid() bool {
	if len(zid) != 14 {
		return false
	}
	for i := range 14 {
		ch := zid[i]
		if ch < '0' || '9' < ch {
			return false
		}
	}
	return true
}

Changes to api/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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-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 api

import "fmt"

// Predefined Zettel Identifier
const (
	// System zettel
	ZidVersion              = ZettelID("00000000000001")
	ZidHost                 = ZettelID("00000000000002")
	ZidOperatingSystem      = ZettelID("00000000000003")
	ZidLicense              = ZettelID("00000000000004")
	ZidAuthors              = ZettelID("00000000000005")
	ZidDependencies         = ZettelID("00000000000006")
	ZidLog                  = ZettelID("00000000000007")

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

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

	// WebUI sxn code zettel are in the range 19000..19999
	ZidSxnTemplate = ZettelID("00000000019100")


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

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

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

	// Other sxn code zettel are in the range 50000..59999








	// Range 90000...99999 is reserved for zettel templates
	ZidTOCNewTemplate    = ZettelID("00000000090000")
	ZidTemplateNewZettel = ZettelID("00000000090001")


	ZidTemplateNewUser   = ZettelID("00000000090002")

	ZidDefaultHome = ZettelID("00010000000000")
)

// LengthZid factors the constant length of a zettel identifier
const LengthZid = len(ZidDefaultHome)

// Values of the metadata key/value type.
const (
	MetaCredential   = "Credential"
	MetaEmpty        = "EString"
	MetaID           = "Identifier"
	MetaIDSet        = "IdentifierSet"
	MetaNumber       = "Number"
	MetaString       = "String"
	MetaTagSet       = "TagSet"
	MetaTimestamp    = "Timestamp"
	MetaURL          = "URL"
	MetaWord         = "Word"
	MetaWordSet      = "WordSet"
	MetaZettelmarkup = "Zettelmarkup"
)

// Predefined general Metadata keys
const (
	KeyID           = "id"
	KeyTitle        = "title"








>
>
>
















>


















|
>


|
|
<







>
>
>
>
>
>
>




>
>




















<







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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------

package api

import "fmt"

// Predefined Zettel Identifier
const (
	// System zettel
	ZidVersion              = ZettelID("00000000000001")
	ZidHost                 = ZettelID("00000000000002")
	ZidOperatingSystem      = ZettelID("00000000000003")
	ZidLicense              = ZettelID("00000000000004")
	ZidAuthors              = ZettelID("00000000000005")
	ZidDependencies         = ZettelID("00000000000006")
	ZidLog                  = ZettelID("00000000000007")
	ZidMemory               = ZettelID("00000000000008")
	ZidBoxManager           = ZettelID("00000000000020")
	ZidMetadataKey          = ZettelID("00000000000090")
	ZidParser               = ZettelID("00000000000092")
	ZidStartupConfiguration = ZettelID("00000000000096")
	ZidConfiguration        = ZettelID("00000000000100")

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

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

	// CSS-related zettel are in the range 20000..29999
	ZidBaseCSS = ZettelID("00000000020001")
	ZidUserCSS = ZettelID("00000000025001")


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

// Values of the metadata key/value type.
const (
	MetaCredential   = "Credential"
	MetaEmpty        = "EString"
	MetaID           = "Identifier"
	MetaIDSet        = "IdentifierSet"
	MetaNumber       = "Number"
	MetaString       = "String"
	MetaTagSet       = "TagSet"
	MetaTimestamp    = "Timestamp"
	MetaURL          = "URL"
	MetaWord         = "Word"

	MetaZettelmarkup = "Zettelmarkup"
)

// Predefined general Metadata keys
const (
	KeyID           = "id"
	KeyTitle        = "title"
121
122
123
124
125
126
127


128
129
130
131
132
133
134

// Predefined Metadata values
const (
	ValueFalse             = "false"
	ValueTrue              = "true"
	ValueLangEN            = "en"
	ValueRoleConfiguration = "configuration"


	ValueRoleZettel        = "zettel"
	ValueSyntaxCSS         = "css"
	ValueSyntaxDraw        = "draw"
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"







>
>







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

// 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"
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
const (
	QueryKeyCommand   = "cmd"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"

	QueryKeySeed      = "_seed"

)

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

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

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

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

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







>

>













<












<







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
const (
	QueryKeyCommand   = "cmd"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"
	QueryKeyRole      = "role"
	QueryKeySeed      = "_seed"
	QueryKeyTag       = "tag"
)

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

	EncodingPlain = "plain"
	EncodingData  = "data"

)

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

	EncodingPlain: EncoderPlain,
	EncodingData:  EncoderData,

}
var mapEnumEncoding = map[EncodingEnum]string{}

func init() {
	for k, v := range mapEncodingEnum {
		mapEnumEncoding[v] = k
	}
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	EncoderSHTML
	EncoderSz
	EncoderText
	EncoderZmk

	EncoderPlain
	EncoderData
	EncoderJson
)

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







<







236
237
238
239
240
241
242

243
244
245
246
247
248
249
	EncoderSHTML
	EncoderSz
	EncoderText
	EncoderZmk

	EncoderPlain
	EncoderData

)

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




// 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 = "|"











	ExistOperator    = "?"
	ExistNotOperator = "!?"

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










>















>
>
>
>
>
>
>
>
>
>



















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

// Supported search operator representations
const (
	BackwardDirective = "BACKWARD"
	ContextDirective  = "CONTEXT"
	CostDirective     = "COST"
	ForwardDirective  = "FORWARD"
	FullDirective     = "FULL"
	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 = "|"

	AtomAction     = "ATOM"
	KeysAction     = "KEYS"
	MinAction      = "MIN"
	MaxAction      = "MAX"
	NumberedAction = "NUMBERED"
	RedirectAction = "REDIRECT"
	ReIndexAction  = "REINDEX"
	RSSAction      = "RSS"
	TitleAction    = "TITLE"

	ExistOperator    = "?"
	ExistNotOperator = "!?"

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

// QueryPrefix is the prefix that denotes a query expression within a reference.
const QueryPrefix = "query:"

Changes to api/urlbuilder.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-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 api

import (
	"net/url"
	"strings"
)

type urlQuery struct{ key, val string }

// URLBuilder should be used to create zettelstore URLs.
type URLBuilder struct {

	prefix   string
	key      byte
	rawLocal string
	path     []string
	query    []urlQuery
	fragment string
}

// NewURLBuilder creates a new URL builder with the given prefix and key.
func NewURLBuilder(prefix string, key byte) *URLBuilder {



	return &URLBuilder{prefix: prefix, key: key}




}

// Clone an URLBuilder
func (ub *URLBuilder) Clone() *URLBuilder {
	cpy := new(URLBuilder)
	cpy.key = ub.key
	if len(ub.path) > 0 {
		cpy.path = make([]string, 0, len(ub.path))
		cpy.path = append(cpy.path, ub.path...)
	}
	if len(ub.query) > 0 {
		cpy.query = make([]urlQuery, 0, len(ub.query))
		cpy.query = append(cpy.query, ub.query...)
	}
	cpy.fragment = ub.fragment
	return cpy
}

// SetRawLocal sets everything that follows the prefix / key.
func (ub *URLBuilder) SetRawLocal(rawLocal string) *URLBuilder {
	for len(rawLocal) > 0 && rawLocal[0] == '/' {
		rawLocal = rawLocal[1:]
	}
	ub.rawLocal = rawLocal
	ub.path = nil
	ub.query = nil
	ub.fragment = ""
	return ub
}

// SetZid sets the zettel identifier.
func (ub *URLBuilder) SetZid(zid ZettelID) *URLBuilder {
	if len(ub.path) > 0 {
		panic("Cannot add Zid")
	}
	ub.rawLocal = ""
	ub.path = append(ub.path, string(zid))
	return ub
}

// AppendPath adds a new path element
func (ub *URLBuilder) AppendPath(p string) *URLBuilder {
	ub.rawLocal = ""
	for len(p) > 0 && p[0] == '/' {
		p = p[1:]
	}
	if p != "" {
		ub.path = append(ub.path, p)
	}
	return ub
}

// AppendKVQuery adds a new key/value query parameter
func (ub *URLBuilder) AppendKVQuery(key, value string) *URLBuilder {
	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})
	}
	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 {
	var sb strings.Builder

	sb.WriteString(ub.prefix)
	if ub.key != '/' {
		sb.WriteByte(ub.key)
	}
	if ub.rawLocal != "" {
		sb.WriteString(ub.rawLocal)
		return sb.String()
	}
	for i, p := range ub.path {
		if i > 0 || ub.key != '/' {
			sb.WriteByte('/')
		}
		sb.WriteString(url.PathEscape(p))
	}
	if len(ub.fragment) > 0 {
		sb.WriteByte('#')
		sb.WriteString(ub.fragment)
	}
	for i, q := range ub.query {
		if i == 0 {
			sb.WriteByte('?')
		} else {
			sb.WriteByte('&')
		}
		sb.WriteString(q.key)
		if val := q.val; val != "" {
			sb.WriteByte('=')
			sb.WriteString(url.QueryEscape(val))
		}
	}
	return sb.String()
}








>
>
>




|
<
<
<
<
<



>
|
<
<
<
<
<




>
>
>
|
>
>
>
>





|
<
<
<
<
<
<
<
<
|



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


<
<
<
<
|





|
<
<
<
<
<
<





<
|






<
|






<
|
<





<
|





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

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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package api

import "t73f.de/r/webs/urlbuilder"






// URLBuilder should be used to create zettelstore URLs.
type URLBuilder struct {
	base   urlbuilder.URLBuilder
	prefix string





}

// NewURLBuilder creates a new URL builder with the given prefix and key.
func NewURLBuilder(prefix string, key byte) *URLBuilder {
	for len(prefix) > 0 && prefix[len(prefix)-1] == '/' {
		prefix = prefix[0 : len(prefix)-1]
	}
	result := URLBuilder{prefix: prefix}
	if key != '/' {
		result.base.AddPath(string([]byte{key}))
	}
	return &result
}

// Clone an URLBuilder
func (ub *URLBuilder) Clone() *URLBuilder {
	cpy := new(URLBuilder)
	ub.base.Copy(&cpy.base)








	cpy.prefix = ub.prefix
	return cpy
}













// SetZid sets the zettel identifier.
func (ub *URLBuilder) SetZid(zid ZettelID) *URLBuilder {




	ub.base.AddPath(string(zid))
	return ub
}

// AppendPath adds a new path element
func (ub *URLBuilder) AppendPath(p string) *URLBuilder {
	ub.base.AddPath(p)






	return ub
}

// AppendKVQuery adds a new key/value query parameter
func (ub *URLBuilder) AppendKVQuery(key, value string) *URLBuilder {

	ub.base.AddQuery(key, value)
	return ub
}

// AppendQuery adds a new query
func (ub *URLBuilder) AppendQuery(value string) *URLBuilder {
	if value != "" {

		ub.base.AddQuery(QueryKeyQuery, value)
	}
	return ub
}

// ClearQuery removes all query parameters.
func (ub *URLBuilder) ClearQuery() *URLBuilder {

	ub.base.RemoveQueries()

	return ub
}

// SetFragment stores the fragment
func (ub *URLBuilder) SetFragment(s string) *URLBuilder {

	ub.base.SetFragment(s)
	return ub
}

// String produces a string value.
func (ub *URLBuilder) String() string {

	return ub.prefix + ub.base.String()































}

Changes to attrs/attrs.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-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 attrs stores attributes of zettel parts.
package attrs

import (
	"strings"

	"zettelstore.de/client.fossil/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 }








>
>
>








|







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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

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

import (
	"strings"

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

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

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

Changes to attrs/attrs_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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-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 attrs_test

import (
	"testing"

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

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








>
>
>







|







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) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package attrs_test

import (
	"testing"

	"t73f.de/r/zsc/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.

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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-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 client provides a client for accessing the Zettelstore via its API.
package client

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

	"zettelstore.de/client.fossil/api"
	"zettelstore.de/client.fossil/sexp"
	"zettelstore.de/sx.fossil"
	"zettelstore.de/sx.fossil/sxreader"

)

// Client contains all data to execute requests.
type Client struct {
	base      string
	username  string
	password  string








>
>
>


















|
|
|
|
>







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
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------

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

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

	"t73f.de/r/sx"
	"t73f.de/r/sx/sxreader"
	"t73f.de/r/zsc/api"
	"t73f.de/r/zsc/sexp"
	"t73f.de/r/zsc/sz"
)

// Client contains all data to execute requests.
type Client struct {
	base      string
	username  string
	password  string
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
	myURL := *u
	myURL.User = nil
	myURL.ForceQuery = false
	myURL.RawQuery = ""
	myURL.Fragment = ""
	myURL.RawFragment = ""
	base := myURL.String()
	if !strings.HasSuffix(base, "/") {
		base += "/"
	}
	c := Client{
		base: base,
		client: http.Client{
			Timeout: 10 * time.Second,
			Transport: &http.Transport{
				DialContext: (&net.Dialer{
					Timeout: 5 * time.Second, // TCP connect timeout
				}).DialContext,
				TLSHandshakeTimeout: 5 * time.Second,
			},
		},
	}
	return &c
}














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







<
<
<














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







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
	myURL := *u
	myURL.User = nil
	myURL.ForceQuery = false
	myURL.RawQuery = ""
	myURL.Fragment = ""
	myURL.RawFragment = ""
	base := myURL.String()



	c := Client{
		base: base,
		client: http.Client{
			Timeout: 10 * time.Second,
			Transport: &http.Transport{
				DialContext: (&net.Dialer{
					Timeout: 5 * time.Second, // TCP connect timeout
				}).DialContext,
				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
}
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
	return &Error{
		StatusCode: resp.StatusCode,
		Message:    resp.Status[4:],
		Body:       body,
	}
}


func (c *Client) newURLBuilder(key byte) *api.URLBuilder {
	return api.NewURLBuilder(c.base, key)
}
func (*Client) newRequest(ctx context.Context, method string, ub *api.URLBuilder, body io.Reader) (*http.Request, error) {
	return http.NewRequestWithContext(ctx, method, ub.String(), body)
}

func (c *Client) executeRequest(req *http.Request) (*http.Response, error) {







>
|







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	return &Error{
		StatusCode: resp.StatusCode,
		Message:    resp.Status[4:],
		Body:       body,
	}
}

// NewURLBuilder creates a new URL builder for the client with the given key.
func (c *Client) NewURLBuilder(key byte) *api.URLBuilder {
	return api.NewURLBuilder(c.base, key)
}
func (*Client) newRequest(ctx context.Context, method string, ub *api.URLBuilder, body io.Reader) (*http.Request, error) {
	return http.NewRequestWithContext(ctx, method, ub.String(), body)
}

func (c *Client) executeRequest(req *http.Request) (*http.Response, error) {
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
	if err != nil {
		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)
	return nil
}

func (c *Client) updateToken(ctx context.Context) error {
	if c.username == "" {
		return nil
	}
	if time.Now().After(c.expires) {
		return c.Authenticate(ctx)
	}
	return c.RefreshToken(ctx)
}

// Authenticate sets a new token by sending user name and password.
func (c *Client) Authenticate(ctx context.Context) error {
	authData := url.Values{"username": {c.username}, "password": {c.password}}
	req, err := c.newRequest(ctx, http.MethodPost, c.newURLBuilder('a'), strings.NewReader(authData.Encode()))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	return c.executeAuthRequest(req)
}

// RefreshToken updates the access token
func (c *Client) RefreshToken(ctx context.Context) error {
	req, err := c.newRequest(ctx, http.MethodPut, c.newURLBuilder('a'), nil)
	if err != nil {
		return err
	}
	return c.executeAuthRequest(req)
}

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







|




|

















|









|








|







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
	if err != nil {
		return err
	}
	vals, err := sexp.ParseList(obj, "ssi")
	if err != nil {
		return err
	}
	token := vals[1].(sx.String).GetValue()
	if len(token) < 4 {
		return fmt.Errorf("no valid token found: %q", token)
	}
	c.token = token
	c.tokenType = vals[0].(sx.String).GetValue()
	c.expires = time.Now().Add(time.Duration(vals[2].(sx.Int64)*9/10) * time.Second)
	return nil
}

func (c *Client) updateToken(ctx context.Context) error {
	if c.username == "" {
		return nil
	}
	if time.Now().After(c.expires) {
		return c.Authenticate(ctx)
	}
	return c.RefreshToken(ctx)
}

// Authenticate sets a new token by sending user name and password.
func (c *Client) Authenticate(ctx context.Context) error {
	authData := url.Values{"username": {c.username}, "password": {c.password}}
	req, err := c.newRequest(ctx, http.MethodPost, c.NewURLBuilder('a'), strings.NewReader(authData.Encode()))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	return c.executeAuthRequest(req)
}

// RefreshToken updates the access token
func (c *Client) RefreshToken(ctx context.Context) error {
	req, err := c.newRequest(ctx, http.MethodPut, c.NewURLBuilder('a'), nil)
	if err != nil {
		return err
	}
	return c.executeAuthRequest(req)
}

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

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

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

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







|




















|







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

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

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

// QueryZettel returns a list of all Zettel.
func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) {
	ub := c.NewURLBuilder('z').AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	switch resp.StatusCode {
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
		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)
	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()







|







301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
		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)
	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()
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
		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
}

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 {







|







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		return "", "", nil, err
	}
	hVals, err := sexp.ParseList(vals[2], "ys")
	if err != nil {
		return "", "", nil, err
	}
	metaList, err := parseMetaList(vals[3].(*sx.Pair))
	return sz.GoValue(qVals[1]), sz.GoValue(hVals[1]), metaList, err
}

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 {
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	return zid, nil
}

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







|







406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	return zid, nil
}

// 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 or of type 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
413
414
415
416
417
418
419
420











































421
422
423
424
425
426
427
428
429
430
					agg[key] = append(agg[key], zid)
				}
			}
		}
	}
	return agg, nil
}












































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








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


|







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
					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
	case http.StatusFound:
		zid := api.ZettelID(data)
		if zid.IsValid() {
			return zid, nil
		}
		return api.InvalidZID, 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
	}
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
		return nil, statusToError(resp)
	}
	return data, err
}

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







|







496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
		return nil, statusToError(resp)
	}
	return data, err
}

// GetZettelData returns a zettel as a struct of its parts.
func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	ub.AppendKVQuery(api.QueryKeyPart, api.PartZettel)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err == nil {
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			return api.ZettelData{}, statusToError(resp)
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

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

func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) {
	ub := c.newURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, enc.String())
	ub.AppendKVQuery(api.QueryKeyPart, api.PartContent)
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
	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)
}

// 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)
}

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

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







|




















|
|



|
|


|
|















|




|







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

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

func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, enc.String())
	ub.AppendKVQuery(api.QueryKeyPart, api.PartContent)
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
	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) (sx.Object, error) {
	return c.getSz(ctx, zid, part, true)
}

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

func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool) (sx.Object, error) {
	ub := c.NewURLBuilder('z').SetZid(zid)
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSz)
	if part != "" {
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	if parseOnly {
		ub.AppendKVQuery(api.QueryKeyParseOnly, "")
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8)).Read()
}

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


















		Meta:   meta,
		Rights: rights,
	}, nil
}

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

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

// RenameZettel renames a zettel.
func (c *Client) RenameZettel(ctx context.Context, oldZid, newZid api.ZettelID) error {
	ub := c.newURLBuilder('z').SetZid(oldZid)
	h := http.Header{
		api.HeaderDestination: {c.newURLBuilder('z').SetZid(newZid).String()},
	}
	resp, err := c.buildAndExecuteRequest(ctx, api.MethodMove, ub, nil, h)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

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

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

// GetVersionInfo returns version information..
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.newURLBuilder('x'), nil, nil)
	if err != nil {
		return VersionInfo{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return VersionInfo{}, statusToError(resp)
	}
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if err == nil {
		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
		}
	}
	return VersionInfo{}, err
}

// VersionInfo contains version information.
type VersionInfo struct {
	Major int
	Minor int
	Patch int
	Info  string
	Hash  string
}

























|

















|













|

|














|













|













|















|
|














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

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

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

// RenameZettel renames a zettel.
func (c *Client) RenameZettel(ctx context.Context, oldZid, newZid api.ZettelID) error {
	ub := c.NewURLBuilder('z').SetZid(oldZid)
	h := http.Header{
		api.HeaderDestination: {c.NewURLBuilder('z').SetZid(newZid).String()},
	}
	resp, err := c.buildAndExecuteRequest(ctx, api.MethodMove, ub, nil, h)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusNoContent {
		return statusToError(resp)
	}
	return nil
}

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

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

// GetVersionInfo returns version information..
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.NewURLBuilder('x'), nil, nil)
	if err != nil {
		return VersionInfo{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return VersionInfo{}, statusToError(resp)
	}
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	if err == nil {
		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).GetValue(),
				Hash:  vals[4].(sx.String).GetValue(),
			}, nil
		}
	}
	return VersionInfo{}, err
}

// VersionInfo contains version information.
type VersionInfo struct {
	Major int
	Minor int
	Patch int
	Info  string
	Hash  string
}

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

Changes to client/client_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
//-----------------------------------------------------------------------------
// 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 client_test

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

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








>
>
>











|
<
|
<







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) 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022-present Detlef Stern
//-----------------------------------------------------------------------------

package client_test

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

	"t73f.de/r/zsc/api"

	"t73f.de/r/zsc/client"

)

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

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







<
<
<
|







44
45
46
47
48
49
50



51
52
53
54
55
56
57
58
		}
		return
	}
}

func TestGetSzZettel(t *testing.T) {
	c := getClient()



	value, err := c.GetEvaluatedSz(context.Background(), api.ZidDefaultHome, api.PartContent)
	if err != nil {
		t.Error(err)
		return
	}
	if value.IsNil() {
		t.Error("No data")
	}

Changes to go.mod.

1
2
3
4
5




module zettelstore.de/client.fossil

go 1.20

require zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284




|

|

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

go 1.22

require (
	t73f.de/r/sx v0.0.0-20240418072254-b6eff7d787f9
	t73f.de/r/sxwebs v0.0.0-20240422143910-320427142398
	t73f.de/r/webs v0.0.0-20240422103534-8f5067bc11bc
)

Changes to go.sum.

1
2




zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284 h1:26xwZWEjdyL3wObczc/PKugv0EY6mgSH5NUe5kYFCt4=
zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284/go.mod h1:nsWXVrQG8RNKtoXzEMrWXNMdnpfIDU6Hb0pk56KpVKE=




|
|
>
>
>
>
1
2
3
4
5
6
t73f.de/r/sx v0.0.0-20240418072254-b6eff7d787f9 h1:lVPkYN8+J9f6JA9SmoF6icvpLxz4u3h1MCTuDYJYwdU=
t73f.de/r/sx v0.0.0-20240418072254-b6eff7d787f9/go.mod h1:G9pD1j2R6y9ZkPBb81mSnmwaAvTOg7r6jKp/OF7WeFA=
t73f.de/r/sxwebs v0.0.0-20240422143910-320427142398 h1:/G054FNxS8zEYbdhOTNk+GhdhjWBVt398FTm1Ud4A4o=
t73f.de/r/sxwebs v0.0.0-20240422143910-320427142398/go.mod h1:PtIkpRfTTiQITciKaWcTiAwy9FJ63WSQKciTp/dJbOA=
t73f.de/r/webs v0.0.0-20240422103534-8f5067bc11bc h1:i6tm/AEJUs8J8m7iDP8bTZgM0wYERh97RR47soJglxs=
t73f.de/r/webs v0.0.0-20240422103534-8f5067bc11bc/go.mod h1:UGAAtul0TK5ACeZ6zTS3SX6GqwMFXxlUpHiV8oqNq5w=

Added input/entity.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
//-----------------------------------------------------------------------------
// 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022-present Detlef Stern
//-----------------------------------------------------------------------------

package input

import (
	"html"
	"unicode"
)

// ScanEntity scans either a named or a numbered entity and returns it as a string.
//
// For numbered entities (like &#123; or &#x123;) html.UnescapeString returns
// sometimes other values as expected, if the number is not well-formed. This
// may happen because of some strange HTML parsing rules. But these do not
// apply to Zettelmarkup. Therefore, I parse the number here in the code.
func (inp *Input) ScanEntity() (res string, success bool) {
	if inp.Ch != '&' {
		return "", false
	}
	pos := inp.Pos
	inp.Next()
	if inp.Ch == '#' {
		inp.Next()
		if inp.Ch == 'x' || inp.Ch == 'X' {
			return inp.scanEntityBase16()
		}
		return inp.scanEntityBase10()
	}
	return inp.scanEntityNamed(pos)
}

func (inp *Input) scanEntityBase16() (string, bool) {
	inp.Next()
	if inp.Ch == ';' {
		return "", false
	}
	code := 0
	for {
		switch ch := inp.Ch; ch {
		case ';':
			inp.Next()
			if r := rune(code); isValidEntity(r) {
				return string(r), true
			}
			return "", false
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			code = 16*code + int(ch-'0')
		case 'a', 'b', 'c', 'd', 'e', 'f':
			code = 16*code + int(ch-'a'+10)
		case 'A', 'B', 'C', 'D', 'E', 'F':
			code = 16*code + int(ch-'A'+10)
		default:
			return "", false
		}
		if code > unicode.MaxRune {
			return "", false
		}
		inp.Next()
	}
}

func (inp *Input) scanEntityBase10() (string, bool) {
	// Base 10 code
	if inp.Ch == ';' {
		return "", false
	}
	code := 0
	for {
		switch ch := inp.Ch; ch {
		case ';':
			inp.Next()
			if r := rune(code); isValidEntity(r) {
				return string(r), true
			}
			return "", false
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			code = 10*code + int(ch-'0')
		default:
			return "", false
		}
		if code > unicode.MaxRune {
			return "", false
		}
		inp.Next()
	}
}
func (inp *Input) scanEntityNamed(pos int) (string, bool) {
	for {
		switch inp.Ch {
		case EOS, '\n', '\r', '&':
			return "", false
		case ';':
			inp.Next()
			es := string(inp.Src[pos:inp.Pos])
			ues := html.UnescapeString(es)
			if es == ues {
				return "", false
			}
			return ues, true
		default:
			inp.Next()
		}
	}
}

// isValidEntity checks if the given code is valid for an entity.
//
// According to https://html.spec.whatwg.org/multipage/syntax.html#character-references
// ""The numeric character reference forms described above are allowed to reference any code point
// excluding U+000D CR, noncharacters, and controls other than ASCII whitespace.""
func isValidEntity(r rune) bool {
	// No C0 control and no "code point in the range U+007F DELETE to U+009F APPLICATION PROGRAM COMMAND, inclusive."
	if r < ' ' || ('\u007f' <= r && r <= '\u009f') {
		return false
	}

	// If below any noncharacter code point, return true
	//
	// See: https://infra.spec.whatwg.org/#noncharacter
	if r < '\ufdd0' {
		return true
	}

	// First range of noncharacter code points: "(...) in the range U+FDD0 to U+FDEF, inclusive"
	if r <= '\ufdef' {
		return false
	}

	// Other noncharacter code points:
	switch r {
	case '\uFFFE', '\uFFFF',
		'\U0001FFFE', '\U0001FFFF',
		'\U0002FFFE', '\U0002FFFF',
		'\U0003FFFE', '\U0003FFFF',
		'\U0004FFFE', '\U0004FFFF',
		'\U0005FFFE', '\U0005FFFF',
		'\U0006FFFE', '\U0006FFFF',
		'\U0007FFFE', '\U0007FFFF',
		'\U0008FFFE', '\U0008FFFF',
		'\U0009FFFE', '\U0009FFFF',
		'\U000AFFFE', '\U000AFFFF',
		'\U000BFFFE', '\U000BFFFF',
		'\U000CFFFE', '\U000CFFFF',
		'\U000DFFFE', '\U000DFFFF',
		'\U000EFFFE', '\U000EFFFF',
		'\U000FFFFE', '\U000FFFFF',
		'\U0010FFFE', '\U0010FFFF':
		return false
	}
	return true
}

Added input/entity_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
55
56
57
58
59
60
61
62
63
64
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package input_test

import (
	"testing"

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

func TestScanEntity(t *testing.T) {
	t.Parallel()
	var testcases = []struct {
		text string
		exp  string
	}{
		{"", ""},
		{"a", ""},
		{"&amp;", "&"},
		{"&#33;", "!"},
		{"&#x33;", "3"},
		{"&quot;", "\""},
	}
	for id, tc := range testcases {
		inp := input.NewInput([]byte(tc.text))
		got, ok := inp.ScanEntity()
		if !ok {
			if tc.exp != "" {
				t.Errorf("ID=%d, text=%q: expected error, but got %q", id, tc.text, got)
			}
			if inp.Pos != 0 {
				t.Errorf("ID=%d, text=%q: input position advances to %d", id, tc.text, inp.Pos)
			}
			continue
		}
		if tc.exp != got {
			t.Errorf("ID=%d, text=%q: expected %q, but got %q", id, tc.text, tc.exp, got)
		}
	}
}

func TestScanIllegalEntity(t *testing.T) {
	t.Parallel()
	testcases := []string{"", "a", "& Input &rarr;", "&#9;", "&#x1f;"}
	for i, tc := range testcases {
		inp := input.NewInput([]byte(tc))
		got, ok := inp.ScanEntity()
		if ok {
			t.Errorf("%d: scanning %q was unexpected successful, got %q", i, tc, got)
			continue
		}
	}
}

Added input/input.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package input provides an abstraction for data to be read.
package input

import "unicode/utf8"

// Input is an abstract input source
type Input struct {
	// Read-only, will never change
	Src []byte // The source string

	// Read-only, will change
	Ch      rune // current character
	Pos     int  // character position in src
	readPos int  // reading position (position after current character)
}

// NewInput creates a new input source.
func NewInput(src []byte) *Input {
	inp := &Input{Src: src}
	inp.Next()
	return inp
}

// EOS = End of source
const EOS = rune(-1)

// Next reads the next rune into inp.Ch and returns it too.
func (inp *Input) Next() rune {
	if inp.readPos >= len(inp.Src) {
		inp.Pos = len(inp.Src)
		inp.Ch = EOS
		return EOS
	}
	inp.Pos = inp.readPos
	r, w := rune(inp.Src[inp.readPos]), 1
	if r >= utf8.RuneSelf {
		r, w = utf8.DecodeRune(inp.Src[inp.readPos:])
	}
	inp.readPos += w
	inp.Ch = r
	return r
}

// Peek returns the rune following the most recently read rune without
// advancing. If end-of-source was already found peek returns EOS.
func (inp *Input) Peek() rune {
	return inp.PeekN(0)
}

// PeekN returns the n-th rune after the most recently read rune without
// advancing. If end-of-source was already found peek returns EOS.
func (inp *Input) PeekN(n int) rune {
	pos := inp.readPos + n
	if pos < len(inp.Src) {
		r := rune(inp.Src[pos])
		if r >= utf8.RuneSelf {
			r, _ = utf8.DecodeRune(inp.Src[pos:])
		}
		if r == '\t' {
			return ' '
		}
		return r
	}
	return EOS
}

// Accept checks if the given string is a prefix of the text to be parsed.
// If successful, advance position and current character.
// String must only contain bytes < 128.
// If not successful, everything remains as it is.
func (inp *Input) Accept(s string) bool {
	pos := inp.Pos
	remaining := len(inp.Src) - pos
	if s == "" || len(s) > remaining {
		return false
	}
	// According to internal documentation of bytes.Equal, the string() will not allocate any memory.
	if readPos := pos + len(s); s == string(inp.Src[pos:readPos]) {
		inp.readPos = readPos
		inp.Next()
		return true
	}
	return false
}

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

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

// SetPos allows to reset the read position.
func (inp *Input) SetPos(pos int) {
	if inp.Pos != pos {
		inp.readPos = pos
		inp.Next()
	}
}

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

// ScanLineContent reads the reaining input stream and interprets it as lines of text.
func (inp *Input) ScanLineContent() []byte {
	result := make([]byte, 0, len(inp.Src)-inp.Pos+1)
	for {
		inp.EatEOL()
		posL := inp.Pos
		if inp.Ch == EOS {
			return result
		}
		inp.SkipToEOL()
		if len(result) > 0 {
			result = append(result, '\n')
		}
		result = append(result, inp.Src[posL:inp.Pos]...)
	}
}

Added input/input_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package input_test provides some unit-tests for reading data.
package input_test

import (
	"testing"

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

func TestEatEOL(t *testing.T) {
	t.Parallel()
	inp := input.NewInput(nil)
	inp.EatEOL()
	if inp.Ch != input.EOS {
		t.Errorf("No EOS found: %q", inp.Ch)
	}
	if inp.Pos != 0 {
		t.Errorf("Pos != 0: %d", inp.Pos)
	}

	inp = input.NewInput([]byte("ABC"))
	if inp.Ch != 'A' {
		t.Errorf("First ch != 'A', got %q", inp.Ch)
	}
	inp.EatEOL()
	if inp.Ch != 'A' {
		t.Errorf("First ch != 'A', got %q", inp.Ch)
	}
}

func TestAccept(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		accept string
		src    string
		acc    bool
		exp    rune
	}{
		{"", "", false, input.EOS},
		{"AB", "abc", false, 'a'},
		{"AB", "ABC", true, 'C'},
		{"AB", "AB", true, input.EOS},
		{"AB", "A", false, 'A'},
	}
	for i, tc := range testcases {
		inp := input.NewInput([]byte(tc.src))
		acc := inp.Accept(tc.accept)
		if acc != tc.acc {
			t.Errorf("%d: %q.Accept(%q) == %v, but got %v", i, tc.src, tc.accept, tc.acc, acc)
		}
		if got := inp.Ch; tc.exp != got {
			t.Errorf("%d: %q.Accept(%q) should result in run %v, but got %v", i, tc.src, tc.accept, tc.exp, got)
		}
	}
}

Added input/runes.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package input

import "unicode"

// IsSpace returns true if rune is a whitespace.
func IsSpace(ch rune) bool {
	switch ch {
	case ' ', '\t':
		return true
	case '\n', '\r', EOS:
		return false
	}
	return unicode.IsSpace(ch)
}

Changes to maps/maps.go.

1
2
3
4
5
6
7
8



9
10
11
12
13
14
15
//-----------------------------------------------------------------------------
// 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 maps

import "sort"

func Keys[T any](m map[string]T) []string {








>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//-----------------------------------------------------------------------------
// 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022-present Detlef Stern
//-----------------------------------------------------------------------------

package maps

import "sort"

func Keys[T any](m map[string]T) []string {

Changes to maps/maps_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
//-----------------------------------------------------------------------------
// 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 maps_test

import (
	"testing"

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

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








>
>
>







|







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) 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022-present Detlef Stern
//-----------------------------------------------------------------------------

package maps_test

import (
	"testing"

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

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

Changes to 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
//-----------------------------------------------------------------------------
// 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()
	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.MakeString(zettel.Encoding)),
		sx.MakeList(sf.MustMake("content"), sx.MakeString(zettel.Content)),
	)
}

func ParseZettel(obj sx.Object) (api.ZettelData, error) {
	vals, err := ParseList(obj, "ypppp")
	if err != nil {
		return api.ZettelData{}, err








>
>
>











|
|




<

|
|
|
|
|







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
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2023-present Detlef Stern
//-----------------------------------------------------------------------------

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

import (
	"errors"
	"fmt"
	"sort"

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

// EncodeZettel transforms zettel data into a sx object.
func EncodeZettel(zettel api.ZettelData) sx.Object {

	return sx.MakeList(
		sx.MakeSymbol("zettel"),
		meta2sz(zettel.Meta),
		sx.MakeList(sx.MakeSymbol("rights"), sx.Int64(int64(zettel.Rights))),
		sx.MakeList(sx.MakeSymbol("encoding"), sx.MakeString(zettel.Encoding)),
		sx.MakeList(sx.MakeSymbol("content"), sx.MakeString(zettel.Content)),
	)
}

func ParseZettel(obj sx.Object) (api.ZettelData, error) {
	vals, err := ParseList(obj, "ypppp")
	if err != nil {
		return api.ZettelData{}, err
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
	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()
	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.MakeString(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")







|
|





<

|
|
|



|
|
|






|
|

|













|







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
	if errSym := CheckSymbol(contentVals[0], "content"); errSym != nil {
		return api.ZettelData{}, errSym
	}

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

// EncodeMetaRights translates metadata/rights into a sx object.
func EncodeMetaRights(mr api.MetaRights) *sx.Pair {

	return sx.MakeList(
		sx.SymbolList,
		meta2sz(mr.Meta),
		sx.MakeList(sx.MakeSymbol("rights"), sx.Int64(int64(mr.Rights))),
	)
}

func meta2sz(m api.ZettelMeta) sx.Object {
	var result sx.ListBuilder
	result.Add(sx.MakeSymbol("meta"))
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		val := sx.MakeList(sx.MakeSymbol(k), sx.MakeString(m[k]))
		result.Add(val)
	}
	return result.List()
}

// 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)).GetValue()] = mVals[1].(sx.String).GetValue()
	}
	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")
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
	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.GetBoolean(car)
		case 'i':
			val, ok = car.(sx.Int64)
		case 'o':
			val, ok = car, true
		case 'p':
			val, ok = sx.GetPair(car)
		case 's':







|











|










|







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
	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.Vector, 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.Vector, 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':
192
193
194
195
196
197
198
199
200
201
202
203

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







|




193
194
195
196
197
198
199
200
201
202
203
204

// 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.GetValue(); got != name {
		return fmt.Errorf("symbol %q expected, but got: %q", name, got)
	}
	return nil
}

Changes to 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
//-----------------------------------------------------------------------------
// 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.MakeString("a"), "s"); err == nil {
		t.Error("expected an error, but got: ", elems)
	}
	if elems, err := sexp.ParseList(sx.Nil(), ""); err != nil {








>
>
>







|
|







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
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2023-present Detlef Stern
//-----------------------------------------------------------------------------

package sexp_test

import (
	"testing"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/sexp"
)

func TestParseObject(t *testing.T) {
	if elems, err := sexp.ParseList(sx.MakeString("a"), "s"); err == nil {
		t.Error("expected an error, but got: ", elems)
	}
	if elems, err := sexp.ParseList(sx.Nil(), ""); err != nil {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

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

	if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "s"); err != nil {
		t.Error(err)







|
|







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

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

	if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "s"); err != nil {
		t.Error(err)

Added shtml/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
//-----------------------------------------------------------------------------
// Copyright (c) 2024-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2024-present Detlef Stern
//-----------------------------------------------------------------------------

package shtml

import "t73f.de/r/sx"

// Symbols for HTML header tags
var (
	SymBody   = sx.MakeSymbol("body")
	SymHead   = sx.MakeSymbol("head")
	SymHtml   = sx.MakeSymbol("html")
	SymMeta   = sx.MakeSymbol("meta")
	SymScript = sx.MakeSymbol("script")
	SymTitle  = sx.MakeSymbol("title")
)

// Symbols for HTML body tags
var (
	SymA          = sx.MakeSymbol("a")
	SymASIDE      = sx.MakeSymbol("aside")
	symBLOCKQUOTE = sx.MakeSymbol("blockquote")
	symBR         = sx.MakeSymbol("br")
	symCITE       = sx.MakeSymbol("cite")
	symCODE       = sx.MakeSymbol("code")
	symDD         = sx.MakeSymbol("dd")
	symDEL        = sx.MakeSymbol("del")
	SymDIV        = sx.MakeSymbol("div")
	symDL         = sx.MakeSymbol("dl")
	symDT         = sx.MakeSymbol("dt")
	symEM         = sx.MakeSymbol("em")
	SymEMBED      = sx.MakeSymbol("embed")
	SymFIGURE     = sx.MakeSymbol("figure")
	SymH1         = sx.MakeSymbol("h1")
	SymH2         = sx.MakeSymbol("h2")
	SymHR         = sx.MakeSymbol("hr")
	SymIMG        = sx.MakeSymbol("img")
	symINS        = sx.MakeSymbol("ins")
	symKBD        = sx.MakeSymbol("kbd")
	SymLI         = sx.MakeSymbol("li")
	symMARK       = sx.MakeSymbol("mark")
	SymOL         = sx.MakeSymbol("ol")
	SymP          = sx.MakeSymbol("p")
	symPRE        = sx.MakeSymbol("pre")
	symSAMP       = sx.MakeSymbol("samp")
	SymSPAN       = sx.MakeSymbol("span")
	SymSTRONG     = sx.MakeSymbol("strong")
	symSUB        = sx.MakeSymbol("sub")
	symSUP        = sx.MakeSymbol("sup")
	symTABLE      = sx.MakeSymbol("table")
	symTBODY      = sx.MakeSymbol("tbody")
	symTHEAD      = sx.MakeSymbol("thead")
	symTD         = sx.MakeSymbol("td")
	symTR         = sx.MakeSymbol("tr")
	SymUL         = sx.MakeSymbol("ul")
)

// Symbols for HTML attribute keys
var (
	symAttrAlt    = sx.MakeSymbol("alt")
	SymAttrClass  = sx.MakeSymbol("class")
	SymAttrHref   = sx.MakeSymbol("href")
	SymAttrId     = sx.MakeSymbol("id")
	SymAttrLang   = sx.MakeSymbol("lang")
	SymAttrOpen   = sx.MakeSymbol("open")
	SymAttrRel    = sx.MakeSymbol("rel")
	SymAttrRole   = sx.MakeSymbol("role")
	SymAttrSrc    = sx.MakeSymbol("src")
	SymAttrTarget = sx.MakeSymbol("target")
	SymAttrTitle  = sx.MakeSymbol("title")
	SymAttrType   = sx.MakeSymbol("type")
	SymAttrValue  = sx.MakeSymbol("value")
)

Changes to shtml/shtml.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
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
//-----------------------------------------------------------------------------
// 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 shtml transforms a s-expr encoded zettel AST into a s-expr representation of HTML.
package shtml

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/sxbuiltins/quote"
	"zettelstore.de/sx.fossil/sxeval"
	"zettelstore.de/sx.fossil/sxhtml"
)

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

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

// NewTransformer creates a new transformer object.
func NewTransformer(headingOffset int, sf sx.SymbolFactory) *Transformer {
	if sf == nil {
		sf = sx.MakeMappedFactory()
	}
	return &Transformer{
		sf:            sf,
		rebinder:      nil,
		headingOffset: int64(headingOffset),
		symAttr:       sf.MustMake(sxhtml.NameSymAttr),
		symClass:      sf.MustMake("class"),
		symMeta:       sf.MustMake("meta"),
		symA:          sf.MustMake("a"),
		symSpan:       sf.MustMake("span"),
	}


}





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

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

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

// Make a new HTML symbol.
func (tr *Transformer) Make(s string) *sx.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 }

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

// TransformMeta creates a HTML meta s-expression






func (tr *Transformer) TransformMeta(a attrs.Attributes) *sx.Pair {


	return sx.Nil().Cons(tr.TransformAttrbute(a)).Cons(tr.symMeta)


}



// Transform an AST s-expression into a list of HTML s-expressions.
func (tr *Transformer) Transform(lst *sx.Pair) (*sx.Pair, error) {
	astSF := sx.FindSymbolFactory(lst)
	if astSF != nil {
		if astSF == tr.sf {
			panic("Invalid AST SymbolFactory")
		}
	} else {
		astSF = sx.MakeMappedFactory()
	}
	astEnv := sx.MakeRootEnvironment()
	engine := sxeval.MakeEngine(astSF, astEnv)
	quote.InstallQuoteSyntax(astEnv, astSF.MustMake(sz.NameSymQuote))
	te := TransformEnv{
		tr:      tr,
		astSF:   astSF,
		astEnv:  astEnv,
		err:     nil,
		textEnc: text.NewEncoder(astSF),
	}
	te.initialize()
	if rb := tr.rebinder; rb != nil {
		rb(&te)
	}







	val, err := engine.Eval(te.astEnv, lst)
	if err != nil {
		return nil, err
	}
	res, isPair := sx.GetPair(val)
	if !isPair {
		panic("Result is not a list")
	}
	for i := 0; i < len(tr.endnotes); i++ {
		// May extend tr.endnotes
		val, err = engine.Eval(te.astEnv, tr.endnotes[i].noteAST)
		if err != nil {
			return res, err
		}
		en, ok := sx.GetPair(val)
		if !ok {
			panic("Endnote is not a list")
		}
		tr.endnotes[i].noteHx = en
	}
	return res, err

}

// Endnotes returns a SHTML object with all collected endnotes.
func (tr *Transformer) Endnotes() *sx.Pair {
	if len(tr.endnotes) == 0 {
		return nil
	}
	result := sx.Nil().Cons(tr.Make("ol"))


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

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

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

		li := sx.Nil().Cons(tr.Make("li"))

		li.AppendBang(attrs).
			ExtendBang(fni.noteHx).
			AppendBang(sx.MakeString(" ")).AppendBang(backref)

		currResult = currResult.AppendBang(li)
	}
	tr.endnotes = nil
	return result
}















// TransformEnv is the environment where the actual transformation takes places.
type TransformEnv struct {
	tr          *Transformer
	astSF       sx.SymbolFactory
	astEnv      sx.Environment
	err         error
	textEnc     *text.Encoder
	symNoEscape *sx.Symbol
	symAttr     *sx.Symbol
	symA        *sx.Symbol
	symSpan     *sx.Symbol
	symP        *sx.Symbol

}

func (te *TransformEnv) initialize() {


	te.symNoEscape = te.Make(sxhtml.NameSymNoEscape)


	te.symAttr = te.tr.symAttr
	te.symA = te.tr.symA
	te.symSpan = te.tr.symSpan

	te.symP = te.Make("p")













	te.bind(sz.NameSymList, 0, listArgs)




	te.bindMetadata()


	te.bindBlocks()







	te.bindInlines()




}


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









func (te *TransformEnv) bindMetadata() {
	te.bind(sz.NameSymMeta, 0, listArgs)
	te.bind(sz.NameSymTypeZettelmarkup, 2, func(args []sx.Object) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getSymbol(args[0]).String()).
			Set("content", te.textEnc.Encode(te.getList(args[1])))
		return te.transformMeta(a)
	})
	metaString := func(args []sx.Object) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", te.getSymbol(args[0]).Name()).
			Set("content", te.getString(args[1]).String())
		return te.transformMeta(a)
	}
	te.bind(sz.NameSymTypeCredential, 2, metaString)
	te.bind(sz.NameSymTypeEmpty, 2, metaString)
	te.bind(sz.NameSymTypeID, 2, metaString)
	te.bind(sz.NameSymTypeNumber, 2, metaString)
	te.bind(sz.NameSymTypeString, 2, metaString)
	te.bind(sz.NameSymTypeTimestamp, 2, metaString)
	te.bind(sz.NameSymTypeURL, 2, metaString)
	te.bind(sz.NameSymTypeWord, 2, metaString)

	metaSet := func(args []sx.Object) sx.Object {
		var sb strings.Builder
		for elem := te.getList(args[1]); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(te.getString(elem.Car()).String())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", te.getSymbol(args[0]).Name()).
			Set("content", s)
		return te.transformMeta(a)
	}
	te.bind(sz.NameSymTypeIDSet, 2, metaSet)
	te.bind(sz.NameSymTypeTagSet, 2, metaSet)







	te.bind(sz.NameSymTypeWordSet, 2, metaSet)



}

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


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


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

		if result, isPair := sx.GetPair(args[4]); isPair && result != nil {
			if len(a) > 0 {
				result = result.Cons(te.transformAttribute(a))
			}
			return result.Cons(te.Make("h" + level))
		}
		return sx.MakeList(te.Make("h"+level), sx.MakeString("<MISSING TEXT>"))
	})
	te.bind(sz.NameSymThematic, 0, func(args []sx.Object) sx.Object {
		result := sx.Nil()
		if len(args) > 0 {
			if attrList := te.getList(args[0]); attrList != nil {
				result = result.Cons(te.transformAttribute(sz.GetAttributes(attrList)))
			}
		}
		return result.Cons(te.Make("hr"))
	})

	te.bind(sz.NameSymListOrdered, 0, te.makeListFn("ol"))
	te.bind(sz.NameSymListUnordered, 0, te.makeListFn("ul"))
	te.bind(sz.NameSymDescription, 0, func(args []sx.Object) sx.Object {
		if len(args) == 0 {
			return sx.Nil()
		}
		items := sx.Nil().Cons(te.Make("dl"))
		curItem := items
		for pos := 0; pos < len(args); pos++ {
			term := te.getList(args[pos])
			curItem = curItem.AppendBang(term.Cons(te.Make("dt")))
			pos++
			if pos >= len(args) {
				break
			}
			ddBlock := te.getList(args[pos])
			if ddBlock == nil {
				break

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

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

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

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

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

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

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

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

	te.bind(sz.NameSymBLOB, 3, func(args []sx.Object) sx.Object {
		return te.transformBLOB(te.getList(args[0]), te.getString(args[1]), te.getString(args[2]))
	})

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

func (te *TransformEnv) makeListFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args []sx.Object) sx.Object {
		result := sx.Nil().Cons(sym)
		last := result
		for _, elem := range args {
			item := sx.Nil().Cons(te.Make("li"))
			if res, isPair := sx.GetPair(elem); isPair {
				item.ExtendBang(res)
			}
			last = last.AppendBang(item)
		}
		return result
	}
}
func (te *TransformEnv) transformTableRow(pairs *sx.Pair) *sx.Pair {








	row := sx.Nil().Cons(te.Make("tr"))

	if pairs == nil {
		return nil
	}
	curRow := row

	for pair := pairs; pair != nil; pair = pair.Tail() {
		curRow = curRow.AppendBang(pair.Car())
	}
	return row
}

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

func (te *TransformEnv) makeRegionFn(sym *sx.Symbol, genericToClass bool) transformFn {
	return func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


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

		result := sx.Nil()
		if len(a) > 0 {
			result = result.Cons(te.transformAttribute(a))
		}
		result = result.Cons(sym)
		currResult := result.LastPair()
		if region, isPair := sx.GetPair(args[1]); isPair {

			currResult = currResult.ExtendBang(region)
		}

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

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

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

	te.bind(sz.NameSymLinkInvalid, 2, func(args []sx.Object) sx.Object {
		// a := te.getAttributes(args)


		var inline *sx.Pair
		if len(args) > 2 {
			inline = sx.MakeList(args[2:]...)
		}
		if inline == nil {
			inline = sx.Nil().Cons(args[1])
		}
		return inline.Cons(te.symSpan)
	})
	transformHREF := func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


		refValue := te.getString(args[1])
		return te.transformLink(a.Set("href", refValue.String()), refValue, args[2:])
	}
	te.bind(sz.NameSymLinkZettel, 2, transformHREF)
	te.bind(sz.NameSymLinkSelf, 2, transformHREF)
	te.bind(sz.NameSymLinkFound, 2, transformHREF)
	te.bind(sz.NameSymLinkBroken, 2, func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


		refValue := te.getString(args[1])
		return te.transformLink(a.AddClass("broken"), refValue, args[2:])
	})
	te.bind(sz.NameSymLinkHosted, 2, transformHREF)
	te.bind(sz.NameSymLinkBased, 2, transformHREF)
	te.bind(sz.NameSymLinkQuery, 2, func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


		refValue := te.getString(args[1])
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String())
		return te.transformLink(a.Set("href", query), refValue, args[2:])
	})
	te.bind(sz.NameSymLinkExternal, 2, func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


		refValue := te.getString(args[1])
		return te.transformLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args[2:])
	})

	te.bind(sz.NameSymEmbed, 3, func(args []sx.Object) sx.Object {
		ref := te.getList(args[1])
		syntax := te.getString(args[2])
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				te.symAttr,
				sx.Cons(te.Make("type"), sx.MakeString("image/svg+xml")),
				sx.Cons(te.Make("src"), sx.MakeString("/"+te.getString(ref.Tail()).String()+".svg")),
			)
			return sx.MakeList(
				te.Make("figure"),

				sx.MakeList(
					te.Make("embed"),
					embedAttr,
				),
			)
		}
		a := te.getAttributes(args[0])
		a = a.Set("src", string(te.getString(ref.Tail().Car())))

		var sb strings.Builder
		te.flattenText(&sb, ref.Tail().Tail().Tail())
		if d := sb.String(); d != "" {
			a = a.Set("alt", d)
		}

		return sx.MakeList(te.Make("img"), te.transformAttribute(a))
	})
	te.bind(sz.NameSymEmbedBLOB, 3, func(args []sx.Object) sx.Object {
		a, syntax, data := te.getAttributes(args[0]), te.getString(args[1]), te.getString(args[2])
		summary, _ := a.Get(api.KeySummary)



		return te.transformBLOB(
			sx.MakeList(te.astSF.MustMake(sz.NameSymInline), sx.MakeString(summary)),
			syntax,
			data,
		)
	})

	te.bind(sz.NameSymCite, 2, func(args []sx.Object) sx.Object {



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

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

	te.bind(sz.NameSymEndnote, 1, func(args []sx.Object) sx.Object {


		attrPlist := sx.Nil()
		if a := te.getAttributes(args[0]); len(a) > 0 {
			if attrs := te.transformAttribute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		text, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.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 := sx.Nil().Cons(sx.Cons(te.Make("role"), sx.MakeString("doc-noteref"))).
			Cons(sx.Cons(te.Make("href"), sx.MakeString("#fn:"+noteID))).
			Cons(sx.Cons(te.tr.symClass, sx.MakeString("zs-noteref"))).
			Cons(te.symAttr)
		href := sx.Nil().Cons(sx.MakeString(noteNum)).Cons(hrefAttr).Cons(te.symA)
		supAttr := sx.Nil().Cons(sx.Cons(te.Make("id"), sx.MakeString("fnref:"+noteID))).Cons(te.symAttr)
		return sx.Nil().Cons(href).Cons(supAttr).Cons(te.Make("sup"))
	})

	te.bind(sz.NameSymFormatDelete, 1, te.makeFormatFn("del"))
	te.bind(sz.NameSymFormatEmph, 1, te.makeFormatFn("em"))
	te.bind(sz.NameSymFormatInsert, 1, te.makeFormatFn("ins"))

	te.bind(sz.NameSymFormatQuote, 1, te.transformQuote)
	te.bind(sz.NameSymFormatSpan, 1, te.makeFormatFn("span"))
	te.bind(sz.NameSymFormatStrong, 1, te.makeFormatFn("strong"))
	te.bind(sz.NameSymFormatSub, 1, te.makeFormatFn("sub"))
	te.bind(sz.NameSymFormatSuper, 1, te.makeFormatFn("sup"))

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

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

func (te *TransformEnv) makeFormatFn(tag string) transformFn {
	sym := te.Make(tag)
	return func(args []sx.Object) sx.Object {
		a := te.getAttributes(args[0])


		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		res := sx.MakeList(args[1:]...)
		if len(a) > 0 {
			res = res.Cons(te.transformAttribute(a))

























		}
		return res.Cons(sym)
	}

}
func (te *TransformEnv) transformQuote(args []sx.Object) sx.Object {






	const langAttr = "lang"

	a := te.getAttributes(args[0])
	langVal, found := a.Get(langAttr)
	if found {
		a = a.Remove(langAttr)
	}
	if val, found2 := a.Get(""); found2 {
		a = a.Remove("").AddClass(val)
	}
	res := sx.MakeList(args[1:]...)

	if len(a) > 0 {

		res = res.Cons(te.transformAttribute(a))

	}
	res = res.Cons(te.Make("q"))
	if found {
		res = sx.Nil().Cons(res).Cons(te.transformAttribute(attrs.Attributes{}.Set(langAttr, langVal))).Cons(te.symSpan)







	}



	return res
}



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

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

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

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

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











func (te *TransformEnv) 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.Pair:
			te.flattenText(sb, obj)
		}
	}
}





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























func (te *TransformEnv) bind(name string, minArity int, fn transformFn) {
	te.astEnv.Bind(te.astSF.MustMake(name), sxeval.BuiltinA(func(args []sx.Object) (sx.Object, error) {








		if nArgs := len(args); nArgs < minArity {

			return sx.Nil(), fmt.Errorf("not enough arguments (%d) for form %v (%d)", nArgs, name, minArity)
		}

		res := fn(args)



		return res, te.err
	}))
}

func (te *TransformEnv) Rebind(name string, fn func([]sx.Object, sxeval.Callable) sx.Object) {


	sym := te.astSF.MustMake(name)
	obj, found := te.astEnv.Lookup(sym)
	if !found {
		panic(sym.String())

	}
	preFn, ok := sxeval.GetCallable(obj)
	if !ok {
		panic(sym.String())

	}
	te.astEnv.Bind(sym, sxeval.BuiltinA(func(args []sx.Object) (sx.Object, error) {
		res := fn(args, preFn)
		return res, te.err
	}))
}








func (te *TransformEnv) Make(name string) *sx.Symbol { return te.tr.Make(name) }
func (te *TransformEnv) getSymbol(val sx.Object) *sx.Symbol {
	if te.err != nil {


		return nil
	}














	if sym, ok := sx.GetSymbol(val); ok {
		return sym
	}
	te.err = fmt.Errorf("%v/%T is not a symbol", val, val)

	return nil
}
func (te *TransformEnv) getString(val sx.Object) sx.String {
	if te.err != nil {
		return ""
	}
	if s, ok := sx.GetString(val); ok {
		return s
	}
	te.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
func (te *TransformEnv) getInt64(val sx.Object) int64 {
	if te.err != nil {
		return -1017
	}
	if num, ok := sx.GetNumber(val); ok {
		return int64(num.(sx.Int64))
	}
	te.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}
func (te *TransformEnv) getList(val sx.Object) *sx.Pair {
	if te.err == nil {
		if res, isPair := sx.GetPair(val); isPair {
			return res
		}
		te.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return nil
}
func (te *TransformEnv) getAttributes(args sx.Object) attrs.Attributes {

	return sz.GetAttributes(te.getList(args))
}

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

		return result.Cons(te.symSpan)
	}
	return result.Cons(te.transformAttribute(a)).Cons(te.symA)
}

func (te *TransformEnv) transformAttribute(a attrs.Attributes) *sx.Pair {
	return te.tr.TransformAttrbute(a)
}

func (te *TransformEnv) transformMeta(a attrs.Attributes) *sx.Pair {
	return te.tr.TransformMeta(a)
}

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









>
>
>











|
<
|
|
|
<
|
|


|

|
<
<


<

<
<
<
<
<
|
|
|
<
<
<


|
|
|
<
<
<
<
<

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


|


|

<
<
<
<
<
<
<
<
<
|
|








|





|


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


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



|
|


|
>
>
|
|

<
<
|
|
|
|
|



|
|
|
|
|

|
>
|
|
|
>
|

<
|

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

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

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

|
|
|

|
|
|
|
|
|
|
|
>
|

|

|






|

|

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


|
|
|
<
<
<
<
<
<
|

|
|

|


|
>

|
>
>
|
|


|

|

|

|

|


|
|


|

>
|
|
|



|
|

|
|




|

<
>


|
|


|

<
|



|
|

|
|


|


|

|
|


|

<
|

|




|
|







|

|
|
|
|

|
|
|

|
|

|
<
|





<
|
|

|
|
|

|
|
|

|

|

|
|
<
|

<
|








|
|
|
|


|






|



|
<
|
|
|

|
|


|

|


|
>
>
>
>
>
>
>
>
|
>



|
>

|

|

<
|
|
|

|

|



|
|
|
>
>





>
|

|

<
<

>
|
|
>

|
|


|



|


|


|
|


|
|
|
|



|

|
<
|

|
|
>
>


|


|

|

|
|
>
>
|
|

|
|
|
|
|
>
>
|
|

|
|
|
|
>
>
|
|
|

|
|
>
>
|
|


|
|
|


|
|
|


<
>

|




|
|
>
|
|
|
|
|
>
|

|
|
|
>
>
>
|
|





|
>
>
>

|

|



|
|




|

|
<
|
|
|
|
|


|

|
|
>
>

|
|




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


|
|
|
>
|
|
|
|
|

|
|

|
|





|
<
|
|

<
|
|
|

<
|
|

|
|


|


|
<
|
|
>
>
|


|

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

|

>

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


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


|

|


|






|



<







|
|
|




|
|


|



|

|

|

|

|


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

|




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

>
|
>
>
>
|
<


<
>
>
|
|
<
<
>

<
|
<
>

<
<
|
<

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

|
|
|




|
|

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



|



|
>
|
|
|
<
|
<
<

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







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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2023-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2023-present Detlef Stern
//-----------------------------------------------------------------------------

// Package shtml transforms a s-expr encoded zettel AST into a s-expr representation of HTML.
package shtml

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

	"t73f.de/r/sx"

	"t73f.de/r/sxwebs/sxhtml"
	"t73f.de/r/zsc/api"
	"t73f.de/r/zsc/attrs"

	"t73f.de/r/zsc/sz"
	"t73f.de/r/zsc/text"
)

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


	headingOffset int64
	unique        string

	noLinks       bool // true iff output must not include links






	fns     map[string]EvalFn
	minArgs map[string]int



}

// NewEvaluator creates a new Evaluator object.
func NewEvaluator(headingOffset int) *Evaluator {
	ev := &Evaluator{





		headingOffset: int64(headingOffset),






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



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

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










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

// 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
	}
	pair, isPair := sx.GetPair(result)
	if !isPair {
		return nil, fmt.Errorf("evaluation does not result in a pair, but %T/%v", result, result)
	}

	for i := 0; i < len(env.endnotes); i++ {
		// May extend tr.endnotes -> do not use for i := range len(...)!!!

		if env.endnotes[i].noteHx != nil {
			continue
		}


		noteHx, _ := ev.EvaluateList(env.endnotes[i].noteAST, env)
		env.endnotes[i].noteHx = noteHx


	}










	return pair, nil

}




// EvaluateList will evaluate all list elements separately and returns them as a sx.Pair list
func (ev *Evaluator) EvaluateList(lst sx.Vector, env *Environment) (*sx.Pair, error) {
	var result sx.ListBuilder
	for _, elem := range lst {
		p := ev.Eval(elem, env)
		result.Add(p)
	}

	if err := env.err; err != nil {
		return nil, err
	}








	return result.List(), nil









}

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

	var result sx.ListBuilder
	result.Add(SymOL)
	result.Add(sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))).Cons(sxhtml.SymAttr))
	for i, fni := range env.endnotes {
		noteNum := strconv.Itoa(i + 1)


		attrs := fni.attrs.Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnote"))).
			Cons(sx.Cons(SymAttrValue, sx.MakeString(noteNum))).
			Cons(sx.Cons(SymAttrId, sx.MakeString("fn:"+fni.noteID))).
			Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-endnote"))).
			Cons(sxhtml.SymAttr)

		backref := sx.Nil().Cons(sx.MakeString("\u21a9\ufe0e")).
			Cons(sx.Nil().
				Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnote-backref"))).
				Cons(sx.Cons(SymAttrHref, sx.MakeString("#fnref:"+fni.noteID))).
				Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-backlink"))).
				Cons(sxhtml.SymAttr)).
			Cons(SymA)

		var li sx.ListBuilder
		li.Add(SymLI)
		li.Add(attrs)
		li.ExtendBang(fni.noteHx)
		li.Add(sx.MakeString(" "))
		li.Add(backref)
		result.Add(li.List())
	}

	return result.List()
}

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

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


		langStack:    langStack,
		endnotes:     nil,


		quoteNesting: 0,
	}
}

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

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

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

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

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

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

func (ev *Evaluator) bind(sym *sx.Symbol, minArgs int, fn EvalFn) {
	symVal := sym.GetValue()
	ev.fns[symVal] = fn
	if minArgs > 0 {
		ev.minArgs[symVal] = minArgs
	}
}

// ResolveBinding returns the function bound to the given name.
func (ev *Evaluator) ResolveBinding(sym *sx.Symbol) EvalFn {
	if fn, found := ev.fns[sym.GetValue()]; found {
		return fn
	}
	return nil
}

// Rebind overwrites a binding, but leaves the minimum number of arguments intact.
func (ev *Evaluator) Rebind(sym *sx.Symbol, fn EvalFn) {
	symVal := sym.GetValue()
	if _, found := ev.fns[symVal]; !found {
		panic(sym)
	}
	ev.fns[symVal] = fn
}

func (ev *Evaluator) bindMetadata() {
	ev.bind(sz.SymMeta, 0, ev.evalList)






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

	evalMetaSet := func(args sx.Vector, env *Environment) sx.Object {
		var sb strings.Builder
		for elem := getList(args[1], env); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(getString(elem.Car(), env).GetValue())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(args[0], env).GetValue()).
			Set("content", s)
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.SymTypeIDSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeTagSet, 2, evalMetaSet)
	ev.bind(sz.SymTypeZettelmarkup, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(args[0], env).GetValue()).
			Set("content", text.EvaluateInlineString(getList(args[1], env)))
		return ev.EvaluateMeta(a)
	})
}

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

func (ev *Evaluator) bindBlocks() {
	ev.bind(sz.SymBlock, 0, ev.evalList)
	ev.bind(sz.SymPara, 0, func(args sx.Vector, env *Environment) sx.Object {






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

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

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

	ev.bind(sz.SymListOrdered, 0, ev.makeListFn(SymOL))
	ev.bind(sz.SymListUnordered, 0, ev.makeListFn(SymUL))
	ev.bind(sz.SymDescription, 0, func(args sx.Vector, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.Nil()
		}
		var items sx.ListBuilder
		items.Add(symDL)
		for pos := 0; pos < len(args); pos++ {
			term := ev.evalDescriptionTerm(getList(args[pos], env), env)
			items.Add(term.Cons(symDT))
			pos++
			if pos >= len(args) {
				break
			}
			ddBlock := getList(ev.Eval(args[pos], env), env)
			if ddBlock == nil {

				continue
			}
			for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() {
				dditem := getList(ddlst.Car(), env)
				items.Add(dditem.Cons(symDD))
			}
		}
		return items.List()
	})

	ev.bind(sz.SymListQuote, 0, func(args sx.Vector, env *Environment) sx.Object {
		if args == nil {
			return sx.Nil()
		}
		var result sx.ListBuilder
		result.Add(symBLOCKQUOTE)
		for _, elem := range args {
			if quote, isPair := sx.GetPair(ev.Eval(elem, env)); isPair {
				result.Add(quote.Cons(sxhtml.SymListSplice))
			}
		}
		return result.List()
	})

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

		var tbody sx.ListBuilder
		if len(args) > 1 {

			tbody.Add(symTBODY)
			for _, row := range args[1:] {
				tbody.Add(ev.evalTableRow(getList(row, env), env))
			}
		}

		table := sx.Nil()
		if !tbody.IsEmpty() {
			table = table.Cons(tbody.List())
		}
		if thead != nil {
			table = table.Cons(thead)
		}
		if table == nil {
			return sx.Nil()
		}
		return table.Cons(symTABLE)
	})
	ev.bind(sz.SymCell, 0, ev.makeCellFn(""))
	ev.bind(sz.SymCellCenter, 0, ev.makeCellFn("center"))
	ev.bind(sz.SymCellLeft, 0, ev.makeCellFn("left"))
	ev.bind(sz.SymCellRight, 0, ev.makeCellFn("right"))

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

	ev.bind(sz.SymVerbatimComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if ev.GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(args[1], env); s.GetValue() != "" {

					return sx.Nil().Cons(s).Cons(sxhtml.SymBlockComment)
				}
			}
		}
		return nil
	})

	ev.bind(sz.SymVerbatimEval, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.GetAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimHTML, 2, ev.evalHTML)
	ev.bind(sz.SymVerbatimMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.GetAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env))
	})
	ev.bind(sz.SymVerbatimProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		content := getString(args[1], env)
		if a.HasDefault() {
			content = sx.MakeString(visibleReplacer.Replace(content.GetValue()))
		}
		return ev.evalVerbatim(a, content)
	})
	ev.bind(sz.SymVerbatimZettel, 0, nilFn)
	ev.bind(sz.SymBLOB, 3, func(args sx.Vector, env *Environment) sx.Object {

		return ev.evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env))
	})

	ev.bind(sz.SymTransclude, 2, func(args sx.Vector, env *Environment) sx.Object {
		ref, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.Nil()
		}
		refKind := ref.Car()
		if sx.IsNil(refKind) {
			return sx.Nil()
		}
		if refValue := getString(ref.Tail().Car(), env); refValue.GetValue() != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.IsEqual(sz.SymRefStateExternal) {
				a := ev.GetAttributes(args[0], env).Set("src", refValue.GetValue()).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(SymIMG)).Cons(SymP)
			}
			return sx.MakeList(
				sxhtml.SymInlineComment,
				sx.MakeString("transclude"),
				refKind,
				sx.MakeString("->"),
				refValue,
			)
		}
		return ev.evalSlice(args, env)
	})
}

func (ev *Evaluator) makeListFn(sym *sx.Symbol) EvalFn {

	return func(args sx.Vector, env *Environment) sx.Object {
		var result sx.ListBuilder
		result.Add(sym)
		for _, elem := range args {
			item := sx.Nil().Cons(SymLI)
			if res, isPair := sx.GetPair(ev.Eval(elem, env)); isPair {
				item.ExtendBang(res)
			}
			result.Add(item)
		}
		return result.List()
	}
}

func (ev *Evaluator) evalDescriptionTerm(term *sx.Pair, env *Environment) *sx.Pair {
	var result sx.ListBuilder
	for node := term; node != nil; node = node.Tail() {
		elem := ev.Eval(node.Car(), env)
		result.Add(elem)
	}
	return result.List()
}

func (ev *Evaluator) evalTableRow(pairs *sx.Pair, env *Environment) *sx.Pair {
	if pairs == nil {
		return nil
	}
	var row sx.ListBuilder
	row.Add(symTR)
	for pair := pairs; pair != nil; pair = pair.Tail() {
		row.Add(ev.Eval(pair.Car(), env))
	}
	return row.List()
}

func (ev *Evaluator) makeCellFn(align string) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		tdata := ev.evalSlice(args, env)
		if align != "" {
			tdata = tdata.Cons(ev.EvaluateAttrbute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(symTD)
	}
}

func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn {
	return func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		var result sx.ListBuilder
		result.Add(sym)
		if len(a) > 0 {
			result.Add(ev.EvaluateAttrbute(a))
		}


		if region, isPair := sx.GetPair(args[1]); isPair {
			if evalRegion := ev.EvalPairList(region, env); evalRegion != nil {
				result.ExtendBang(evalRegion)
			}
		}
		if len(args) > 2 {
			if cite, _ := ev.EvaluateList(args[2:], env); cite != nil {
				result.Add(cite.Cons(symCITE))
			}
		}
		return result.List()
	}
}

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

func (ev *Evaluator) bindInlines() {
	ev.bind(sz.SymInline, 0, ev.evalList)
	ev.bind(sz.SymText, 1, func(args sx.Vector, env *Environment) sx.Object { return getString(args[0], env) })
	ev.bind(sz.SymSpace, 0, func(args sx.Vector, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.MakeString(" ")
		}
		return getString(args[0], env)
	})
	ev.bind(sz.SymSoft, 0, func(sx.Vector, *Environment) sx.Object { return sx.MakeString(" ") })

	ev.bind(sz.SymHard, 0, func(sx.Vector, *Environment) sx.Object { return sx.Nil().Cons(symBR) })

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

	ev.bind(sz.SymEmbed, 3, func(args sx.Vector, env *Environment) sx.Object {
		ref := getList(args[1], env)
		syntax := getString(args[2], env).GetValue()
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				sxhtml.SymAttr,
				sx.Cons(SymAttrType, sx.MakeString("image/svg+xml")),
				sx.Cons(SymAttrSrc, sx.MakeString("/"+getString(ref.Tail(), env).GetValue()+".svg")),
			)
			return sx.MakeList(

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

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

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






		noteNum := strconv.Itoa(len(env.endnotes) + 1)
		noteID := ev.unique + noteNum
		env.endnotes = append(env.endnotes, endnoteInfo{
			noteID: noteID, noteAST: args[1:], noteHx: nil, attrs: attrPlist})
		hrefAttr := sx.Nil().Cons(sx.Cons(SymAttrRole, sx.MakeString("doc-noteref"))).
			Cons(sx.Cons(SymAttrHref, sx.MakeString("#fn:"+noteID))).
			Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-noteref"))).
			Cons(sxhtml.SymAttr)
		href := sx.Nil().Cons(sx.MakeString(noteNum)).Cons(hrefAttr).Cons(SymA)
		supAttr := sx.Nil().Cons(sx.Cons(SymAttrId, sx.MakeString("fnref:"+noteID))).Cons(sxhtml.SymAttr)
		return sx.Nil().Cons(href).Cons(supAttr).Cons(symSUP)
	})

	ev.bind(sz.SymFormatDelete, 1, ev.makeFormatFn(symDEL))
	ev.bind(sz.SymFormatEmph, 1, ev.makeFormatFn(symEM))
	ev.bind(sz.SymFormatInsert, 1, ev.makeFormatFn(symINS))
	ev.bind(sz.SymFormatMark, 1, ev.makeFormatFn(symMARK))
	ev.bind(sz.SymFormatQuote, 1, ev.evalQuote)
	ev.bind(sz.SymFormatSpan, 1, ev.makeFormatFn(SymSPAN))
	ev.bind(sz.SymFormatStrong, 1, ev.makeFormatFn(SymSTRONG))
	ev.bind(sz.SymFormatSub, 1, ev.makeFormatFn(symSUB))
	ev.bind(sz.SymFormatSuper, 1, ev.makeFormatFn(symSUP))

	ev.bind(sz.SymLiteralComment, 1, func(args sx.Vector, env *Environment) sx.Object {
		if ev.GetAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" {
					return sx.Nil().Cons(s).Cons(sxhtml.SymInlineComment)
				}
			}
		}
		return sx.Nil()
	})
	ev.bind(sz.SymLiteralHTML, 2, ev.evalHTML)

	ev.bind(sz.SymLiteralInput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symKBD, env)
	})

	ev.bind(sz.SymLiteralMath, 2, func(args sx.Vector, env *Environment) sx.Object {
		a := ev.GetAttributes(args[0], env).AddClass("zs-math")
		return ev.evalLiteral(args, a, symCODE, env)
	})

	ev.bind(sz.SymLiteralOutput, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symSAMP, env)
	})
	ev.bind(sz.SymLiteralProg, 2, func(args sx.Vector, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, symCODE, env)
	})

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

func (ev *Evaluator) makeFormatFn(sym *sx.Symbol) EvalFn {

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

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

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

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

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

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

	defer env.popAttributes()

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

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

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

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

func (ev *Evaluator) evalLiteral(args sx.Vector, a attrs.Attributes, sym *sx.Symbol, env *Environment) sx.Object {
	if a == nil {
		a = ev.GetAttributes(args[0], env)
	}
	a = setProgLang(a)
	literal := getString(args[1], env).GetValue()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sx.Nil().Cons(sx.MakeString(literal))
	if len(a) > 0 {
		res = res.Cons(ev.EvaluateAttrbute(a))
	}
	return res.Cons(sym)
}

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

func (ev *Evaluator) evalHTML(args sx.Vector, env *Environment) sx.Object {
	if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" && IsSafe(s.GetValue()) {
		return sx.Nil().Cons(s).Cons(sxhtml.SymNoEscape)
	}
	return nil
}

func (ev *Evaluator) evalBLOB(description *sx.Pair, syntax, data sx.String) sx.Object {
	if data.GetValue() == "" {
		return sx.Nil()
	}
	switch syntax.GetValue() {
	case "":
		return sx.Nil()
	case api.ValueSyntaxSVG:
		return sx.Nil().Cons(sx.Nil().Cons(data).Cons(sxhtml.SymNoEscape)).Cons(SymP)
	default:
		imgAttr := sx.Nil().Cons(sx.Cons(SymAttrSrc, sx.MakeString("data:image/"+syntax.GetValue()+";base64,"+data.GetValue())))
		var sb strings.Builder
		flattenText(&sb, description)
		if d := sb.String(); d != "" {
			imgAttr = imgAttr.Cons(sx.Cons(symAttrAlt, sx.MakeString(d)))
		}
		return sx.Nil().Cons(sx.Nil().Cons(imgAttr.Cons(sxhtml.SymAttr)).Cons(SymIMG)).Cons(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.GetValue())
		case *sx.Symbol:
			if obj.IsEqual(sz.SymSpace) {
				sb.WriteByte(' ')
				break
			}





		case *sx.Pair:
			flattenText(sb, obj)
		}
	}
}

func (ev *Evaluator) evalList(args sx.Vector, env *Environment) sx.Object {
	return ev.evalSlice(args, env)
}
func nilFn(sx.Vector, *Environment) sx.Object { return sx.Nil() }

// Eval evaluates an object in an environment.
func (ev *Evaluator) Eval(obj sx.Object, env *Environment) sx.Object {
	if env.err != nil {
		return sx.Nil()
	}
	if sx.IsNil(obj) {
		return obj
	}
	lst, isLst := sx.GetPair(obj)
	if !isLst {
		return obj
	}
	sym, found := sx.GetSymbol(lst.Car())
	if !found {
		env.err = fmt.Errorf("symbol expected, but got %T/%v", lst.Car(), lst.Car())
		return sx.Nil()
	}
	symVal := sym.GetValue()
	fn, found := ev.fns[symVal]
	if !found {
		env.err = fmt.Errorf("symbol %q not bound", sym)
		return sx.Nil()
	}
	var args sx.Vector
	for cdr := lst.Cdr(); !sx.IsNil(cdr); {
		pair, isPair := sx.GetPair(cdr)
		if !isPair {
			break
		}
		args = append(args, pair.Car())
		cdr = pair.Cdr()
	}
	if minArgs, hasMinArgs := ev.minArgs[symVal]; hasMinArgs {
		if minArgs > len(args) {
			env.err = fmt.Errorf("%v needs at least %d arguments, but got only %d", sym, minArgs, len(args))
			return sx.Nil()
		}
	}
	result := fn(args, env)
	if env.err != nil {
		return sx.Nil()
	}
	return result

}


func (ev *Evaluator) evalSlice(args sx.Vector, env *Environment) *sx.Pair {
	var result sx.ListBuilder
	for _, arg := range args {
		elem := ev.Eval(arg, env)


		result.Add(elem)
	}

	if env.err == nil {

		return result.List()
	}


	return nil

}

// EvaluatePairList evaluates a list of lists.
func (ev *Evaluator) EvalPairList(pair *sx.Pair, env *Environment) *sx.Pair {
	var result sx.ListBuilder
	for node := pair; node != nil; node = node.Tail() {
		elem := ev.Eval(node.Car(), env)
		result.Add(elem)
	}


	if env.err == nil {
		return result.List()
	}
	return nil
}

func (ev *Evaluator) evalLink(a attrs.Attributes, refValue sx.String, inline sx.Vector, env *Environment) sx.Object {
	result := ev.evalSlice(inline, env)
	if len(inline) == 0 {
		result = sx.Nil().Cons(refValue)
	}
	if ev.noLinks {
		return result.Cons(SymSPAN)
	}
	return result.Cons(ev.EvaluateAttrbute(a)).Cons(SymA)
}

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










func getList(val sx.Object, env *Environment) *sx.Pair {
	if env.err == nil {
		if res, isPair := sx.GetPair(val); isPair {
			return res
		}
		env.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return nil
}
func getInt64(val sx.Object, env *Environment) int64 {
	if env.err != nil {
		return -1017
	}
	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
}


// GetAttributes evaluates the given arg in the given environment and returns
// the contained attributes.


func (ev *Evaluator) GetAttributes(arg sx.Object, env *Environment) attrs.Attributes {

	return sz.GetAttributes(getList(arg, env))
}

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

Changes to sz/const.go.

1
2
3
4
5
6
7
8



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262









263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

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



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

package sz

import "zettelstore.de/sx.fossil"

// 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  *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
	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(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)
}









>
>
>




|





|
|
<
|
<

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


|
|
|
|
|
|
|
|
|


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


|
|
|
|
|
|
|
|
|


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

package sz

import "t73f.de/r/sx"

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

const (
	// Symbols for Metanodes
	NameBlock  = "BLOCK"
	NameInline = "INLINE"

	NameMeta   = "META"


	// Symbols for Zettel noMakede types.
	NameBLOB            = "BLOB"
	NameCell            = "CELL"
	NameCellCenter      = "CELL-CENTER"
	NameCellLeft        = "CELL-LEFT"
	NameCellRight       = "CELL-RIGHT"
	NameCite            = "CITE"
	NameDescription     = "DESCRIPTION"
	NameEmbed           = "EMBED"
	NameEmbedBLOB       = "EMBED-BLOB"
	NameEndnote         = "ENDNOTE"
	NameFormatEmph      = "FORMAT-EMPH"
	NameFormatDelete    = "FORMAT-DELETE"
	NameFormatInsert    = "FORMAT-INSERT"
	NameFormatMark      = "FORMAT-MARK"
	NameFormatQuote     = "FORMAT-QUOTE"
	NameFormatSpan      = "FORMAT-SPAN"
	NameFormatSub       = "FORMAT-SUB"
	NameFormatSuper     = "FORMAT-SUPER"
	NameFormatStrong    = "FORMAT-STRONG"
	NameHard            = "HARD"
	NameHeading         = "HEADING"
	NameLinkInvalid     = "LINK-INVALID"
	NameLinkZettel      = "LINK-ZETTEL"
	NameLinkSelf        = "LINK-SELF"
	NameLinkFound       = "LINK-FOUND"
	NameLinkBroken      = "LINK-BROKEN"
	NameLinkHosted      = "LINK-HOSTED"
	NameLinkBased       = "LINK-BASED"
	NameLinkQuery       = "LINK-QUERY"
	NameLinkExternal    = "LINK-EXTERNAL"
	NameListOrdered     = "ORDERED"
	NameListUnordered   = "UNORDERED"
	NameListQuote       = "QUOTATION"
	NameLiteralProg     = "LITERAL-CODE"
	NameLiteralComment  = "LITERAL-COMMENT"
	NameLiteralHTML     = "LITERAL-HTML"
	NameLiteralInput    = "LITERAL-INPUT"
	NameLiteralMath     = "LITERAL-MATH"
	NameLiteralOutput   = "LITERAL-OUTPUT"
	NameLiteralZettel   = "LITERAL-ZETTEL"
	NameMark            = "MARK"
	NamePara            = "PARA"
	NameRegionBlock     = "REGION-BLOCK"
	NameRegionQuote     = "REGION-QUOTE"
	NameRegionVerse     = "REGION-VERSE"
	NameSoft            = "SOFT"
	NameSpace           = "SPACE"
	NameTable           = "TABLE"
	NameText            = "TEXT"
	NameThematic        = "THEMATIC"
	NameTransclude      = "TRANSCLUDE"
	NameUnknown         = "UNKNOWN-NODE"
	NameVerbatimComment = "VERBATIM-COMMENT"
	NameVerbatimEval    = "VERBATIM-EVAL"
	NameVerbatimHTML    = "VERBATIM-HTML"
	NameVerbatimMath    = "VERBATIM-MATH"
	NameVerbatimProg    = "VERBATIM-CODE"
	NameVerbatimZettel  = "VERBATIM-ZETTEL"

	// Constant symbols for reference states.
	NameRefStateInvalid  = "INVALID"
	NameRefStateZettel   = "ZETTEL"
	NameRefStateSelf     = "SELF"
	NameRefStateFound    = "FOUND"
	NameRefStateBroken   = "BROKEN"
	NameRefStateHosted   = "HOSTED"
	NameRefStateBased    = "BASED"
	NameRefStateQuery    = "QUERY"
	NameRefStateExternal = "EXTERNAL"

	// Symbols for metadata types.
	NameTypeCredential   = "CREDENTIAL"
	NameTypeEmpty        = "EMPTY-STRING"



























	NameTypeID           = "ZID"

	NameTypeIDSet        = "ZID-SET"
	NameTypeNumber       = "NUMBER"
	NameTypeString       = "STRING"





	NameTypeTagSet       = "TAG-SET"
	NameTypeTimestamp    = "TIMESTAMP"

	NameTypeURL          = "URL"
	NameTypeWord         = "WORD"

	NameTypeZettelmarkup = "ZETTELMARKUP"
















)

var (
	// Symbols for Metanodes
	SymBlock  = sx.MakeSymbol(NameBlock)
	SymInline = sx.MakeSymbol(NameInline)
	SymMeta   = sx.MakeSymbol(NameMeta)

	// Symbols for Zettel noMakede types.
	SymBLOB            = sx.MakeSymbol(NameBLOB)
	SymCell            = sx.MakeSymbol(NameCell)
	SymCellCenter      = sx.MakeSymbol(NameCellCenter)

	SymCellLeft        = sx.MakeSymbol(NameCellLeft)
	SymCellRight       = sx.MakeSymbol(NameCellRight)




	SymCite            = sx.MakeSymbol(NameCite)
	SymDescription     = sx.MakeSymbol(NameDescription)

	SymEmbed           = sx.MakeSymbol(NameEmbed)
	SymEmbedBLOB       = sx.MakeSymbol(NameEmbedBLOB)
	SymEndnote         = sx.MakeSymbol(NameEndnote)


	SymFormatEmph      = sx.MakeSymbol(NameFormatEmph)
	SymFormatDelete    = sx.MakeSymbol(NameFormatDelete)


	SymFormatInsert    = sx.MakeSymbol(NameFormatInsert)
	SymFormatMark      = sx.MakeSymbol(NameFormatMark)
	SymFormatQuote     = sx.MakeSymbol(NameFormatQuote)
	SymFormatSpan      = sx.MakeSymbol(NameFormatSpan)

	SymFormatSub       = sx.MakeSymbol(NameFormatSub)
	SymFormatSuper     = sx.MakeSymbol(NameFormatSuper)
	SymFormatStrong    = sx.MakeSymbol(NameFormatStrong)
	SymHard            = sx.MakeSymbol(NameHard)
	SymHeading         = sx.MakeSymbol(NameHeading)
	SymLinkInvalid     = sx.MakeSymbol(NameLinkInvalid)
	SymLinkZettel      = sx.MakeSymbol(NameLinkZettel)
	SymLinkSelf        = sx.MakeSymbol(NameLinkSelf)
	SymLinkFound       = sx.MakeSymbol(NameLinkFound)
	SymLinkBroken      = sx.MakeSymbol(NameLinkBroken)
	SymLinkHosted      = sx.MakeSymbol(NameLinkHosted)

	SymLinkBased       = sx.MakeSymbol(NameLinkBased)

	SymLinkQuery       = sx.MakeSymbol(NameLinkQuery)
	SymLinkExternal    = sx.MakeSymbol(NameLinkExternal)

	SymListOrdered     = sx.MakeSymbol(NameListOrdered)
	SymListUnordered   = sx.MakeSymbol(NameListUnordered)
	SymListQuote       = sx.MakeSymbol(NameListQuote)
	SymLiteralProg     = sx.MakeSymbol(NameLiteralProg)
	SymLiteralComment  = sx.MakeSymbol(NameLiteralComment)
	SymLiteralHTML     = sx.MakeSymbol(NameLiteralHTML)
	SymLiteralInput    = sx.MakeSymbol(NameLiteralInput)


































	SymLiteralMath     = sx.MakeSymbol(NameLiteralMath)
	SymLiteralOutput   = sx.MakeSymbol(NameLiteralOutput)
	SymLiteralZettel   = sx.MakeSymbol(NameLiteralZettel)
	SymMark            = sx.MakeSymbol(NameMark)
	SymPara            = sx.MakeSymbol(NamePara)
	SymRegionBlock     = sx.MakeSymbol(NameRegionBlock)
	SymRegionQuote     = sx.MakeSymbol(NameRegionQuote)
	SymRegionVerse     = sx.MakeSymbol(NameRegionVerse)
	SymSoft            = sx.MakeSymbol(NameSoft)
	SymSpace           = sx.MakeSymbol(NameSpace)
	SymTable           = sx.MakeSymbol(NameTable)
	SymText            = sx.MakeSymbol(NameText)
	SymThematic        = sx.MakeSymbol(NameThematic)
	SymTransclude      = sx.MakeSymbol(NameTransclude)
	SymUnknown         = sx.MakeSymbol(NameUnknown)
	SymVerbatimComment = sx.MakeSymbol(NameVerbatimComment)
	SymVerbatimEval    = sx.MakeSymbol(NameVerbatimEval)
	SymVerbatimHTML    = sx.MakeSymbol(NameVerbatimHTML)
	SymVerbatimMath    = sx.MakeSymbol(NameVerbatimMath)
	SymVerbatimProg    = sx.MakeSymbol(NameVerbatimProg)
	SymVerbatimZettel  = sx.MakeSymbol(NameVerbatimZettel)










	// Constant symbols for reference states.
	SymRefStateInvalid  = sx.MakeSymbol(NameRefStateInvalid)
	SymRefStateZettel   = sx.MakeSymbol(NameRefStateZettel)
	SymRefStateSelf     = sx.MakeSymbol(NameRefStateSelf)
	SymRefStateFound    = sx.MakeSymbol(NameRefStateFound)
	SymRefStateBroken   = sx.MakeSymbol(NameRefStateBroken)
	SymRefStateHosted   = sx.MakeSymbol(NameRefStateHosted)
	SymRefStateBased    = sx.MakeSymbol(NameRefStateBased)
	SymRefStateQuery    = sx.MakeSymbol(NameRefStateQuery)
	SymRefStateExternal = sx.MakeSymbol(NameRefStateExternal)

	// Symbols for metadata types.
	SymTypeCredential   = sx.MakeSymbol(NameTypeCredential)
	SymTypeEmpty        = sx.MakeSymbol(NameTypeEmpty)
	SymTypeID           = sx.MakeSymbol(NameTypeID)
	SymTypeIDSet        = sx.MakeSymbol(NameTypeIDSet)
	SymTypeNumber       = sx.MakeSymbol(NameTypeNumber)
	SymTypeString       = sx.MakeSymbol(NameTypeString)
	SymTypeTagSet       = sx.MakeSymbol(NameTypeTagSet)
	SymTypeTimestamp    = sx.MakeSymbol(NameTypeTimestamp)
	SymTypeURL          = sx.MakeSymbol(NameTypeURL)
	SymTypeWord         = sx.MakeSymbol(NameTypeWord)

	SymTypeZettelmarkup = sx.MakeSymbol(NameTypeZettelmarkup)

)

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

package sz_test

import (
	"testing"

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

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




















































Added sz/parser.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
//-----------------------------------------------------------------------------
// Copyright (c) 2024-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2024-present Detlef Stern
//-----------------------------------------------------------------------------

package sz

import (
	"t73f.de/r/sx"
	"t73f.de/r/zsc/api"
	"t73f.de/r/zsc/input"
)

// --- Contains some simple parsers

// ---- Syntax: none

// ParseNoneBlocks parses no block.
func ParseNoneBlocks(*input.Input) *sx.Pair { return nil }

// ParseNoneInlines skips to the end of line and parses no inline.
func ParseNoneInlines(inp *input.Input) *sx.Pair {
	inp.SkipToEOL()
	return nil
}

// ---- Some plain text syntaxes

// ParsePlainBlocks parses the block as plain text with the given syntax.
func ParsePlainBlocks(inp *input.Input, syntax string) *sx.Pair {
	var sym *sx.Symbol
	if syntax == api.ValueSyntaxHTML {
		sym = SymVerbatimHTML
	} else {
		sym = SymVerbatimProg
	}
	return sx.MakeList(
		sym,
		sx.MakeList(sx.Cons(sx.MakeString(""), sx.MakeString(syntax))),
		sx.MakeString(string(inp.ScanLineContent())),
	)
}

// ParsePlainInlines parses the inline as plain text with the given syntax.
func ParsePlainInlines(inp *input.Input, syntax string) *sx.Pair {
	var sym *sx.Symbol
	if syntax == api.ValueSyntaxHTML {
		sym = SymLiteralHTML
	} else {
		sym = SymLiteralProg
	}
	pos := inp.Pos
	inp.SkipToEOL()
	return sx.MakeList(
		sym,
		sx.MakeList(sx.Cons(sx.MakeString(""), sx.MakeString(syntax))),
		sx.MakeString(string(inp.Src[pos:inp.Pos])),
	)
}

Added sz/parser_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//-----------------------------------------------------------------------------
// Copyright (c) 2024-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2024-present Detlef Stern
//-----------------------------------------------------------------------------

package sz_test

import (
	"testing"

	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz"
)

func TestParseNone(t *testing.T) {
	if got := sz.ParseNoneBlocks(nil); got != nil {
		t.Error("GOTB", got)
	}

	inp := input.NewInput([]byte("1234\n6789"))
	if got := sz.ParseNoneInlines(inp); got != nil {
		t.Error("GOTI", got)
	}
	if got := inp.Pos; got != 4 {
		t.Errorf("input should be on position 4, but is %d", got)
	}
	if got := inp.Ch; got != '\n' {
		t.Errorf("input character should be 10, but is %d", got)
	}
}

func TestParsePlani(t *testing.T) {
	testcases := []struct {
		src        string
		syntax     string
		expBlocks  string
		expInlines string
	}{
		{"abc", "html",
			"(VERBATIM-HTML ((\"\" . \"html\")) \"abc\")",
			"(LITERAL-HTML ((\"\" . \"html\")) \"abc\")"},
		{"abc\ndef", "html",
			"(VERBATIM-HTML ((\"\" . \"html\")) \"abc\\ndef\")",
			"(LITERAL-HTML ((\"\" . \"html\")) \"abc\")"},
		{"abc", "text",
			"(VERBATIM-CODE ((\"\" . \"text\")) \"abc\")",
			"(LITERAL-CODE ((\"\" . \"text\")) \"abc\")"},
		{"abc\nDEF", "text",
			"(VERBATIM-CODE ((\"\" . \"text\")) \"abc\\nDEF\")",
			"(LITERAL-CODE ((\"\" . \"text\")) \"abc\")"},
	}
	for i, tc := range testcases {
		t.Run(tc.syntax+":"+tc.src, func(t *testing.T) {
			inp := input.NewInput([]byte(tc.src))
			if got := sz.ParsePlainBlocks(inp, tc.syntax).String(); tc.expBlocks != got {
				t.Errorf("%d: %q/%v\nexpected: %q\ngot     : %q", i, tc.src, tc.syntax, tc.expBlocks, got)
			}
			inp.SetPos(0)
			if got := sz.ParsePlainInlines(inp, tc.syntax).String(); tc.expInlines != got {
				t.Errorf("%d: %q/%v\nexpected: %q\ngot     : %q", i, tc.src, tc.syntax, tc.expInlines, got)
			}
		})
	}
}

Changes to 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
//-----------------------------------------------------------------------------
// 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 {








>
>
>


>



|
|




















|



>
>
>
>
>
>
>
>
>
>
>







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

// Package sz contains zettel data handling as sx expressions.
package sz

import (
	"t73f.de/r/sx"
	"t73f.de/r/zsc/attrs"
)

// 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(GoValue(key), GoValue(val))
	}
	return result
}

// GoValue returns the string value of the sx.Object suitable for Go processing.
func GoValue(obj sx.Object) string {
	switch o := obj.(type) {
	case sx.String:
		return o.GetValue()
	case *sx.Symbol:
		return o.GetValue()
	}
	return obj.String()
}

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





























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
}




































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


<

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


>
|



<
|
<
<
|
|
|





|












>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
func MakeMeta(obj sx.Object) Meta {
	if result := doMakeMeta(obj); len(result) > 0 {
		return result
	}
	return nil
}
func doMakeMeta(obj sx.Object) Meta {





	lst, isList := sx.GetPair(obj)
	if !isList || !lst.Car().IsEqual(SymMeta) {
		return nil
	}
	result := make(map[string]MetaValue)
	for node := lst.Tail(); node != nil; node = node.Tail() {
		if mv, found := makeMetaValue(node.Head()); found {
			result[mv.Key] = mv
		}

	}





	return result




}

func makeMetaValue(mnode *sx.Pair) (MetaValue, bool) {
	var result MetaValue





	typeSym, isSymbol := sx.GetSymbol(mnode.Car())
	if !isSymbol {
		return result, false
	}
	next := mnode.Tail()
	keySym, isSymbol := sx.GetSymbol(next.Car())
	if !isSymbol {
		return result, false
	}

	next = next.Tail()


	result.Type = typeSym.GetValue()
	result.Key = keySym.GetValue()
	result.Value = next.Car()
	return result, true
}

func (m Meta) GetString(key string) string {
	if v, found := m[key]; found {
		return GoValue(v.Value)
	}
	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
}

// MapRefStateToLinkEmbed maps a reference state symbol to a link symbol or to
// an embed symbol, depending on 'forLink'.
func MapRefStateToLinkEmbed(symRefState *sx.Symbol, forLink bool) *sx.Symbol {
	if !forLink {
		return SymEmbed
	}
	if sym, found := mapRefStateLink[symRefState.GetValue()]; found {
		return sym
	}
	return SymLinkInvalid
}

var mapRefStateLink = map[string]*sx.Symbol{
	NameRefStateInvalid:  SymLinkInvalid,
	NameRefStateZettel:   SymLinkZettel,
	NameRefStateSelf:     SymLinkSelf,
	NameRefStateFound:    SymLinkFound,
	NameRefStateBroken:   SymLinkBroken,
	NameRefStateHosted:   SymLinkHosted,
	NameRefStateBased:    SymLinkBased,
	NameRefStateQuery:    SymLinkQuery,
	NameRefStateExternal: SymLinkExternal,
}

// IsBreakSym return true if the object is either a soft or a hard break symbol.
func IsBreakSym(obj sx.Object) bool {
	return SymSoft.IsEqual(obj) || SymHard.IsEqual(obj)
}

Added sz/walk.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
//-----------------------------------------------------------------------------
// Copyright (c) 2024-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2024-present Detlef Stern
//-----------------------------------------------------------------------------

package sz

import "t73f.de/r/sx"

// Visitor is walking the sx-based AST.
type Visitor interface {
	Visit(node *sx.Pair, env *sx.Pair) sx.Object
}

// Walk a sx-based AST through a Visitor.
func Walk(v Visitor, node *sx.Pair, env *sx.Pair) *sx.Pair {
	if node == nil {
		return nil
	}
	if result, isPair := sx.GetPair(v.Visit(node, env)); isPair {
		return result
	}

	if sym, isSymbol := sx.GetSymbol(node.Car()); isSymbol {
		symVal := sym.GetValue()
		if fn, found := mapChildrenWalk[symVal]; found {
			return fn(v, node, env)
		}
		return node
	}
	panic(node)
}

var mapChildrenWalk map[string]func(Visitor, *sx.Pair, *sx.Pair) *sx.Pair

func init() {
	mapChildrenWalk = map[string]func(Visitor, *sx.Pair, *sx.Pair) *sx.Pair{
		NameBlock:         walkChildrenTail,
		NamePara:          walkChildrenTail,
		NameRegionBlock:   walkChildrenRegion,
		NameRegionQuote:   walkChildrenRegion,
		NameRegionVerse:   walkChildrenRegion,
		NameHeading:       walkChildrenHeading,
		NameListOrdered:   walkChildrenTail,
		NameListUnordered: walkChildrenTail,
		NameListQuote:     walkChildrenTail,
		NameDescription:   walkChildrenDescription,
		NameTable:         walkChildrenTable,

		NameInline:       walkChildrenTail,
		NameEndnote:      walkChildrenInlines3,
		NameMark:         walkChildrenMark,
		NameLinkBased:    walkChildrenInlines4,
		NameLinkBroken:   walkChildrenInlines4,
		NameLinkExternal: walkChildrenInlines4,
		NameLinkFound:    walkChildrenInlines4,
		NameLinkHosted:   walkChildrenInlines4,
		NameLinkInvalid:  walkChildrenInlines4,
		NameLinkQuery:    walkChildrenInlines4,
		NameLinkSelf:     walkChildrenInlines4,
		NameLinkZettel:   walkChildrenInlines4,
		NameEmbed:        walkChildrenInlines4,
		NameCite:         walkChildrenInlines4,
		NameFormatDelete: walkChildrenInlines3,
		NameFormatEmph:   walkChildrenInlines3,
		NameFormatInsert: walkChildrenInlines3,
		NameFormatMark:   walkChildrenInlines3,
		NameFormatQuote:  walkChildrenInlines3,
		NameFormatStrong: walkChildrenInlines3,
		NameFormatSpan:   walkChildrenInlines3,
		NameFormatSub:    walkChildrenInlines3,
		NameFormatSuper:  walkChildrenInlines3,
	}
}

func walkChildrenTail(v Visitor, node *sx.Pair, env *sx.Pair) *sx.Pair {
	hasNil := false
	for n := node.Tail(); n != nil; n = n.Tail() {
		obj := Walk(v, n.Head(), env)
		if sx.IsNil(obj) {
			hasNil = true
		}
		n.SetCar(obj)
	}
	if !hasNil {
		return node
	}
	for n := node; ; {
		next := n.Tail()
		if next == nil {
			break
		}
		if sx.IsNil(next.Car()) {
			n.SetCdr(next.Cdr())
			continue
		}
		n = next
	}
	return node
}

func walkChildrenList(v Visitor, lst *sx.Pair, env *sx.Pair) *sx.Pair {
	hasNil := false
	for n := lst; n != nil; n = n.Tail() {
		obj := Walk(v, n.Head(), env)
		if sx.IsNil(obj) {
			hasNil = true
		}
		n.SetCar(obj)
	}
	if !hasNil {
		return lst
	}
	var result sx.ListBuilder
	for n := lst; n != nil; n = n.Tail() {
		obj := n.Car()
		if !sx.IsNil(obj) {
			result.Add(obj)
		}
	}
	return result.List()
}

func walkChildrenRegion(v Visitor, node *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := node.Car()
	next := node.Tail()
	// attrs := next.Car()
	next = next.Tail()
	next.SetCar(walkChildrenList(v, next.Head(), env))
	next.SetCdr(walkChildrenList(v, next.Tail(), env))
	return node
}

func walkChildrenHeading(v Visitor, node *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := node.Car()
	next := node.Tail()
	// level := next.Car()
	next = next.Tail()
	// attrs := next.Car()
	next = next.Tail()
	// slug := next.Car()
	next = next.Tail()
	// fragment := next.Car()
	next.SetCdr(walkChildrenList(v, next.Tail(), env))
	return node
}

func walkChildrenDescription(v Visitor, dn *sx.Pair, env *sx.Pair) *sx.Pair {
	for n := dn.Tail(); n != nil; n = n.Tail() {
		n.SetCar(walkChildrenList(v, n.Head(), env))
		n = n.Tail()
		if n == nil {
			break
		}
		n.SetCar(Walk(v, n.Head(), env))
	}
	return dn
}

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

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

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

func walkChildrenInlines3(v Visitor, node *sx.Pair, env *sx.Pair) *sx.Pair {
	// sym := node.Car()
	next := node.Tail() // Attrs
	// attrs := next.Car()
	next.SetCdr(walkChildrenList(v, next.Tail(), env))
	return node
}

Added sz/zmk/block.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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package zmk

import (
	"fmt"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz"
)

// parseBlock parses one block.
func (cp *zmkP) parseBlock(lastPara *sx.Pair) (res *sx.Pair, cont bool) {
	inp := cp.inp
	pos := inp.Pos
	if cp.nestingLevel <= maxNestingLevel {
		cp.nestingLevel++
		defer func() { cp.nestingLevel-- }()

		var bn *sx.Pair
		success := false

		switch inp.Ch {
		case input.EOS:
			return nil, false
		case '\n', '\r':
			inp.EatEOL()
			cp.cleanupListsAfterEOL()
			return nil, false
		case ':':
			bn, success = cp.parseColon()
		case '@', '`', runeModGrave, '%', '~', '$':
			cp.clearStacked()
			bn, success = cp.parseVerbatim()
		case '"', '<':
			cp.clearStacked()
			bn, success = cp.parseRegion()
		case '=':
			cp.clearStacked()
			bn, success = cp.parseHeading()
		case '-':
			cp.clearStacked()
			bn, success = cp.parseHRule()
		case '*', '#', '>':
			cp.lastRow = nil
			cp.descrl = nil
			bn, success = cp.parseNestedList()
		case ';':
			cp.lists = nil
			cp.lastRow = nil
			bn, success = cp.parseDefTerm()
		case ' ':
			cp.lastRow = nil
			bn, success = nil, cp.parseIndent()
		case '|':
			cp.lists = nil
			cp.descrl = nil
			bn, success = cp.parseRow(), true
		case '{':
			cp.clearStacked()
			bn, success = cp.parseTransclusion()
		}

		if success {
			return bn, false
		}
	}
	inp.SetPos(pos)
	cp.clearStacked()
	ins := cp.parsePara()
	if startsWithSpaceSoftBreak(ins) {
		ins = ins[2:]
	} else if lastPara != nil {
		lastPair := lastPara.LastPair()
		lastPair.ExtendBang(sx.MakeList(ins...))
		return nil, true
	}
	return sx.MakeList(ins...).Cons(sz.SymPara), false
}

func startsWithSpaceSoftBreak(ins sx.Vector) bool {
	if len(ins) < 2 {
		return false
	}
	pair0, isPair0 := sx.GetPair(ins[0])
	pair1, isPair1 := sx.GetPair(ins[0])
	if !isPair0 || !isPair1 {
		return false
	}
	car1 := pair1.Car()
	return pair0.Car().IsEqual(sz.SymSpace) && sz.IsBreakSym(car1)
}

var symSeparator = sx.MakeSymbol("sEpArAtOr")

func (cp *zmkP) cleanupListsAfterEOL() {
	for _, l := range cp.lists {
		l.LastPair().Head().LastPair().AppendBang(sx.Cons(symSeparator, nil))
	}
	if descrl := cp.descrl; descrl != nil {
		if lastPair, pos := lastPairPos(descrl); pos > 1 && pos%2 == 0 {
			lastPair.Head().LastPair().AppendBang(sx.Cons(symSeparator, nil))
		}
	}
}

// parseColon determines which element should be parsed.
func (cp *zmkP) parseColon() (*sx.Pair, bool) {
	inp := cp.inp
	if inp.PeekN(1) == ':' {
		cp.clearStacked()
		return cp.parseRegion()
	}
	return cp.parseDefDescr()
}

// parsePara parses paragraphed inline material as a sx.Vector.
func (cp *zmkP) parsePara() (result sx.Vector) {
	for {
		in := cp.parseInline()
		if in == nil {
			return result
		}
		result = append(result, in)
		if sz.IsBreakSym(in.Car()) {
			ch := cp.inp.Ch
			switch ch {
			// Must contain all cases from above switch in parseBlock.
			case input.EOS, '\n', '\r', '@', '`', runeModGrave, '%', '~', '$', '"', '<', '=', '-', '*', '#', '>', ';', ':', ' ', '|', '{':
				return result
			}
		}
	}
}

// countDelim read from input until a non-delimiter is found and returns number of delimiter chars.
func (cp *zmkP) countDelim(delim rune) int {
	inp := cp.inp
	cnt := 0
	for inp.Ch == delim {
		cnt++
		inp.Next()
	}
	return cnt
}

// parseVerbatim parses a verbatim block.
func (cp *zmkP) parseVerbatim() (rn *sx.Pair, success bool) {
	inp := cp.inp
	fch := inp.Ch
	cnt := cp.countDelim(fch)
	if cnt < 3 {
		return nil, false
	}
	attrs := cp.parseBlockAttributes()
	inp.SkipToEOL()
	if inp.Ch == input.EOS {
		return nil, false
	}
	var sym *sx.Symbol
	switch fch {
	case '@':
		sym = sz.SymVerbatimZettel
	case '`', runeModGrave:
		sym = sz.SymVerbatimProg
	case '%':
		sym = sz.SymVerbatimComment
	case '~':
		sym = sz.SymVerbatimEval
	case '$':
		sym = sz.SymVerbatimMath
	default:
		panic(fmt.Sprintf("%q is not a verbatim char", fch))
	}
	content := make([]byte, 0, 512)
	for {
		inp.EatEOL()
		posL := inp.Pos
		switch inp.Ch {
		case fch:
			if cp.countDelim(fch) >= cnt {
				inp.SkipToEOL()
				rn = sx.MakeList(sym, attrs, sx.MakeString(string(content)))
				return rn, true
			}
			inp.SetPos(posL)
		case input.EOS:
			return nil, false
		}
		inp.SkipToEOL()
		if len(content) > 0 {
			content = append(content, '\n')
		}
		content = append(content, inp.Src[posL:inp.Pos]...)
	}
}

// parseRegion parses a block region.
func (cp *zmkP) parseRegion() (rn *sx.Pair, success bool) {
	inp := cp.inp
	fch := inp.Ch
	cnt := cp.countDelim(fch)
	if cnt < 3 {
		return nil, false
	}

	var sym *sx.Symbol
	oldInVerse := cp.inVerse
	defer func() { cp.inVerse = oldInVerse }()
	switch fch {
	case ':':
		sym = sz.SymRegionBlock
	case '<':
		sym = sz.SymRegionQuote
	case '"':
		sym = sz.SymRegionVerse
		cp.inVerse = true
	default:
		panic(fmt.Sprintf("%q is not a region char", fch))
	}
	attrs := cp.parseBlockAttributes()
	inp.SkipToEOL()
	if inp.Ch == input.EOS {
		return nil, false
	}
	var blocksBuilder sx.ListBuilder
	var lastPara *sx.Pair
	inp.EatEOL()
	for {
		posL := inp.Pos
		switch inp.Ch {
		case fch:
			if cp.countDelim(fch) >= cnt {
				ins := cp.parseRegionLastLine()
				rn = ins.Cons(blocksBuilder.List()).Cons(attrs).Cons(sym)
				return rn, true
			}
			inp.SetPos(posL)
		case input.EOS:
			return nil, false
		}
		bn, cont := cp.parseBlock(lastPara)
		if bn != nil {
			blocksBuilder.Add(bn)
		}
		if !cont {
			lastPara = bn
		}
	}
}

// parseRegionLastLine parses the last line of a region and returns its inline text.
func (cp *zmkP) parseRegionLastLine() *sx.Pair {
	cp.clearStacked() // remove any lists defined in the region
	cp.skipSpace()
	var region sx.ListBuilder
	for {
		switch cp.inp.Ch {
		case input.EOS, '\n', '\r':
			return region.List()
		}
		in := cp.parseInline()
		if in == nil {
			return region.List()
		}
		region.Add(in)
	}
}

// parseHeading parses a head line.
func (cp *zmkP) parseHeading() (hn *sx.Pair, success bool) {
	inp := cp.inp
	delims := cp.countDelim(inp.Ch)
	if delims < 3 {
		return nil, false
	}
	if inp.Ch != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	if delims > 7 {
		delims = 7
	}
	level := int64(delims - 2)
	var attrs *sx.Pair
	var text sx.ListBuilder
	for {
		if input.IsEOLEOS(inp.Ch) {
			return createHeading(level, attrs, text.List()), true
		}
		in := cp.parseInline()
		if in == nil {
			return createHeading(level, attrs, text.List()), true
		}
		text.Add(in)
		if inp.Ch == '{' && inp.Peek() != '{' {
			attrs = cp.parseBlockAttributes()
			inp.SkipToEOL()
			return createHeading(level, attrs, text.List()), true
		}
	}
}
func createHeading(level int64, attrs, text *sx.Pair) *sx.Pair {
	return text.
		Cons(sx.MakeString("")). // Fragment
		Cons(sx.MakeString("")). // Slug
		Cons(attrs).
		Cons(sx.Int64(level)).
		Cons(sz.SymHeading)
}

// parseHRule parses a horizontal rule.
func (cp *zmkP) parseHRule() (hn *sx.Pair, success bool) {
	inp := cp.inp
	if cp.countDelim(inp.Ch) < 3 {
		return nil, false
	}

	attrs := cp.parseBlockAttributes()
	inp.SkipToEOL()
	return sx.MakeList(sz.SymThematic, attrs), true
}

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

	if len(kinds) < len(cp.lists) {
		cp.lists = cp.lists[:len(kinds)]
	}
	ln, newLnCount := cp.buildNestedList(kinds)
	pv := cp.parseLinePara()
	bn := sx.Cons(sz.SymBlock, nil)
	if len(pv) != 0 {
		bn.AppendBang(pv.MakeList().Cons(sz.SymPara))
	}
	lastItemPair := ln.LastPair()
	lastItemPair.AppendBang(bn)
	return cp.cleanupParsedNestedList(newLnCount)
}

func (cp *zmkP) parseNestedListKinds() []*sx.Symbol {
	inp := cp.inp
	result := make([]*sx.Symbol, 0, 8)
	for {
		var sym *sx.Symbol
		switch inp.Ch {
		case '*':
			sym = sz.SymListUnordered
		case '#':
			sym = sz.SymListOrdered
		case '>':
			sym = sz.SymListQuote
		default:
			panic(fmt.Sprintf("%q is not a region char", inp.Ch))
		}
		result = append(result, sym)
		switch inp.Next() {
		case '*', '#', '>':
		case ' ', input.EOS, '\n', '\r':
			return result
		default:
			return nil
		}
	}
}

func (cp *zmkP) buildNestedList(kinds []*sx.Symbol) (ln *sx.Pair, newLnCount int) {
	for i, kind := range kinds {
		if i < len(cp.lists) {
			if !cp.lists[i].Car().IsEqual(kind) {
				ln = sx.Cons(kind, nil)
				newLnCount++
				cp.lists[i] = ln
				cp.lists = cp.lists[:i+1]
			} else {
				ln = cp.lists[i]
			}
		} else {
			ln = sx.Cons(kind, nil)
			newLnCount++
			cp.lists = append(cp.lists, ln)
		}
	}
	return ln, newLnCount
}

func (cp *zmkP) cleanupParsedNestedList(newLnCount int) (res *sx.Pair, success bool) {
	childPos := len(cp.lists) - 1
	parentPos := childPos - 1
	for i := 0; i < newLnCount; i++ {
		if parentPos < 0 {
			return cp.lists[0], true
		}
		parentLn := cp.lists[parentPos]
		childLn := cp.lists[childPos]
		if firstParent := parentLn.Tail(); firstParent != nil {
			// Add list to last item of the parent list
			lastParent := firstParent.LastPair()
			lastParent.Head().LastPair().AppendBang(childLn)
		} else {
			// Set list to first child of parent.
			childBlock := sx.MakeList(sz.SymBlock, cp.lists[childPos])
			parentLn.LastPair().AppendBang(childBlock)
		}
		childPos--
		parentPos--
	}
	return nil, true
}

// parseDefTerm parses a term of a definition list.
func (cp *zmkP) parseDefTerm() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	descrl := cp.descrl
	if descrl == nil {
		descrl = sx.Cons(sz.SymDescription, nil)
		cp.descrl = descrl
		res = descrl
	}
	lastPair, pos := lastPairPos(descrl)
	for {
		in := cp.parseInline()
		if in == nil {
			if pos%2 == 0 {
				// lastPair is either the empty description list or the last block of definitions
				return nil, false
			}
			// lastPair is the definition term
			return res, true
		}
		if pos%2 == 0 {
			// lastPair is either the empty description list or the last block of definitions
			lastPair = lastPair.AppendBang(sx.Cons(in, nil))
			pos++
		} else {
			// lastPair is the term part and we need to append the inline list just read
			lastPair.Head().LastPair().AppendBang(in)
		}
		if sz.IsBreakSym(in.Car()) {
			return res, true
		}
	}
}

// parseDefDescr parses a description of a definition list.
func (cp *zmkP) parseDefDescr() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != ' ' {
		return nil, false
	}
	inp.Next()
	cp.skipSpace()
	descrl := cp.descrl
	lastPair, pos := lastPairPos(descrl)
	if descrl == nil || pos <= 0 {
		// No term given
		return nil, false
	}

	pn := cp.parseLinePara()
	if len(pn) == 0 {
		return nil, false
	}

	newDef := sx.MakeList(sz.SymBlock, pn.MakeList().Cons(sz.SymPara))
	if pos%2 == 1 {
		// Just a term, but no definitions
		lastPair.AppendBang(sx.MakeList(sz.SymBlock, newDef))
	} else {
		// lastPara points a the last definition
		lastPair.Head().LastPair().AppendBang(newDef)
	}
	return nil, true
}

func lastPairPos(p *sx.Pair) (*sx.Pair, int) {
	cnt := 0
	for node := p; node != nil; {
		next := node.Tail()
		if next == nil {
			return node, cnt
		}
		node = next
		cnt++
	}
	return nil, -1
}

// parseIndent parses initial spaces to continue a list.
func (cp *zmkP) parseIndent() bool {
	inp := cp.inp
	cnt := 0
	for {
		if inp.Next() != ' ' {
			break
		}
		cnt++
	}
	if cp.lists != nil {
		return cp.parseIndentForList(cnt)
	}
	if cp.descrl != nil {
		return cp.parseIndentForDescription(cnt)
	}
	return false
}

func (cp *zmkP) parseIndentForList(cnt int) bool {
	if len(cp.lists) < cnt {
		cnt = len(cp.lists)
	}
	cp.lists = cp.lists[:cnt]
	if cnt == 0 {
		return false
	}
	pv := cp.parseLinePara()
	if len(pv) == 0 {
		return false
	}
	ln := cp.lists[cnt-1]
	lbn := ln.LastPair().Head()
	lpn := lbn.LastPair().Head()
	pvList := pv.MakeList()
	if lpn.Car().IsEqual(sz.SymPara) {
		lpn.LastPair().SetCdr(pvList)
	} else {
		lbn.LastPair().AppendBang(pvList.Cons(sz.SymPara))
	}
	return true
}

func (cp *zmkP) parseIndentForDescription(cnt int) bool {
	descrl := cp.descrl
	lastPair, pos := lastPairPos(descrl)
	if cnt < 1 || pos < 1 {
		return false
	}
	if pos%2 == 1 {
		// Continuation of a definition term
		for {
			in := cp.parseInline()
			if in == nil {
				return true
			}
			lastPair.Head().LastPair().AppendBang(in)
			if sz.IsBreakSym(in.Car()) {
				return true
			}
		}
	}

	// Continuation of a definition description
	pn := cp.parseLinePara()
	if len(pn) == 0 {
		return false
	}
	bn := lastPair.Head()
	para := bn.LastPair().Head().LastPair().Head()
	pnList := pn.MakeList()
	if para.Car().IsEqual(sz.SymPara) {
		para.LastPair().SetCdr(pnList)
	} else {
		bn.LastPair().AppendBang(pnList.Cons(sz.SymPara))
	}
	return true
}

// parseLinePara parses one paragraph of inline material.
func (cp *zmkP) parseLinePara() sx.Vector {
	var ins sx.Vector
	for {
		in := cp.parseInline()
		if in == nil {
			return ins
		}
		ins = append(ins, in)
		if sz.IsBreakSym(in.Car()) {
			return ins
		}
	}
}

// parseRow parse one table row.
func (cp *zmkP) parseRow() *sx.Pair {
	inp := cp.inp
	if inp.Peek() == '%' {
		inp.SkipToEOL()
		return nil
	}
	//var row, curr *sx.Pair
	var row sx.ListBuilder
	for {
		inp.Next()
		cell := cp.parseCell()
		if cell != nil {
			row.Add(cell)
		}
		switch inp.Ch {
		case '\n', '\r':
			inp.EatEOL()
			fallthrough
		case input.EOS:
			// add to table
			if cp.lastRow == nil {
				if row.IsEmpty() {
					return nil
				}
				cp.lastRow = sx.Cons(row.List(), nil)
				return cp.lastRow.Cons(nil).Cons(sz.SymTable)
			}
			cp.lastRow = cp.lastRow.AppendBang(row.List())
			return nil
		}
		// inp.Ch must be '|'
	}
}

// parseCell parses one single cell of a table row.
func (cp *zmkP) parseCell() *sx.Pair {
	inp := cp.inp
	var cell sx.ListBuilder
	for {
		if input.IsEOLEOS(inp.Ch) {
			if cell.IsEmpty() {
				return nil
			}
			return cell.List().Cons(sz.SymCell)
		}
		if inp.Ch == '|' {
			return cell.List().Cons(sz.SymCell)
		}

		in := cp.parseInline()
		cell.Add(in)
	}
}

// parseTransclusion parses '{' '{' '{' ZID '}' '}' '}'
func (cp *zmkP) parseTransclusion() (*sx.Pair, bool) {
	if cp.countDelim('{') != 3 {
		return nil, false
	}
	inp := cp.inp
	posA, posE := inp.Pos, 0

loop:

	for {
		switch inp.Ch {
		case input.EOS:
			return nil, false
		case '\n', '\r', ' ', '\t':
			if !hasQueryPrefix(inp.Src[posA:]) {
				return nil, false
			}
		case '\\':
			switch inp.Next() {
			case input.EOS, '\n', '\r':
				return nil, false
			}
		case '}':
			posE = inp.Pos
			if posA >= posE {
				return nil, false
			}
			if inp.Next() != '}' {
				continue
			}
			if inp.Next() != '}' {
				continue
			}
			break loop
		}
		inp.Next()
	}
	inp.Next() // consume last '}'
	a := cp.parseBlockAttributes()
	inp.SkipToEOL()
	refText := string(inp.Src[posA:posE])
	ref := ParseReference(refText)
	return sx.MakeList(sz.SymTransclude, a, ref), true
}

Added sz/zmk/inline.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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package zmk

import (
	"fmt"
	"strings"
	"unicode/utf8"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/api"
	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz"
)

func (cp *zmkP) parseInline() *sx.Pair {
	inp := cp.inp
	pos := inp.Pos
	if cp.nestingLevel <= maxNestingLevel {
		cp.nestingLevel++
		defer func() { cp.nestingLevel-- }()

		var in *sx.Pair
		success := false
		switch inp.Ch {
		case input.EOS:
			return nil
		case '\n', '\r':
			return cp.parseSoftBreak()
		case ' ', '\t':
			return cp.parseSpace()
		case '[':
			switch inp.Next() {
			case '[':
				in, success = cp.parseLinkEmbed('[', ']', true)
			case '@':
				in, success = cp.parseCite()
			case '^':
				in, success = cp.parseEndnote()
			case '!':
				in, success = cp.parseMark()
			}
		case '{':
			if inp.Next() == '{' {
				in, success = cp.parseLinkEmbed('{', '}', false)
			}
		case '%':
			in, success = cp.parseComment()
		case '_', '*', '>', '~', '^', ',', '"', '#', ':':
			in, success = cp.parseFormat()
		case '@', '\'', '`', '=', runeModGrave:
			in, success = cp.parseLiteral()
		case '$':
			in, success = cp.parseLiteralMath()
		case '\\':
			return cp.parseBackslash()
		case '-':
			in, success = cp.parseNdash()
		case '&':
			in, success = cp.parseEntity()
		}
		if success {
			return in
		}
	}
	inp.SetPos(pos)
	return cp.parseText()
}

func (cp *zmkP) parseText() *sx.Pair {
	return sx.MakeList(sz.SymText, cp.parseString())
}

func (cp *zmkP) parseString() sx.String {
	inp := cp.inp
	pos := inp.Pos
	if inp.Ch == '\\' {
		cp.inp.Next()
		return cp.parseBackslashRest()
	}
	for {
		switch inp.Next() {
		// The following case must contain all runes that occur in parseInline!
		// Plus the closing brackets ] and } and ) and the middle |
		case input.EOS, '\n', '\r', ' ', '\t', '[', ']', '{', '}', '(', ')', '|', '%', '_', '*', '>', '~', '^', ',', '"', '#', ':', '\'', '@', '`', runeModGrave, '$', '=', '\\', '-', '&':
			return sx.MakeString(string(inp.Src[pos:inp.Pos]))
		}
	}
}

func (cp *zmkP) parseBackslash() *sx.Pair {
	inp := cp.inp
	switch inp.Next() {
	case '\n', '\r':
		inp.EatEOL()
		return sx.MakeList(sz.SymHard)
	default:
		return sx.MakeList(sz.SymText, cp.parseBackslashRest())
	}
}

func (cp *zmkP) parseBackslashRest() sx.String {
	inp := cp.inp
	if input.IsEOLEOS(inp.Ch) {
		return sx.MakeString("\\")
	}
	if inp.Ch == ' ' {
		inp.Next()
		return sx.MakeString("\u00a0")
	}
	pos := inp.Pos
	inp.Next()
	return sx.MakeString(string(inp.Src[pos:inp.Pos]))
}

func (cp *zmkP) parseSpace() *sx.Pair {
	inp := cp.inp
	pos := inp.Pos
	for {
		switch inp.Next() {
		case ' ', '\t':
		default:
			if cp.inVerse {
				spaces := utf8.RuneCount(inp.Src[pos:inp.Pos])
				s := strings.Repeat("\u00a0", spaces)
				return sx.MakeList(sz.SymSpace, sx.MakeString(s))
			}
			return sx.MakeList(sz.SymSpace)
		}
	}
}

func (cp *zmkP) parseSoftBreak() *sx.Pair {
	cp.inp.EatEOL()
	return sx.MakeList(sz.SymSoft)
}

func (cp *zmkP) parseLinkEmbed(openCh, closeCh rune, forLink bool) (*sx.Pair, bool) {
	if refString, text, ok := cp.parseReference(openCh, closeCh); ok {
		attrs := cp.parseInlineAttributes()
		if len(refString) > 0 {
			ref := ParseReference(refString)
			refSym, _ := sx.GetSymbol(ref.Car())
			sym := sz.MapRefStateToLinkEmbed(refSym, forLink)
			ln := text.
				Cons(ref.Tail().Car()). // reference value
				Cons(attrs).
				Cons(sym)
			return ln, true
		}
	}
	return nil, false
}

func hasQueryPrefix(src []byte) bool {
	return len(src) > len(api.QueryPrefix) && string(src[:len(api.QueryPrefix)]) == api.QueryPrefix
}

func (cp *zmkP) parseReference(openCh, closeCh rune) (ref string, text *sx.Pair, _ bool) {
	inp := cp.inp
	inp.Next()
	cp.skipSpace()
	if inp.Ch == openCh {
		// Additional opening chars result in a fail
		return "", nil, false
	}
	var is sx.Vector
	pos := inp.Pos
	if !hasQueryPrefix(inp.Src[pos:]) {
		hasSpace, ok := cp.readReferenceToSep(closeCh)
		if !ok {
			return "", nil, false
		}
		if inp.Ch == '|' { // First part must be inline text
			if pos == inp.Pos { // [[| or {{|
				return "", nil, false
			}
			cp.inp = input.NewInput(inp.Src[pos:inp.Pos])
			for {
				in := cp.parseInline()
				if in == nil {
					break
				}
				is = append(is, in)
			}
			cp.inp = inp
			inp.Next()
		} else {
			if hasSpace {
				return "", nil, false
			}
			inp.SetPos(pos)
		}
	}

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

func (cp *zmkP) readReferenceToSep(closeCh rune) (bool, bool) {
	hasSpace := false
	inp := cp.inp
	for {
		switch inp.Ch {
		case input.EOS:
			return false, false
		case '\n', '\r', ' ':
			hasSpace = true
		case '|':
			return hasSpace, true
		case '\\':
			switch inp.Next() {
			case input.EOS:
				return false, false
			case '\n', '\r':
				hasSpace = true
			}
		case '%':
			if inp.Next() == '%' {
				inp.SkipToEOL()
			}
			continue
		case closeCh:
			if inp.Next() == closeCh {
				return hasSpace, true
			}
			continue
		}
		inp.Next()
	}
}

func (cp *zmkP) readReferenceToClose(closeCh rune) bool {
	inp := cp.inp
	pos := inp.Pos
	for {
		switch inp.Ch {
		case input.EOS:
			return false
		case '\t', '\r', '\n', ' ':
			if !hasQueryPrefix(inp.Src[pos:]) {
				return false
			}
		case '\\':
			switch inp.Next() {
			case input.EOS, '\n', '\r':
				return false
			}
		case closeCh:
			return true
		}
		inp.Next()
	}
}

func (cp *zmkP) parseCite() (*sx.Pair, bool) {
	inp := cp.inp
	switch inp.Next() {
	case ' ', ',', '|', ']', '\n', '\r':
		return nil, false
	}
	pos := inp.Pos
loop:
	for {
		switch inp.Ch {
		case input.EOS:
			return nil, false
		case ' ', ',', '|', ']', '\n', '\r':
			break loop
		}
		inp.Next()
	}
	posL := inp.Pos
	switch inp.Ch {
	case ' ', ',', '|':
		inp.Next()
	}
	ins, ok := cp.parseLinkLikeRest()
	if !ok {
		return nil, false
	}
	attrs := cp.parseInlineAttributes()
	cn := ins.Cons(sx.MakeString(string(inp.Src[pos:posL]))).Cons(attrs).Cons(sz.SymCite)
	return cn, true
}

func (cp *zmkP) parseEndnote() (*sx.Pair, bool) {
	cp.inp.Next()
	ins, ok := cp.parseLinkLikeRest()
	if !ok {
		return nil, false
	}
	attrs := cp.parseInlineAttributes()
	return ins.Cons(attrs).Cons(sz.SymEndnote), true
}

func (cp *zmkP) parseMark() (*sx.Pair, bool) {
	inp := cp.inp
	inp.Next()
	pos := inp.Pos
	for inp.Ch != '|' && inp.Ch != ']' {
		if !isNameRune(inp.Ch) {
			return nil, false
		}
		inp.Next()
	}
	mark := inp.Src[pos:inp.Pos]
	var ins *sx.Pair
	if inp.Ch == '|' {
		inp.Next()
		var ok bool
		ins, ok = cp.parseLinkLikeRest()
		if !ok {
			return nil, false
		}
	} else {
		inp.Next()
	}
	mn := ins.
		Cons(sx.MakeString("")). // Fragment
		Cons(sx.MakeString("")). // Slug
		Cons(sx.MakeString(string(mark))).
		Cons(sz.SymMark)
	return mn, true
	// Problematisch ist, dass hier noch nicht mn.Fragment und mn.Slug gesetzt werden.
	// Evtl. muss es ein PreMark-Symbol geben
}

func (cp *zmkP) parseLinkLikeRest() (*sx.Pair, bool) {
	cp.skipSpace()
	var ins sx.Vector
	inp := cp.inp
	for inp.Ch != ']' {
		in := cp.parseInline()
		if in == nil {
			return nil, false
		}
		ins = append(ins, in)
		if input.IsEOLEOS(inp.Ch) && sz.IsBreakSym(in.Car()) {
			return nil, false
		}
	}
	inp.Next()
	if len(ins) == 0 {
		return nil, true
	}
	return sx.MakeList(ins...), true
}

func (cp *zmkP) parseComment() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Next() != '%' {
		return nil, false
	}
	for inp.Ch == '%' {
		inp.Next()
	}
	attrs := cp.parseInlineAttributes()
	cp.skipSpace()
	pos := inp.Pos
	for {
		if input.IsEOLEOS(inp.Ch) {
			return sx.MakeList(
				sz.SymLiteralComment,
				attrs,
				sx.MakeString(string(inp.Src[pos:inp.Pos])),
			), true
		}
		inp.Next()
	}
}

var mapRuneFormat = map[rune]*sx.Symbol{
	'_': sz.SymFormatEmph,
	'*': sz.SymFormatStrong,
	'>': sz.SymFormatInsert,
	'~': sz.SymFormatDelete,
	'^': sz.SymFormatSuper,
	',': sz.SymFormatSub,
	'"': sz.SymFormatQuote,
	'#': sz.SymFormatMark,
	':': sz.SymFormatSpan,
}

func (cp *zmkP) parseFormat() (res *sx.Pair, success bool) {
	inp := cp.inp
	fch := inp.Ch
	symFormat, ok := mapRuneFormat[fch]
	if !ok {
		panic(fmt.Sprintf("%q is not a formatting char", fch))
	}
	// read 2nd formatting character
	if inp.Next() != fch {
		return nil, false
	}
	inp.Next()
	var inlines sx.Vector
	for {
		if inp.Ch == input.EOS {
			return nil, false
		}
		if inp.Ch == fch {
			if inp.Next() == fch {
				inp.Next()
				attrs := cp.parseInlineAttributes()
				fn := sx.MakeList(inlines...).Cons(attrs).Cons(symFormat)
				return fn, true
			}
			inlines = append(inlines, sx.MakeList(sz.SymText, sx.MakeString(string(fch))))
		} else if in := cp.parseInline(); in != nil {
			if input.IsEOLEOS(inp.Ch) && sz.IsBreakSym(in.Car()) {
				return nil, false
			}
			inlines = append(inlines, in)
		}
	}
}

var mapRuneLiteral = map[rune]*sx.Symbol{
	'@':          sz.SymLiteralZettel,
	'`':          sz.SymLiteralProg,
	runeModGrave: sz.SymLiteralProg,
	'\'':         sz.SymLiteralInput,
	'=':          sz.SymLiteralOutput,
	// No '$': sz.SymLiteralMath, because pairing literal math is a little different
}

func (cp *zmkP) parseLiteral() (res *sx.Pair, success bool) {
	inp := cp.inp
	fch := inp.Ch
	symLiteral, ok := mapRuneLiteral[fch]
	if !ok {
		panic(fmt.Sprintf("%q is not a formatting char", fch))
	}
	// read 2nd formatting character
	if inp.Next() != fch {
		return nil, false
	}
	inp.Next()
	var sb strings.Builder
	for {
		if inp.Ch == input.EOS {
			return nil, false
		}
		if inp.Ch == fch {
			if inp.Peek() == fch {
				inp.Next()
				inp.Next()
				return createLiteralNode(symLiteral, cp.parseInlineAttributes(), sb.String()), true
			}
			sb.WriteRune(fch)
			inp.Next()
		} else {
			s := cp.parseString()
			sb.WriteString(s.GetValue())
		}
	}
}

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

func (cp *zmkP) parseLiteralMath() (res *sx.Pair, success bool) {
	inp := cp.inp
	// read 2nd formatting character
	if inp.Next() != '$' {
		return nil, false
	}
	inp.Next()
	pos := inp.Pos
	for {
		if inp.Ch == input.EOS {
			return nil, false
		}
		if inp.Ch == '$' && inp.Peek() == '$' {
			content := append([]byte{}, inp.Src[pos:inp.Pos]...)
			inp.Next()
			inp.Next()
			fn := sx.MakeList(sz.SymLiteralMath, cp.parseInlineAttributes(), sx.MakeString(string(content)))
			return fn, true
		}
		inp.Next()
	}
}

func (cp *zmkP) parseNdash() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Peek() != inp.Ch {
		return nil, false
	}
	inp.Next()
	inp.Next()
	return sx.MakeList(sz.SymText, sx.MakeString("\u2013")), true
}

func (cp *zmkP) parseEntity() (res *sx.Pair, success bool) {
	if text, ok := cp.inp.ScanEntity(); ok {
		return sx.MakeList(sz.SymText, sx.MakeString(text)), true
	}
	return nil, false
}

Added sz/zmk/post-processor.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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

package zmk

import (
	"t73f.de/r/sx"
	"t73f.de/r/zsc/sz"
)

var symInVerse = sx.MakeSymbol("in-verse")
var symNoBlock = sx.MakeSymbol("no-block")

type postProcessor struct{}

func (pp *postProcessor) Visit(lst *sx.Pair, env *sx.Pair) sx.Object {
	if lst == nil {
		return nil
	}
	sym, isSym := sx.GetSymbol(lst.Car())
	if !isSym {
		panic(lst)
	}
	symVal := sym.GetValue()
	if fn, found := symMap[symVal]; found {
		return fn(pp, lst, env)
	}
	return sx.Int64(0)
}

func (pp *postProcessor) visitPairList(lst *sx.Pair, env *sx.Pair) *sx.Pair {
	var pList sx.ListBuilder
	for node := lst; node != nil; node = node.Tail() {
		if elem := sz.Walk(pp, node.Head(), env); elem != nil {
			pList.Add(elem)
		}
	}
	return pList.List()
}

var symMap map[string]func(*postProcessor, *sx.Pair, *sx.Pair) *sx.Pair

func init() {
	symMap = map[string]func(*postProcessor, *sx.Pair, *sx.Pair) *sx.Pair{
		sz.NameBlock:           postProcessBlockList,
		sz.NamePara:            postProcessInlineList,
		sz.NameRegionBlock:     postProcessRegion,
		sz.NameRegionQuote:     postProcessRegion,
		sz.NameRegionVerse:     postProcessRegionVerse,
		sz.NameVerbatimComment: postProcessVerbatim,
		sz.NameVerbatimEval:    postProcessVerbatim,
		sz.NameVerbatimMath:    postProcessVerbatim,
		sz.NameVerbatimProg:    postProcessVerbatim,
		sz.NameVerbatimZettel:  postProcessVerbatim,
		sz.NameHeading:         postProcessHeading,
		sz.NameListOrdered:     postProcessItemList,
		sz.NameListUnordered:   postProcessItemList,
		sz.NameListQuote:       postProcessQuoteList,
		sz.NameDescription:     postProcessDescription,
		sz.NameTable:           postProcessTable,

		sz.NameInline:       postProcessInlineList,
		sz.NameText:         postProcessText,
		sz.NameSoft:         postProcessSoft,
		sz.NameEndnote:      postProcessEndnote,
		sz.NameMark:         postProcessMark,
		sz.NameLinkBased:    postProcessInlines4,
		sz.NameLinkBroken:   postProcessInlines4,
		sz.NameLinkExternal: postProcessInlines4,
		sz.NameLinkFound:    postProcessInlines4,
		sz.NameLinkHosted:   postProcessInlines4,
		sz.NameLinkInvalid:  postProcessInlines4,
		sz.NameLinkQuery:    postProcessInlines4,
		sz.NameLinkSelf:     postProcessInlines4,
		sz.NameLinkZettel:   postProcessInlines4,
		sz.NameEmbed:        postProcessInlines4,
		sz.NameCite:         postProcessInlines4,
		sz.NameFormatDelete: postProcessFormat,
		sz.NameFormatEmph:   postProcessFormat,
		sz.NameFormatInsert: postProcessFormat,
		sz.NameFormatMark:   postProcessFormat,
		sz.NameFormatQuote:  postProcessFormat,
		sz.NameFormatStrong: postProcessFormat,
		sz.NameFormatSpan:   postProcessFormat,
		sz.NameFormatSub:    postProcessFormat,
		sz.NameFormatSuper:  postProcessFormat,

		symSeparator.GetValue(): ignoreProcess,
	}
}

func ignoreProcess(*postProcessor, *sx.Pair, *sx.Pair) *sx.Pair { return nil }

func postProcessBlockList(pp *postProcessor, lst *sx.Pair, env *sx.Pair) *sx.Pair {
	result := pp.visitPairList(lst.Tail(), env)
	if result == nil {
		if noBlockPair := env.Assoc(symNoBlock); noBlockPair == nil || sx.IsTrue(noBlockPair.Cdr()) {
			return nil
		}
	}
	return result.Cons(lst.Car())
}

func postProcessInlineList(pp *postProcessor, lst *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := lst.Car()
	if rest := pp.visitInlines(lst.Tail(), env); rest != nil {
		return rest.Cons(sym)
	}
	return nil
}

func postProcessRegion(pp *postProcessor, rn *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := rn.Car()
	next := rn.Tail()
	attrs := next.Car()
	next = next.Tail()
	blocks := pp.visitPairList(next.Head(), env)
	text := pp.visitInlines(next.Tail(), env)
	if blocks == nil && text == nil {
		return nil
	}
	return text.Cons(blocks).Cons(attrs).Cons(sym)
}

func postProcessRegionVerse(pp *postProcessor, rn *sx.Pair, env *sx.Pair) *sx.Pair {
	return postProcessRegion(pp, rn, env.Cons(sx.Cons(symInVerse, nil)))
}

func postProcessVerbatim(_ *postProcessor, verb *sx.Pair, _ *sx.Pair) *sx.Pair {
	if content, isString := sx.GetString(verb.Tail().Tail().Car()); isString && content.GetValue() != "" {
		return verb
	}
	return nil
}

func postProcessHeading(pp *postProcessor, hn *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := hn.Car()
	next := hn.Tail()
	level := next.Car()
	next = next.Tail()
	attrs := next.Car()
	next = next.Tail()
	slug := next.Car()
	next = next.Tail()
	fragment := next.Car()
	if text := pp.visitInlines(next.Tail(), env); text != nil {
		return text.Cons(fragment).Cons(slug).Cons(attrs).Cons(level).Cons(sym)
	}
	return nil
}

func postProcessItemList(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
	elems := pp.visitListElems(ln, env)
	if elems == nil {
		return nil
	}
	return elems.Cons(ln.Car())
}

func postProcessQuoteList(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
	elems := pp.visitListElems(ln, env.Cons(sx.Cons(symNoBlock, nil)))

	// Collect multiple paragraph items into one item.

	var newElems sx.ListBuilder
	var newPara sx.ListBuilder

	addtoParagraph := func() {
		if !newPara.IsEmpty() {
			newElems.Add(sx.MakeList(sz.SymBlock, newPara.List().Cons(sz.SymPara)))
		}
	}
	for node := elems; node != nil; node = node.Tail() {
		item := node.Head()
		if !item.Car().IsEqual(sz.SymBlock) {
			continue
		}
		itemTail := item.Tail()
		if itemTail == nil || itemTail.Tail() != nil {
			addtoParagraph()
			newElems.Add(item)
			continue
		}
		if pn := itemTail.Head(); pn.Car().IsEqual(sz.SymPara) {
			if !newPara.IsEmpty() {
				newPara.Add(sx.Cons(sz.SymSoft, nil))
			}
			newPara.ExtendBang(pn.Tail())
			continue
		}
		addtoParagraph()
		newElems.Add(item)
	}
	addtoParagraph()
	return newElems.List().Cons(ln.Car())
}

func (pp *postProcessor) visitListElems(ln *sx.Pair, env *sx.Pair) *sx.Pair {
	var pList sx.ListBuilder
	for node := ln.Tail(); node != nil; node = node.Tail() {
		if elem := sz.Walk(pp, node.Head(), env); elem != nil {
			pList.Add(elem)
		}
	}
	return pList.List()
}

func postProcessDescription(pp *postProcessor, dl *sx.Pair, env *sx.Pair) *sx.Pair {
	var dList sx.ListBuilder
	isTerm := false
	for node := dl.Tail(); node != nil; node = node.Tail() {
		isTerm = !isTerm
		if isTerm {
			dList.Add(pp.visitInlines(node.Head(), env))
		} else {
			dList.Add(sz.Walk(pp, node.Head(), env))
		}
	}
	return dList.List().Cons(dl.Car())
}

func postProcessTable(pp *postProcessor, tbl *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := tbl.Car()
	next := tbl.Tail()
	header := next.Head()
	if header != nil {
		// Already post-processed
		return tbl
	}
	rows, width := pp.visitRows(next.Tail(), env)
	if rows == nil {
		// Header and row are nil -> no table
		return nil
	}
	header, rows, align := splitTableHeader(rows, width)
	alignRow(header, align)
	for node := rows; node != nil; node = node.Tail() {
		alignRow(node.Head(), align)
	}
	return rows.Cons(header).Cons(sym)
}

func (pp *postProcessor) visitRows(rows *sx.Pair, env *sx.Pair) (*sx.Pair, int) {
	maxWidth := 0
	var pRows sx.ListBuilder
	for node := rows; node != nil; node = node.Tail() {
		row := node.Head()
		row, width := pp.visitCells(row, env)
		if maxWidth < width {
			maxWidth = width
		}
		pRows.Add(row)
	}
	return pRows.List(), maxWidth
}

func (pp *postProcessor) visitCells(cells *sx.Pair, env *sx.Pair) (*sx.Pair, int) {
	width := 0
	var pCells sx.ListBuilder
	for node := cells; node != nil; node = node.Tail() {
		cell := node.Head()
		ins := pp.visitInlines(cell.Tail(), env)
		newCell := ins.Cons(cell.Car())
		pCells.Add(newCell)
		width++
	}
	return pCells.List(), width
}

func splitTableHeader(rows *sx.Pair, width int) (header, realRows *sx.Pair, align []*sx.Symbol) {
	align = make([]*sx.Symbol, width)

	foundHeader := false
	cellCount := 0

	// assert: rows != nil (checked in postProcessTable)
	for node := rows.Head(); node != nil; node = node.Tail() {
		cellCount++
		cell := node.Head()
		cellTail := cell.Tail()
		if cellTail == nil {
			continue
		}

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

		// move to the last cell inline element
		for {
			next := cellTail.Tail()
			if next == nil {
				break
			}
			cellTail = next
		}

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

	if !foundHeader {
		for i := 0; i < width; i++ {
			align[i] = sz.SymCell // Default alignment
		}
		return nil, rows, align
	}

	for i := 0; i < width; i++ {
		if align[i] == nil {
			align[i] = sz.SymCell // Default alignment
		}
	}
	return rows.Head(), rows.Tail(), align
}

func alignRow(row *sx.Pair, align []*sx.Symbol) {
	if row == nil {
		return
	}
	var lastCellNode *sx.Pair
	cellCount := 0
	for node := row; node != nil; node = node.Tail() {
		lastCellNode = node
		cell := node.Head()
		cell.SetCar(align[cellCount])
		cellCount++
		cellTail := cell.Tail()
		if cellTail == nil {
			continue
		}

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

	for cellCount < len(align) {
		lastCellNode = lastCellNode.AppendBang(sx.Cons(align[cellCount], nil))
		cellCount++
	}
}

func getCellAlignment(ch byte) *sx.Symbol {
	switch ch {
	case ':':
		return sz.SymCellCenter
	case '<':
		return sz.SymCellLeft
	case '>':
		return sz.SymCellRight
	default:
		return sz.SymCell
	}
}

func (pp *postProcessor) visitInlines(lst *sx.Pair, env *sx.Pair) *sx.Pair {
	length := lst.Length()
	if length <= 0 {
		return nil
	}
	inVerse := env.Assoc(symInVerse) != nil
	vector := make([]*sx.Pair, 0, length)
	// 1st phase: process all childs, ignore SPACE at start, and merge some elements
	for node := lst; node != nil; node = node.Tail() {
		elem := sz.Walk(pp, node.Head(), env)
		if elem == nil {
			continue
		}
		elemSym := elem.Car()
		if len(vector) == 0 {
			// The 1st element is always moved, except for a SPACE outside a verse block
			if !inVerse && elemSym.IsEqual(sz.SymSpace) {
				continue
			}
			vector = append(vector, elem)
			continue
		}
		last := vector[len(vector)-1]
		lastSym := last.Car()

		if lastSym.IsEqual(sz.SymText) && elemSym.IsEqual(sz.SymText) {
			// Merge two TEXT elements into one
			lastText := last.Tail().Car().(sx.String).GetValue()
			elemText := elem.Tail().Car().(sx.String).GetValue()
			last.SetCdr(sx.Cons(sx.MakeString(lastText+elemText), sx.Nil()))
			continue
		}

		if lastSym.IsEqual(sz.SymSpace) && elemSym.IsEqual(sz.SymSoft) {
			// Merge (SPACE) (SOFT) to (HARD)
			vector[len(vector)-1] = sx.Cons(sz.SymHard, sx.Nil())
			continue
		}

		vector = append(vector, elem)
	}
	if len(vector) == 0 {
		return nil
	}

	// 2nd phase: remove (SOFT), (HARD), (SPACE) at the end
	lastPos := len(vector) - 1
	for lastPos >= 0 {
		elem := vector[lastPos]
		elemSym := elem.Car()
		if !elemSym.IsEqual(sz.SymSpace) && !sz.IsBreakSym(elemSym) {
			break
		}
		lastPos--
	}
	if lastPos < 0 {
		return nil
	}

	result := sx.Cons(vector[0], nil)
	curr := result
	for i := 1; i <= lastPos; i++ {
		curr = curr.AppendBang(vector[i])
	}
	return result
}

func postProcessText(_ *postProcessor, txt *sx.Pair, _ *sx.Pair) *sx.Pair {
	if tail := txt.Tail(); tail != nil {
		if content, isString := sx.GetString(tail.Car()); isString && content.GetValue() != "" {
			return txt
		}
	}
	return nil
}

func postProcessSoft(_ *postProcessor, sn *sx.Pair, env *sx.Pair) *sx.Pair {
	if env.Assoc(symInVerse) == nil {
		return sn
	}
	return sx.Cons(sz.SymHard, nil)
}

func postProcessEndnote(pp *postProcessor, en *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := en.Car()
	next := en.Tail()
	attrs := next.Car()
	if text := pp.visitInlines(next.Tail(), env); text != nil {
		return text.Cons(attrs).Cons(sym)
	}
	return sx.MakeList(sym, attrs)
}

func postProcessMark(pp *postProcessor, en *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := en.Car()
	next := en.Tail()
	mark := next.Car()
	next = next.Tail()
	slug := next.Car()
	next = next.Tail()
	fragment := next.Car()
	text := pp.visitInlines(next.Tail(), env)
	return text.Cons(fragment).Cons(slug).Cons(mark).Cons(sym)
}

func postProcessInlines4(pp *postProcessor, ln *sx.Pair, env *sx.Pair) *sx.Pair {
	sym := ln.Car()
	next := ln.Tail()
	attrs := next.Car()
	next = next.Tail()
	val3 := next.Car()
	text := pp.visitInlines(next.Tail(), env)
	return text.Cons(val3).Cons(attrs).Cons(sym)
}

func postProcessFormat(pp *postProcessor, fn *sx.Pair, env *sx.Pair) *sx.Pair {
	symFormat := fn.Car()
	next := fn.Tail() // Attrs
	attrs := next.Car()
	next = next.Tail() // Possible inlines
	if next == nil {
		return fn
	}
	inlines := pp.visitInlines(next, env)
	return inlines.Cons(attrs).Cons(symFormat)
}

Added sz/zmk/ref.go.





















































































































































































































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

import (
	"net/url"
	"strings"

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

// ParseReference parses a string and returns a reference.
func ParseReference(s string) *sx.Pair {
	if invalidReference(s) {
		return makePairRef(sz.SymRefStateInvalid, s)
	}
	if strings.HasPrefix(s, api.QueryPrefix) {
		return makePairRef(sz.SymRefStateQuery, s[len(api.QueryPrefix):])
	}
	if state, ok := localState(s); ok {
		if state.IsEqual(sz.SymRefStateBased) {
			s = s[1:]
		}
		_, err := url.Parse(s)
		if err == nil {
			return makePairRef(state, s)
		}
	}
	u, err := url.Parse(s)
	if err != nil {
		return makePairRef(sz.SymRefStateInvalid, s)
	}
	if !externalURL(u) {
		zid := api.ZettelID(u.Path)
		if zid.IsValid() {
			return makePairRef(sz.SymRefStateZettel, s)
		}
		if u.Path == "" && u.Fragment != "" {
			return makePairRef(sz.SymRefStateSelf, s)
		}
	}
	return makePairRef(sz.SymRefStateExternal, s)
}
func makePairRef(sym *sx.Symbol, val string) *sx.Pair {
	return sx.MakeList(sym, sx.MakeString(val))
}

func invalidReference(s string) bool { return s == "" || s == "00000000000000" }

func externalURL(u *url.URL) bool {
	return u.Scheme != "" || u.Opaque != "" || u.Host != "" || u.User != nil
}

func localState(path string) (*sx.Symbol, bool) {
	if len(path) > 0 && path[0] == '/' {
		if len(path) > 1 && path[1] == '/' {
			return sz.SymRefStateBased, true
		}
		return sz.SymRefStateHosted, true
	}
	if len(path) > 1 && path[0] == '.' {
		if len(path) > 2 && path[1] == '.' && path[2] == '/' {
			return sz.SymRefStateHosted, true
		}
		return sz.SymRefStateHosted, path[1] == '/'
	}
	return sz.SymRefStateInvalid, false
}

// ReferenceIsValid returns true if reference is valid
func ReferenceIsValid(ref *sx.Pair) bool {
	return !ref.Car().IsEqual(sz.SymRefStateInvalid)
}

// ReferenceIsZettel returns true if it is a reference to a local zettel.
func ReferenceIsZettel(ref *sx.Pair) bool {
	state := ref.Car()
	return state.IsEqual(sz.SymRefStateZettel) ||
		state.IsEqual(sz.SymRefStateSelf) ||
		state.IsEqual(sz.SymRefStateFound) ||
		state.IsEqual(sz.SymRefStateBroken)
}

// ReferenceIsLocal returns true if reference is local
func ReferenceIsLocal(ref *sx.Pair) bool {
	state := ref.Car()
	return state.IsEqual(sz.SymRefStateHosted) ||
		state.IsEqual(sz.SymRefStateBased)
}

// ReferenceIsExternal returns true if it is a reference to external material.
func ReferenceIsExternal(ref *sx.Pair) bool {
	return ref.Car().IsEqual(sz.SymRefStateExternal)
}

Added sz/zmk/ref_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
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
// -----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
// -----------------------------------------------------------------------------
package zmk_test

import (
	"testing"

	"t73f.de/r/zsc/sz/zmk"
)

func TestParseReference(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		link string
		err  bool
		exp  string
	}{
		{"", true, ""},
		{"12345678901234", false, "(ZETTEL \"12345678901234\")"},
		{"123", false, "(EXTERNAL \"123\")"},
		{",://", true, ""},
	}

	for i, tc := range testcases {
		got := zmk.ParseReference(tc.link)
		gotIsValid := zmk.ReferenceIsValid(got)
		if gotIsValid == tc.err {
			t.Errorf(
				"TC=%d, expected parse error of %q: %v, but got %q", i, tc.link, tc.err, got)
		}
		if gotIsValid && got.String() != tc.exp {
			t.Errorf("TC=%d, Reference of %q is %q, but got %q", i, tc.link, tc.exp, got)
		}
	}
}

func TestReferenceIsZettelMaterial(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		link       string
		isZettel   bool
		isExternal bool
		isLocal    bool
	}{
		{"", false, false, false},
		{"00000000000000", false, false, false},
		{"http://zettelstore.de/z/ast", false, true, false},
		{"12345678901234", true, false, false},
		{"12345678901234#local", true, false, false},
		{"http://12345678901234", false, true, false},
		{"http://zettelstore.de/z/12345678901234", false, true, false},
		{"http://zettelstore.de/12345678901234", false, true, false},
		{"/12345678901234", false, false, true},
		{"//12345678901234", false, false, true},
		{"./12345678901234", false, false, true},
		{"../12345678901234", false, false, true},
		{".../12345678901234", false, true, false},
	}

	for i, tc := range testcases {
		ref := zmk.ParseReference(tc.link)
		isZettel := zmk.ReferenceIsZettel(ref)
		if isZettel != tc.isZettel {
			t.Errorf(
				"TC=%d, Reference %q isZettel=%v expected, but got %v",
				i,
				tc.link,
				tc.isZettel,
				isZettel)
		}
		isLocal := zmk.ReferenceIsLocal(ref)
		if isLocal != tc.isLocal {
			t.Errorf(
				"TC=%d, Reference %q isLocal=%v expected, but got %v",
				i,
				tc.link,
				tc.isLocal, isLocal)
		}
		isExternal := zmk.ReferenceIsExternal(ref)
		if isExternal != tc.isExternal {
			t.Errorf(
				"TC=%d, Reference %q isExternal=%v expected, but got %v",
				i,
				tc.link,
				tc.isExternal,
				isExternal)
		}
	}
}

Added sz/zmk/zmk.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package zmk provides a parser for zettelmarkup.
package zmk

import (
	"slices"
	"strings"
	"unicode"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz"
)

func ParseBlocks(inp *input.Input) *sx.Pair {
	parser := zmkP{inp: inp}

	var lastPara *sx.Pair
	var blkBuild sx.ListBuilder
	for inp.Ch != input.EOS {
		bn, cont := parser.parseBlock(lastPara)
		if bn != nil {
			blkBuild.Add(bn)
		}
		if !cont {
			if bn.Car().IsEqual(sz.SymPara) {
				lastPara = bn
			} else {
				lastPara = nil
			}
		}
	}
	if parser.nestingLevel != 0 {
		panic("Nesting level was not decremented")
	}

	bnl := blkBuild.List()
	var pp postProcessor
	if bs := pp.visitPairList(bnl, nil); bs != nil {
		return bs.Cons(sz.SymBlock)
	}
	return nil
}

func ParseInlines(inp *input.Input) *sx.Pair {
	parser := zmkP{inp: inp}
	var ins sx.Vector
	for inp.Ch != input.EOS {
		in := parser.parseInline()
		if in == nil {
			break
		}
		ins = append(ins, in)
	}

	inl := ins.MakeList().Cons(sz.SymInline)
	var pp postProcessor
	return sz.Walk(&pp, inl, nil)
}

type zmkP struct {
	inp          *input.Input // Input stream
	lists        []*sx.Pair   // Stack of lists
	lastRow      *sx.Pair     // Last row of table, or nil if not in table.
	descrl       *sx.Pair     // Current description list
	nestingLevel int          // Count nesting of block and inline elements

	inVerse bool // Currently in a vers region?
}

// runeModGrave is Unicode code point U+02CB (715) called "MODIFIER LETTER
// GRAVE ACCENT". On the iPad it is much more easier to type in this code point
// than U+0060 (96) "Grave accent" (aka backtick). Therefore, U+02CB will be
// considered equivalent to U+0060.
const runeModGrave = 'ˋ' // This is NOT '`'!

const maxNestingLevel = 50

// clearStacked removes all multi-line nodes from parser.
func (cp *zmkP) clearStacked() {
	cp.lists = nil
	cp.lastRow = nil
	cp.descrl = nil
}

type attrMap map[string]string

func (attrs attrMap) updateAttrs(key, val string) {
	if prevVal := attrs[key]; len(prevVal) > 0 {
		attrs[key] = prevVal + " " + val
	} else {
		attrs[key] = val
	}
}

func (attrs attrMap) asPairAssoc() *sx.Pair {
	names := make([]string, 0, len(attrs))
	for n := range attrs {
		names = append(names, n)
	}
	slices.Sort(names)
	var assoc *sx.Pair = nil
	for i := len(names) - 1; i >= 0; i-- {
		n := names[i]
		assoc = assoc.Cons(sx.Cons(sx.MakeString(n), sx.MakeString(attrs[n])))
	}
	return assoc
}

func (cp *zmkP) parseNormalAttribute(attrs attrMap) bool {
	inp := cp.inp
	posK := inp.Pos
	for isNameRune(inp.Ch) {
		inp.Next()
	}
	if posK == inp.Pos {
		return false
	}
	key := string(inp.Src[posK:inp.Pos])
	if inp.Ch != '=' {
		attrs[key] = ""
		return true
	}
	return cp.parseAttributeValue(key, attrs)
}

func (cp *zmkP) parseAttributeValue(key string, attrs attrMap) bool {
	inp := cp.inp
	if inp.Next() == '"' {
		return cp.parseQuotedAttributeValue(key, attrs)
	}
	posV := inp.Pos
	for {
		switch inp.Ch {
		case input.EOS:
			return false
		case '\n', '\r', ' ', ',', '}':
			attrs.updateAttrs(key, string(inp.Src[posV:inp.Pos]))
			return true
		}
		inp.Next()
	}
}

func (cp *zmkP) parseQuotedAttributeValue(key string, attrs attrMap) bool {
	inp := cp.inp
	inp.Next()
	var sb strings.Builder
	for {
		switch inp.Ch {
		case input.EOS:
			return false
		case '"':
			attrs.updateAttrs(key, sb.String())
			inp.Next()
			return true
		case '\\':
			switch inp.Next() {
			case input.EOS, '\n', '\r':
				return false
			}
			fallthrough
		default:
			sb.WriteRune(inp.Ch)
			inp.Next()
		}
	}

}

func (cp *zmkP) parseBlockAttributes() *sx.Pair {
	inp := cp.inp
	pos := inp.Pos
	for isNameRune(inp.Ch) {
		inp.Next()
	}
	if pos < inp.Pos {
		return attrMap{"": string(inp.Src[pos:inp.Pos])}.asPairAssoc()
	}

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

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

// doParseAttributes reads attributes.
func (cp *zmkP) doParseAttributes() (res *sx.Pair, success bool) {
	inp := cp.inp
	if inp.Ch != '{' {
		return nil, false
	}
	inp.Next()
	a := attrMap{}
	if !cp.parseAttributeValues(a) {
		return nil, false
	}
	inp.Next()
	return a.asPairAssoc(), true
}

func (cp *zmkP) parseAttributeValues(a attrMap) bool {
	inp := cp.inp
	for {
		cp.skipSpaceLine()
		switch inp.Ch {
		case input.EOS:
			return false
		case '}':
			return true
		case '.':
			inp.Next()
			posC := inp.Pos
			for isNameRune(inp.Ch) {
				inp.Next()
			}
			if posC == inp.Pos {
				return false
			}
			a.updateAttrs("class", string(inp.Src[posC:inp.Pos]))
		case '=':
			delete(a, "")
			if !cp.parseAttributeValue("", a) {
				return false
			}
		default:
			if !cp.parseNormalAttribute(a) {
				return false
			}
		}

		switch inp.Ch {
		case '}':
			return true
		case '\n', '\r':
		case ' ', ',':
			inp.Next()
		default:
			return false
		}
	}
}

func (cp *zmkP) skipSpaceLine() {
	for inp := cp.inp; ; {
		switch inp.Ch {
		case ' ':
			inp.Next()
		case '\n', '\r':
			inp.EatEOL()
		default:
			return
		}
	}
}

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

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

Added sz/zmk/zmk_fuzz_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
//-----------------------------------------------------------------------------
// 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.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022-present Detlef Stern
//-----------------------------------------------------------------------------

package zmk_test

import (
	"testing"

	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz/zmk"
)

func FuzzParseBlocks(f *testing.F) {
	f.Fuzz(func(t *testing.T, src []byte) {
		t.Parallel()
		inp := input.NewInput(src)
		zmk.ParseBlocks(inp)
	})
}

Added sz/zmk/zmk_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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of zettelstore-client.
//
// Zettelstore client is licensed under the latest version of the EUPL
// (European Union Public License). Please see file LICENSE.txt for your rights
// and obligations under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------

// Package zmk_test provides some tests for the zettelmarkup parser.
package zmk_test

import (
	"fmt"
	"strings"
	"testing"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/input"
	"t73f.de/r/zsc/sz"
	"t73f.de/r/zsc/sz/zmk"
)

type TestCase struct{ source, want string }
type TestCases []TestCase
type symbolMap map[string]*sx.Symbol

func replace(s string, sm symbolMap, tcs TestCases) TestCases {
	var sym string
	if len(sm) > 0 {
		sym = sm[s].GetValue()
	}
	var testCases TestCases
	for _, tc := range tcs {
		source := strings.ReplaceAll(tc.source, "$", s)
		want := tc.want
		if sym != "" {
			want = strings.ReplaceAll(want, "$%", sym)
		}
		want = strings.ReplaceAll(want, "$", s)
		testCases = append(testCases, TestCase{source, want})
	}
	return testCases
}

func checkTcs(t *testing.T, isBlock bool, tcs TestCases) {
	t.Helper()

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

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

func TestText(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"abcd", "(INLINE (TEXT \"abcd\"))"},
		{"ab cd", "(INLINE (TEXT \"ab\") (SPACE) (TEXT \"cd\"))"},
		{"abcd ", "(INLINE (TEXT \"abcd\"))"},
		{" abcd", "(INLINE (TEXT \"abcd\"))"},
		{"\\", "(INLINE (TEXT \"\\\\\"))"},
		{"\\\n", "()"},
		{"\\\ndef", "(INLINE (HARD) (TEXT \"def\"))"},
		{"\\\r", "()"},
		{"\\\rdef", "(INLINE (HARD) (TEXT \"def\"))"},
		{"\\\r\n", "()"},
		{"\\\r\ndef", "(INLINE (HARD) (TEXT \"def\"))"},
		{"\\a", "(INLINE (TEXT \"a\"))"},
		{"\\aa", "(INLINE (TEXT \"aa\"))"},
		{"a\\a", "(INLINE (TEXT \"aa\"))"},
		{"\\+", "(INLINE (TEXT \"+\"))"},
		{"\\ ", "(INLINE (TEXT \"\u00a0\"))"},
		{"http://a, http://b", "(INLINE (TEXT \"http://a,\") (SPACE) (TEXT \"http://b\"))"},
	})
}

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

func TestSoftBreak(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"x\ny", "(INLINE (TEXT \"x\") (SOFT) (TEXT \"y\"))"},
		{"z\n", "(INLINE (TEXT \"z\"))"},
		{" \n ", "()"},
		{" \n", "()"},
	})
}

func TestHardBreak(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"x  \ny", "(INLINE (TEXT \"x\") (HARD) (TEXT \"y\"))"},
		{"z  \n", "(INLINE (TEXT \"z\"))"},
		{"   \n ", "()"},
		{"   \n", "()"},
	})
}

func TestLink(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"[", "(INLINE (TEXT \"[\"))"},
		{"[[", "(INLINE (TEXT \"[[\"))"},
		{"[[|", "(INLINE (TEXT \"[[|\"))"},
		{"[[]", "(INLINE (TEXT \"[[]\"))"},
		{"[[|]", "(INLINE (TEXT \"[[|]\"))"},
		{"[[]]", "(INLINE (TEXT \"[[]]\"))"},
		{"[[|]]", "(INLINE (TEXT \"[[|]]\"))"},
		{"[[ ]]", "(INLINE (TEXT \"[[\") (SPACE) (TEXT \"]]\"))"},
		{"[[\n]]", "(INLINE (TEXT \"[[\") (SOFT) (TEXT \"]]\"))"},
		{"[[ a]]", "(INLINE (LINK-EXTERNAL () \"a\"))"},
		{"[[a ]]", "(INLINE (TEXT \"[[a\") (SPACE) (TEXT \"]]\"))"},
		{"[[a\n]]", "(INLINE (TEXT \"[[a\") (SOFT) (TEXT \"]]\"))"},
		{"[[a]]", "(INLINE (LINK-EXTERNAL () \"a\"))"},
		{"[[12345678901234]]", "(INLINE (LINK-ZETTEL () \"12345678901234\"))"},
		{"[[a]", "(INLINE (TEXT \"[[a]\"))"},
		{"[[|a]]", "(INLINE (TEXT \"[[|a]]\"))"},
		{"[[b|]]", "(INLINE (TEXT \"[[b|]]\"))"},
		{"[[b|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b\")))"},
		{"[[b| a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b\")))"},
		{"[[b%c|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b%c\")))"},
		{"[[b%%c|a]]", "(INLINE (TEXT \"[[b\") (LITERAL-COMMENT () \"c|a]]\"))"},
		{"[[b|a]", "(INLINE (TEXT \"[[b|a]\"))"},
		{"[[b\nc|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b\") (SOFT) (TEXT \"c\")))"},
		{"[[b c|a#n]]", "(INLINE (LINK-EXTERNAL () \"a#n\" (TEXT \"b\") (SPACE) (TEXT \"c\")))"},
		{"[[a]]go", "(INLINE (LINK-EXTERNAL () \"a\") (TEXT \"go\"))"},
		{"[[b|a]]{go}", "(INLINE (LINK-EXTERNAL ((\"go\" . \"\")) \"a\" (TEXT \"b\")))"},
		{"[[[[a]]|b]]", "(INLINE (TEXT \"[[\") (LINK-EXTERNAL () \"a\") (TEXT \"|b]]\"))"},
		{"[[a[b]c|d]]", "(INLINE (LINK-EXTERNAL () \"d\" (TEXT \"a[b]c\")))"},
		{"[[[b]c|d]]", "(INLINE (TEXT \"[\") (LINK-EXTERNAL () \"d\" (TEXT \"b]c\")))"},
		{"[[a[]c|d]]", "(INLINE (LINK-EXTERNAL () \"d\" (TEXT \"a[]c\")))"},
		{"[[a[b]|d]]", "(INLINE (LINK-EXTERNAL () \"d\" (TEXT \"a[b]\")))"},
		{"[[\\|]]", "(INLINE (LINK-EXTERNAL () \"\\\\|\"))"},
		{"[[\\||a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"|\")))"},
		{"[[b\\||a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b|\")))"},
		{"[[b\\|c|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b|c\")))"},
		{"[[\\]]]", "(INLINE (LINK-EXTERNAL () \"\\\\]\"))"},
		{"[[\\]|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"]\")))"},
		{"[[b\\]|a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"b]\")))"},
		{"[[\\]\\||a]]", "(INLINE (LINK-EXTERNAL () \"a\" (TEXT \"]|\")))"},
		{"[[http://a]]", "(INLINE (LINK-EXTERNAL () \"http://a\"))"},
		{"[[http://a|http://a]]", "(INLINE (LINK-EXTERNAL () \"http://a\" (TEXT \"http://a\")))"},
		{"[[[[a]]]]", "(INLINE (TEXT \"[[\") (LINK-EXTERNAL () \"a\") (TEXT \"]]\"))"},
		{"[[query:title]]", "(INLINE (LINK-QUERY () \"title\"))"},
		{"[[query:title syntax]]", "(INLINE (LINK-QUERY () \"title syntax\"))"},
		{"[[query:title | action]]", "(INLINE (LINK-QUERY () \"title | action\"))"},
		{"[[Text|query:title]]", "(INLINE (LINK-QUERY () \"title\" (TEXT \"Text\")))"},
		{"[[Text|query:title syntax]]", "(INLINE (LINK-QUERY () \"title syntax\" (TEXT \"Text\")))"},
		{"[[Text|query:title | action]]", "(INLINE (LINK-QUERY () \"title | action\" (TEXT \"Text\")))"},
	})
}

func TestEmbed(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"{", "(INLINE (TEXT \"{\"))"},
		{"{{", "(INLINE (TEXT \"{{\"))"},
		{"{{|", "(INLINE (TEXT \"{{|\"))"},
		{"{{}", "(INLINE (TEXT \"{{}\"))"},
		{"{{|}", "(INLINE (TEXT \"{{|}\"))"},
		{"{{}}", "(INLINE (TEXT \"{{}}\"))"},
		{"{{|}}", "(INLINE (TEXT \"{{|}}\"))"},
		{"{{ }}", "(INLINE (TEXT \"{{\") (SPACE) (TEXT \"}}\"))"},
		{"{{\n}}", "(INLINE (TEXT \"{{\") (SOFT) (TEXT \"}}\"))"},
		{"{{a }}", "(INLINE (TEXT \"{{a\") (SPACE) (TEXT \"}}\"))"},
		{"{{a\n}}", "(INLINE (TEXT \"{{a\") (SOFT) (TEXT \"}}\"))"},
		{"{{a}}", "(INLINE (EMBED () \"a\"))"},
		{"{{12345678901234}}", "(INLINE (EMBED () \"12345678901234\"))"},
		{"{{ a}}", "(INLINE (EMBED () \"a\"))"},
		{"{{a}", "(INLINE (TEXT \"{{a}\"))"},
		{"{{|a}}", "(INLINE (TEXT \"{{|a}}\"))"},
		{"{{b|}}", "(INLINE (TEXT \"{{b|}}\"))"},
		{"{{b|a}}", "(INLINE (EMBED () \"a\" (TEXT \"b\")))"},
		{"{{b| a}}", "(INLINE (EMBED () \"a\" (TEXT \"b\")))"},
		{"{{b|a}", "(INLINE (TEXT \"{{b|a}\"))"},
		{"{{b\nc|a}}", "(INLINE (EMBED () \"a\" (TEXT \"b\") (SOFT) (TEXT \"c\")))"},
		{"{{b c|a#n}}", "(INLINE (EMBED () \"a#n\" (TEXT \"b\") (SPACE) (TEXT \"c\")))"},
		{"{{a}}{go}", "(INLINE (EMBED ((\"go\" . \"\")) \"a\"))"},
		{"{{{{a}}|b}}", "(INLINE (TEXT \"{{\") (EMBED () \"a\") (TEXT \"|b}}\"))"},
		{"{{\\|}}", "(INLINE (EMBED () \"\\\\|\"))"},
		{"{{\\||a}}", "(INLINE (EMBED () \"a\" (TEXT \"|\")))"},
		{"{{b\\||a}}", "(INLINE (EMBED () \"a\" (TEXT \"b|\")))"},
		{"{{b\\|c|a}}", "(INLINE (EMBED () \"a\" (TEXT \"b|c\")))"},
		{"{{\\}}}", "(INLINE (EMBED () \"\\\\}\"))"},
		{"{{\\}|a}}", "(INLINE (EMBED () \"a\" (TEXT \"}\")))"},
		{"{{b\\}|a}}", "(INLINE (EMBED () \"a\" (TEXT \"b}\")))"},
		{"{{\\}\\||a}}", "(INLINE (EMBED () \"a\" (TEXT \"}|\")))"},
		{"{{http://a}}", "(INLINE (EMBED () \"http://a\"))"},
		{"{{http://a|http://a}}", "(INLINE (EMBED () \"http://a\" (TEXT \"http://a\")))"},
		{"{{{{a}}}}", "(INLINE (TEXT \"{{\") (EMBED () \"a\") (TEXT \"}}\"))"},
	})
}

func TestCite(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"[@", "(INLINE (TEXT \"[@\"))"},
		{"[@]", "(INLINE (TEXT \"[@]\"))"},
		{"[@a]", "(INLINE (CITE () \"a\"))"},
		{"[@ a]", "(INLINE (TEXT \"[@\") (SPACE) (TEXT \"a]\"))"},
		{"[@a ]", "(INLINE (CITE () \"a\"))"},
		{"[@a\n]", "(INLINE (CITE () \"a\"))"},
		{"[@a\nx]", "(INLINE (CITE () \"a\" (SOFT) (TEXT \"x\")))"},
		{"[@a\n\n]", "(INLINE (TEXT \"[@a\") (SOFT) (SOFT) (TEXT \"]\"))"},
		{"[@a,\n]", "(INLINE (CITE () \"a\"))"},
		{"[@a,n]", "(INLINE (CITE () \"a\" (TEXT \"n\")))"},
		{"[@a| n]", "(INLINE (CITE () \"a\" (TEXT \"n\")))"},
		{"[@a|n ]", "(INLINE (CITE () \"a\" (TEXT \"n\")))"},
		{"[@a,[@b]]", "(INLINE (CITE () \"a\" (CITE () \"b\")))"},
		{"[@a]{color=green}", "(INLINE (CITE ((\"color\" . \"green\")) \"a\"))"},
	})
	checkTcs(t, true, TestCases{
		{"[@a\n\n]", "(BLOCK (PARA (TEXT \"[@a\")) (PARA (TEXT \"]\")))"},
	})
}

func TestEndnote(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"[^", "(INLINE (TEXT \"[^\"))"},
		{"[^]", "(INLINE (ENDNOTE ()))"},
		{"[^abc]", "(INLINE (ENDNOTE () (TEXT \"abc\")))"},
		{"[^abc ]", "(INLINE (ENDNOTE () (TEXT \"abc\")))"},
		{"[^abc\ndef]", "(INLINE (ENDNOTE () (TEXT \"abc\") (SOFT) (TEXT \"def\")))"},
		{"[^abc\n\ndef]", "(INLINE (TEXT \"[^abc\") (SOFT) (SOFT) (TEXT \"def]\"))"},
		{"[^abc[^def]]", "(INLINE (ENDNOTE () (TEXT \"abc\") (ENDNOTE () (TEXT \"def\"))))"},
		{"[^abc]{-}", "(INLINE (ENDNOTE ((\"-\" . \"\")) (TEXT \"abc\")))"},
	})
	checkTcs(t, true, TestCases{
		{"[^abc\n\ndef]", "(BLOCK (PARA (TEXT \"[^abc\")) (PARA (TEXT \"def]\")))"},
	})
}

func TestMark(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"[!", "(INLINE (TEXT \"[!\"))"},
		{"[!\n", "(INLINE (TEXT \"[!\"))"},
		{"[!]", "(INLINE (MARK \"\" \"\" \"\"))"},
		{"[!][!]", "(INLINE (MARK \"\" \"\" \"\") (MARK \"\" \"\" \"\"))"},
		{"[! ]", "(INLINE (TEXT \"[!\") (SPACE) (TEXT \"]\"))"},
		{"[!a]", "(INLINE (MARK \"a\" \"\" \"\"))"},
		{"[!a][!a]", "(INLINE (MARK \"a\" \"\" \"\") (MARK \"a\" \"\" \"\"))"},
		{"[!a ]", "(INLINE (TEXT \"[!a\") (SPACE) (TEXT \"]\"))"},
		{"[!a_]", "(INLINE (MARK \"a_\" \"\" \"\"))"},
		{"[!a_][!a]", "(INLINE (MARK \"a_\" \"\" \"\") (MARK \"a\" \"\" \"\"))"},
		{"[!a-b]", "(INLINE (MARK \"a-b\" \"\" \"\"))"},
		{"[!a|b]", "(INLINE (MARK \"a\" \"\" \"\" (TEXT \"b\")))"},
		{"[!a|]", "(INLINE (MARK \"a\" \"\" \"\"))"},
		{"[!|b]", "(INLINE (MARK \"\" \"\" \"\" (TEXT \"b\")))"},
		{"[!|b ]", "(INLINE (MARK \"\" \"\" \"\" (TEXT \"b\")))"},
		{"[!|b c]", "(INLINE (MARK \"\" \"\" \"\" (TEXT \"b\") (SPACE) (TEXT \"c\")))"},
	})
}

func TestComment(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"%", "(INLINE (TEXT \"%\"))"},
		{"%%", "(INLINE (LITERAL-COMMENT () \"\"))"},
		{"%\n", "(INLINE (TEXT \"%\"))"},
		{"%%\n", "(INLINE (LITERAL-COMMENT () \"\"))"},
		{"%%a", "(INLINE (LITERAL-COMMENT () \"a\"))"},
		{"%%%a", "(INLINE (LITERAL-COMMENT () \"a\"))"},
		{"%% a", "(INLINE (LITERAL-COMMENT () \"a\"))"},
		{"%%%  a", "(INLINE (LITERAL-COMMENT () \"a\"))"},
		{"%% % a", "(INLINE (LITERAL-COMMENT () \"% a\"))"},
		{"%%a", "(INLINE (LITERAL-COMMENT () \"a\"))"},
		{"a%%b", "(INLINE (TEXT \"a\") (LITERAL-COMMENT () \"b\"))"},
		{"a %%b", "(INLINE (TEXT \"a\") (SPACE) (LITERAL-COMMENT () \"b\"))"},
		{" %%b", "(INLINE (LITERAL-COMMENT () \"b\"))"},
		{"%%b ", "(INLINE (LITERAL-COMMENT () \"b \"))"},
		{"100%", "(INLINE (TEXT \"100%\"))"},
		{"%%{=}a", "(INLINE (LITERAL-COMMENT ((\"\" . \"\")) \"a\"))"},
	})
}

func TestFormat(t *testing.T) {
	symMap := symbolMap{
		"_": sz.SymFormatEmph,
		"*": sz.SymFormatStrong,
		">": sz.SymFormatInsert,
		"~": sz.SymFormatDelete,
		"^": sz.SymFormatSuper,
		",": sz.SymFormatSub,
		"#": sz.SymFormatMark,
		":": sz.SymFormatSpan,
	}
	t.Parallel()
	// Not for Insert / '>', because collision with quoted list
	// Not for Quote / '"', because escaped representation.
	for _, ch := range []string{"_", "*", "~", "^", ",", "#", ":"} {
		checkTcs(t, false, replace(ch, symMap, TestCases{
			{"$", "(INLINE (TEXT \"$\"))"},
			{"$$", "(INLINE (TEXT \"$$\"))"},
			{"$$$", "(INLINE (TEXT \"$$$\"))"},
			{"$$$$", "(INLINE ($% ()))"},
		}))
	}
	// Not for Quote / '"', because escaped representation.
	for _, ch := range []string{"_", "*", ">", "~", "^", ",", "#", ":"} {
		checkTcs(t, false, replace(ch, symMap, TestCases{
			{"$$a$$", "(INLINE ($% () (TEXT \"a\")))"},
			{"$$a$$$", "(INLINE ($% () (TEXT \"a\")) (TEXT \"$\"))"},
			{"$$$a$$", "(INLINE ($% () (TEXT \"$a\")))"},
			{"$$$a$$$", "(INLINE ($% () (TEXT \"$a\")) (TEXT \"$\"))"},
			{"$\\$", "(INLINE (TEXT \"$$\"))"},
			{"$\\$$", "(INLINE (TEXT \"$$$\"))"},
			{"$$\\$", "(INLINE (TEXT \"$$$\"))"},
			{"$$a\\$$", "(INLINE (TEXT \"$$a$$\"))"},
			{"$$a$\\$", "(INLINE (TEXT \"$$a$$\"))"},
			{"$$a\\$$$", "(INLINE ($% () (TEXT \"a$\")))"},
			{"$$a\na$$", "(INLINE ($% () (TEXT \"a\") (SOFT) (TEXT \"a\")))"},
			{"$$a\n\na$$", "(INLINE (TEXT \"$$a\") (SOFT) (SOFT) (TEXT \"a$$\"))"},
			{"$$a$${go}", "(INLINE ($% ((\"go\" . \"\")) (TEXT \"a\")))"},
		}))
		checkTcs(t, true, replace(ch, symMap, TestCases{
			{"$$a\n\na$$", "(BLOCK (PARA (TEXT \"$$a\")) (PARA (TEXT \"a$$\")))"},
		}))
	}
	checkTcs(t, false, replace(`"`, symbolMap{`"`: sz.SymFormatQuote}, TestCases{
		{"$", "(INLINE (TEXT \"\\\"\"))"},
		{"$$", "(INLINE (TEXT \"\\\"\\\"\"))"},
		{"$$$", "(INLINE (TEXT \"\\\"\\\"\\\"\"))"},
		{"$$$$", "(INLINE ($% ()))"},

		{"$$a$$", "(INLINE ($% () (TEXT \"a\")))"},
		{"$$a$$$", "(INLINE ($% () (TEXT \"a\")) (TEXT \"\\\"\"))"},
		{"$$$a$$", "(INLINE ($% () (TEXT \"\\\"a\")))"},
		{"$$$a$$$", "(INLINE ($% () (TEXT \"\\\"a\")) (TEXT \"\\\"\"))"},
		{"$\\$", "(INLINE (TEXT \"\\\"\\\"\"))"},
		{"$\\$$", "(INLINE (TEXT \"\\\"\\\"\\\"\"))"},
		{"$$\\$", "(INLINE (TEXT \"\\\"\\\"\\\"\"))"},
		{"$$a\\$$", "(INLINE (TEXT \"\\\"\\\"a\\\"\\\"\"))"},
		{"$$a$\\$", "(INLINE (TEXT \"\\\"\\\"a\\\"\\\"\"))"},
		{"$$a\\$$$", "(INLINE ($% () (TEXT \"a\\\"\")))"},
		{"$$a\na$$", "(INLINE ($% () (TEXT \"a\") (SOFT) (TEXT \"a\")))"},
		{"$$a\n\na$$", "(INLINE (TEXT \"\\\"\\\"a\") (SOFT) (SOFT) (TEXT \"a\\\"\\\"\"))"},
		{"$$a$${go}", "(INLINE ($% ((\"go\" . \"\")) (TEXT \"a\")))"},
	}))
	checkTcs(t, false, TestCases{
		{"__****__", "(INLINE (FORMAT-EMPH () (FORMAT-STRONG ())))"},
		{"__**a**__", "(INLINE (FORMAT-EMPH () (FORMAT-STRONG () (TEXT \"a\"))))"},
		{"__**__**", "(INLINE (TEXT \"__\") (FORMAT-STRONG () (TEXT \"__\")))"},
	})
}

func TestLiteral(t *testing.T) {
	symMap := symbolMap{
		"@": sz.SymLiteralZettel,
		"`": sz.SymLiteralProg,
		"'": sz.SymLiteralInput,
		"=": sz.SymLiteralOutput,
	}
	t.Parallel()
	for _, ch := range []string{"@", "`", "'", "="} {
		checkTcs(t, false, replace(ch, symMap, TestCases{
			{"$", "(INLINE (TEXT \"$\"))"},
			{"$$", "(INLINE (TEXT \"$$\"))"},
			{"$$$", "(INLINE (TEXT \"$$$\"))"},
			{"$$$$", "(INLINE ($% () \"\"))"},
			{"$$a$$", "(INLINE ($% () \"a\"))"},
			{"$$a$$$", "(INLINE ($% () \"a\") (TEXT \"$\"))"},
			{"$$$a$$", "(INLINE ($% () \"$a\"))"},
			{"$$$a$$$", "(INLINE ($% () \"$a\") (TEXT \"$\"))"},
			{"$\\$", "(INLINE (TEXT \"$$\"))"},
			{"$\\$$", "(INLINE (TEXT \"$$$\"))"},
			{"$$\\$", "(INLINE (TEXT \"$$$\"))"},
			{"$$a\\$$", "(INLINE (TEXT \"$$a$$\"))"},
			{"$$a$\\$", "(INLINE (TEXT \"$$a$$\"))"},
			{"$$a\\$$$", "(INLINE ($% () \"a$\"))"},
			{"$$a$${go}", "(INLINE ($% ((\"go\" . \"\")) \"a\"))"},
		}))
	}
	checkTcs(t, false, TestCases{
		{"''````''", "(INLINE (LITERAL-INPUT () \"````\"))"},
		{"''``a``''", "(INLINE (LITERAL-INPUT () \"``a``\"))"},
		{"''``''``", "(INLINE (LITERAL-INPUT () \"``\") (TEXT \"``\"))"},
		{"''\\'''", "(INLINE (LITERAL-INPUT () \"'\"))"},
	})
	checkTcs(t, false, TestCases{
		{"@@HTML@@{=html}", "(INLINE (LITERAL-HTML () \"HTML\"))"},
		{"@@HTML@@{=html lang=en}", "(INLINE (LITERAL-HTML ((\"lang\" . \"en\")) \"HTML\"))"},
		{"@@HTML@@{=html,lang=en}", "(INLINE (LITERAL-HTML ((\"lang\" . \"en\")) \"HTML\"))"},
	})
}

func TestLiteralMath(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"$", "(INLINE (TEXT \"$\"))"},
		{"$$", "(INLINE (TEXT \"$$\"))"},
		{"$$$", "(INLINE (TEXT \"$$$\"))"},
		{"$$$$", "(INLINE (LITERAL-MATH () \"\"))"},
		{"$$a$$", "(INLINE (LITERAL-MATH () \"a\"))"},
		{"$$a$$$", "(INLINE (LITERAL-MATH () \"a\") (TEXT \"$\"))"},
		{"$$$a$$", "(INLINE (LITERAL-MATH () \"$a\"))"},
		{"$$$a$$$", "(INLINE (LITERAL-MATH () \"$a\") (TEXT \"$\"))"},
		{`$\$`, "(INLINE (TEXT \"$$\"))"},
		{`$\$$`, "(INLINE (TEXT \"$$$\"))"},
		{`$$\$`, "(INLINE (TEXT \"$$$\"))"},
		{`$$a\$$`, "(INLINE (LITERAL-MATH () \"a\\\\\"))"},
		{`$$a$\$`, "(INLINE (TEXT \"$$a$$\"))"},
		{`$$a\$$$`, "(INLINE (LITERAL-MATH () \"a\\\\\") (TEXT \"$\"))"},
		{"$$a$${go}", "(INLINE (LITERAL-MATH ((\"go\" . \"\")) \"a\"))"},
	})
}

func TestMixFormatCode(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"__abc__\n**def**", "(INLINE (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (FORMAT-STRONG () (TEXT \"def\")))"},
		{"''abc''\n==def==", "(INLINE (LITERAL-INPUT () \"abc\") (SOFT) (LITERAL-OUTPUT () \"def\"))"},
		{"__abc__\n==def==", "(INLINE (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (LITERAL-OUTPUT () \"def\"))"},
		{"__abc__\n``def``", "(INLINE (FORMAT-EMPH () (TEXT \"abc\")) (SOFT) (LITERAL-CODE () \"def\"))"},
		{
			"\"\"ghi\"\"\n::abc::\n``def``\n",
			"(INLINE (FORMAT-QUOTE () (TEXT \"ghi\")) (SOFT) (FORMAT-SPAN () (TEXT \"abc\")) (SOFT) (LITERAL-CODE () \"def\"))",
		},
	})
}

func TestNDash(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"--", "(INLINE (TEXT \"\u2013\"))"},
		{"a--b", "(INLINE (TEXT \"a\u2013b\"))"},
	})
}

func TestEntity(t *testing.T) {
	t.Parallel()
	checkTcs(t, false, TestCases{
		{"&", "(INLINE (TEXT \"&\"))"},
		{"&;", "(INLINE (TEXT \"&;\"))"},
		{"&#;", "(INLINE (TEXT \"&#;\"))"},
		{"&#1a;", "(INLINE (TEXT \"&#1a;\"))"},
		{"&#x;", "(INLINE (TEXT \"&#x;\"))"},
		{"&#x0z;", "(INLINE (TEXT \"&#x0z;\"))"},
		{"&1;", "(INLINE (TEXT \"&1;\"))"},
		{"&#9;", "(INLINE (TEXT \"&#9;\"))"}, // Numeric entities below space are not allowed.
		{"&#x1f;", "(INLINE (TEXT \"&#x1f;\"))"},

		// Good cases
		{"&lt;", "(INLINE (TEXT \"<\"))"},
		{"&#48;", "(INLINE (TEXT \"0\"))"},
		{"&#x4A;", "(INLINE (TEXT \"J\"))"},
		{"&#X4a;", "(INLINE (TEXT \"J\"))"},
		{"&hellip;", "(INLINE (TEXT \"\u2026\"))"},
		{"&nbsp;", "(INLINE (TEXT \"\u00a0\"))"},
		{"E: &amp;,&#63;;&#x63;.", "(INLINE (TEXT \"E:\") (SPACE) (TEXT \"&,?;c.\"))"},
	})
}

func TestVerbatimZettel(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"@@@\n@@@", "()"},
		{"@@@\nabc\n@@@", "(BLOCK (VERBATIM-ZETTEL () \"abc\"))"},
		{"@@@@def\nabc\n@@@@", "(BLOCK (VERBATIM-ZETTEL ((\"\" . \"def\")) \"abc\"))"},
	})
}

func TestVerbatimCode(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"```\n```", "()"},
		{"```\nabc\n```", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
		{"```\nabc\n````", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
		{"````\nabc\n````", "(BLOCK (VERBATIM-CODE () \"abc\"))"},
		{"````\nabc\n```\n````", "(BLOCK (VERBATIM-CODE () \"abc\\n```\"))"},
		{"````go\nabc\n````", "(BLOCK (VERBATIM-CODE ((\"\" . \"go\")) \"abc\"))"},
	})
}

func TestVerbatimEval(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"~~~\n~~~", "()"},
		{"~~~\nabc\n~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
		{"~~~\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
		{"~~~~\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\"))"},
		{"~~~~\nabc\n~~~\n~~~~", "(BLOCK (VERBATIM-EVAL () \"abc\\n~~~\"))"},
		{"~~~~go\nabc\n~~~~", "(BLOCK (VERBATIM-EVAL ((\"\" . \"go\")) \"abc\"))"},
	})
}

func TestVerbatimMath(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"$$$\n$$$", "()"},
		{"$$$\nabc\n$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
		{"$$$\nabc\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
		{"$$$$\nabc\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\"))"},
		{"$$$$\nabc\n$$$\n$$$$", "(BLOCK (VERBATIM-MATH () \"abc\\n$$$\"))"},
		{"$$$$go\nabc\n$$$$", "(BLOCK (VERBATIM-MATH ((\"\" . \"go\")) \"abc\"))"},
	})
}

func TestVerbatimComment(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"%%%\n%%%", "()"},
		{"%%%\nabc\n%%%", "(BLOCK (VERBATIM-COMMENT () \"abc\"))"},
		{"%%%%go\nabc\n%%%%", "(BLOCK (VERBATIM-COMMENT ((\"\" . \"go\")) \"abc\"))"},
	})
}

func TestPara(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"a\n\nb", "(BLOCK (PARA (TEXT \"a\")) (PARA (TEXT \"b\")))"},
		{"a\n \nb", "(BLOCK (PARA (TEXT \"a\") (SOFT) (HARD) (TEXT \"b\")))"},
	})
}

func TestSpanRegion(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{":::\n:::", "()"},
		{":::\nabc\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
		{":::\nabc\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
		{"::::\nabc\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")))))"},
		{"::::\nabc\n:::\ndef\n:::\n::::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\")) (REGION-BLOCK () ((PARA (TEXT \"def\")))))))"},
		{":::{go}\n:::a", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) () (TEXT \"a\")))"},
		{":::\nabc\n::: def ", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
	})
}

func TestQuoteRegion(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"<<<\n<<<", "()"},
		{"<<<\nabc\n<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
		{"<<<\nabc\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
		{"<<<<\nabc\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")))))"},
		{"<<<<\nabc\n<<<\ndef\n<<<\n<<<<", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\")) (REGION-QUOTE () ((PARA (TEXT \"def\")))))))"},
		{"<<<go\n<<< a", "(BLOCK (REGION-QUOTE ((\"\" . \"go\")) () (TEXT \"a\")))"},
		{"<<<\nabc\n<<< def ", "(BLOCK (REGION-QUOTE () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
	})
}

func TestVerseRegion(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, replace("\"", nil, TestCases{
		{"$$$\n$$$", "()"},
		{"$$$\nabc\n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
		{"$$$\nabc\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
		{"$$$$\nabc\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")))))"},
		{"$$$\nabc\ndef\n$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\") (HARD) (TEXT \"def\")))))"},
		{"$$$$\nabc\n$$$\ndef\n$$$\n$$$$", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\")) (REGION-VERSE () ((PARA (TEXT \"def\")))))))"},
		{"$$$go\n$$$x", "(BLOCK (REGION-VERSE ((\"\" . \"go\")) () (TEXT \"x\")))"},
		{"$$$\nabc\n$$$ def ", "(BLOCK (REGION-VERSE () ((PARA (TEXT \"abc\"))) (TEXT \"def\")))"},
		{"$$$\n space \n$$$", "(BLOCK (REGION-VERSE () ((PARA (SPACE \"\u00a0\") (TEXT \"space\")))))"},
		{"$$$\n  spaces  \n$$$", "(BLOCK (REGION-VERSE () ((PARA (SPACE \"\u00a0\u00a0\") (TEXT \"spaces\")))))"},
		{"$$$\n  spaces  \n space  \n$$$", "(BLOCK (REGION-VERSE () ((PARA (SPACE \"\u00a0\u00a0\") (TEXT \"spaces\") (SPACE \"\u00a0\u00a0\") (HARD) (SPACE \"\u00a0\") (TEXT \"space\")))))"},
	}))
}

func TestHeading(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"=h", "(BLOCK (PARA (TEXT \"=h\")))"},
		{"= h", "(BLOCK (PARA (TEXT \"=\") (SPACE) (TEXT \"h\")))"},
		{"==h", "(BLOCK (PARA (TEXT \"==h\")))"},
		{"== h", "(BLOCK (PARA (TEXT \"==\") (SPACE) (TEXT \"h\")))"},
		{"===h", "(BLOCK (PARA (TEXT \"===h\")))"},
		{"===", "(BLOCK (PARA (TEXT \"===\")))"},
		{"=== h", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")))"},
		{"===  h", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")))"},
		{"==== h", "(BLOCK (HEADING 2 () \"\" \"\" (TEXT \"h\")))"},
		{"===== h", "(BLOCK (HEADING 3 () \"\" \"\" (TEXT \"h\")))"},
		{"====== h", "(BLOCK (HEADING 4 () \"\" \"\" (TEXT \"h\")))"},
		{"======= h", "(BLOCK (HEADING 5 () \"\" \"\" (TEXT \"h\")))"},
		{"======== h", "(BLOCK (HEADING 5 () \"\" \"\" (TEXT \"h\")))"},
		{"=", "(BLOCK (PARA (TEXT \"=\")))"},
		{"=== h=__=a__", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h=\") (FORMAT-EMPH () (TEXT \"=a\"))))"},
		{"=\n", "(BLOCK (PARA (TEXT \"=\")))"},
		{"a=", "(BLOCK (PARA (TEXT \"a=\")))"},
		{" =", "(BLOCK (PARA (TEXT \"=\")))"},
		{"=== h\na", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")) (PARA (TEXT \"a\")))"},
		{"=== h i {-}", "(BLOCK (HEADING 1 ((\"-\" . \"\")) \"\" \"\" (TEXT \"h\") (SPACE) (TEXT \"i\")))"},
		{"=== h {{a}}", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\") (SPACE) (EMBED () \"a\")))"},
		{"=== h{{a}}", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\") (EMBED () \"a\")))"},
		{"=== {{a}}", "(BLOCK (HEADING 1 () \"\" \"\" (EMBED () \"a\")))"},
		{"=== h {{a}}{-}", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\") (SPACE) (EMBED ((\"-\" . \"\")) \"a\")))"},
		{"=== h {{a}} {-}", "(BLOCK (HEADING 1 ((\"-\" . \"\")) \"\" \"\" (TEXT \"h\") (SPACE) (EMBED () \"a\")))"},
		{"=== h {-}{{a}}", "(BLOCK (HEADING 1 ((\"-\" . \"\")) \"\" \"\" (TEXT \"h\")))"},
		{"=== h{id=abc}", "(BLOCK (HEADING 1 ((\"id\" . \"abc\")) \"\" \"\" (TEXT \"h\")))"},
		{"=== h\n=== h", "(BLOCK (HEADING 1 () \"\" \"\" (TEXT \"h\")) (HEADING 1 () \"\" \"\" (TEXT \"h\")))"},
	})
}

func TestHRule(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"-", "(BLOCK (PARA (TEXT \"-\")))"},
		{"---", "(BLOCK (THEMATIC ()))"},
		{"----", "(BLOCK (THEMATIC ()))"},
		{"---A", "(BLOCK (THEMATIC ((\"\" . \"A\"))))"},
		{"---A-", "(BLOCK (THEMATIC ((\"\" . \"A-\"))))"},
		{"-1", "(BLOCK (PARA (TEXT \"-1\")))"},
		{"2-1", "(BLOCK (PARA (TEXT \"2-1\")))"},
		{"---  {  go  }  ", "(BLOCK (THEMATIC ((\"go\" . \"\"))))"},
		{"---  {  .go  }  ", "(BLOCK (THEMATIC ((\"class\" . \"go\"))))"},
	})
}

func TestList(t *testing.T) {
	t.Parallel()
	// No ">" in the following, because quotation lists may have empty items.
	for _, ch := range []string{"*", "#"} {
		checkTcs(t, true, replace(ch, nil, TestCases{
			{"$", "(BLOCK (PARA (TEXT \"$\")))"},
			{"$$", "(BLOCK (PARA (TEXT \"$$\")))"},
			{"$$$", "(BLOCK (PARA (TEXT \"$$$\")))"},
			{"$ ", "(BLOCK (PARA (TEXT \"$\")))"},
			{"$$ ", "(BLOCK (PARA (TEXT \"$$\")))"},
			{"$$$ ", "(BLOCK (PARA (TEXT \"$$$\")))"},
		}))
	}
	checkTcs(t, true, TestCases{
		{"* abc", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))))"},
		{"** abc", "(BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))))))"},
		{"*** abc", "(BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))))))))"},
		{"**** abc", "(BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))))))))))"},
		{"** abc\n**** def", "(BLOCK (UNORDERED (BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (UNORDERED (BLOCK (UNORDERED (BLOCK (PARA (TEXT \"def\")))))))))))"},
		{"* abc\ndef", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
		{"* abc\n def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
		{"* abc\n* def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\"))) (BLOCK (PARA (TEXT \"def\")))))"},
		{"* abc\n  def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\") (SOFT) (TEXT \"def\")))))"},
		{"* abc\n   def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\") (SOFT) (TEXT \"def\")))))"},
		{"* abc\n\ndef", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
		{"* abc\n\n def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (PARA (TEXT \"def\")))"},
		{"* abc\n\n  def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (PARA (TEXT \"def\")))))"},
		{"* abc\n\n   def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (PARA (TEXT \"def\")))))"},
		{"* abc\n** def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (UNORDERED (BLOCK (PARA (TEXT \"def\")))))))"},
		{"* abc\n** def\n* ghi", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (UNORDERED (BLOCK (PARA (TEXT \"def\"))))) (BLOCK (PARA (TEXT \"ghi\")))))"},
		{"* abc\n\n  def\n* ghi", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (PARA (TEXT \"def\"))) (BLOCK (PARA (TEXT \"ghi\")))))"},
		{"* abc\n** def\n   ghi\n  jkl", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")) (UNORDERED (BLOCK (PARA (TEXT \"def\") (SOFT) (TEXT \"ghi\")))) (PARA (TEXT \"jkl\")))))"},

		// A list does not last beyond a region
		{":::\n# abc\n:::\n# def", "(BLOCK (REGION-BLOCK () ((ORDERED (BLOCK (PARA (TEXT \"abc\")))))) (ORDERED (BLOCK (PARA (TEXT \"def\")))))"},

		// A HRule creates a new list
		{"* abc\n---\n* def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (THEMATIC ()) (UNORDERED (BLOCK (PARA (TEXT \"def\")))))"},

		// Changing list type adds a new list
		{"* abc\n# def", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))) (ORDERED (BLOCK (PARA (TEXT \"def\")))))"},

		// Quotation lists may have empty items
		{">", "(BLOCK (QUOTATION (BLOCK)))"},

		// Empty continuation
		{"* abc\n  ", "(BLOCK (UNORDERED (BLOCK (PARA (TEXT \"abc\")))))"},
	})
}

func TestQuoteList(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"> w1 w2", "(BLOCK (QUOTATION (BLOCK (PARA (TEXT \"w1\") (SPACE) (TEXT \"w2\")))))"},
		{"> w1\n> w2", "(BLOCK (QUOTATION (BLOCK (PARA (TEXT \"w1\") (SOFT) (TEXT \"w2\")))))"},
		{"> w1\n>w2", "(BLOCK (QUOTATION (BLOCK (PARA (TEXT \"w1\")))) (PARA (TEXT \">w2\")))"},
		{"> w1\n>\n>w2", "(BLOCK (QUOTATION (BLOCK (PARA (TEXT \"w1\"))) (BLOCK)) (PARA (TEXT \">w2\")))"},
		{"> w1\n> \n> w2", "(BLOCK (QUOTATION (BLOCK (PARA (TEXT \"w1\"))) (BLOCK) (BLOCK (PARA (TEXT \"w2\")))))"},
	})
}

func TestEnumAfterPara(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"abc\n* def", "(BLOCK (PARA (TEXT \"abc\")) (UNORDERED (BLOCK (PARA (TEXT \"def\")))))"},
		{"abc\n*def", "(BLOCK (PARA (TEXT \"abc\") (SOFT) (TEXT \"*def\")))"},
	})
}

func TestDefinition(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{";", "(BLOCK (PARA (TEXT \";\")))"},
		{"; ", "(BLOCK (PARA (TEXT \";\")))"},
		{"; abc", "(BLOCK (DESCRIPTION ((TEXT \"abc\"))))"},
		{"; abc\ndef", "(BLOCK (DESCRIPTION ((TEXT \"abc\"))) (PARA (TEXT \"def\")))"},
		{"; abc\n def", "(BLOCK (DESCRIPTION ((TEXT \"abc\"))) (PARA (TEXT \"def\")))"},
		{"; abc\n  def", "(BLOCK (DESCRIPTION ((TEXT \"abc\") (SOFT) (TEXT \"def\"))))"},
		{"; abc\n  def\n  ghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\") (SOFT) (TEXT \"def\") (SOFT) (TEXT \"ghi\"))))"},
		{":", "(BLOCK (PARA (TEXT \":\")))"},
		{": ", "(BLOCK (PARA (TEXT \":\")))"},
		{": abc", "(BLOCK (PARA (TEXT \":\") (SPACE) (TEXT \"abc\")))"},
		{"; abc\n: def", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))))))"},
		{"; abc\n: def\nghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))))) (PARA (TEXT \"ghi\")))"},
		{"; abc\n: def\n ghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))))) (PARA (TEXT \"ghi\")))"},
		{"; abc\n: def\n  ghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\") (SOFT) (TEXT \"ghi\"))))))"},
		{"; abc\n: def\n\n  ghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))) (PARA (TEXT \"ghi\")))))"},
		{"; abc\n:", "(BLOCK (DESCRIPTION ((TEXT \"abc\"))) (PARA (TEXT \":\")))"},
		{"; abc\n: def\n: ghi", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))) (BLOCK (PARA (TEXT \"ghi\"))))))"},
		{"; abc\n: def\n; ghi\n: jkl", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\")))) ((TEXT \"ghi\")) (BLOCK (BLOCK (PARA (TEXT \"jkl\"))))))"},

		// Empty description
		{"; abc\n: ", "(BLOCK (DESCRIPTION ((TEXT \"abc\"))) (PARA (TEXT \":\")))"},
		// Empty continuation of definition
		{"; abc\n: def\n  ", "(BLOCK (DESCRIPTION ((TEXT \"abc\")) (BLOCK (BLOCK (PARA (TEXT \"def\"))))))"},
	})
}

func TestTable(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"|", "()"},
		{"||", "(BLOCK (TABLE () ((CELL))))"},
		{"| |", "(BLOCK (TABLE () ((CELL))))"},
		{"|a", "(BLOCK (TABLE () ((CELL (TEXT \"a\")))))"},
		{"|a|", "(BLOCK (TABLE () ((CELL (TEXT \"a\")))))"},
		{"|a| ", "(BLOCK (TABLE () ((CELL (TEXT \"a\")) (CELL))))"},
		{"|a|b", "(BLOCK (TABLE () ((CELL (TEXT \"a\")) (CELL (TEXT \"b\")))))"},
		{"|a\n|b", "(BLOCK (TABLE () ((CELL (TEXT \"a\"))) ((CELL (TEXT \"b\")))))"},
		{"|a|b\n|c|d", "(BLOCK (TABLE () ((CELL (TEXT \"a\")) (CELL (TEXT \"b\"))) ((CELL (TEXT \"c\")) (CELL (TEXT \"d\")))))"},
		{"|%", "()"},
		{"|=a", "(BLOCK (TABLE ((CELL (TEXT \"a\")))))"},
		{"|=a\n|b", "(BLOCK (TABLE ((CELL (TEXT \"a\"))) ((CELL (TEXT \"b\")))))"},
		{"|a|b\n|%---\n|c|d", "(BLOCK (TABLE () ((CELL (TEXT \"a\")) (CELL (TEXT \"b\"))) ((CELL (TEXT \"c\")) (CELL (TEXT \"d\")))))"},
		{"|a|b\n|c", "(BLOCK (TABLE () ((CELL (TEXT \"a\")) (CELL (TEXT \"b\"))) ((CELL (TEXT \"c\")) (CELL))))"},
		{"|=<a>\n|b|c", "(BLOCK (TABLE ((CELL-LEFT (TEXT \"a\")) (CELL)) ((CELL-RIGHT (TEXT \"b\")) (CELL (TEXT \"c\")))))"},
		{"|=<a|=b>\n||", "(BLOCK (TABLE ((CELL-LEFT (TEXT \"a\")) (CELL-RIGHT (TEXT \"b\"))) ((CELL) (CELL-RIGHT))))"},
	})
}

func TestTransclude(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"{{{a}}}", "(BLOCK (TRANSCLUDE () (EXTERNAL \"a\")))"},
		{"{{{a}}}b", "(BLOCK (TRANSCLUDE ((\"\" . \"b\")) (EXTERNAL \"a\")))"},
		{"{{{a}}}}", "(BLOCK (TRANSCLUDE () (EXTERNAL \"a\")))"},
		{"{{{a\\}}}}", "(BLOCK (TRANSCLUDE () (EXTERNAL \"a\\\\}\")))"},
		{"{{{a\\}}}}b", "(BLOCK (TRANSCLUDE ((\"\" . \"b\")) (EXTERNAL \"a\\\\}\")))"},
		{"{{{a}}", "(BLOCK (PARA (TEXT \"{\") (EMBED () \"a\")))"},
		{"{{{a}}}{go=b}", "(BLOCK (TRANSCLUDE ((\"go\" . \"b\")) (EXTERNAL \"a\")))"},
	})
}

func TestBlockAttr(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{":::go\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
		{":::go=\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
		{":::{}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
		{":::{ }\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
		{":::{.go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\")) ((PARA (TEXT \"a\")))))"},
		{":::{=go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
		{":::{go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{go=py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"py\")) ((PARA (TEXT \"a\")))))"},
		{":::{.go=py}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
		{":::{go=}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{.go=}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
		{":::{go py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{go\npy}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{.go py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\") (\"py\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{go .py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"py\") (\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{.go py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\") (\"py\" . \"3\")) ((PARA (TEXT \"a\")))))"},
		{":::  {  go  }  \na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::  {  .go  }  \na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go\")) ((PARA (TEXT \"a\")))))"},
	})
	checkTcs(t, true, replace("\"", nil, TestCases{
		{":::{py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"3\")) ((PARA (TEXT \"a\")))))"},
		{":::{py=$2 3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2 3\")) ((PARA (TEXT \"a\")))))"},
		{":::{py=$2\\$3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\\"3\")) ((PARA (TEXT \"a\")))))"},
		{":::{py=2$3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\\"3\")) ((PARA (TEXT \"a\")))))"},
		{":::{py=$2\n3$}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2\\n3\")) ((PARA (TEXT \"a\")))))"},
		{":::{py=$2 3}\na\n:::", "(BLOCK (REGION-BLOCK () ((PARA (TEXT \"a\")))))"},
		{":::{py=2 py=3}\na\n:::", "(BLOCK (REGION-BLOCK ((\"py\" . \"2 3\")) ((PARA (TEXT \"a\")))))"},
		{":::{.go .py}\na\n:::", "(BLOCK (REGION-BLOCK ((\"class\" . \"go py\")) ((PARA (TEXT \"a\")))))"},
		{":::{go go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"go\" . \"\")) ((PARA (TEXT \"a\")))))"},
		{":::{=py =go}\na\n:::", "(BLOCK (REGION-BLOCK ((\"\" . \"go\")) ((PARA (TEXT \"a\")))))"},
	}))
}

func TestInlineAttr(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"::a::{}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\"))))"},
		{"::a::{ }", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\"))))"},
		{"::a::{.go}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go\")) (TEXT \"a\"))))"},
		{"::a::{=go}", "(BLOCK (PARA (FORMAT-SPAN ((\"\" . \"go\")) (TEXT \"a\"))))"},
		{"::a::{go}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{go=py}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"py\")) (TEXT \"a\"))))"},
		{"::a::{.go=py}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{.go=py}\")))"},
		{"::a::{go=}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{.go=}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{.go=}\")))"},
		{"::a::{go py}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\") (\"py\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{go\npy}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\") (\"py\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{.go py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go\") (\"py\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{go .py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{  \n go \n .py\n  \n}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{  \n go \n .py\n\n}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"py\") (\"go\" . \"\")) (TEXT \"a\"))))"},
		{"::a::{\ngo\n}", "(BLOCK (PARA (FORMAT-SPAN ((\"go\" . \"\")) (TEXT \"a\"))))"},
	})
	checkTcs(t, true, TestCases{
		{"::a::{py=3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"3\")) (TEXT \"a\"))))"},
		{"::a::{py=\"2 3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2 3\")) (TEXT \"a\"))))"},
		{"::a::{py=\"2\\\"3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\\"3\")) (TEXT \"a\"))))"},
		{"::a::{py=2\"3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\\"3\")) (TEXT \"a\"))))"},
		{"::a::{py=\"2\n3\"}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2\\n3\")) (TEXT \"a\"))))"},
		{"::a::{py=\"2 3}", "(BLOCK (PARA (FORMAT-SPAN () (TEXT \"a\")) (TEXT \"{py=\\\"2\") (SPACE) (TEXT \"3}\")))"},
	})
	checkTcs(t, true, TestCases{
		{"::a::{py=2 py=3}", "(BLOCK (PARA (FORMAT-SPAN ((\"py\" . \"2 3\")) (TEXT \"a\"))))"},
		{"::a::{.go .py}", "(BLOCK (PARA (FORMAT-SPAN ((\"class\" . \"go py\")) (TEXT \"a\"))))"},
	})
}

func TestTemp(t *testing.T) {
	t.Parallel()
	checkTcs(t, true, TestCases{
		{"", "()"},
	})
}

Changes to text/text.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
//-----------------------------------------------------------------------------
// 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 text provides types, constants and function to work with text output.
package text

import (
	"strings"

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

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

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

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

func (enc *Encoder) Encode(lst *sx.Pair) 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 {
		return NewEncoder(sf).Encode(lst)
	}
	return ""
}

func (enc *Encoder) executeList(lst *sx.Pair) {
	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 {
		return
	}
	sym := cmd.Car()
	if sx.IsNil(sym) {
		return
	}
	if sym.IsEqual(enc.symText) {
		args := cmd.Tail()
		if args == nil {
			return
		}
		if val, isString := sx.GetString(args.Car()); isString {
			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())
	}
}








>
>
>








|
|




<

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

<
|
<
<
<
<
<













<
|
<
<
















|





|

|

|

|



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

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

import (
	"strings"

	"t73f.de/r/sx"
	"t73f.de/r/zsc/sz"
)

// Encoder is the structure to hold relevant data to execute the encoding.
type Encoder struct {

	sb strings.Builder
}







func NewEncoder() *Encoder {



	enc := &Encoder{

		sb: strings.Builder{},





	}
	return enc
}

func (enc *Encoder) Encode(lst *sx.Pair) 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 {

	return NewEncoder().Encode(lst)


}

func (enc *Encoder) executeList(lst *sx.Pair) {
	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 {
		return
	}
	sym := cmd.Car()
	if sx.IsNil(sym) {
		return
	}
	if sym.IsEqual(sz.SymText) {
		args := cmd.Tail()
		if args == nil {
			return
		}
		if val, isString := sx.GetString(args.Car()); isString {
			enc.sb.WriteString(val.GetValue())
		}
	} else if sym.IsEqual(sz.SymSpace) || sym.IsEqual(sz.SymSoft) {
		enc.sb.WriteByte(' ')
	} else if sym.IsEqual(sz.SymHard) {
		enc.sb.WriteByte('\n')
	} else if !sym.IsEqual(sx.SymbolQuote) {
		enc.executeList(cmd.Tail())
	}
}

Changes to text/text_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) 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 text_test

import (
	"strings"
	"testing"

	"zettelstore.de/client.fossil/text"
	"zettelstore.de/sx.fossil"
	"zettelstore.de/sx.fossil/sxreader"
)

func TestSzText(t *testing.T) {
	testcases := []struct {
		src string
		exp string
	}{








>
>
>








|
|
|







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

package text_test

import (
	"strings"
	"testing"

	"t73f.de/r/sx"
	"t73f.de/r/sx/sxreader"
	"t73f.de/r/zsc/text"
)

func TestSzText(t *testing.T) {
	testcases := []struct {
		src string
		exp string
	}{

Changes to www/changes.wiki.

1
2


























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



























<a name="0_13"></a>
<h2>Changes for Version 0.13.0 (pending)</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>
  *  Rename "sexpr" to "sz".



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

|







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
<title>Change Log</title>

<a name="0_18"></a>
<h2>Changes for Version 0.18.0 (pending)</h2>

<a name="0_17"></a>
<h2>Changes for Version 0.17.0 (2024-03-04)</h2>
  *  Generic GET method for HTTP client.
  *  Adapt to sz changes; see manual for current syntax.
  *  Got some packages from Zettelstore, for general use.

<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>
  *  Rename "sexpr" to "sz".

Changes to www/index.wiki.

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.13.0 (2023-08-07)</h3>
  *  [./changes.wiki#0_13|Change summary]
  *  [/timeline?p=v0.13.0&bt=v0.20.0&y=ci|Check-ins for version 0.13.0],
     [/vdiff?to=v0.13.0&from=v0.20.0|content diff]
  *  [/timeline?df=v0.13.0&y=ci|Check-ins derived from the 0.13.0 release],
     [/vdiff?from=v0.13.0&to=trunk|content diff]
  *  [/timeline?t=release|Timeline of all past releases]




























|
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<title>Home</title>

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

<h3>Latest Release: 0.17.0 (2024-03-04)</h3>
  *  [./changes.wiki#0_17|Change summary]
  *  [/timeline?p=v0.17.0&bt=v0.16.0&y=ci|Check-ins for version 0.17.0],
     [/vdiff?to=v0.17.0&from=v0.16.0|content diff]
  *  [/timeline?df=v0.17.0&y=ci|Check-ins derived from the 0.17.0 release],
     [/vdiff?from=v0.17.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 t73f.de/r/zsc@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.