Zettelstore Client

Check-in Differences
Login

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

Difference From v0.6.0 To trunk

2024-03-18
10:02
Update to latest Sx; add Zid constant for new computed zettel "memory" ... (Leaf check-in: bbbd4f8805 user: stern tags: trunk)
2024-03-14
11:56
Update Sx dependency ... (check-in: 6c93edf1f2 user: stern tags: trunk)
2022-08-12
11:25
Remove constants to work with URL query parameter based search; update API to use search expressions ... (check-in: 0e4cf52fe7 user: stern tags: trunk)
2022-08-11
16:58
Version 0.6.0 ... (check-in: 7ef11c70e3 user: stern tags: trunk, release, v0.6.0)
08:53
Typo ... (check-in: 6f9ea3ec0b user: stern tags: trunk)

Deleted .deepsource.toml.

1
2
3
4
5
6
7
8








-
-
-
-
-
-
-
-
version = 1

[[analyzers]]
name = "go"
enabled = true

  [analyzers.meta]
  import_root = "github.com/zettelstore/client"

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 LICENSE.txt.

1

2
3
4
5
6
7
8

1
2
3
4
5
6
7
8
-
+







Copyright (c) 2021-2022 Detlef Stern
Copyright (c) 2021-present Detlef Stern

                          Licensed under the EUPL

Zettelstore client is licensed under the European Union Public License,
version 1.2 or later (EUPL v. 1.2). The license is available in the
official languages of the EU. The English version is included here. Please
see https://joinup.ec.europa.eu/community/eupl/og_page/eupl for official

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
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-2022 Detlef Stern
// 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 := 0; i < 14; i++ {
	for i := range 14 {
		ch := zid[i]
		if ch < '0' || '9' < ch {
			return false
		}
	}
	return true
}
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
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





















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


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


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


-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
const (
	ZettelCanNone   ZettelRights = 1 << iota
	ZettelCanCreate              // Current user is allowed to create a new zettel
	ZettelCanRead                // Requesting user is allowed to read the zettel
	ZettelCanWrite               // Requesting user is allowed to update the zettel
	ZettelCanRename              // Requesting user is allowed to provide the zettel with a new identifier
	ZettelCanDelete              // Requesting user is allowed to delete the zettel
)

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

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


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

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

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

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

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

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

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

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

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

Changes to api/const.go.

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

-
+






+
+
+
















+







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


-
-
+
+
-






+
+
+
+
+
+
+
+
+



+
+




















-










-
+



-

+


+

+





+

+

+
+
+










-
-
-
-
-
-
-
-



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







//-----------------------------------------------------------------------------
// Copyright (c) 2021-2022 Detlef Stern
// 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")
	ZidContextTemplate = ZettelID("00000000010406")
	ZidRolesTemplate   = ZettelID("00000000010500")
	ZidTagsTemplate    = ZettelID("00000000010600")
	ZidErrorTemplate   = ZettelID("00000000010700")
	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")
	ZidBaseCSS = ZettelID("00000000020001")
	ZidUserCSS = ZettelID("00000000025001")
	ZidRoleCSSMap = ZettelID("00000000029000") // Maps roles to CSS zettel, which should be in the range 29001..29999.

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

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

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

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

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

	ZidDefaultHome = ZettelID("00010000000000")
)

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

// 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"
	KeyRole         = "role"
	KeyTags         = "tags"
	KeySyntax       = "syntax"
	KeyAllTags      = "all-" + KeyTags
	KeyAuthor       = "author"
	KeyBack         = "back"
	KeyBackward     = "backward"
	KeyBoxNumber    = "box-number"
	KeyContentTags  = "content-" + KeyTags
	KeyCopyright    = "copyright"
	KeyCreated      = "created"
	KeyCredential   = "credential"
	KeyDead         = "dead"
	KeyExpire       = "expire"
	KeyFolge        = "folge"
	KeyFolgeRole    = "folge-role"
	KeyForward      = "forward"
	KeyLang         = "lang"
	KeyLicense      = "license"
	KeyModified     = "modified"
	KeyPrecursor    = "precursor"
	KeyPredecessor  = "predecessor"
	KeyPublished    = "published"
	KeyQuery        = "query"
	KeyReadOnly     = "read-only"
	KeySubordinates = "subordinates"
	KeySuccessors   = "successors"
	KeySuperior     = "superior"
	KeySummary      = "summary"
	KeyURL          = "url"
	KeyUselessFiles = "useless-files"
	KeyUserID       = "user-id"
	KeyUserRole     = "user-role"
	KeyVisibility   = "visibility"
)

// Predefined Metadata values
const (
	ValueRoleConfiguration = "configuration"
	ValueRoleZettel        = "zettel"
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxNone        = "none"
	ValueSyntaxSVG         = "svg"
	ValueSyntaxText        = "text"
	ValueSyntaxZmk         = "zmk"
	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"
	ValueSyntaxNone        = "none"
	ValueSyntaxSVG         = "svg"
	ValueSyntaxSxn         = "sxn"
	ValueSyntaxText        = "text"
	ValueSyntaxZmk         = "zmk"
	ValueUserRoleCreator   = "creator"
	ValueUserRoleOwner     = "owner"
	ValueUserRoleReader    = "reader"
	ValueUserRoleWriter    = "writer"
	ValueVisibilityCreator = "creator"
	ValueVisibilityExpert  = "expert"
	ValueVisibilityLogin   = "login"
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
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







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





+
+
-
+

-

+
+
+




+
-
+
+

-

+
+
+







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

// Values for HTTP query parameter.
const (
	QueryKeyCommand  = "_cmd"
	QueryKeyCommand   = "cmd"
	QueryKeyDepth    = "_depth"
	QueryKeyDir      = "_dir"
	QueryKeyEncoding = "_enc"
	QueryKeyKey      = "_key"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyLimit    = "_limit"
	QueryKeyMin      = "_min"
	QueryKeyNegate   = "_negate"
	QueryKeyOffset   = "_offset"
	QueryKeyOrder    = "_order"
	QueryKeyPart     = "_part"
	QueryKeyPhrase   = "_phrase"
	QueryKeySearch   = "_s"
	QueryKeySort     = "_sort"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"
	QueryKeyRole      = "role"
)

	QueryKeySeed      = "_seed"
// Supported dir values.
const (
	DirBackward = "backward"
	DirForward  = "forward"
	QueryKeyTag       = "tag"
)

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

	EncodingPlain = "plain"
	EncodingData  = "data"
)

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

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

func init() {
	for k, v := range mapEncodingEnum {
		mapEnumEncoding[v] = k
	}
203
204
205
206
207
208
209

210


211
212
213




214
215
216
217
218
219
220
228
229
230
231
232
233
234
235

236
237
238


239
240
241
242
243
244
245
246
247
248
249







+
-
+
+

-
-
+
+
+
+







// EncodingEnum lists all valid encoder keys.
type EncodingEnum uint8

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

	EncoderPlain
	EncoderData
)

// String representation of an encoder key.
func (e EncodingEnum) String() string {
	if f, ok := mapEnumEncoding[e]; ok {
		return f
	}
232
233
234
235
236
237
238























































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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
type Command string

// Supported command values
const (
	CommandAuthenticated = Command("authenticated")
	CommandRefresh       = Command("refresh")
)

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

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

-
+






+
+
+







//-----------------------------------------------------------------------------
// Copyright (c) 2020-2022 Detlef Stern
// 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 (
	"net/url"
	"strings"
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
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







-
-
+
+





-
-
-
-
+
+
+
+
+
+







	}
	if p != "" {
		ub.path = append(ub.path, p)
	}
	return ub
}

// AppendQuery adds a new query parameter
func (ub *URLBuilder) AppendQuery(key, value string) *URLBuilder {
// 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
}

// AppendSearch adds a new search
func (ub *URLBuilder) AppendSearch(value string) *URLBuilder {
	ub.rawLocal = ""
	ub.query = append(ub.query, urlQuery{QueryKeySearch, value})
// 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

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
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-2022 Detlef Stern
// 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"

	"zettelstore.de/c/maps"
	"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 }

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
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-2022 Detlef Stern
// 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"

	"zettelstore.de/c/attrs"
	"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")

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
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-2022 Detlef Stern
// 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"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"codeberg.org/t73fde/sxpf"
	"zettelstore.de/c/api"
	"zettelstore.de/c/sexpr"
	"zettelstore.de/c/zjson"
	"zettelstore.de/client.fossil/api"
	"zettelstore.de/client.fossil/sexp"
	"zettelstore.de/client.fossil/sz"
	"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
66
67
68
69
70
71
72













73
74
75
76
77
78
79
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







+
+
+
+
+
+
+
+
+
+
+
+
+







				}).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
125
126
127
128
129
130
131
132

133
134
135
136
137
138
139
140







+
-
+







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







-
-
-
+
+
+
+
+
+



+
+
+
+
-
-
-
+
+
+
















-
+









-
+








-
+


















-
-
+
+

-
+


-
+





+
+



-
-
-



-
-
+
-
-
-
-
-
-
-
-




-
-
-
+
+
+





+
-
+
+
+
+
+


-










-
-
-
+
+
+




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



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


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
+

-
+






+
-
+
+
+
+
+


-
+


-
-
-
+
+
+
+
+

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




-
+




-
+


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






-
+
+
+
+


-
+
-


-
-
-
+
+
+


-
-
-
+
+
+


-
-
-
+
+
+

-
+
+
+
+









-
+


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


-
+


+
+

-
+

-
-
-

-
+

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

-
+

-
-
-
+
+
-
-
-
-
-

-
+

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




-
+











-
-
+
+

-
+


-
+













-
+

-
+














-
+













-
+











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

-
+

-
-
-
-


-
+

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

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

-
+


-
-
+
+
+
+
+
+

-
+
-
-
-
-
-
-
+

	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var tinfo api.AuthJSON
	err = dec.Decode(&tinfo)
	rd := sxreader.MakeReader(resp.Body)
	obj, err := rd.Read()
	if err != nil {
		return err
	}
	vals, err := sexp.ParseList(obj, "ssi")
	if err != nil {
		return err
	}
	token := string(vals[1].(sx.String))
	if len(token) < 4 {
		return fmt.Errorf("no valid token found: %q", token)
	}
	c.token = tinfo.Token
	c.tokenType = tinfo.Type
	c.expires = time.Now().Add(time.Duration(tinfo.Expires*10/9) * time.Second)
	c.token = token
	c.tokenType = string(vals[0].(sx.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()))
	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)
	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')
	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)
	}
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return api.InvalidZID, err
	}
	if zid := api.ZettelID(b); zid.IsValid() {
		return zid, nil
	}
	return api.InvalidZID, err
}

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

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

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

// ListZettel returns a list of all Zettel.
func (c *Client) ListZettel(ctx context.Context, query url.Values) ([][]byte, error) {
	ub := c.newQueryURLBuilder('z', query)
// 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)
	if resp.StatusCode != http.StatusOK {
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	data, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	lines := bytes.Split(data, bsLF)
	if len(lines[len(lines)-1]) == 0 {
		lines = lines[:len(lines)-1]
	}
	return lines, nil
}

// ListZettelJSON returns a list of zettel.
func (c *Client) ListZettelJSON(ctx context.Context, query url.Values) (string, string, []api.ZidMetaJSON, error) {
	ub := c.newQueryURLBuilder('j', query)
// QueryZettelData returns a list of zettel metadata.
func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) {
	ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData).AppendQuery(query)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return "", "", nil, err
	}
	defer resp.Body.Close()
	rdr := sxreader.MakeReader(resp.Body)
	obj, err := rdr.Read()
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return "", "", nil, nil
	default:
		return "", "", nil, statusToError(resp)
	}
	if err != nil {
		return "", "", nil, err
	}
	vals, err := sexp.ParseList(obj, "yppp")
	if err != nil {
		return "", "", nil, err
	}
	qVals, err := sexp.ParseList(vals[1], "ys")
	if err != nil {
		return "", "", nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return "", "", nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var zl api.ZettelListJSON
	hVals, err := sexp.ParseList(vals[2], "ys")
	err = dec.Decode(&zl)
	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 {
		return nil, errSym
	}
	var result []api.ZidMetaRights
	for node := metaPair.Cdr(); !sx.IsNil(node); {
		elem, isPair := sx.GetPair(node)
		if !isPair {
			return nil, fmt.Errorf("meta-list not a proper list: %v", metaPair.String())
		}
		node = elem.Cdr()
		vals, err := sexp.ParseList(elem.Car(), "yppp")
		if err != nil {
			return nil, err
		}

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

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

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

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

		result = append(result, api.ZidMetaRights{
			ID:     zid,
			Meta:   meta,
			Rights: rights,
		})
	}
	return result, nil
}
func makeZettelID(obj sx.Object) (api.ZettelID, error) {
	val, isInt64 := obj.(sx.Int64)
	if !isInt64 || val <= 0 {
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	sVal := strconv.FormatInt(int64(val), 10)
	if len(sVal) < 14 {
		sVal = "00000000000000"[0:14-len(sVal)] + sVal
	}
	zid := api.ZettelID(sVal)
	if !zid.IsValid() {
		return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
	}
	return zl.Query, zl.Human, zl.List, nil
	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
	}
	agg := make(api.Aggregate, len(lines))
	for _, line := range lines {
		if fields := bytes.Fields(line); len(fields) > 1 {
			key := string(fields[0])
			for _, field := range fields[1:] {
				if zid := api.ZettelID(string(field)); zid.IsValid() {
					agg[key] = append(agg[key], zid)
				}
			}
		}
	}
	return agg, nil
}

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

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

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

	switch resp.StatusCode {
	case http.StatusNotFound:
		return "", nil
	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)
	ub := c.NewURLBuilder('z').SetZid(zid)
	if part != "" && part != api.PartContent {
		ub.AppendQuery(api.QueryKeyPart, part)
		ub.AppendKVQuery(api.QueryKeyPart, part)
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	return io.ReadAll(resp.Body)
	return data, err
}

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

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

// 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, 'v', zid, enc)
	return c.getZettelString(ctx, zid, enc, false)
}

func (c *Client) getZettelString(ctx context.Context, key byte, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) {
	ub := c.newURLBuilder(key).SetZid(zid)
	ub.AppendQuery(api.QueryKeyEncoding, enc.String())
	ub.AppendQuery(api.QueryKeyPart, api.PartContent)
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)
	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 io.ReadAll(resp.Body)
}

// GetParsedZettelZJSON returns an parsed zettel as a JSON-decoded data structure.
func (c *Client) GetParsedSexpr(ctx context.Context, zid api.ZettelID, part string) (sxpf.Value, error) {
	return c.getSexpr(ctx, 'p', zid, part)
}

// GetEvaluatedZettelZJSON returns an evaluated zettel as a JSON-decoded data structure.
func (c *Client) GetEvaluatedSexpr(ctx context.Context, zid api.ZettelID, part string) (sxpf.Value, error) {
	return c.getSexpr(ctx, 'v', zid, part)
}

func (c *Client) getSexpr(ctx context.Context, key byte, zid api.ZettelID, part string) (sxpf.Value, error) {
	ub := c.newURLBuilder(key).SetZid(zid)
	ub.AppendQuery(api.QueryKeyEncoding, api.EncodingSexpr)
	if part != "" {
		ub.AppendQuery(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 {
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
	default:
		return nil, statusToError(resp)
	}

	return io.ReadAll(resp.Body)
	return sxpf.ParseValue(sexpr.Smk, bufio.NewReaderSize(resp.Body, 8))
}

// GetParsedZettelZJSON returns an parsed zettel as a JSON-decoded data structure.
func (c *Client) GetParsedZJSON(ctx context.Context, zid api.ZettelID, part string) (zjson.Value, error) {
	return c.getZJSON(ctx, 'p', zid, part)
// 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)
}

// GetEvaluatedZettelZJSON returns an evaluated zettel as a JSON-decoded data structure.
func (c *Client) GetEvaluatedZJSON(ctx context.Context, zid api.ZettelID, part string) (zjson.Value, error) {
	return c.getZJSON(ctx, 'v', zid, part)
// 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) getZJSON(ctx context.Context, key byte, zid api.ZettelID, part string) (zjson.Value, error) {
	ub := c.newURLBuilder(key).SetZid(zid)
	ub.AppendQuery(api.QueryKeyEncoding, api.EncodingZJSON)
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.AppendQuery(api.QueryKeyPart, 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 zjson.Decode(resp.Body)
	return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8)).Read()
}

// GetMeta returns the metadata of a zettel.
func (c *Client) GetMeta(ctx context.Context, zid api.ZettelID) (api.ZettelMeta, error) {
	ub := c.newURLBuilder('m').SetZid(zid)
// 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)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.MetaJSON
	ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
	}
	return out.Meta, nil
}

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

		return api.MetaRights{}, err
	}
	if errSym := sexp.CheckSymbol(vals[0], "list"); errSym != nil {
// ContextDirection specifies how the context should be calculated.
type ContextDirection uint8

		return api.MetaRights{}, err
// Allowed values for ContextDirection
const (
	_ ContextDirection = iota
	DirBoth
	DirBackward
	DirForward
)

	}
// GetZettelContext returns metadata of the given zettel and, more important,
// metadata of zettel that for the context of the first zettel.
func (c *Client) GetZettelContext(
	ctx context.Context, zid api.ZettelID, dir ContextDirection, depth, limit int) (
	*api.ZidMetaRelatedList, error,
) {
	ub := c.newURLBuilder('x').SetZid(zid)
	switch dir {
	case DirBackward:
		ub.AppendQuery(api.QueryKeyDir, api.DirBackward)
	case DirForward:
		ub.AppendQuery(api.QueryKeyDir, api.DirForward)
	}

	if depth > 0 {
		ub.AppendQuery(api.QueryKeyDepth, strconv.Itoa(depth))
	}
	if limit > 0 {
		ub.AppendQuery(api.QueryKeyLimit, strconv.Itoa(limit))
	}
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
	meta, err := sexp.ParseMeta(vals[1].(*sx.Pair))
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	var out api.ZidMetaRelatedList
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
		return api.MetaRights{}, err
	}
	return &out, nil
}


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

	dec := json.NewDecoder(resp.Body)
	var out api.ZidMetaRelatedList
	err = dec.Decode(&out)
	if err != nil {
		return nil, err
	return api.MetaRights{
	}
	return &out, nil
		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)
	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
}

// UpdateZettelJSON updates an existing zettel.
func (c *Client) UpdateZettelJSON(ctx context.Context, zid api.ZettelID, data *api.ZettelDataJSON) error {
// 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 := encodeZettelData(&buf, data); err != nil {
	if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
		return err
	}
	ub := c.newURLBuilder('j').SetZid(zid)
	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)
	ub := c.NewURLBuilder('z').SetZid(oldZid)
	h := http.Header{
		api.HeaderDestination: {c.newURLBuilder('z').SetZid(newZid).String()},
		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)
	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').AppendQuery(api.QueryKeyCommand, string(command))
	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
}

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

func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
// ListMapMeta returns a map of all metadata values with the given key to the
// list of zettel IDs containing this value.
func (c *Client) ListMapMeta(ctx context.Context, key string) (api.MapMeta, error) {
	err := c.updateToken(ctx)
	if err != nil {
		return nil, err
	}
	req, err := c.newRequest(ctx, http.MethodGet, c.newURLBuilder('m').AppendQuery(api.QueryKeyKey, key), nil)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.NewURLBuilder('x'), nil, nil)
	if err != nil {
		return nil, err
		return VersionInfo{}, err
	}
	resp, err := c.executeRequest(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, statusToError(resp)
		return VersionInfo{}, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	rdr := sxreader.MakeReader(resp.Body)
	var mlj api.MapListJSON
	err = dec.Decode(&mlj)
	if err != nil {
		return nil, err
	}
	return mlj.Map, nil
	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:  string(vals[3].(sx.String)),
				Hash:  string(vals[4].(sx.String)),
			}, nil
		}
	}
	return VersionInfo{}, err
}

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

// GetVersionJSON returns version information..
func (c *Client) GetVersionJSON(ctx context.Context) (api.VersionJSON, error) {
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.newURLBuilder('x'), nil, nil)
}

// 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 api.VersionJSON{}, err
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return api.VersionJSON{}, statusToError(resp)
	switch resp.StatusCode {
	case http.StatusOK:
	case http.StatusNoContent:
		return nil, nil
	default:
		return nil, statusToError(resp)
	}
	dec := json.NewDecoder(resp.Body)
	data, err := io.ReadAll(resp.Body)
	var version api.VersionJSON
	err = dec.Decode(&version)
	if err != nil {
		return api.VersionJSON{}, err
	}
	return version, nil
	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
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
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

-
+






+
+
+







-




-
-
+
-
-
+




-
+



















-
+

-
+




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


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







//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
// 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"
	"log"
	"net/http"
	"net/url"
	"testing"

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

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

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

func TestGetZJSONZettel(t *testing.T) {
func TestGetSzZettel(t *testing.T) {
	c := getClient()
	data, err := c.GetEvaluatedZJSON(context.Background(), api.ZidDefaultHome, api.PartContent)
	value, err := c.GetEvaluatedSz(context.Background(), api.ZidDefaultHome, api.PartContent)
	if err != nil {
		t.Error(err)
		return
	}
	if data == nil {
		t.Error("No data")
	}
	var v vis
	zjson.WalkBlock(&v, data.(zjson.Array), -1)
	// t.Error("Argh")
}

type vis struct{}

func (*vis) BlockArray(a zjson.Array, pos int) zjson.CloseFunc {
	log.Println("SBLO", pos, a)
	return nil
}
func (*vis) InlineArray(a zjson.Array, pos int) zjson.CloseFunc {
	log.Println("SINL", pos, a)
	return nil
}
func (*vis) ItemArray(a zjson.Array, pos int) zjson.CloseFunc {
	log.Println("SITE", pos, a)
	return nil
}
func (*vis) BlockObject(t string, obj zjson.Object, pos int) (bool, zjson.CloseFunc) {
	log.Println("BOBJ", pos, t, obj)
	return true, nil
}
func (*vis) InlineObject(t string, obj zjson.Object, pos int) (bool, zjson.CloseFunc) {
	log.Println("IOBJ", pos, t, obj)
	return true, nil
}
func (*vis) Unexpected(val zjson.Value, pos int, exp string) { log.Println("Expect", pos, exp, val) }

func TestGetSexprZettel(t *testing.T) {
	c := getClient()
	value, err := c.GetEvaluatedSexpr(context.Background(), api.ZidDefaultHome, api.PartContent)
	if err != nil {
		t.Error(err)
		return
	}
	if value == nil {
	if value.IsNil() {
		t.Error("No data")
	}
	var env testEnv
	env.t = t
	res, err := sxpf.Eval(&env, value)
	if err != nil {
		t.Error(res, err)
	}
}
}

type testEnv struct{ t *testing.T }

func noneFn(sxpf.Environment, *sxpf.Pair, int) (sxpf.Value, error) { return sxpf.Nil(), nil }
func (*testEnv) LookupForm(*sxpf.Symbol) (sxpf.Form, error) {
	return sxpf.NewBuiltin("none", false, 0, -1, noneFn), nil
}
func (*testEnv) EvalSymbol(sym *sxpf.Symbol) (sxpf.Value, error) { return sym, nil }
func (*testEnv) EvalOther(val sxpf.Value) (sxpf.Value, error)    { return val, nil }
func (te *testEnv) EvalPair(p *sxpf.Pair) (sxpf.Value, error)    { return sxpf.EvalCallOrSeq(te, p) }

var baseURL string

func init() {
	flag.StringVar(&baseURL, "base-url", "http://localhost:23123/", "Base URL")
}

Changes to go.mod.

1

2
3

4
5


1
2

3
4

5
-
+

-
+

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

go 1.19
go 1.22

require codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0
require zettelstore.de/sx.fossil v0.0.0-20240316123622-97ad67098e8a

Changes to go.sum.

1
2




1
2
-
-
+
+
codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0 h1:viya/OgeF16+i8caBPJmcLQhGpZodPh+/nxtJzSSO1s=
codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0/go.mod h1:4fAHEF3VH+ofbZkF6NzqiItTNy2X11tVCnZX99jXouA=
zettelstore.de/sx.fossil v0.0.0-20240316123622-97ad67098e8a h1:4t/TbT5X3KTMQSa3TQdACxzgoo+V450MgMy/maqWoos=
zettelstore.de/sx.fossil v0.0.0-20240316123622-97ad67098e8a/go.mod h1:/iGHxFXoo6GSV04PUkwaLuFrrCa5LMorxD73iLMAruI=

Deleted html/html.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) 2022 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 html provides types, constants and function to work with HTML.
package html

import (
	"io"
	"strings"
)

const (
	htmlQuot     = "&quot;" // longer than "&#34;", but often requested in standards
	htmlAmp      = "&amp;"
	htmlLt       = "&lt;"
	htmlGt       = "&gt;"
	htmlNull     = "\uFFFD"
	htmlLitSpace = "\u00a0"
	htmlVisSpace = "\u2423"
)

var (
	htmlEscapes = []string{`&`, htmlAmp,
		`<`, htmlLt,
		`>`, htmlGt,
		`"`, htmlQuot,
		"\000", htmlNull,
	}
	htmlEscaper = strings.NewReplacer(htmlEscapes...)

	htmlVisEscapes = append(append([]string{}, htmlEscapes...),
		" ", htmlVisSpace,
		htmlLitSpace, htmlVisSpace,
	)
	htmlVisEscaper = strings.NewReplacer(htmlVisEscapes...)
)

// Escape writes to w the escaped HTML equivalent of the given string.
func Escape(w io.Writer, s string) (int, error) { return htmlEscaper.WriteString(w, s) }

// EscapeVisible writes to w the escaped HTML equivalent of the given string.
// Each space is written as U-2423.
func EscapeVisible(w io.Writer, s string) (int, error) { return htmlVisEscaper.WriteString(w, s) }

var (
	escQuot = []byte(htmlQuot) // longer than "&#34;", but often requested in standards
	escAmp  = []byte(htmlAmp)
	escNull = []byte(htmlNull)
)

// AttributeEscape writes to w the escaped HTML equivalent of the given string to be used
// in attributes.
func AttributeEscape(w io.Writer, s string) (int, error) {
	length := 0
	last := 0
	var html []byte
	lenS := len(s)
	for i := 0; i < lenS; i++ {
		switch s[i] {
		case '\000':
			html = escNull
		case '"':
			html = escQuot
		case '&':
			html = escAmp
		default:
			continue
		}
		l, err := io.WriteString(w, s[last:i])
		length += l
		if err != nil {
			return length, err
		}
		l, err = w.Write(html)
		length += l
		if err != nil {
			return length, err
		}
		last = i + 1
	}
	l, err := io.WriteString(w, s[last:])
	return length + l, err
}

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

// IsSafe returns true if the given string does not contain unsafe HTML elements.
func IsSafe(s string) bool {
	lower := strings.ToLower(s)
	for _, snippet := range unsafeSnippets {
		if strings.Contains(lower, snippet) {
			return false
		}
	}
	return true
}

Deleted html/html_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

























































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 html_test

import (
	"bytes"
	"testing"

	"zettelstore.de/c/html"
)

func TestEscape(t *testing.T) {
	testcases := []struct {
		in, exp string
	}{
		{"", ""},
		{"<", "&lt;"},
	}
	for _, tc := range testcases {
		var buf bytes.Buffer
		_, err := html.Escape(&buf, tc.in)
		if err != nil {
			t.Errorf("Escape(%q) got error: %v", tc.in, err)
		}
		if got := buf.String(); tc.exp != got {
			t.Errorf("Escape(%q) == %q, but got %q", tc.in, tc.exp, got)
		}
	}
}

func TestEscapeVisible(t *testing.T) {
	testcases := []struct {
		in, exp string
	}{
		{"", ""},
		{"<", "&lt;"},
		{" a  b ", "␣a␣␣b␣"},
	}
	for _, tc := range testcases {
		var buf bytes.Buffer
		_, err := html.EscapeVisible(&buf, tc.in)
		if err != nil {
			t.Errorf("Escape(%q) got error: %v", tc.in, err)
		}
		if got := buf.String(); tc.exp != got {
			t.Errorf("Escape(%q) == %q, but got %q", tc.in, tc.exp, got)
		}
	}
}

Deleted html/sencoder.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



































































































































































































































































































































































































































































































































































































































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 html

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"net/url"
	"strconv"

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

// EncEnvironment represent the encoding environment.
// It is itself a sxpf.Environment.
//
// Builtins is public, so that HTML encoders based on this one can modify some
// functionality. Builtins should not be updated, but can be used as a parent
// map when creating a new one.
type EncEnvironment struct {
	err            error
	Builtins       *sxpf.SymbolMap
	w              io.Writer
	headingOffset  int
	unique         string
	footnotes      []sfootnodeInfo
	writeFootnotes bool // true iff output should include footnotes and marks
	noLinks        bool // true iff output must not include links
	visibleSpace   bool // true iff space should be "visible" by using EscapeVisible
}
type sfootnodeInfo struct {
	note  *sxpf.Pair
	attrs attrs.Attributes
}

func NewEncEnvironment(w io.Writer, headingOffset int) *EncEnvironment {
	return &EncEnvironment{
		Builtins:       buildBuiltins(),
		w:              w,
		headingOffset:  headingOffset,
		footnotes:      nil,
		writeFootnotes: true,
	}
}

func buildBuiltins() *sxpf.SymbolMap {
	builtins := sxpf.NewSymbolMap(sexpr.Smk, nil)
	for _, b := range defaultEncodingFunctions {
		name := b.sym.GetValue()
		primFunc := b.fn
		builtins.Set(b.sym, sxpf.NewBuiltin(
			name,
			true, b.minArgs, -1,
			func(env sxpf.Environment, args *sxpf.Pair, _ int) (sxpf.Value, error) {
				primFunc(env.(*EncEnvironment), args)
				return nil, nil
			},
		))
	}
	return builtins
}

// SetError marks the environment with an error, if there is not already a marked error.
// The effect is that all future output is not written.
func (env *EncEnvironment) SetError(err error) {
	if env.err == nil {
		env.err = err
	}
}

// GetError returns the first encountered error during encoding.
func (env *EncEnvironment) GetError() error { return env.err }

// ReplaceWriter flushes the previous writer and installs the new one.
func (env *EncEnvironment) ReplaceWriter(w io.Writer) { env.w = w }

// SetUnique sets a string that maked footnote, heading, and mark fragments unique.
func (env *EncEnvironment) SetUnique(s string) {
	if s == "" {
		env.unique = ""
	} else {
		env.unique = ":" + s
	}
}

// IgnoreLinks returns true, if HTML links must not be encoded. This happens if
// the encoded HTML is used in a link itself.
func (env *EncEnvironment) IgnoreLinks() bool { return env.noLinks }

// WriteString encodes a string literally.
func (env *EncEnvironment) Write(b []byte) (l int, err error) {
	if env.err == nil {
		l, env.err = env.w.Write(b)
	}
	return l, env.err
}

// WriteString encodes a string literally.
func (env *EncEnvironment) WriteString(s string) (l int, err error) {
	if env.err == nil {
		l, env.err = io.WriteString(env.w, s)
	}
	return l, env.err
}

// WriteStrings encodes many string literally.
func (env *EncEnvironment) WriteStrings(sl ...string) {
	if env.err == nil {
		for _, s := range sl {
			_, env.err = io.WriteString(env.w, s)
			if env.err != nil {
				return
			}
		}
	}
}

// WriteEscape encodes a string so that it cannot interfere with other HTML code.
func (env *EncEnvironment) WriteEscaped(s string) {
	if env.err == nil {
		_, env.err = Escape(env.w, s)
	}
}

func (env *EncEnvironment) WriteEscapedOrVisible(s string) {
	if env.err == nil {
		if env.visibleSpace {
			_, env.err = EscapeVisible(env.w, s)
		} else {
			_, env.err = Escape(env.w, s)
		}
	}
}

func (env *EncEnvironment) GetSymbol(p *sxpf.Pair) (res *sxpf.Symbol) {
	if env.err != nil {
		return nil
	}
	res, env.err = p.GetSymbol()
	return res
}

func (env *EncEnvironment) GetString(p *sxpf.Pair) (res string) {
	if env.err != nil {
		return ""
	}
	res, env.err = p.GetString()
	return res
}

func (env *EncEnvironment) GetInteger(p *sxpf.Pair) (res int64) {
	if env.err != nil {
		return 0
	}
	res, env.err = p.GetInteger()
	return res
}

func (env *EncEnvironment) GetPair(p *sxpf.Pair) (res *sxpf.Pair) {
	if env.err != nil {
		return nil
	}
	res, env.err = p.GetPair()
	return res
}

func (env *EncEnvironment) GetAttributes(p *sxpf.Pair) attrs.Attributes {
	if env.err != nil {
		return nil
	}
	return sexpr.GetAttributes(env.GetPair(p))
}

func (env *EncEnvironment) WriteAttributes(a attrs.Attributes) {
	if len(a) == 0 {
		return
	}
	for _, key := range a.Keys() {
		if key == "" || key == attrs.DefaultAttribute {
			continue
		}
		val, found := a.Get(key)
		if !found {
			continue
		}
		env.WriteString(" ")
		env.WriteOneAttribute(key, val)
	}
}

func (env *EncEnvironment) WriteOneAttribute(key, val string) {
	if env.err == nil {
		env.WriteString(key)
		if val != "" {
			env.WriteString(`="`)
			_, env.err = AttributeEscape(env.w, val)
			env.WriteString(`"`)
		}
	}
}

func (env *EncEnvironment) WriteStartTag(tag string, a attrs.Attributes) {
	env.WriteStrings("<", tag)
	env.WriteAttributes(a)
	env.WriteString(">")
}

func (env *EncEnvironment) WriteEndTag(tag string) {
	env.WriteStrings("</", tag, ">")
}

func (env *EncEnvironment) WriteImage(args *sxpf.Pair) {
	ref := env.GetPair(args.GetTail())
	env.WriteImageWithSource(args, env.GetString(ref.GetTail()))
}

func (env *EncEnvironment) WriteImageWithSource(args *sxpf.Pair, src string) {
	a := env.GetAttributes(args)
	a = a.Set("src", src)
	if title := args.GetTail().GetTail().GetTail(); !title.IsNil() {
		a = a.Set("title", text.EvaluateInlineString(title))
	}
	env.WriteStartTag("img", a)
}

func (env *EncEnvironment) LookupForm(sym *sxpf.Symbol) (sxpf.Form, error) {
	return env.Builtins.LookupForm(sym)
}

func (env *EncEnvironment) EvalOther(val sxpf.Value) (sxpf.Value, error) {
	if strVal, ok := val.(*sxpf.String); ok {
		env.WriteEscaped(strVal.GetValue())
		return nil, nil
	}
	return val, nil
}

func (env *EncEnvironment) EvalSymbol(val *sxpf.Symbol) (sxpf.Value, error) {
	env.WriteEscaped(val.GetValue())
	return nil, nil
}

func (env *EncEnvironment) EvalPair(p *sxpf.Pair) (sxpf.Value, error) {
	return sxpf.EvalCallOrSeq(env, p)
}

func EvaluateInline(baseEnv *EncEnvironment, value sxpf.Value, withFootnotes, noLinks bool) string {
	var buf bytes.Buffer
	env := EncEnvironment{w: &buf, noLinks: noLinks}
	if baseEnv != nil {
		env.Builtins = baseEnv.Builtins
		env.writeFootnotes = withFootnotes && baseEnv.writeFootnotes
		env.footnotes = baseEnv.footnotes
	} else {
		env.Builtins = buildBuiltins()
	}
	_, err := sxpf.Eval(&env, value)
	if err != nil {
		return err.Error()
	}
	if baseEnv != nil {
		baseEnv.footnotes = env.footnotes
	}
	return buf.String()
}

func (env *EncEnvironment) WriteEndnotes() {
	if len(env.footnotes) == 0 {
		return
	}
	env.WriteString("<ol class=\"zs-endnotes\">")
	for i := 0; i < len(env.footnotes); i++ {
		fni := env.footnotes[i]
		n := strconv.Itoa(i + 1)
		un := env.unique + n
		a := fni.attrs.Clone().AddClass("zs-endnote").Set("value", n)
		if _, found := a.Get("id"); !found {
			a = a.Set("id", "fn:"+un)
		}
		if _, found := a.Get("role"); !found {
			a = a.Set("role", "doc-endnote")
		}
		env.WriteStartTag("li", a)
		sxpf.EvalSequence(env, fni.note) // may add more footnotes
		env.WriteStrings(
			` <a class="zs-endnote-backref" href="#fnref:`,
			un,
			"\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></li>")
	}
	env.footnotes = nil
	env.WriteString("</ol>")
}

type encodingFunc func(env *EncEnvironment, args *sxpf.Pair)

var defaultEncodingFunctions = []struct {
	sym     *sxpf.Symbol
	minArgs int
	fn      encodingFunc
}{
	{sexpr.SymPara, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteString("<p>")
		sxpf.EvalSequence(env, args)
		env.WriteString("</p>")
	}},
	{sexpr.SymHeading, 5, func(env *EncEnvironment, args *sxpf.Pair) {
		nLevel := env.GetInteger(args)
		if nLevel <= 0 {
			return
		}
		level := strconv.FormatInt(nLevel+int64(env.headingOffset), 10)

		argAttr := args.GetTail()
		a := env.GetAttributes(argAttr)
		argFragment := argAttr.GetTail().GetTail()
		if fragment := env.GetString(argFragment); fragment != "" {
			a = a.Set("id", fragment)
		}

		env.WriteStrings("<h", level)
		env.WriteAttributes(a)
		env.WriteString(">")
		sxpf.EvalSequence(env, argFragment.GetTail())
		env.WriteStrings("</h", level, ">")
	}},
	{sexpr.SymThematic, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteString("<hr")
		if !sxpf.IsNil(args) {
			env.WriteAttributes(env.GetAttributes(args))
		}
		env.WriteString(">")
	}},
	{sexpr.SymListUnordered, 0, makeListFn("ul")},
	{sexpr.SymListOrdered, 0, makeListFn("ol")},
	{sexpr.SymListQuote, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteString("<blockquote>")
		if !args.IsNil() && args.GetFirst().IsNil() {
			sxpf.Eval(env, env.GetPair(args))
		} else {
			for elem := args; !elem.IsNil(); elem = elem.GetTail() {
				env.WriteString("<p>")
				sxpf.Eval(env, env.GetPair(elem))
				env.WriteString("</p>")
			}
		}
		env.WriteString("</blockquote>")
	}},
	{sexpr.SymDescription, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteString("<dl>")
		for elem := args; !elem.IsNil(); elem = elem.GetTail() {
			env.WriteString("<dt>")
			sxpf.Eval(env, elem.GetFirst())
			env.WriteString("</dt>")
			elem = elem.GetTail()
			if elem.IsNil() {
				break
			}
			ddlist, err := elem.GetPair()
			if err != nil {
				continue
			}
			for dditem := ddlist; !dditem.IsNil(); dditem = dditem.GetTail() {
				env.WriteString("<dd>")
				sxpf.Eval(env, dditem.GetFirst())
				env.WriteString("</dd>")
			}
		}
		env.WriteString("</dl>")
	}},
	{sexpr.SymTable, 1, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteString("<table>")
		if header := env.GetPair(args); !header.IsNil() {
			env.WriteString("<thead>")
			env.writeTableRow(header)
			env.WriteString("</thead>")
		}
		if argBody := args.GetTail(); !argBody.IsNil() {
			env.WriteString("<tbody>")
			for row := argBody; !row.IsNil(); row = row.GetTail() {
				env.writeTableRow(env.GetPair(row))
			}
			env.WriteString("</tbody>")
		}
		env.WriteString("</table>")
	}},
	{sexpr.SymCell, 0, makeCellFn("")},
	{sexpr.SymCellCenter, 0, makeCellFn("center")},
	{sexpr.SymCellLeft, 0, makeCellFn("left")},
	{sexpr.SymCellRight, 0, makeCellFn("right")},
	{sexpr.SymRegionBlock, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := env.GetAttributes(args)
		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		env.writeRegion(args, a, "div")
	}},
	{sexpr.SymRegionQuote, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		env.writeRegion(args, nil, "blockquote")
	}},
	{sexpr.SymRegionVerse, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		env.writeRegion(args, nil, "div")
	}},
	{sexpr.SymVerbatimComment, 1, func(env *EncEnvironment, args *sxpf.Pair) {
		if env.GetAttributes(args).HasDefault() {
			if s := env.GetString(args.GetTail()); s != "" {
				env.WriteString("<!--\n")
				env.WriteEscaped(s)
				env.WriteString("\n-->")
			}
		}
	}},
	{sexpr.SymVerbatimEval, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := env.GetAttributes(args).AddClass("zs-eval")
		env.writeVerbatim(args, a)
	}},
	{sexpr.SymVerbatimHTML, 2, execHTML},
	{sexpr.SymVerbatimMath, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := env.GetAttributes(args).AddClass("zs-math")
		env.writeVerbatim(args, a)
	}},
	{sexpr.SymVerbatimProg, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := setProgLang(env.GetAttributes(args))
		oldVisible := env.visibleSpace
		if a.HasDefault() {
			a = a.RemoveDefault()
			env.visibleSpace = true
		}
		env.writeVerbatim(args, a)
		env.visibleSpace = oldVisible
	}},
	{sexpr.SymVerbatimZettel, 0, DoNothingFn},
	{sexpr.SymBLOB, 3, func(env *EncEnvironment, args *sxpf.Pair) {
		argSyntax := args.GetTail()
		env.writeBLOB(env.GetString(args), env.GetString(argSyntax), env.GetString(argSyntax.GetTail()))
	}},
	{sexpr.SymTransclude, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		ref := env.GetPair(args)
		refKind := env.GetSymbol(ref)
		if refKind == nil {
			return
		}
		if refValue := env.GetString(ref.GetTail()); refValue != "" {
			if sexpr.SymRefStateExternal.Equal(refKind) {
				a := attrs.Attributes{}.Set("src", refValue).AddClass("external")
				env.WriteString("<p><img")
				env.WriteAttributes(a)
				env.WriteString("></p>")
				return
			}
			env.WriteStrings("<!-- transclude ", refKind.GetValue(), ": ")
			env.WriteEscaped(refValue)
			env.WriteString(" -->")
			return
		}
		if env.err == nil {
			_, env.err = fmt.Fprintf(env.w, "%v\n", args)
		}
		log.Println("TRAN", args)
	}},
	{sexpr.SymText, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		if !sxpf.IsNil(args) {
			env.WriteEscaped(env.GetString(args))
		}
	}},
	{sexpr.SymSpace, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		if sxpf.IsNil(args) {
			env.WriteString(" ")
			return
		}
		env.WriteEscaped(env.GetString(args))
	}},
	{sexpr.SymSoft, 0, func(env *EncEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }},
	{sexpr.SymHard, 0, func(env *EncEnvironment, _ *sxpf.Pair) { env.WriteString("<br>") }},
	{sexpr.SymTag, 0, func(env *EncEnvironment, args *sxpf.Pair) {
		if !sxpf.IsNil(args) {
			env.WriteEscaped(env.GetString(args))
		}
	}},
	{sexpr.SymLinkInvalid, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteAsSpan(env, args) }},
	{sexpr.SymLinkZettel, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }},
	{sexpr.SymLinkSelf, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }},
	{sexpr.SymLinkFound, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }},
	{sexpr.SymLinkBroken, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		if a, refValue, ok := PrepareLink(env, args); ok {
			WriteLink(env, args, a.AddClass("broken"), refValue, "")
		}
	}},
	{sexpr.SymLinkHosted, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }},
	{sexpr.SymLinkBased, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }},
	{sexpr.SymLinkSearch, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		if a, refValue, ok := PrepareLink(env, args); ok {
			query := "?" + api.QueryKeySearch + "=" + url.QueryEscape(refValue)
			WriteLink(env, args, a.Set("href", query), refValue, "")
		}
	}},
	{sexpr.SymLinkExternal, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		if a, refValue, ok := PrepareLink(env, args); ok {
			WriteLink(env, args, a.Set("href", refValue).AddClass("external"), refValue, "")
		}
	}},
	{sexpr.SymEmbed, 3, func(env *EncEnvironment, args *sxpf.Pair) {
		argRef := args.GetTail()
		if syntax := env.GetString(argRef.GetTail()); syntax == api.ValueSyntaxSVG {
			ref := env.GetPair(argRef)
			env.WriteStrings(
				`<figure><embed type="image/svg+xml" src="`, "/", env.GetString(ref.GetTail()), ".svg", "\" /></figure>")
		} else {
			env.WriteImage(args)
		}
	}},
	{sexpr.SymEmbedBLOB, 3, func(env *EncEnvironment, args *sxpf.Pair) {
		argSyntax := args.GetTail()
		a, syntax, data := env.GetAttributes(args), env.GetString(argSyntax), env.GetString(argSyntax.GetTail())
		title, _ := a.Get("title")
		env.writeBLOB(title, syntax, data)
	}},
	{sexpr.SymCite, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteStartTag("span", env.GetAttributes(args))
		argKey := args.GetTail()
		if key := env.GetString(argKey); key != "" {
			env.WriteEscaped(key)
			if text := argKey.GetTail(); !text.IsNil() {
				env.WriteString(", ")
				sxpf.EvalSequence(env, text)
			}
		}
		env.WriteString("</span>")
	}},
	{sexpr.SymMark, 3, func(env *EncEnvironment, args *sxpf.Pair) {
		if env.noLinks {
			sxpf.Eval(env, sxpf.NewPair(sexpr.SymFormatSpan, args))
			return
		}
		argFragment := args.GetTail().GetTail()
		if fragment := env.GetString(argFragment); fragment != "" {
			env.WriteString(`<a id="`)
			env.WriteString(env.unique)
			env.WriteString(fragment)
			env.WriteString(`">`)
			sxpf.EvalSequence(env, argFragment.GetTail())
			env.WriteString("</a>")
		} else {
			sxpf.EvalSequence(env, argFragment.GetTail())
		}
	}},
	{sexpr.SymFootnote, 1, func(env *EncEnvironment, args *sxpf.Pair) {
		if env.writeFootnotes {
			a := env.GetAttributes(args)
			env.footnotes = append(env.footnotes, sfootnodeInfo{args.GetTail(), a})
			n := strconv.Itoa(len(env.footnotes))
			un := env.unique + n
			env.WriteStrings(
				`<sup id="fnref:`, un, `"><a class="zs-noteref" href="#fn:`, un,
				`" role="doc-noteref">`, n, `</a></sup>`)
		}
	}},
	{sexpr.SymFormatDelete, 1, makeFormatFn("del")},
	{sexpr.SymFormatEmph, 1, makeFormatFn("em")},
	{sexpr.SymFormatInsert, 1, makeFormatFn("ins")},
	{sexpr.SymFormatQuote, 1, writeQuote},
	{sexpr.SymFormatSpan, 1, makeFormatFn("span")},
	{sexpr.SymFormatStrong, 1, makeFormatFn("strong")},
	{sexpr.SymFormatSub, 1, makeFormatFn("sub")},
	{sexpr.SymFormatSuper, 1, makeFormatFn("sup")},
	{sexpr.SymLiteralComment, 1, func(env *EncEnvironment, args *sxpf.Pair) {
		if env.GetAttributes(args).HasDefault() {
			if s := env.GetString(args.GetTail()); s != "" {
				env.WriteString("<!-- ")
				env.WriteEscaped(s)
				env.WriteString(" -->")
			}
		}
	}},
	{sexpr.SymLiteralHTML, 2, execHTML},
	{sexpr.SymLiteralInput, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		env.writeLiteral(args, nil, "kbd")
	}},
	{sexpr.SymLiteralMath, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := env.GetAttributes(args).AddClass("zs-math")
		env.writeLiteral(args, a, "code")
	}},
	{sexpr.SymLiteralOutput, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		env.writeLiteral(args, nil, "samp")
	}},
	{sexpr.SymLiteralProg, 2, func(env *EncEnvironment, args *sxpf.Pair) {
		a := setProgLang(env.GetAttributes(args))
		env.writeLiteral(args, a, "code")
	}},
	{sexpr.SymLiteralZettel, 0, DoNothingFn},
}

// DoNothingFn is a function that does nothing.
func DoNothingFn(*EncEnvironment, *sxpf.Pair) { /* Should really do nothing */ }

func makeListFn(tag string) encodingFunc {
	return func(env *EncEnvironment, args *sxpf.Pair) {
		env.WriteStartTag(tag, nil)
		for elem := args; !elem.IsNil(); elem = elem.GetTail() {
			env.WriteStartTag("li", nil)
			sxpf.Eval(env, elem.GetFirst())
			env.WriteEndTag("li")
		}
		env.WriteEndTag(tag)
	}
}

func (env *EncEnvironment) writeTableRow(cells *sxpf.Pair) {
	if !cells.IsNil() {
		env.WriteString("<tr>")
		for cell := cells; !cell.IsNil(); cell = cell.GetTail() {
			sxpf.Eval(env, cell.GetFirst())
		}
		env.WriteString("</tr>")
	}
}
func makeCellFn(align string) encodingFunc {
	return func(env *EncEnvironment, args *sxpf.Pair) {
		if align == "" {
			env.WriteString("<td>")
		} else {
			env.WriteStrings(`<td class="`, align, `">`)
		}
		sxpf.EvalSequence(env, args)
		env.WriteString("</td>")
	}
}

func (env *EncEnvironment) writeRegion(args *sxpf.Pair, a attrs.Attributes, tag string) {
	if a == nil {
		a = env.GetAttributes(args)
	}
	env.WriteStartTag(tag, a)
	sxpf.Eval(env, env.GetPair(args.GetTail()))
	if cite := env.GetPair(args.GetTail().GetTail()); !cite.IsNil() {
		env.WriteString("<cite>")
		sxpf.EvalSequence(env, cite)
		env.WriteString("</cite>")
	}
	env.WriteEndTag(tag)
}

func (env *EncEnvironment) writeVerbatim(args *sxpf.Pair, a attrs.Attributes) {
	env.WriteString("<pre>")
	env.WriteStartTag("code", a)
	env.WriteEscapedOrVisible(env.GetString(args.GetTail()))
	env.WriteString("</code></pre>")
}

func execHTML(env *EncEnvironment, args *sxpf.Pair) {
	if s := env.GetString(args.GetTail()); s != "" && IsSafe(s) {
		env.WriteString(s)
	}
}

func (env *EncEnvironment) writeBLOB(title, syntax, data string) {
	if data == "" {
		return
	}
	switch syntax {
	case "":
	case api.ValueSyntaxSVG:
		// TODO: add  title as description
		env.WriteStrings("<p>", data, "</p>")
	default:
		env.WriteStrings(`<p><img src="data:image/`, syntax, ";base64,", data)
		if title != "" {
			env.WriteString(`" `)
			env.WriteOneAttribute("title", title)
			env.WriteString(`></p>`)
		} else {
			env.WriteString(`"></p>`)
		}
	}
}

func PrepareLink(env *EncEnvironment, args *sxpf.Pair) (attrs.Attributes, string, bool) {
	if env.noLinks {
		WriteAsSpan(env, args)
		return nil, "", false
	}
	return env.GetAttributes(args), env.GetString(args.GetTail()), true
}

func WriteAsSpan(env *EncEnvironment, args *sxpf.Pair) {
	if args.Length() > 2 {
		sxpf.Eval(env, sxpf.NewPair(sexpr.SymFormatSpan, sxpf.NewPair(args.GetFirst(), args.GetTail().GetTail())))
	}
}

func WriteLink(env *EncEnvironment, args *sxpf.Pair, a attrs.Attributes, refValue, suffix string) {
	env.WriteString("<a")
	env.WriteAttributes(a)
	env.WriteString(">")

	if args.Length() > 2 {
		sxpf.EvalSequence(env, args.GetTail().GetTail())
	} else {
		env.WriteString(refValue)
	}
	env.WriteStrings("</a>", suffix)
}

func WriteHRefLink(env *EncEnvironment, args *sxpf.Pair) {
	if a, refValue, ok := PrepareLink(env, args); ok {
		WriteLink(env, args, a.Set("href", refValue), refValue, "")
	}
}

func makeFormatFn(tag string) encodingFunc {
	return func(env *EncEnvironment, args *sxpf.Pair) {
		a := env.GetAttributes(args)
		if val, found := a.Get(""); found {
			a = a.Remove("").AddClass(val)
		}
		env.WriteStartTag(tag, a)
		sxpf.EvalSequence(env, args.GetTail())
		env.WriteEndTag(tag)
	}
}

func writeQuote(env *EncEnvironment, args *sxpf.Pair) {
	const langAttr = "lang"
	a := env.GetAttributes(args)
	lang, hasLang := a.Get(langAttr)
	if hasLang {
		a = a.Remove(langAttr)
		env.WriteStartTag("span", attrs.Attributes{}.Set(langAttr, lang))
	}
	env.WriteStartTag("q", a)
	sxpf.EvalSequence(env, args.GetTail())
	env.WriteEndTag("q")
	if hasLang {
		env.WriteEndTag("span")
	}
}

func (env *EncEnvironment) writeLiteral(args *sxpf.Pair, a attrs.Attributes, tag string) {
	if a == nil {
		a = env.GetAttributes(args)
	}
	oldVisible := env.visibleSpace
	if a.HasDefault() {
		env.visibleSpace = true
		a = a.RemoveDefault()
	}
	env.WriteStartTag(tag, a)
	env.WriteEscapedOrVisible(env.GetString(args.GetTail()))
	env.visibleSpace = oldVisible
	env.WriteEndTag(tag)
}

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

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"

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

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

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

-
+






+
+
+







//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
// 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
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 Detlef Stern
// 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"

	"zettelstore.de/c/maps"
	"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
		}

Added sexp/sexp.go.













































































































































































































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

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

// 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.String(zettel.Encoding)),
		sx.MakeList(sx.MakeSymbol("content"), sx.String(zettel.Content)),
	)
}

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

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

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

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

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

	return api.ZettelData{
		Meta:     meta,
		Rights:   rights,
		Encoding: string(encVals[1].(sx.String)),
		Content:  string(contentVals[1].(sx.String)),
	}, 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.String(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()] = string(mVals[1].(sx.String))
	}
	return res, nil
}

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

// ParseList parses the given object as a proper list, based on a type specification.
func ParseList(obj sx.Object, spec string) (sx.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':
			val, ok = sx.GetString(car)
		case 'y':
			val, ok = sx.GetSymbol(car)
		default:
			return nil, fmt.Errorf("unknown spec '%c'", spec[i])
		}
		if !ok {
			return nil, fmt.Errorf("does not match spec '%v': %v", spec[i], car)
		}
		result = append(result, val)
		next, isNextPair := sx.GetPair(node.Cdr())
		if !isNextPair {
			return nil, sx.ErrImproper{Pair: pair}
		}
		node = next
	}
	if i < len(spec) {
		return nil, ErrElementsMissing
	}
	return result, nil
}

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

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

Added sexp/sexp_test.go.


























































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
//-----------------------------------------------------------------------------
// 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"

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

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

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

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

}

Deleted sexpr/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
25
26
27
28
29
30
31
32
33
34
35
36




































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

package sexpr

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

// GetAttributes traverses a s-expression list and returns an attribute structure.
func GetAttributes(seq *sxpf.Pair) (result attrs.Attributes) {
	for elem := seq; !elem.IsNil(); elem = elem.GetTail() {
		attr, err := elem.GetPair()
		if err != nil {
			continue
		}
		key, err := attr.GetString()
		if err != nil {
			continue
		}
		val, err := attr.GetTail().GetString()
		if err != nil {
			continue
		}
		result = result.Set(key, val)
	}
	return result
}

Deleted sexpr/const.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107











































































































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

package sexpr

import "codeberg.org/t73fde/sxpf"

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

// Symbols for Zettel node types.
var (
	Smk                = sxpf.NewTrivialSymbolMaker()
	SymBLOB            = Smk.MakeSymbol("BLOB")
	SymCell            = Smk.MakeSymbol("CELL")
	SymCellCenter      = Smk.MakeSymbol("CELL-CENTER")
	SymCellLeft        = Smk.MakeSymbol("CELL-LEFT")
	SymCellRight       = Smk.MakeSymbol("CELL-RIGHT")
	SymCite            = Smk.MakeSymbol("CITE")
	SymDescription     = Smk.MakeSymbol("DESCRIPTION")
	SymEmbed           = Smk.MakeSymbol("EMBED")
	SymEmbedBLOB       = Smk.MakeSymbol("EMBED-BLOB")
	SymFootnote        = Smk.MakeSymbol("FOOTNOTE")
	SymFormatEmph      = Smk.MakeSymbol("FORMAT-EMPH")
	SymFormatDelete    = Smk.MakeSymbol("FORMAT-DELETE")
	SymFormatInsert    = Smk.MakeSymbol("FORMAT-INSERT")
	SymFormatQuote     = Smk.MakeSymbol("FORMAT-QUOTE")
	SymFormatSpan      = Smk.MakeSymbol("FORMAT-SPAN")
	SymFormatSub       = Smk.MakeSymbol("FORMAT-SUB")
	SymFormatSuper     = Smk.MakeSymbol("FORMAT-SUPER")
	SymFormatStrong    = Smk.MakeSymbol("FORMAT-STRONG")
	SymHard            = Smk.MakeSymbol("HARD")
	SymHeading         = Smk.MakeSymbol("HEADING")
	SymLinkInvalid     = Smk.MakeSymbol("LINK-INVALID")
	SymLinkZettel      = Smk.MakeSymbol("LINK-ZETTEL")
	SymLinkSelf        = Smk.MakeSymbol("LINK-SELF")
	SymLinkFound       = Smk.MakeSymbol("LINK-FOUND")
	SymLinkBroken      = Smk.MakeSymbol("LINK-BROKEN")
	SymLinkHosted      = Smk.MakeSymbol("LINK-HOSTED")
	SymLinkBased       = Smk.MakeSymbol("LINK-BASED")
	SymLinkSearch      = Smk.MakeSymbol("LINK-SEARCH")
	SymLinkExternal    = Smk.MakeSymbol("LINK-EXTERNAL")
	SymListOrdered     = Smk.MakeSymbol("ORDERED")
	SymListUnordered   = Smk.MakeSymbol("UNORDERED")
	SymListQuote       = Smk.MakeSymbol("QUOTATION")
	SymLiteralProg     = Smk.MakeSymbol("LITERAL-CODE")
	SymLiteralComment  = Smk.MakeSymbol("LITERAL-COMMENT")
	SymLiteralHTML     = Smk.MakeSymbol("LITERAL-HTML")
	SymLiteralInput    = Smk.MakeSymbol("LITERAL-INPUT")
	SymLiteralMath     = Smk.MakeSymbol("LITERAL-MATH")
	SymLiteralOutput   = Smk.MakeSymbol("LITERAL-OUTPUT")
	SymLiteralZettel   = Smk.MakeSymbol("LITERAL-ZETTEL")
	SymMark            = Smk.MakeSymbol("MARK")
	SymPara            = Smk.MakeSymbol("PARA")
	SymRegionBlock     = Smk.MakeSymbol("REGION-BLOCK")
	SymRegionQuote     = Smk.MakeSymbol("REGION-QUOTE")
	SymRegionVerse     = Smk.MakeSymbol("REGION-VERSE")
	SymSoft            = Smk.MakeSymbol("SOFT")
	SymSpace           = Smk.MakeSymbol("SPACE")
	SymTable           = Smk.MakeSymbol("TABLE")
	SymTag             = Smk.MakeSymbol("TAG")
	SymText            = Smk.MakeSymbol("TEXT")
	SymThematic        = Smk.MakeSymbol("THEMATIC")
	SymTransclude      = Smk.MakeSymbol("TRANSCLUDE")
	SymUnknown         = Smk.MakeSymbol("UNKNOWN-NODE")
	SymVerbatimComment = Smk.MakeSymbol("VERBATIM-COMMENT")
	SymVerbatimEval    = Smk.MakeSymbol("VERBATIM-EVAL")
	SymVerbatimHTML    = Smk.MakeSymbol("VERBATIM-HTML")
	SymVerbatimMath    = Smk.MakeSymbol("VERBATIM-MATH")
	SymVerbatimProg    = Smk.MakeSymbol("VERBATIM-CODE")
	SymVerbatimZettel  = Smk.MakeSymbol("VERBATIM-ZETTEL")
)

// Constant symbols for reference states.
var (
	SymRefStateInvalid  = Smk.MakeSymbol("INVALID")
	SymRefStateZettel   = Smk.MakeSymbol("ZETTEL")
	SymRefStateSelf     = Smk.MakeSymbol("SELF")
	SymRefStateFound    = Smk.MakeSymbol("FOUND")
	SymRefStateBroken   = Smk.MakeSymbol("BROKEN")
	SymRefStateHosted   = Smk.MakeSymbol("HOSTED")
	SymRefStateBased    = Smk.MakeSymbol("BASED")
	SymRefStateSearch   = Smk.MakeSymbol("SEARCH")
	SymRefStateExternal = Smk.MakeSymbol("EXTERNAL")
)

// Symbols for metadata types
var (
	SymTypeCredential   = Smk.MakeSymbol("CREDENTIAL")
	SymTypeEmpty        = Smk.MakeSymbol("EMPTY-STRING")
	SymTypeID           = Smk.MakeSymbol("ZID")
	SymTypeIDSet        = Smk.MakeSymbol("ZID-SET")
	SymTypeNumber       = Smk.MakeSymbol("NUMBER")
	SymTypeString       = Smk.MakeSymbol("STRING")
	SymTypeTagSet       = Smk.MakeSymbol("TAG-SET")
	SymTypeTimestamp    = Smk.MakeSymbol("TIMESTAMP")
	SymTypeURL          = Smk.MakeSymbol("URL")
	SymTypeWord         = Smk.MakeSymbol("WORD")
	SymTypeWordSet      = Smk.MakeSymbol("WORD-SET")
	SymTypeZettelmarkup = Smk.MakeSymbol("ZETTELMARKUP")
)

Deleted sexpr/sexpr.go.

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













































































































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

package sexpr

import "codeberg.org/t73fde/sxpf"

func MakeString(val sxpf.Value) string {
	if strVal, ok := val.(*sxpf.String); ok {
		return strVal.GetValue()
	}
	return ""
}

// GetMetaContent returns the metadata and the content of a sexpr encoded zettel.
func GetMetaContent(zettel sxpf.Value) (Meta, *sxpf.Pair) {
	if pair, ok := zettel.(*sxpf.Pair); ok {
		m := pair.GetFirst()
		if s := pair.GetSecond(); s != nil {
			if p, ok := s.(*sxpf.Pair); ok {
				if content, err := p.GetPair(); err == nil {
					return MakeMeta(m), content
				}
			}
		}
		return MakeMeta(m), nil
	}
	return nil, nil
}

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

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

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

func (m Meta) GetPair(key string) *sxpf.Pair {
	if mv, found := m[key]; found {
		if seq, ok := mv.Value.(*sxpf.Pair); ok && !seq.IsEmpty() {
			return seq
		}
	}
	return nil
}

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

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

Added 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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
//-----------------------------------------------------------------------------
// 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"

	"zettelstore.de/client.fossil/api"
	"zettelstore.de/client.fossil/attrs"
	"zettelstore.de/client.fossil/sz"
	"zettelstore.de/client.fossil/text"
	"zettelstore.de/sx.fossil"
	"zettelstore.de/sx.fossil/sxhtml"
)

// 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.String(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.String("zs-endnotes"))).Cons(sxhtml.SymAttr))
	for i, fni := range env.endnotes {
		noteNum := strconv.Itoa(i + 1)
		attrs := fni.attrs.Cons(sx.Cons(SymAttrClass, sx.String("zs-endnote"))).
			Cons(sx.Cons(SymAttrValue, sx.String(noteNum))).
			Cons(sx.Cons(SymAttrId, sx.String("fn:"+fni.noteID))).
			Cons(sx.Cons(SymAttrRole, sx.String("doc-endnote"))).
			Cons(sxhtml.SymAttr)

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

		var li sx.ListBuilder
		li.Add(SymLI)
		li.Add(attrs)
		li.ExtendBang(fni.noteHx)
		li.Add(sx.String(" "))
		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", string(getString(args[1], env)))
		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(string(getString(elem.Car(), env)))
		}
		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 := string(getString(args[3], env)); 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.String("<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 != "" {
					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.String(visibleReplacer.Replace(string(content)))
		}
		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 != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.IsEqual(sz.SymRefStateExternal) {
				a := ev.GetAttributes(args[0], env).Set("src", string(refValue)).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(SymIMG)).Cons(SymP)
			}
			return sx.MakeList(
				sxhtml.SymInlineComment,
				sx.String("transclude"),
				refKind,
				sx.String("->"),
				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.String(" ")
		}
		return getString(args[0], env)
	})
	ev.bind(sz.SymSoft, 0, func(sx.Vector, *Environment) sx.Object { return sx.String(" ") })
	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", string(refValue)), 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(string(refValue))
		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", string(refValue)).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)
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				sxhtml.SymAttr,
				sx.Cons(SymAttrType, sx.String("image/svg+xml")),
				sx.Cons(SymAttrSrc, sx.String("/"+string(getString(ref.Tail(), env))+".svg")),
			)
			return sx.MakeList(
				SymFIGURE,
				sx.MakeList(
					SymEMBED,
					embedAttr,
				),
			)
		}
		a := ev.GetAttributes(args[0], env)
		a = a.Set("src", string(getString(ref.Tail().Car(), env)))
		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.String(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 != "" {
			if len(args) > 2 {
				result = ev.evalSlice(args[2:], env).Cons(sx.String(", "))
			}
			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); fragment != "" {
				a := attrs.Attributes{"id": string(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.String("doc-noteref"))).
			Cons(sx.Cons(SymAttrHref, sx.String("#fn:"+noteID))).
			Cons(sx.Cons(SymAttrClass, sx.String("zs-noteref"))).
			Cons(sxhtml.SymAttr)
		href := sx.Nil().Cons(sx.String(noteNum)).Cons(hrefAttr).Cons(SymA)
		supAttr := sx.Nil().Cons(sx.Cons(SymAttrId, sx.String("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 != "" {
					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.String(leftQ), sx.String(rightQ)), sx.Nil())
	} else {
		if quotes.nbsp {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.String("&nbsp;"), sx.String(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.String(leftQ), sx.String("&nbsp;")))
		} else {
			lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.String(rightQ)))
			res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.String(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 := string(getString(args[1], env))
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sx.Nil().Cons(sx.String(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 != "" && IsSafe(string(s)) {
		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 == "" {
		return sx.Nil()
	}
	switch syntax {
	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.String("data:image/"+string(syntax)+";base64,"+string(data))))
		var sb strings.Builder
		flattenText(&sb, description)
		if d := sb.String(); d != "" {
			imgAttr = imgAttr.Cons(sx.Cons(symAttrAlt, sx.String(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(string(obj))
		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 ""
	}
	if s, ok := sx.GetString(val); ok {
		return s
	}
	env.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
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",
}

// IsSafe returns true if the given string does not contain unsafe HTML elements.
func IsSafe(s string) bool {
	lower := strings.ToLower(s)
	for _, snippet := range unsafeSnippets {
		if strings.Contains(lower, snippet) {
			return false
		}
	}
	return true
}

Added sz/const.go.









































































































































































































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

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

Added sz/sz.go.
































































































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
//-----------------------------------------------------------------------------
// 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 (
	"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(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 string(o)
	case *sx.Symbol:
		return o.GoString()
	}
	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 {
				return MakeMeta(m), content
			}
		}
		return MakeMeta(m), nil
	}
	return nil, nil
}

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

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

Deleted text/stext.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


































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 (
	"bytes"
	"io"

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

// EvaluateInlineString returns the text content of the given inline list as a string.
func EvaluateInlineString(pl *sxpf.Pair) string {
	var buf bytes.Buffer
	env := newTextEnvironment(&buf)
	env.EvalPair(pl)
	return buf.String()
}

type textEnvironment struct {
	err error
	w   io.Writer
	sm  *sxpf.SymbolMap
}

func newTextEnvironment(w io.Writer) textEnvironment {
	sm := sxpf.NewSymbolMap(sexpr.Smk, nil)
	for _, bFn := range builtins {
		sym := bFn.sym
		minArgs := bFn.minArgs
		fn := bFn.fn
		sm.Set(sym, sxpf.NewBuiltin(
			sym.GetValue(),
			true, minArgs, -1,
			func(env sxpf.Environment, args *sxpf.Pair, _ int) (sxpf.Value, error) {
				fn(env.(*textEnvironment), args)
				return sxpf.Nil(), nil
			},
		))
	}

	return textEnvironment{
		w:  w,
		sm: sm,
	}
}

func (env *textEnvironment) GetString(p *sxpf.Pair) (res string) {
	if env.err == nil {
		res, env.err = p.GetString()
		return res
	}
	return ""
}

func (env *textEnvironment) WriteString(s string) {
	if env.err == nil {
		_, env.err = io.WriteString(env.w, s)
	}
}

func (env *textEnvironment) LookupForm(sym *sxpf.Symbol) (sxpf.Form, error) {
	return env.sm.LookupForm(sym)
}

func (*textEnvironment) EvalSymbol(*sxpf.Symbol) (sxpf.Value, error) { return nil, nil }
func (env *textEnvironment) EvalPair(p *sxpf.Pair) (sxpf.Value, error) {
	return sxpf.EvalCallOrSeq(env, p)
}
func (env *textEnvironment) EvalOther(val sxpf.Value) (sxpf.Value, error) {
	if strVal, ok := val.(*sxpf.String); ok {
		env.WriteString(strVal.GetValue())
		return nil, nil
	}
	return val, nil
}

var builtins = []struct {
	sym     *sxpf.Symbol
	minArgs int
	fn      func(env *textEnvironment, args *sxpf.Pair)
}{
	{sexpr.SymText, 1, func(env *textEnvironment, args *sxpf.Pair) { env.WriteString(env.GetString(args)) }},
	{sexpr.SymTag, 1, func(env *textEnvironment, args *sxpf.Pair) { env.WriteString(env.GetString(args)) }},
	{sexpr.SymSpace, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }},
	{sexpr.SymSoft, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }},
	{sexpr.SymHard, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString("\n") }},
}

Added 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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
//-----------------------------------------------------------------------------
// 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"

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

// 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(string(val))
		}
	} 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
27
28


29
30
31

32
33
34
35
36
37


38
39
40
41
42
43
44
45
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

-
+






+
+
+





+


-
-
-
+
+
+


-
+





-
-
+
+


-
+




-
-
+
+








//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
// 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"

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

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

Changes to www/changes.wiki.

1
2






















































3
4
5
6
7
8
9
10
11
12
13
14
15
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


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+













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

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

<a name="0_10"></a>
<h2>Changes for Version 0.10.0 (2023-01-24)</h2>
  *  Add query key <tt>parseonly</tt> and two encoding names (plain, json) to
     allow to merge various API endpoints into <tt>/z</tt>
  *  Client method do not use endpoints <tt>/j, /m, /q, /p, /v</tt> any more.
     They are merged into endpoint <tt>/z</tt>. Use this client only with
     Zettelstore v0.10.0 or better.
     (breaking)

<a name="0_9"></a>
<h2>Changes for Version 0.9.0 (2022-12-12)</h2>
  *  Rename <tt>api.QueryKeyDepth</tt> to <tt>api.QueryKeyCost</tt>
  *  Update encode / syntax names

<a name="0_6"></a>
<h2>Changes for Version 0.6.0 (2022-08-11)</h2>
  *  Add support to build URLs with search expressions
  *  Use Go 1.19
  *  Fix some bugs

<a name="0_5"></a>
<h2>Changes for Version 0.5.1 (2022-08-08)</h2>
  *  Support for search references
     (minor: api, zjson, sexpr, html)

<h2>Changes for Version 0.5 (2022-07-29)</h2>
  *  Initial public release.

Changes to www/index.wiki.

1
2
3
4
5
6
7
8
9
10
11






12
























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





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

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
<title>Home</title>

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

<h3>Latest Release: 0.6.0 (2022-08-11)</h3>
  *  [./changes.wiki#0_6|Change summary]
  *  [/timeline?p=v0.6.0&bt=v0.5&y=ci|Check-ins for version 0.6.0],
     [/vdiff?to=v0.6.0&from=v0.5|content diff]
  *  [/timeline?df=v0.6.0&y=ci|Check-ins derived from the 0.6.0 release],
     [/vdiff?from=v0.6.0&to=trunk|content diff]
<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
zettelstore.de/client.fossil@HASH</code>

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

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

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




































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 zjson

// Values for Zettelmarkup element object names
const (
	NameType        = ""
	NameAttribute   = "a"
	NameBLOB        = "j"
	NameBinary      = "o"
	NameBlock       = "b"
	NameDescrList   = "d"
	NameDescription = "e"
	NameInline      = "i"
	NameList        = "c"
	NameNumeric     = "n"
	NameSet         = "y"
	NameString      = "s"
	NameString2     = "q"
	NameString3     = "v"
	NameTable       = "p"
)

// Values to specify the Zettelmarkup element type
const (
	TypeBLOB            = "BLOB"
	TypeBlock           = "Block"
	TypeBreakHard       = "Hard"
	TypeBreakSoft       = "Soft"
	TypeBreakThematic   = "Thematic"
	TypeCitation        = "Cite"
	TypeDescrList       = "Description"
	TypeEmbed           = "Embed"
	TypeEmbedBLOB       = "EmbedBLOB"
	TypeExcerpt         = "Excerpt"
	TypeFootnote        = "Footnote"
	TypeFormatDelete    = "Delete"
	TypeFormatEmph      = "Emph"
	TypeFormatInsert    = "Insert"
	TypeFormatQuote     = "Quote"
	TypeFormatSpan      = "Span"
	TypeFormatStrong    = "Strong"
	TypeFormatSub       = "Sub"
	TypeFormatSuper     = "Super"
	TypeHeading         = "Heading"
	TypeLink            = "Link"
	TypeListBullet      = "Bullet"
	TypeListOrdered     = "Ordered"
	TypeListQuotation   = "Quotation"
	TypeLiteralCode     = "Code"
	TypeLiteralComment  = "Comment"
	TypeLiteralHTML     = "HTML"
	TypeLiteralInput    = "Input"
	TypeLiteralOutput   = "Output"
	TypeLiteralMath     = "Math"
	TypeLiteralZettel   = "Zettel"
	TypeMark            = "Mark"
	TypeParagraph       = "Para"
	TypePoem            = "Poem"
	TypeSpace           = "Space"
	TypeTable           = "Table"
	TypeTag             = "Tag"
	TypeText            = "Text"
	TypeTransclude      = "Transclude"
	TypeVerbatimCode    = "CodeBlock"
	TypeVerbatimComment = "CommentBlock"
	TypeVerbatimEval    = "EvalBlock"
	TypeVerbatimHTML    = "HTMLBlock"
	TypeVerbatimMath    = "MathBlock"
	TypeVerbatimZettel  = "ZettelBlock"
)

// Values to specify the state of a reference
const (
	RefStateBased    = "based"
	RefStateBroken   = "broken"
	RefStateExternal = "external"
	RefStateFound    = "found"
	RefStateHosted   = "local"
	RefStateInvalid  = "invalid"
	RefStateSearch   = "search"
	RefStateSelf     = "self"
	RefStateZettel   = "zettel"
)

// Values for table cell alignment
const (
	AlignDefault = ""
	AlignLeft    = "<"
	AlignCenter  = ":"
	AlignRight   = ">"
)

Deleted zjson/meta.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

































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 zjson

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

func MakeMeta(val Value) Meta {
	obj := MakeObject(val)
	if len(obj) == 0 {
		return nil
	}
	result := make(Meta, len(obj))
	for k, v := range obj {
		mvObj := MakeObject(v)
		if len(mvObj) == 0 {
			continue
		}
		mv := makeMetaValue(mvObj)
		if mv.Type != "" {
			result[k] = mv
		}
	}
	return result
}
func makeMetaValue(mvObj Object) MetaValue {
	mv := MetaValue{}
	for n, val := range mvObj {
		if n == NameType {
			if t, ok := val.(string); ok {
				mv.Type = t
			}
		} else {
			mv.Key = n
			mv.Value = val
		}
	}
	return mv
}

func (m Meta) GetArray(key string) Array {
	if v, found := m[key]; found {
		return MakeArray(v.Value)
	}
	return nil
}

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

Deleted zjson/zjson.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






































































































































































































































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
//-----------------------------------------------------------------------------
// Copyright (c) 2022 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 zjson provides types, constants and function to work with the ZJSON
// encoding of zettel.
package zjson

import (
	"encoding/json"
	"fmt"
	"io"

	"zettelstore.de/c/attrs"
)

// Value is the gerneric JSON value.
type Value = interface{}

// Array represents a JSON array.
type Array = []Value

// Object represents a JSON object.
type Object = map[string]Value

// Decode some JSON data and return the tree.
func Decode(data io.Reader) (Value, error) {
	var result interface{}
	dec := json.NewDecoder(data)
	dec.UseNumber()
	err := dec.Decode(&result)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// GetMetaContent returns the metadata and the content of a zettel ZJSON.
func GetMetaContent(zjZettel Value) (Meta, Array) {
	if zettel, ok := zjZettel.(Object); ok {
		meta := MakeMeta(zettel["meta"])
		content := MakeArray(zettel["content"])
		return meta, content
	}
	return nil, nil
}

// Visitor provides functionality when a Value is traversed.
type Visitor interface {
	BlockArray(a Array, pos int) CloseFunc
	InlineArray(a Array, pos int) CloseFunc
	ItemArray(a Array, pos int) CloseFunc

	BlockObject(t string, obj Object, pos int) (bool, CloseFunc)
	InlineObject(t string, obj Object, pos int) (bool, CloseFunc)

	Unexpected(val Value, pos int, exp string)
}

// CloseFunc is a function that executes after a ZJSON element is visited.
type CloseFunc func()

// WalkBlock traverses a block array.
func WalkBlock(v Visitor, a Array, pos int) {
	ef := v.BlockArray(a, pos)
	for i, elem := range a {
		WalkBlockObject(v, elem, i)
	}
	if ef != nil {
		ef()
	}
}

// WalkInline traverses an inline array.
func WalkInline(v Visitor, a Array, pos int) {
	ef := v.InlineArray(a, pos)
	for i, elem := range a {
		WalkInlineObject(v, elem, i)
	}
	if ef != nil {
		ef()
	}
}

// WalkBlockObject traverses a value as a JSON object in a block array.
func WalkBlockObject(v Visitor, val Value, pos int) { walkObject(v, val, pos, v.BlockObject) }

// WalkInlineObject traverses a value as a JSON object in an inline array.
func WalkInlineObject(v Visitor, val Value, pos int) { walkObject(v, val, pos, v.InlineObject) }

func walkObject(v Visitor, val Value, pos int, objFunc func(string, Object, int) (bool, CloseFunc)) {
	obj, ok := val.(Object)
	if !ok {
		v.Unexpected(val, pos, "Object")
		return
	}

	tVal, ok := obj[NameType]
	if !ok {
		v.Unexpected(obj, pos, "Object type")
		return
	}
	t, ok := tVal.(string)
	if !ok {
		v.Unexpected(obj, pos, "Object type value")
		return
	}

	doChilds, ef := objFunc(t, obj, pos)
	if doChilds {
		WalkBlockChild(v, obj, pos)
		WalkItemChild(v, obj, pos)
		WalkInlineChild(v, obj, pos)
		walkDescriptionList(v, obj)
		walkTable(v, obj, pos)
	}
	if ef != nil {
		ef()
	}
}

// WalkInlineChild traverses the array found at the name NameInline ('i').
func WalkInlineChild(v Visitor, obj Object, pos int) {
	if iVal, ok := obj[NameInline]; ok {
		if il, ok := iVal.(Array); ok {
			WalkInline(v, il, 0)
		} else {
			v.Unexpected(iVal, pos, "Inline array")
		}
	}
}

// WalkBlockChild traverses the array found at the name NameBlock ('b').
func WalkBlockChild(v Visitor, obj Object, pos int) {
	if bVal, ok := obj[NameBlock]; ok {
		if bl, ok := bVal.(Array); ok {
			WalkBlock(v, bl, 0)
		} else {
			v.Unexpected(bVal, pos, "Block array")
		}
	}
}

// WalkItemChild traverses the arrays found at the name NameList ('c').
func WalkItemChild(v Visitor, obj Object, pos int) {
	iVal, ok := obj[NameList]
	if !ok {
		return
	}
	it, ok := iVal.(Array)
	if !ok {
		v.Unexpected(iVal, pos, "Item array")
		return
	}
	for i, l := range it {
		ef := v.ItemArray(it, i)
		if bl, ok := l.(Array); ok {
			WalkBlock(v, bl, i)
		} else {
			v.Unexpected(l, i, "Item block array")
		}
		if ef != nil {
			ef()
		}
	}
}

func walkDescriptionList(v Visitor, obj Object) {
	descrs := GetArray(obj, NameDescrList)
	if len(descrs) == 0 {
		return
	}
	for i, elem := range descrs {
		dObj := MakeObject(elem)
		if dObj == nil {
			continue
		}
		WalkInlineChild(v, dObj, i)
		descr := GetArray(dObj, NameDescription)
		if len(descr) == 0 {
			continue
		}
		for j, ddv := range descr {
			dd := MakeArray(ddv)
			if len(dd) == 0 {
				continue
			}
			WalkBlock(v, dd, j)
		}
	}
}

func walkTable(v Visitor, obj Object, pos int) {
	tdata := GetArray(obj, NameTable)
	if len(tdata) == 0 {
		return
	}
	if len(tdata) != 2 {
		v.Unexpected(obj, pos, "Table header/rows")
		return
	}
	walkRow(v, MakeArray(tdata[0]))
	if bArray := MakeArray(tdata[1]); len(bArray) > 0 {
		for _, row := range bArray {
			if rArray := MakeArray(row); rArray != nil {
				walkRow(v, rArray)
			}
		}
	}
}
func walkRow(v Visitor, row Array) {
	if len(row) > 0 {
		for _, cell := range row {
			if cObj := MakeObject(cell); cObj != nil {
				WalkInlineChild(v, cObj, 0)
			}
		}
	}
}

// GetArray returns the array-typed value under the given name.
func GetArray(obj Object, name string) Array {
	if v, ok := obj[name]; ok && v != nil {
		return MakeArray(v)
	}
	return nil
}

// GetNumber returns the numeric value at NameNumberic ('n') as a string.
func GetNumber(obj Object) string {
	if v, ok := obj[NameNumeric]; ok {
		if n, ok := v.(json.Number); ok {
			return string(n)
		}
		if f, ok := v.(float64); ok {
			return fmt.Sprint(f)
		}
	}
	return ""
}

// GetString returns the string value at the given name.
func GetString(obj Object, name string) string {
	if v, ok := obj[name]; ok {
		return MakeString(v)
	}
	return ""
}

// MakeArray returns the given value as a JSON array.
func MakeArray(val Value) Array {
	if a, ok := val.(Array); ok {
		return a
	}
	return nil
}

// MakeString returns the given value as a string.
func MakeString(val Value) string {
	if s, ok := val.(string); ok {
		return s
	}
	return ""
}

// GetAttribute returns a copy of the attributes of the given object.
func GetAttributes(obj Object) attrs.Attributes {
	a := GetObject(obj, NameAttribute)
	if len(a) == 0 {
		return nil
	}
	result := make(attrs.Attributes, len(a))
	for n, v := range a {
		if val, ok := v.(string); ok {
			result[n] = val
		}
	}
	return result
}

// SetAttributes copies the attributes to the given object.
func SetAttributes(obj Object, a attrs.Attributes) {
	if len(a) == 0 {
		delete(obj, NameAttribute)
	}
	val := make(Object)
	for k, v := range a {
		val[k] = v
	}
	obj[NameAttribute] = val
}

// GetObject returns the object found at the given object with the given name.
func GetObject(obj Object, name string) Object {
	if v, ok := obj[name]; ok && v != nil {
		return MakeObject(v)
	}
	return nil
}

// MakeObject returns the given value as a JSON object.
func MakeObject(val Value) Object {
	if o, ok := val.(Object); ok {
		return o
	}
	return nil
}

// GetParagraphInline return the inline list of the first paragraph (or nil if there is no such thing)
func GetParagraphInline(a Array) Array {
	if len(a) != 1 {
		return nil
	}
	if o := MakeObject(a[0]); o != nil {
		if GetString(o, NameType) == TypeParagraph {
			return GetArray(o, NameInline)
		}
	}
	return nil
}