Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From v0.21.0
To trunk
2025-06-28
| | |
12:38 |
|
...
(Leaf
check-in: 4e8e110e8f user: t73fde tags: trunk)
|
2025-06-27
| | |
17:36 |
|
...
(check-in: 0796a3da98 user: stern tags: trunk)
|
2025-04-26
| | |
15:13 |
|
...
(check-in: 7ae5e31c4a user: stern tags: trunk)
|
2025-04-17
| | |
15:29 |
|
...
(check-in: 7220c2d479 user: stern tags: trunk, release, v0.21.0)
|
09:24 |
|
...
(check-in: b3283fc6d6 user: stern tags: trunk)
|
| | |
Changes to VERSION.
1
|
1
|
-
+
|
0.21.0
0.22.0-dev
|
Changes to cmd/cmd_run.go.
︙ | | |
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
+
|
"context"
"flag"
"net/http"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/auth/user"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/usecase"
"zettelstore.de/z/internal/web/adapter/api"
"zettelstore.de/z/internal/web/adapter/webui"
"zettelstore.de/z/internal/web/server"
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
+
-
+
|
kernel.Main.WaitForShutdown()
return exitCode, err
}
func setupRouting(webSrv server.Server, boxManager box.Manager, authManager auth.Manager, rtConfig config.Config) {
protectedBoxManager, authPolicy := authManager.BoxWithPolicy(boxManager, rtConfig)
kern := kernel.Main
webLog := kern.GetLogger(kernel.WebService)
webLogger := kern.GetLogger(kernel.WebService)
var getUser getUserImpl
logAuth := kern.GetLogger(kernel.AuthService)
logUc := kern.GetLogger(kernel.CoreService).WithUser(&getUser)
authLogger := kern.GetLogger(kernel.AuthService)
ucLogger := kern.GetLogger(kernel.CoreService)
ucGetUser := usecase.NewGetUser(authManager, boxManager)
ucAuthenticate := usecase.NewAuthenticate(logAuth, authManager, &ucGetUser)
ucIsAuth := usecase.NewIsAuthenticated(logUc, &getUser, authManager)
ucCreateZettel := usecase.NewCreateZettel(logUc, rtConfig, protectedBoxManager)
ucAuthenticate := usecase.NewAuthenticate(authLogger, authManager, &ucGetUser)
ucIsAuth := usecase.NewIsAuthenticated(ucLogger, &getUser, authManager)
ucCreateZettel := usecase.NewCreateZettel(ucLogger, rtConfig, protectedBoxManager)
ucGetAllZettel := usecase.NewGetAllZettel(protectedBoxManager)
ucGetZettel := usecase.NewGetZettel(protectedBoxManager)
ucParseZettel := usecase.NewParseZettel(rtConfig, ucGetZettel)
ucGetReferences := usecase.NewGetReferences()
ucQuery := usecase.NewQuery(protectedBoxManager)
ucEvaluate := usecase.NewEvaluate(rtConfig, &ucGetZettel, &ucQuery)
ucQuery.SetEvaluate(&ucEvaluate)
ucTagZettel := usecase.NewTagZettel(protectedBoxManager, &ucQuery)
ucRoleZettel := usecase.NewRoleZettel(protectedBoxManager, &ucQuery)
ucListSyntax := usecase.NewListSyntax(protectedBoxManager)
ucListRoles := usecase.NewListRoles(protectedBoxManager)
ucDelete := usecase.NewDeleteZettel(logUc, protectedBoxManager)
ucUpdate := usecase.NewUpdateZettel(logUc, protectedBoxManager)
ucRefresh := usecase.NewRefresh(logUc, protectedBoxManager)
ucReIndex := usecase.NewReIndex(logUc, protectedBoxManager)
ucDelete := usecase.NewDeleteZettel(ucLogger, protectedBoxManager)
ucUpdate := usecase.NewUpdateZettel(ucLogger, protectedBoxManager)
ucRefresh := usecase.NewRefresh(ucLogger, protectedBoxManager)
ucReIndex := usecase.NewReIndex(ucLogger, protectedBoxManager)
ucVersion := usecase.NewVersion(kernel.Main.GetConfig(kernel.CoreService, kernel.CoreVersion).(string))
a := api.New(
webLog.Clone().Str("adapter", "api").Child(),
webLogger.With("system", "WEBAPI"),
webSrv, authManager, authManager, rtConfig, authPolicy)
wui := webui.New(
webLog.Clone().Str("adapter", "wui").Child(),
webLogger.With("system", "WEBUI"),
webSrv, authManager, rtConfig, authManager, boxManager, authPolicy, &ucEvaluate)
webSrv.Handle("/", wui.MakeGetRootHandler(protectedBoxManager))
if assetDir := kern.GetConfig(kernel.WebService, kernel.WebAssetDir).(string); assetDir != "" {
const assetPrefix = "/assets/"
webSrv.Handle(assetPrefix, http.StripPrefix(assetPrefix, http.FileServer(http.Dir(assetDir))))
webSrv.Handle("/favicon.ico", wui.MakeFaviconHandler(assetDir))
|
︙ | | |
132
133
134
135
136
137
138
139
|
133
134
135
136
137
138
139
140
|
-
+
|
if authManager.WithAuth() {
webSrv.SetUserRetriever(usecase.NewGetUserByZid(boxManager))
}
}
type getUserImpl struct{}
func (*getUserImpl) GetUser(ctx context.Context) *meta.Meta { return server.GetUser(ctx) }
func (*getUserImpl) GetCurrentUser(ctx context.Context) *meta.Meta { return user.GetCurrentUser(ctx) }
|
Changes to cmd/command.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
+
-
-
|
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------
package cmd
import (
"flag"
"log/slog"
"maps"
"slices"
"zettelstore.de/z/internal/logger"
)
// Command stores information about commands / sub-commands.
type Command struct {
Name string // command name as it appears on the command line
Func CommandFunc // function that executes a command
Simple bool // Operate in simple-mode
|
︙ | | |
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
-
+
|
if cmd.Name == "" || cmd.Func == nil {
panic("Required command values missing")
}
if _, ok := commands[cmd.Name]; ok {
panic("Command already registered: " + cmd.Name)
}
cmd.flags = flag.NewFlagSet(cmd.Name, flag.ExitOnError)
cmd.flags.String("l", logger.InfoLevel.String(), "log level specification")
cmd.flags.String("l", slog.LevelInfo.String(), "log level specification")
if cmd.SetFlags != nil {
cmd.SetFlags(cmd.flags)
}
commands[cmd.Name] = cmd
}
|
︙ | | |
Changes to cmd/main.go.
︙ | | |
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
|
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
|
+
-
+
|
// Package cmd provides the commands to call Zettelstore from the command line.
package cmd
import (
"crypto/sha256"
"flag"
"fmt"
"log/slog"
"net"
"net/url"
"os"
"runtime/debug"
"strconv"
"strings"
"time"
"t73f.de/r/zsc/api"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"t73f.de/r/zsx/input"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/auth/impl"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/compbox"
"zettelstore.de/z/internal/box/manager"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/web/server"
)
const strRunSimple = "run-simple"
func init() {
RegisterCommand(Command{
|
︙ | | |
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
|
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
|
+
+
+
-
-
+
+
|
keyBoxOneURI = kernel.BoxURIs + "1"
keyDebug = "debug-mode"
keyDefaultDirBoxType = "default-dir-box-type"
keyInsecureCookie = "insecure-cookie"
keyInsecureHTML = "insecure-html"
keyListenAddr = "listen-addr"
keyLogLevel = "log-level"
keyLoopbackIdent = "loopback-ident"
keyLoopbackZid = "loopback-zid"
keyMaxRequestSize = "max-request-size"
keyOwner = "owner"
keyPersistentCookie = "persistent-cookie"
keyReadOnly = "read-only-mode"
keyRuntimeProfiling = "runtime-profiling"
keySxNesting = "sx-max-nesting"
keyTokenLifetimeHTML = "token-lifetime-html"
keyTokenLifetimeAPI = "token-lifetime-api"
keyURLPrefix = "url-prefix"
keyVerbose = "verbose-mode"
)
func setServiceConfig(cfg *meta.Meta) bool {
debugMode := cfg.GetBool(keyDebug)
if debugMode && kernel.Main.GetKernelLogger().Level() > logger.DebugLevel {
kernel.Main.SetLogLevel(logger.DebugLevel.String())
if debugMode && kernel.Main.GetKernelLogLevel() > slog.LevelDebug {
kernel.Main.SetLogLevel(logging.LevelString(slog.LevelDebug))
}
if logLevel, found := cfg.Get(keyLogLevel); found {
kernel.Main.SetLogLevel(string(logLevel))
}
err := setConfigValue(nil, kernel.CoreService, kernel.CoreDebug, debugMode)
err = setConfigValue(err, kernel.CoreService, kernel.CoreVerbose, cfg.GetBool(keyVerbose))
if val, found := cfg.Get(keyAdminPort); found {
|
︙ | | |
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
|
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
|
+
+
+
+
+
-
+
-
-
+
+
|
}
err = setConfigValue(
err, kernel.ConfigService, kernel.ConfigInsecureHTML, cfg.GetDefault(keyInsecureHTML, kernel.ConfigSecureHTML))
err = setConfigValue(
err, kernel.WebService, kernel.WebListenAddress, cfg.GetDefault(keyListenAddr, "127.0.0.1:23123"))
err = setConfigValue(err, kernel.WebService, kernel.WebLoopbackIdent, cfg.GetDefault(keyLoopbackIdent, ""))
err = setConfigValue(err, kernel.WebService, kernel.WebLoopbackZid, cfg.GetDefault(keyLoopbackZid, ""))
if val, found := cfg.Get(keyBaseURL); found {
err = setConfigValue(err, kernel.WebService, kernel.WebBaseURL, val)
}
if val, found := cfg.Get(keyURLPrefix); found {
err = setConfigValue(err, kernel.WebService, kernel.WebURLPrefix, val)
}
err = setConfigValue(err, kernel.WebService, kernel.WebSecureCookie, !cfg.GetBool(keyInsecureCookie))
err = setConfigValue(err, kernel.WebService, kernel.WebPersistentCookie, cfg.GetBool(keyPersistentCookie))
if val, found := cfg.Get(keyMaxRequestSize); found {
err = setConfigValue(err, kernel.WebService, kernel.WebMaxRequestSize, val)
}
err = setConfigValue(
err, kernel.WebService, kernel.WebTokenLifetimeAPI, cfg.GetDefault(keyTokenLifetimeAPI, ""))
err = setConfigValue(
err, kernel.WebService, kernel.WebTokenLifetimeHTML, cfg.GetDefault(keyTokenLifetimeHTML, ""))
err = setConfigValue(err, kernel.WebService, kernel.WebProfiling, debugMode || cfg.GetBool(keyRuntimeProfiling))
if val, found := cfg.Get(keyAssetDir); found {
err = setConfigValue(err, kernel.WebService, kernel.WebAssetDir, val)
}
if val, found := cfg.Get(keySxNesting); found {
err = setConfigValue(err, kernel.WebService, kernel.WebSxMaxNesting, val)
}
return err == nil
}
func setConfigValue(err error, subsys kernel.Service, key string, val any) error {
if err == nil {
err = kernel.Main.SetConfig(subsys, key, fmt.Sprint(val))
if err = kernel.Main.SetConfig(subsys, key, fmt.Sprint(val)); err != nil {
if err != nil {
kernel.Main.GetKernelLogger().Error().Str("key", key).Str("value", fmt.Sprint(val)).Err(err).Msg("Unable to set configuration")
kernel.Main.GetKernelLogger().Error("Unable to set configuration",
"key", key, "value", val, "err", err)
}
}
return err
}
func executeCommand(name string, args ...string) int {
command, ok := Get(name)
|
︙ | | |
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
-
+
+
+
+
|
func(srv server.Server, plMgr box.Manager, authMgr auth.Manager, rtConfig config.Config) error {
setupRouting(srv, plMgr, authMgr, rtConfig)
return nil
},
)
if command.Simple {
kern.SetConfig(kernel.ConfigService, kernel.ConfigSimpleMode, "true")
if err := kern.SetConfig(kernel.ConfigService, kernel.ConfigSimpleMode, "true"); err != nil {
kern.GetKernelLogger().Error("unable to set simple-mode", "err", err)
return 1
}
}
kern.Start(command.Header, command.LineServer, filename)
exitCode, err := command.Func(fs)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", name, err)
}
kern.Shutdown(true)
|
︙ | | |
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
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
|
+
-
+
-
+
+
+
+
+
+
-
+
+
+
+
|
fullVersion := info.revision
if info.dirty {
fullVersion += "-dirty"
}
kernel.Main.Setup(progName, fullVersion, info.time)
flag.Parse()
if *cpuprofile != "" || *memprofile != "" {
var err error
if *cpuprofile != "" {
kernel.Main.StartProfiling(kernel.ProfileCPU, *cpuprofile)
err = kernel.Main.StartProfiling(kernel.ProfileCPU, *cpuprofile)
} else {
kernel.Main.StartProfiling(kernel.ProfileHead, *memprofile)
err = kernel.Main.StartProfiling(kernel.ProfileHead, *memprofile)
}
if err != nil {
kernel.Main.GetKernelLogger().Error("start profiling", "err", err)
return 1
}
defer func() {
defer kernel.Main.StopProfiling()
if err = kernel.Main.StopProfiling(); err != nil {
kernel.Main.GetKernelLogger().Error("stop profiling", "err", err)
}
}()
}
args := flag.Args()
if len(args) == 0 {
return runSimple()
}
return executeCommand(args[0], args[1:]...)
}
|
︙ | | |
Changes to docs/manual/00001002000000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
-
+
|
id: 00001002000000
title: Design goals for the Zettelstore
role: manual
tags: #design #goal #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102191434
modified: 20250602181324
Zettelstore supports the following design goals:
; Longevity of stored notes / zettel
: Every zettel you create should be readable without the help of any tool, even without Zettelstore.
: It should not hard to write other software that works with your zettel.
: It should not be hard to write other software that works with your zettel.
: Normal zettel should be stored in a single file.
If this is not possible: at most in two files: one for the metadata, one for the content.
The only exceptions are [[predefined zettel|00001005090000]] stored in the Zettelstore executable.
: There is no additional database.
; Single user
: All zettel belong to you, only to you.
Zettelstore provides its services only to one person: you.
|
︙ | | |
35
36
37
38
39
40
41
42
43
|
35
36
37
38
39
40
41
42
43
|
-
+
|
; Simple service
: The purpose of Zettelstore is to safely store your zettel and to provide some initial relations between them.
: External software can be written to deeply analyze your zettel and the structures they form.
; Security by default
: Without any customization, Zettelstore provides its services in a safe and secure manner and does not expose you (or other users) to security risks.
: If you know what you are doing, Zettelstore allows you to relax some security-related preferences.
However, even in this case, the more secure way is chosen.
: The Zettelstore software uses a minimal design and uses other software dependencies only is essential needed.
: Zettelstore features a minimal design and relies on external software only when absolutely necessary.
: There will be no plugin mechanism, which allows external software to control the inner workings of the Zettelstore software.
|
Changes to docs/manual/00001003300000.zettel.
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
|
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
|
-
+
-
+
|
id: 00001003300000
title: Zettelstore installation for the intermediate user
role: manual
tags: #installation #manual #zettelstore
syntax: zmk
created: 20211125191727
modified: 20250227220050
modified: 20250627152419
You have already tried the Zettelstore software and now you want to use it permanently.
Zettelstore should start automatically when you log into your computer.
* Grab the appropriate executable and copy it into the appropriate directory
* If you want to place your zettel into another directory, or if you want more than one [[Zettelstore box|00001004011200]], or if you want to [[enable authentication|00001010040100]], or if you want to tweak your Zettelstore in some other way, create an appropriate [[startup configuration file|00001004010000]].
* If you created a startup configuration file, you need to test it:
** Start a command line prompt for your operating system.
** Navigate to the directory, where you placed the Zettelstore executable.
In most cases, this is done by the command ``cd DIR``, where ''DIR'' denotes the directory, where you placed the executable.
** Start the Zettelstore:
*** On Windows execute the command ``zettelstore.exe run -c CONFIG_FILE``
*** On macOS execute the command ``./zettelstore run -c CONFIG_FILE``
*** On Linux execute the command ``./zettelstore run -c CONFIG_FILE``
** In all cases ''CONFIG_FILE'' must be replaced with the file name where you wrote the startup configuration.
** In all cases, ''CONFIG_FILE'' must be replaced with the name of the file where you wrote the startup configuration.
** If you encounter some error messages, update the startup configuration, and try again.
* Depending on your operating system, there are different ways to register Zettelstore to start automatically:
** [[Windows|00001003305000]]
** [[macOS|00001003310000]]
** [[Linux|00001003315000]]
A word of caution: Never expose Zettelstore directly to the Internet.
As a personal service, Zettelstore is not designed to handle all aspects of the open web.
For instance, it lacks support for certificate handling, which is necessary for encrypted HTTP connections.
To ensure security, [[install Zettelstore on a server|00001003600000]] and place it behind a proxy server designed for Internet exposure.
For more details, see: [[External server to encrypt message transport|00001010090100]].
|
Changes to docs/manual/00001003305000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001003305000
title: Enable Zettelstore to start automatically on Windows
role: manual
tags: #installation #manual #zettelstore
syntax: zmk
created: 20211125191727
modified: 20241213103259
modified: 20250627152603
Windows is a complicated beast. There are several ways to automatically start Zettelstore.
=== Startup folder
One way is to use the [[autostart folder|https://support.microsoft.com/en-us/windows/add-an-app-to-run-automatically-at-startup-in-windows-10-150da165-dcd9-7230-517b-cf3c295d89dd]].
Open the folder where you have placed in the Explorer.
|
︙ | | |
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
-
+
|
The next time you log into your computer, Zettelstore will be started automatically.
However, it remains visible, at least in the task bar.
You can modify the behavior by changing some properties of the shortcut file.
=== Task scheduler
The Windows Task scheduler allows you to start Zettelstore as an background task.
The Windows Task scheduler allows you to start Zettelstore as a background task.
This is both an advantage and a disadvantage.
On the plus side, Zettelstore runs in the background, and it does not disturb you.
All you have to do is to open your web browser, enter the appropriate URL, and there you go.
On the negative side, you will not be notified when you enter the wrong data in the Task scheduler and Zettelstore fails to start.
|
︙ | | |
109
110
111
112
113
114
115
116
117
118
119
120
|
109
110
111
112
113
114
115
116
117
118
119
120
|
-
+
|
Under some circumstances, Windows asks for permission and you have to enter your password.
As the last step, you could run the freshly created task manually.
Open your browser, enter the appropriate URL and use your Zettelstore.
In case of errors, the task will most likely stop immediately.
Make sure that all data you have entered is valid.
To not forget to check the content of the startup configuration file.
Do not forget to check the content of the startup configuration file.
Use the command prompt to debug your configuration.
Sometimes, for example when your computer was in stand-by and it wakes up, these tasks are not started.
In this case execute the task scheduler and run the task manually.
|
Changes to docs/manual/00001003315000.zettel.
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
|
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
|
-
+
-
+
-
+
|
id: 00001003315000
title: Enable Zettelstore to start automatically on Linux
role: manual
tags: #installation #manual #zettelstore
syntax: zmk
created: 20220114181521
modified: 20250102221716
modified: 20250627153033
Since there is no such thing as the one Linux, there are too many different ways to automatically start Zettelstore.
* One way is to interpret your Linux desktop system as a server and use the [[recipe to install Zettelstore on a server|00001003600000]].
** See below for a lighter alternative.
* If you are using the [[Gnome Desktop|https://www.gnome.org/]], you could use the tool [[Tweak|https://wiki.gnome.org/action/show/Apps/Tweaks]] (formerly known as ""GNOME Tweak Tool"" or just ""Tweak Tool"").
It allows to specify application that should run on startup / login.
It allows you to specify applications that should run on startup / login.
* [[KDE|https://kde.org/]] provides a system setting to [[autostart|https://docs.kde.org/stable5/en/plasma-workspace/kcontrol/autostart/]] applications.
* [[Xfce|https://xfce.org/]] allows to specify [[autostart applications|https://docs.xfce.org/xfce/xfce4-session/preferences#application_autostart]].
* [[LXDE|https://www.lxde.org/]] uses [[LXSession Edit|https://wiki.lxde.org/en/LXSession_Edit]] to allow users to specify autostart applications.
If you use a different desktop environment, it often helps to to provide its name and the string ""autostart"" to google for it with the search engine of your choice.
If you're using a different desktop environment, try searching for its name together with the word ""autostart"".
Yet another way is to make use of the middleware that is provided.
Many Linux distributions make use of [[systemd|https://systemd.io/]], which allows to start processes on behalf of a user.
On the command line, adapt the following script to your own needs and execute it:
```
# mkdir -p "$HOME/.config/systemd/user"
# cd "$HOME/.config/systemd/user"
|
︙ | | |
Changes to docs/manual/00001004010000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001004010000
title: Zettelstore startup configuration
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102180346
modified: 20250627155145
The configuration file, specified by the ''-c CONFIGFILE'' [[command line option|00001004051000]], allows you to specify some startup options.
These cannot be stored in a [[configuration zettel|00001004020000]] because they are needed before Zettelstore can start or because of security reasons.
For example, Zettelstore needs to know in advance on which network address it must listen or where zettel are stored.
An attacker that is able to change the owner can do anything.
Therefore, only the owner of the computer on which Zettelstore runs can change this information.
|
︙ | | |
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
-
+
|
Note: [[''url-prefix''|#url-prefix]] must be the suffix of ''base-url'', otherwise the web service will not start.
Default: ""http://127.0.0.1:23123/"".
; [!box-uri-x|''box-uri-X''], where __X__ is a number greater or equal to one
: Specifies a [[box|00001004011200]] where zettel are stored.
During startup, __X__ is incremented, starting with one, until no key is found.
This allows to configuring than one box.
This allows you to configure than one box.
If no ''box-uri-1'' key is given, the overall effect will be the same as if only ''box-uri-1'' was specified with the value ""dir://.zettel"".
In this case, even a key ''box-uri-2'' will be ignored.
; [!debug-mode|''debug-mode'']
: If set to [[true|00001006030500]], allows to debug the Zettelstore software (mostly used by Zettelstore developers).
Disables any timeout values of the internal web server and does not send some security-related data.
Sets [[''log-level''|#log-level]] to ""debug"".
|
︙ | | |
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
|
Default: ""info"".
Examples: ""error"" will produce just error messages (e.g. no ""info"" messages).
""error;web:debug"" will emit debugging messages for the web component of Zettelstore while still producing error messages for all other components.
When you are familiar with operating the Zettelstore, you might set the level to ""error"" to receive fewer noisy messages from it.
; [!loopback-ident|''loopback-ident''], [!loopback-zid|''loopback-zid'']
: These keys are effective only if [[authentication is enabled|00001010000000]].
They must specify the user ID and zettel ID of a [[user zettel|00001010040200]].
When these keys are set and an HTTP request originates from the loopback device, no further authentication is required.
The loopback device typically uses the IP address ''127.0.0.1'' (IPv4) or ''::1'' (IPv6).
This configuration allows client software running on the same computer as the Zettelstore to access it through its API or web user interface.
However, this setup is not recommended if the Zettelstore is running on a computer shared with untrusted or unknown users.
Default: (empty string)/00000000000000
; [!max-request-size|''max-request-size'']
: It limits the maximum byte size of a web request body to prevent clients from accidentally or maliciously sending a large request and wasting server resources.
The minimum value is 1024.
Default: 16777216 (16 MiB).
; [!owner|''owner'']
: [[Identifier|00001006050000]] of a zettel that contains data about the owner of the Zettelstore.
|
︙ | | |
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
|
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
|
-
+
+
+
+
+
+
+
|
Therefore, an authenticated user will be logged off.
If ""true"", a persistent cookie is used.
Its lifetime exceeds the lifetime of the authentication token by 30 seconds (see option ''token-lifetime-html'').
Default: ""false""
; [!read-only-mode|''read-only-mode'']
: If set to a [[true value|00001006030500]] the Zettelstore service puts into a read-only mode.
: If set to a [[true value|00001006030500]] the Zettelstore service will beputs into a read-only mode.
No changes are possible.
Default: ""false"".
; [!runtime-profiling|''runtime-profiling'']
: A boolean value that enables a web interface to obtain [[runtime profiling information|00001004010200]].
Default: ""false"", but it is set to ""true"" if [[''debug-mode''|#debug-mode]] is enabled.
In this case, it cannot be disabled.
; [!secret|''secret'']
: A string value to make the communication with external clients strong enough so that sessions of the [[web user interface|00001014000000]] or [[API access token|00001010040700]] cannot be altered by some external unfriendly party.
The string must have a length of at least 16 bytes.
This value is only needed to be set if [[authentication is enabled|00001010040100]] by setting the key [[''owner''|#owner]] to some user identification value.
; [!sx-max-nesting|''sx-max-nesting'']
: Limits nesting of template evaluation to the given value.
This is used to prevent the application from crashing when an error occurs in a template zettel of the Web User Interface.
Default: ""32768""
; [!token-lifetime-api|''token-lifetime-api''], [!token-lifetime-html|''token-lifetime-html'']
: Define lifetime of access tokens in minutes.
Values are only valid if authentication is enabled, i.e. key ''owner'' is set.
''token-lifetime-api'' is for accessing Zettelstore via its [[API|00001012000000]].
Default: ""10"".
|
︙ | | |
Changes to docs/manual/00001004011200.zettel.
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
|
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
|
-
+
-
+
-
-
-
+
+
+
|
id: 00001004011200
title: Zettelstore boxes
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102185551
modified: 20250627162212
A Zettelstore must store its zettel somehow and somewhere.
In most cases you want to store your zettel as files in a directory.
Under certain circumstances you may want to store your zettel elsewhere.
An example is the [[predefined zettel|00001005090000]] that come with a Zettelstore.
They are stored within the software itself.
In another situation you may want to store your zettel volatile, e.g. if you want to provide a sandbox for experimenting.
To cope with these (and more) situations, you configure Zettelstore to use one or more __boxes__.
This is done via the ''box-uri-X'' keys of the [[startup configuration|00001004010000#box-uri-X]] (X is a number).
Boxes are specified using special [[URIs|https://en.wikipedia.org/wiki/Uniform_Resource_Identifier]], somehow similar to web addresses.
The following box URIs are supported:
; [!dir|''dir://DIR'']
: Specifies a directory where zettel files are stored.
''DIR'' is the file path.
Although it is possible to use relative file paths, such as ''./zettel'' (→ URI is ''dir://.zettel''), it is preferable to use absolute file paths, e.g. ''/home/user/zettel''.
The directory must exist before starting the Zettelstore[^There is one exception: when Zettelstore is [[started without any parameter|00001004050000]], e.g. via double-clicking its icon, an directory called ''./zettel'' will be created.].
The directory must exist before starting the Zettelstore[^There is one exception: when Zettelstore is [[started without any parameter|00001004050000]], e.g. via double-clicking its icon, a directory called ''./zettel'' will be created.].
It is possible to [[configure|00001004011400]] a directory box.
; [!file|''file:FILE.zip'' or ''file:///path/to/file.zip'']
: Specifies a ZIP file which contains files that store zettel.
You can create such a ZIP file, if you zip a directory full of zettel files.
This box is always read-only.
; [!mem|''mem:'']
: Stores all its zettel in volatile memory.
If you stop the Zettelstore, all changes are lost.
To limit usage of volatile memory, you should [[configure|00001004011600]] this type of box, although the default values might be valid for your use case.
All boxes that you configure via the ''box-uri-X'' keys form a chain of boxes.
If a zettel should be retrieved, a search starts in the box specified with the ''box-uri-2'' key, then ''box-uri-3'' and so on.
If a zettel is created or changed, it is always stored in the box specified with the ''box-uri-1'' key.
This allows to overwrite zettel from other boxes, e.g. the predefined zettel.
If you use the ''mem:'' box, where zettel are stored in volatile memory, it makes only sense if you configure it as ''box-uri-1''.
Such a box will be empty when Zettelstore starts and only the first box will receive updates.
You must make sure that your computer has enough RAM to store all zettel.
If you use the ''mem:'' box, where zettel are stored in volatile memory, it only makes sense if you configure it as ''box-uri-1''.
Such a box will be empty when Zettelstore starts, and only the first box will receive updates.
You must ensure that your computer has enough RAM to store all zettel.
|
Changes to docs/manual/00001004011400.zettel.
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
|
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
|
-
+
-
+
-
-
-
-
-
+
+
+
+
+
|
id: 00001004011400
title: Configure file directory boxes
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102180416
modified: 20250627155540
Under certain circumstances, it is preferable to further configure a file directory box.
This is done by appending query parameters after the base box URI ''dir:\//DIR''.
The following parameters are supported:
|= Parameter:|Description|Default value:|
|type|(Sub-) Type of the directory service|(value of ""[[default-dir-box-type|00001004010000#default-dir-box-type]]"")
|worker|Number of workers that can access the directory in parallel|7
|readonly|Allow only operations that do not create or change zettel|n/a
=== Type
On some operating systems, Zettelstore tries to detect changes to zettel files outside of Zettelstore's control[^This includes Linux, Windows, and macOS.].
On other operating systems, this may be not possible, due to technical limitations.
On other operating systems, this may not be possible due to technical limitations.
Automatic detection of external changes is also not possible, if zettel files are put on an external service, such as a file server accessed via SMB/CIFS or NFS.
To cope with this uncertainty, Zettelstore provides various internal implementations of a directory box.
The default values should match the needs of different users, as explained in the [[installation part|00001003000000]] of this manual.
The following values are supported:
; simple
: Is not able to detect external changes.
Works on all platforms.
Is a little slower than other implementations (up to three times).
; notify
: Automatically detect external changes.
Tries to optimize performance, at a little cost of main memory (RAM).
=== Worker
Internally, Zettelstore parallels concurrent requests for a zettel or its metadata.
The number of parallel activities is configured by the ''worker'' parameter.
A computer contains a limited number of internal processing units (CPU).
Its number ranges from 1 to (currently) 128, e.g. in bigger server environments.
Zettelstore typically runs on a system with 1 to 8 CPUs.
Access to zettel file is ultimately managed by the underlying operating system.
Depending on the hardware and on the type of the directory box, only a limited number of parallel accesses are desirable.
A computer contains a limited number of internal processing units (CPUs).
Their number ranges from 1 up to (currently) 128, for example in larger server environments.
Zettelstore typically runs on systems with 1 to 8 CPUs.
Access to zettel files is ultimately managed by the underlying operating system.
Depending on the hardware and the type of directory box, only a limited number of parallel accesses is desirable.
On smaller hardware[^In comparison to a normal desktop or laptop computer], such as the [[Raspberry Zero|https://www.raspberrypi.org/products/raspberry-pi-zero/]], a smaller value might be appropriate.
Every worker needs some amount of main memory (RAM) and some amount of processing power.
On bigger hardware, with some fast file services, a bigger value could result in higher performance, if needed.
For various reasons, the value should be a prime number.
The software might enforce this restriction by selecting the next prime number of a specified non-prime value.
|
︙ | | |
Changes to docs/manual/00001004011600.zettel.
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
|
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
|
-
+
-
+
-
+
-
-
+
+
-
-
+
|
id: 00001004011600
title: Configure memory boxes
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20220307112918
modified: 20250102222236
modified: 20250627155851
Under most circumstances, it is preferable to further configure a memory box.
This is done by appending query parameters after the base box URI ''mem:''.
The following parameters are supported:
|= Parameter:|Description|Default value:|Maximum value:
|max-bytes|Maximum number of bytes the box will store|65535|1073741824 (1 GiB)
|max-zettel|Maximum number of zettel|127|65535
The default values are somehow arbitrarily, but applicable for many use cases.
The default values are somewhat arbitrary but applicable to many use cases.
While the number of zettel should be easily calculable by a user, the number of bytes might be a little more difficult.
While the number of zettel should be easily calculable by a user, the number of bytes might be a bit more difficult to determine.
Metadata consumes 6 bytes for the zettel identifier and for each metadata value one byte for the separator, plus the length of key and data.
Then size of the content is its size in bytes.
Metadata consumes 6 bytes for the zettel identifier, plus one byte for the separator for each metadata value, in addition to the length of the key and data.
The size of the content is its size in bytes. For text content, this corresponds to the number of bytes in its UTF-8 encoding.
For text content, its the number of bytes for its UTF-8 encoding.
If one of the limits are exceeded, Zettelstore will give an error indication, based on the HTTP status code 507.
If one of the limits is exceeded, Zettelstore will return an error indicated by the HTTP status code 507.
|
Changes to docs/manual/00001004020000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
-
+
|
id: 00001004020000
title: Configure a running Zettelstore
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250131151530
modified: 20250627160124
show-back-links: false
You can configure a running Zettelstore by modifying the special zettel with the ID [[00000000000100]].
This zettel is called __configuration zettel__.
The following metadata keys change the appearance / behavior of Zettelstore.
Some of them can be overwritten in an [[user zettel|00001010040200]], a subset of those may be overwritten in zettel that is currently used.
Some of them can be overwritten in a [[user zettel|00001010040200]], a subset of those may be overwritten in the zettel that is currently used.
See the full list of [[metadata that may be overwritten|00001004020200]].
; [!default-copyright|''default-copyright'']
: Copyright value to be used when rendering content.
Can be overwritten in a zettel with [[meta key|00001006020000]] ''copyright''.
Default: (the empty string).
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
Default: ""00000000080001"".
; [!max-transclusions|''max-transclusions'']
: Maximum number of indirect transclusion.
This is used to avoid an exploding ""transclusion bomb"", a form of a [[billion laughs attack|https://en.wikipedia.org/wiki/Billion_laughs_attack]].
Default: ""1024"".
; [!show-back-links|''show-back-links''], [!show-folge-links|''show-folge-links''], [!show-sequel-links|''show-sequel-links''], [!show-subordinate-links|''show-subordinate-links''], [!show-successor-links|''show-successor-links'']
; [!show-back-links|''show-back-links''], [!show-folge-links|''show-folge-links''], [!show-sequel-links|''show-sequel-links''], [!show-subordinate-links|''show-subordinate-links'']
: When displaying a zettel in the web user interface, references to other zettel are normally shown below the content of the zettel.
This affects the metadata keys [[''back''|00001006020000#back]], [[''folge''|00001006020000#folge]], [[''sequel''|00001006020000#sequel]], [[''subordinates''|00001006020000#subordinates]], and [[''successors''|00001006020000#successors]].
This affects the metadata keys [[''back''|00001006020000#back]], [[''folge''|00001006020000#folge]], [[''sequel''|00001006020000#sequel]], and [[''subordinates''|00001006020000#subordinates]].
These configuration keys may be used to show, not to show, or to close the list of referenced zettel.
Allowed values are: ""false"" (will not show the list), ""close"" (will show the list closed), and ""open"" / """" (will show the list).
Default: """".
May be [[overwritten|00001004020200]] in a user zettel, so that setting will only affect the given user.
Alternatively, it may be overwritten in a zettel, so that that the setting will affect only the given zettel.
Alternatively, it may be overwritten in a zettel, so that the setting will affect only the given zettel.
This zettel is an example of a zettel that sets ''show-back-links'' to ""false"".
; [!site-name|''site-name'']
: Name of the Zettelstore instance.
Will be used when displaying some lists.
Default: ""Zettelstore"".
|
︙ | | |
Changes to docs/manual/00001004020200.zettel.
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
|
-
+
-
|
id: 00001004020200
title: Runtime configuration data that may be user specific or zettel specific
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
created: 20221205155521
modified: 20250131151259
modified: 20250626114354
Some metadata of the [[runtime configuration|00001004020000]] may be overwritten in an [[user zettel|00001010040200]].
A subset of those may be overwritten in the zettel that is currently used.
This allows to specify user specific or zettel specific behavior.
The following metadata keys are supported to provide a more specific behavior:
|=Key|User:|Zettel:|Remarks
|[[''footer-zettel''|00001004020000#footer-zettel]]|Y|N|
|[[''home-zettel''|00001004020000#home-zettel]]|Y|N|
|[[''lang''|00001004020000#lang]]|Y|Y|Making it user-specific could make zettel for other user less useful
|[[''lists-menu-zettel''|00001004020000#lists-menu-zettel]]|Y|N|
|[[''show-back-links''|00001004020000#show-back-links]]|Y|Y|
|[[''show-folge-links''|00001004020000#show-folge-links]]|Y|Y|
|[[''show-sequel-links''|00001004020000#show-sequel-links]]|Y|Y|
|[[''show-subordinate-links''|00001004020000#show-subordinate-links]]|Y|Y|
|[[''show-successor-links''|00001004020000#show-successor-links]]|Y|Y|
|
Changes to docs/manual/00001004050000.zettel.
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
|
-
+
-
+
|
id: 00001004050000
title: Command line parameters
role: manual
tags: #command #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102174436
modified: 20250627160307
Zettelstore is not just a service that provides services of a zettelkasten.
It allows some tasks to be executed at the command line.
Typically, the task (""sub-command"") will be given at the command line as the first parameter.
If no parameter is given, the Zettelstore is called as
```
zettelstore
```
This is equivalent to call it this way:
This is equivalent to calling it this way:
```sh
mkdir -p ./zettel
zettelstore run -d ./zettel -c ./.zscfg
```
Typically this is done by starting Zettelstore via a graphical user interface by double-clicking its file icon.
=== Sub-commands
* [[``zettelstore help``|00001004050200]] lists all available sub-commands.
|
︙ | | |
Changes to docs/manual/00001004050400.zettel.
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
|
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
|
-
+
-
-
+
+
-
+
-
+
|
id: 00001004050400
title: The ''version'' sub-command
role: manual
tags: #command #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20211124182041
modified: 20250627160522
Emits some information about the Zettelstore's version.
This allows you to check, whether your installed Zettelstore is
Emits some information about the Zettelstore version.
This allows you to check whether your installed Zettelstore is up to date.
The name of the software (""Zettelstore"") and the build version information is given, as well as the compiler version, and an indication about the operating system and the processor architecture of that computer.
The build version information is a string like ''1.0.2+351ae138b4''.
The part ""1.0.2"" is the release version.
""+351ae138b4"" is a code uniquely identifying the version to the developer.
Everything after the release version is optional, eg. ""1.4.3"" is a valid build version information too.
Everything after the release version is optional, e.g. ""1.4.3"" is a valid build version information also.
Example:
```
# zettelstore version
Zettelstore 1.0.2+351ae138b4 (go1.16.5@linux/amd64)
Licensed under the latest version of the EUPL (European Union Public License)
```
In this example, Zettelstore is running in the released version ""1.0.2"" and was compiled using [[Go, version 1.16.5|https://golang.org/doc/go1.16]].
The software was build for running under a Linux operating system with an ""amd64"" processor.
The software was built for running under a Linux operating system with an ""amd64"" processor.
The build version is also stored in the public, [[predefined|00001005090000]] zettel titled ""[[Zettelstore Version|00000000000001]]"".
However, to access this zettel, you need a [[running zettelstore|00001004051000]].
|
Changes to docs/manual/00001004051000.zettel.
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
|
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
|
-
+
-
+
|
id: 00001004051000
title: The ''run'' sub-command
role: manual
tags: #command #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20220724162050
modified: 20250627160733
=== ``zettelstore run``
This starts the web service.
```
zettelstore run [-a PORT] [-c CONFIGFILE] [-d DIR] [-debug] [-p PORT] [-r] [-v]
```
; [!a|''-a PORT'']
: Specifies the TCP port through which you can reach the [[administrator console|00001004100000]].
See the explanation of [[''admin-port''|00001004010000#admin-port]] for more details.
; [!c|''-c CONFIGFILE'']
: Specifies ''CONFIGFILE'' as a file, where [[startup configuration data|00001004010000]] is read.
It is ignored, when the given file is not available, nor readable.
It is ignored if the given file is not available or not readable.
Default: tries to read the following files in the ""current directory"": ''zettelstore.cfg'', ''zsconfig.txt'', ''zscfg.txt'', ''_zscfg'', and ''.zscfg''.
; [!d|''-d DIR'']
: Specifies ''DIR'' as the directory that contains all zettel.
Default is ''./zettel'' (''.\\zettel'' on Windows), where ''.'' denotes the ""current directory"".
; [!debug|''-debug'']
|
︙ | | |
Changes to docs/manual/00001004051100.zettel.
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
|
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
|
-
+
-
+
-
+
-
-
-
-
+
+
+
|
id: 00001004051100
title: The ''run-simple'' sub-command
role: manual
tags: #command #configuration #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102221633
modified: 20250627161010
=== ``zettelstore run-simple``
This sub-command is implicitly called, when a user starts Zettelstore by double-clicking on its GUI icon.
This sub-command is implicitly called when a user starts Zettelstore by double-clicking its GUI icon.
It is a simplified variant of the [[''run'' sub-command|00001004051000]].
First, this sub-command checks if it can read a [[Zettelstore startup configuration|00001004010000]] file by trying the [[default values|00001004051000#c]].
If this is the case, ''run-simple'' just continues as the [[''run'' sub-command|00001004051000]], but ignores any command line options (including ''-d DIR'').[^This allows a [[curious user|00001003000000]] to become an intermediate user.]
If no startup configuration is found, the sub-command only allows specifying a zettel directory.
If no startup configuration was found, the sub-command allows only to specify a zettel directory.
The directory will be created automatically, if it does not exist.
This is a difference to the ''run'' sub-command, where the directory must exist.
In contrast to the ''run'' sub-command, other command line parameter are not allowed.
The directory will be created automatically if it does not exist.
This differs from the ''run'' sub-command, where the directory must already exist.
Unlike the ''run'' sub-command, no other command-line parameters are allowed.
```
zettelstore run-simple [-d DIR]
```
; [!d|''-d DIR'']
: Specifies ''DIR'' as the directory that contains all zettel.
|
︙ | | |
Changes to docs/manual/00001004059900.zettel.
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
|
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
|
-
+
-
+
|
id: 00001004059900
title: Command line flags for profiling the application
role: manual
tags: #command #configuration #manual #zettelstore
syntax: zmk
created: 20211122170506
modified: 20211122174951
modified: 20250627161347
If you want to measure potential bottlenecks within the software Zettelstore,
there are two [[command line|00001004050000]] flags for enabling the measurement (also called __profiling__):
; ''-cpuprofile FILE''
: Enables CPU profiling.
''FILE'' must be the name of the file where the data is stored.
; ''-memprofile FILE''
: Enables memory profiling.
''FILE'' must be the name of the file where the data is stored.
Normally, profiling will stop when you stop the software Zettelstore.
The given ''FILE'' can be used to analyze the data via the tool ``go tool pprof FILE``.
Please notice that ''-cpuprofile'' takes precedence over ''-memprofile''.
You cannot measure both.
You also can use the [[administrator console|00001004100000]] to begin and end profiling manually for a already running Zettelstore.
You also can use the [[administrator console|00001004100000]] to start and stop profiling manually for an already running Zettelstore.
|
Changes to docs/manual/00001005000000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001005000000
title: Structure of Zettelstore
role: manual
tags: #design #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102191502
modified: 20250627161222
Zettelstore is a software that manages your zettel.
Since every zettel must be readable without any special tool, most zettel have to be stored as ordinary files within specific directories.
Typically, file names and file content must comply with specific rules so that Zettelstore can manage them.
If you add, delete, or change zettel files with other tools, e.g. a text editor, Zettelstore will monitor these actions.
Zettelstore provides additional services to the user.
|
︙ | | |
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
|
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
|
-
+
-
+
|
The directory has to be specified at [[startup time|00001004010000]].
Nested directories are not supported (yet).
Every file in this directory that should be monitored by Zettelstore must have a file name that begins with 14 digits (0-9), the [[zettel identifier|00001006050000]].
If you create a new zettel via the [[web user interface|00001014000000]] or via the [[API|00001012053200]], the zettel identifier will be the timestamp of the current date and time (format is ''YYYYMMDDhhmmss'').
This allows zettel to be sorted naturally by creation time.
Since the only restriction on zettel identifiers are the 14 digits, you are free to use other digit sequences.
Since the only restriction on zettel identifiers is that they consist of 14 digits, you are free to use other digit sequences.
The [[configuration zettel|00001004020000]] is one prominent example, as well as these manual zettel.
You can create these special zettel by manually renaming the underlying zettel files.
It is allowed that the file name contains other characters after the 14 digits.
These are ignored by Zettelstore.
Two filename extensions are used by Zettelstore:
# ''.zettel'' is a format that stores metadata and content together in one file,
# the empty file extension is used, when the content must be stored in its own file, e.g. image data;
in this case, the filename contains just the 14 digits of the zettel identifier, and optional characters except the period ''"."''.
Other filename extensions are used to determine the ""syntax"" of a zettel.
This allows to use other content within the Zettelstore, e.g. images or HTML templates.
For example, you want to store an important figure in the Zettelstore that is encoded as a ''.png'' file.
Since each zettel contains some metadata, e.g. the title of the figure, the question arises where these data should be stored.
The solution is a meta-file with the same zettel identifier, but without a filename extension.
Zettelstore recognizes this situation and reads in both files for the one zettel containing the figure.
It maintains this relationship as long as these files exist.
In case of some textual zettel content you do not want to store the metadata and the zettel content in two different files.
In the case of some textual zettel content, you may not want to store the metadata and the zettel content in two separate files.
Here the ''.zettel'' extension will signal that the metadata and the zettel content will be stored in the same file, separated by an empty line or a line with three dashes (""''-\-\-''"", also known as ""YAML separator"").
=== Predefined zettel
Zettelstore contains some [[predefined zettel|00001005090000]] to work properly.
The [[configuration zettel|00001004020000]] is one example.
To render the built-in [[web user interface|00001014000000]], some templates are used, as well as a [[layout specification in CSS|00000000020001]].
|
︙ | | |
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
79
80
81
82
83
84
85
86
87
88
89
|
-
-
-
-
+
+
+
+
-
-
-
|
* [[List of predefined zettel|00001005090000]]
=== Boxes: alternative ways to store zettel
As described above, a zettel may be stored either as a file inside a directory or within the Zettelstore software itself.
Zettelstore allows other ways to store zettel by providing an abstraction called __box__.[^Formerly, zettel were stored physically in boxes, often made of wood.]
A file directory which stores zettel is called a ""directory box"".
But zettel may be also stored in a ZIP file, which is called ""file box"".
For testing purposes, zettel may be stored in volatile memory (called __RAM__).
This way is called ""memory box"".
A file directory that stores zettel is called a ""directory box"".
Alternatively, zettel can also be stored in a ZIP file, known as a ""file box"".
For testing purposes, zettel can be stored in volatile memory (called __RAM__).
This method is referred to as a ""memory box"".
Other types of boxes could be added to Zettelstore.
What about a ""remote Zettelstore box""?
|
Changes to docs/manual/00001005090000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001005090000
title: List of predefined zettel
role: manual
tags: #manual #reference #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250416180103
modified: 20250627163000
The following table lists all predefined zettel with their purpose.
The content of most[^To be more exact: zettel with an identifier greater or equal ''00000999999900'' will have their content indexed.] of these zettel will not be indexed by Zettelstore.
You will not find zettel when searched for some content, e.g. ""[[query:european]]"" will not find the [[Zettelstore License|00000000000004]].
However, metadata is always indexed, e.g. ""[[query:title:license]]"" will find the Zettelstore License zettel.
|
︙ | | |
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
|
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
|
-
+
-
+
|
| [[00000000000009]] | Zettelstore Log | Lists the last 8192 log messages
| [[00000000000010]] | Zettelstore Memory | Some statistics about main memory usage
| [[00000000000011]] | Zettelstore Sx Engine | Statistics about the [[Sx|https://t73f.de/r/sx]] engine, which interprets symbolic expressions
| [[00000000000020]] | Zettelstore Box Manager | Contains some statistics about zettel boxes and the index process
| [[00000000000090]] | Zettelstore Supported Metadata Keys | Contains all supported metadata keys, their [[types|00001006030000]], and more
| [[00000000000092]] | Zettelstore Supported Parser | Lists all supported values for metadata [[syntax|00001006020000#syntax]] that are recognized by Zettelstore
| [[00000000000096]] | Zettelstore Startup Configuration | Contains the effective values of the [[startup configuration|00001004010000]]
| [[00000000000100]] | Zettelstore Runtime Configuration | Allows to [[configure Zettelstore at runtime|00001004020000]]
| [[00000000000100]] | Zettelstore Runtime Configuration | Allows you to [[configure Zettelstore at runtime|00001004020000]]
| [[00000000010100]] | Zettelstore Base HTML Template | Contains the general layout of the HTML view
| [[00000000010200]] | Zettelstore Login Form HTML Template | Layout of the login form, when authentication is [[enabled|00001010040100]]
| [[00000000010300]] | Zettelstore List Zettel HTML Template | Used when displaying a list of zettel
| [[00000000010401]] | Zettelstore Detail HTML Template | Layout for the HTML detail view of one zettel
| [[00000000010402]] | Zettelstore Info HTML Template | Layout for the information view of a specific zettel
| [[00000000010403]] | Zettelstore Form HTML Template | Form that is used to create a new or to change an existing zettel that contains text
| [[00000000010405]] | Zettelstore Delete HTML Template | View to confirm the deletion of a zettel
| [[00000000010700]] | Zettelstore Error HTML Template | View to show an error message
| [[00000000019000]] | Zettelstore Sxn Start Code | Starting point of sxn functions to build the templates
| [[00000000019990]] | Zettelstore Sxn Base Code | Base sxn functions to build the templates
| [[00000000020001]] | Zettelstore Base CSS | System-defined CSS file that is included by the [[Base HTML Template|00000000010100]]
| [[00000000025001]] | Zettelstore User CSS | User-defined CSS file that is included by the [[Base HTML Template|00000000010100]]
| [[00000000040001]] | Generic Emoji | Image that is shown if [[original image reference|00001007040322]] is invalid
| [[00000000060010]] | zettel | [[Role zettel|00001012051800]] for the role ""[[zettel|00001006020100#zettel]]""
| [[00000000060020]] | configuration | [[Role zettel|00001012051800]] for the role ""[[confguration|00001006020100#configuration]]""
| [[00000000060020]] | configuration | [[Role zettel|00001012051800]] for the role ""[[configuration|00001006020100#configuration]]""
| [[00000000060030]] | role | [[Role zettel|00001012051800]] for the role ""[[role|00001006020100#role]]""
| [[00000000060040]] | tag | [[Role zettel|00001012051800]] for the role ""[[tag|00001006020100#tag]]""
| [[00000000080001]] | Lists Menu | Default items of the ""Lists"" menu; see [[lists-menu-zettel|00001004020000#lists-menu-zettel]] for customization options
| [[00000000090000]] | New Menu | Contains items that should be in the zettel template menu
| [[00000000090001]] | New Zettel | Template for a new zettel with role ""[[zettel|00001006020100#zettel]]""
| [[00000000090002]] | New User | Template for a new [[user zettel|00001010040200]]
| [[00000000090003]] | New Tag | Template for a new [[tag zettel|00001006020100#tag]]
|
︙ | | |
Changes to docs/manual/00001006020000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001006020000
title: Supported Metadata Keys
role: manual
tags: #manual #meta #reference #zettel #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250115163835
modified: 20250626114159
Although you are free to define your own metadata, by using any key (according to the [[syntax|00001006010000]]), some keys have a special meaning that is enforced by Zettelstore.
See the [[computed list of supported metadata keys|00000000000090]] for details.
Most keys conform to a [[type|00001006030000]].
; [!author|''author'']
|
︙ | | |
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
+
-
+
+
|
: A user-entered time stamp that document the point in time when the zettel should expire.
When a zettel expires, Zettelstore does nothing.
It is up to you to define required actions.
''expire'' is just a documentation.
You could define a query and execute it regularly, for example [[query:expire? ORDER expire]].
Alternatively, a Zettelstore client software could define some actions when it detects expired zettel.
; [!folge|''folge'']
: Is a property that contains identifiers of all zettel that reference this zettel via the [[''precursor''|#precursor]] value.
: Is a property that contains identifier of all zettel that reference this zettel through the [[''precursor''|#precursor]] value.
It is used to reference __folge zettel__, those that develop the idea of the current zettel a bit further, following a ""train of thought.""
; [!folge-role|''folge-role'']
: Specifies a suggested [[''role''|#role]] the zettel should use in the future, if zettel currently has a preliminary role.
; [!forward|''forward'']
: Property that contains all references that identify another zettel within the content of the zettel.
; [!id|''id'']
: Contains the [[zettel identifier|00001006050000]], as given by the Zettelstore.
It cannot be set manually, because it is a computed value.
|
︙ | | |
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
-
-
-
-
-
-
+
+
+
|
If you edit a zettel with an editor software outside Zettelstore, you should set it manually to an appropriate value.
This is a computed value.
There is no need to set it via Zettelstore.
; [!precursor|''precursor'']
: References zettel for which this zettel is a ""Folgezettel"" / follow-up zettel.
Basically the inverse of key [[''folge''|#folge]].
; [!predecessor|''predecessor'']
: References the zettel that contains a previous version of the content.
In contrast to [[''precursor''|#precurso]] / [[''folge''|#folge]], this is a reference because of technical reasons, not because of content-related reasons.
Basically the inverse of key [[''successors''|#successors]].
; [!prequel|''prequel'']
: Specifies a zettel that is conceptually a prequel zettel.
This is a zettel that occurred somehow before the current zettel.
: Specifies one or more zettel that are conceptually a prequel zettel.
These are zettel that occurred somehow before the current zettel.
Basically the inverse of key [[''sequel''|#sequel]].
; [!published|''published'']
: This property contains the timestamp of the last modification / creation of the zettel.
If [[''modified''|#modified]] is set with a valid timestamp, it contains the its value.
Otherwise, if [[''created''|#created]] is set with a valid timestamp, it contains the its value.
Otherwise, if the zettel identifier contains a valid timestamp, the identifier is used.
In all other cases, this property is not set.
|
︙ | | |
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
+
+
-
-
-
-
-
-
|
; [!role|''role'']
: Defines the role of the zettel.
Can be used for selecting zettel.
See [[supported zettel roles|00001006020100]].
If not given, it is ignored.
; [!sequel|''sequel'']
: Is a property that contains identifier of all zettel that reference this zettel through the [[''prequel''|#prequel]] value.
A __sequel zettel__ acts as a branching thought of the current zettel.
; [!subordinates|''subordinates'']
: Is a property that contains identifier of all zettel that reference this zettel through the [[''superior''|#superior]] value.
; [!successors|''successors'']
: Is a property that contains identifier of all zettel that reference this zettel through the [[''predecessor''|#predecessor]] value.
Therefore, it references all zettel that contain a new version of the content and/or metadata.
In contrast to [[''folge''|#folge]], these are references because of technical reasons, not because of content-related reasons.
In most cases, zettel referencing the current zettel should be updated to reference a successor zettel.
The [[query reference|00001007040310]] [[query:backward? successors?]] lists all such zettel.
; [!summary|''summary'']
: Summarizes the content of the zettel using plain text.
; [!superior|''superior'']
: Specifies a zettel that is conceptually a superior zettel.
This might be a more abstract zettel, or a zettel that should be higher in a hierarchy.
; [!syntax|''syntax'']
: Specifies the syntax that should be used for interpreting the zettel.
|
︙ | | |
Changes to docs/manual/00001006020400.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001006020400
title: Supported values for metadata key ''read-only''
role: manual
tags: #manual #meta #reference #zettel #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102205707
modified: 20250602181627
A zettel can be marked as read-only, if it contains a metadata value for key
[[''read-only''|00001006020000#read-only]].
If user authentication is [[enabled|00001010040100]], it is possible to allow some users to change the zettel, depending on their [[user role|00001010070300]].
Otherwise, the read-only mark is just a binary value.
=== No authentication
|
︙ | | |
22
23
24
25
26
27
28
29
30
31
32
33
34
|
22
23
24
25
26
27
28
29
30
31
32
33
34
|
-
+
|
If the metadata value is the same as an explicit [[user role|00001010070300]], users with that role (or a role with lower rights) are not allowed to modify the zettel.
; ""reader""
: Neither an unauthenticated user nor a user with role ""reader"" is allowed to modify the zettel.
Users with role ""writer"" or the owner itself still can modify the zettel.
; ""writer""
: Neither an unauthenticated user, nor users with roles ""reader"" or ""writer"" are allowed to modify the zettel.
: Neither an unauthenticated user nor users with roles ""reader"" or ""writer"" are allowed to modify the zettel.
Only the owner of the Zettelstore can modify the zettel.
If the metadata value is something else (one of the values ""true"" or ""owner"" is recommended), no user is allowed to modify the zettel through the [[web user interface|00001014000000]].
However, if the zettel is accessible as a file in a [[directory box|00001004011400]], the zettel could be modified using an external editor.
Typically the owner of a Zettelstore has such access.
|
Changes to docs/manual/00001006050000.zettel.
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
|
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
|
-
+
-
+
-
+
-
+
|
id: 00001006050000
title: Zettel identifier
role: manual
tags: #design #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102165749
modified: 20250627130018
Each zettel is given a unique identifier.
To some degree, the zettel identifier is part of the metadata.
Basically, the identifier is given by the [[Zettelstore|00001005000000]] software.
Every zettel identifier consists of 14 digits.
They resemble a timestamp: the first four digits could represent the year, the next two represent the month, followed by day, hour, minute, and second.
This allows to order zettel chronologically in a canonical way.
This allows zettel to be ordered chronologically in a canonical way.
In most cases the zettel identifier is the timestamp when the zettel was created.
However, the Zettelstore software just checks for exactly 14 digits.
Anybody is free to assign a ""non-timestamp"" identifier to a zettel, e.g. with a month part of ""35"" or with ""99"" as the last two digits.
Some zettel identifier are [[reserved|00001006055000]] and should not be used otherwise.
Some zettel identifiers are [[reserved|00001006055000]] and should not be used otherwise.
All identifiers of zettel initially provided by an empty Zettelstore begin with ""000000"", except the home zettel ''00010000000000''.
Zettel identifier of this manual have been chosen to begin with ""000010"".
Zettel identifiers of this manual have been chosen to begin with ""000010"".
A zettel can have any identifier that contains 14 digits and that is not in use by another zettel managed by the same Zettelstore.
|
Changes to docs/manual/00001006055000.zettel.
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
|
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
|
-
+
-
-
+
+
-
-
+
+
+
-
+
|
id: 00001006055000
title: Reserved zettel identifier
role: manual
tags: #design #manual #zettelstore
syntax: zmk
created: 20210721105704
modified: 20250102222416
modified: 20250627152022
[[Zettel identifier|00001006050000]] are typically created by examining the current date and time.
By renaming the name of the underlying zettel file, you are able to provide any sequence of 14 digits.
[[Zettel identifiers|00001006050000]] are typically created based on the current date and time.
By renaming the underlying zettel files, you can provide any sequence of 14 digits.
To make things easier, you must not use zettel identifier that begin with four zeroes (''0000'').
All zettel provided by an empty zettelstore begin with six zeroes[^Exception: the predefined home zettel is ''00010000000000''. But you can [[configure|00001004020000#home-zettel]] another zettel with another identifier as the new home zettel.].
Zettel identifier of this manual have be chosen to begin with ''000010''.
All zettel provided by an empty zettelstore begin with six zeroes[^Exception: the predefined home zettel is ''00010000000000''.
But you can [[configure|00001004020000#home-zettel]] another zettel with another identifier as the new home zettel.].
Zettel identifiers of this manual have been chosen to begin with ''000010''.
However, some external applications may need at least one defined zettel identifier to work properly.
Zettel [[Zettelstore Application Directory|00000999999999]] (''00000999999999'') can be used to associate a name to a zettel identifier.
For example, if your application is named ""app"", you create a metadata key ''app-zid''.
Its value is the zettel identifier of the zettel that configures your application.
=== Reserved Zettel Identifier
=== Reserved Zettel Identifiers
|= From | To | Description
| 00000000000000 | 00000000000000 | This is an invalid zettel identifier
| 00000000000001 | 00000999999999 | [[Predefined zettel|00001005090000]]
| 00001000000000 | 00001099999999 | This [[Zettelstore manual|00001000000000]]
| 00001100000000 | 00008999999999 | Reserved, do not use
| 00009000000000 | 00009999999999 | Reserved for applications
|
︙ | | |
Changes to docs/manual/00001007000000.zettel.
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
|
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
|
-
+
-
+
-
+
|
id: 00001007000000
title: Zettelmarkup
role: manual
tags: #manual #zettelmarkup #zettelstore
syntax: zmk
created: 20210126175322
modified: 20241212152823
modified: 20250627161609
Zettelmarkup is a rich plain-text based markup language for writing zettel content.
Besides the zettel content, Zettelmarkup is also used for specifying the title of a zettel, regardless of the syntax of a zettel.
Zettelmarkup supports the longevity of stored notes by providing a syntax that any person can easily read, as well as a computer.
Zettelmarkup can be much easier parsed / consumed by a software compared to other markup languages.
Writing a parser for [[Markdown|https://daringfireball.net/projects/markdown/syntax]] is quite challenging.
[[CommonMark|00001008010500]] is an attempt to make it simpler by providing a comprehensive specification, combined with an extra chapter to give hints for the implementation.
Zettelmarkup follows some simple principles that anybody who knows how ho write software should be able understand to create an implementation.
Zettelmarkup follows a few simple principles that anyone familiar with software development should be able to understand and implement.
Zettelmarkup is a markup language on its own.
This is in contrast to Markdown, which is basically a super-set of HTML: every HTML document is a valid Markdown document.[^To be precise: the content of the ``<body>`` of each HTML document is a valid Markdown document.]
While HTML is a markup language that will probably last for a long time, it cannot be easily translated to other formats, such as PDF, JSON, or LaTeX.
Additionally, it is allowed to embed other languages into HTML, such as CSS or even JavaScript.
This could create problems with longevity as well as security problems.
Zettelmarkup is a rich markup language, but it focuses on relatively short zettel content.
It allows embedding other content, simple tables, quotations, description lists, and images.
It provides a broad range of inline formatting, including __emphasized__, **strong**, ~~deleted~~{-} and >>inserted>> text.
Footnotes[^like this] are supported, links to other zettel and to external material, as well as citation keys.
Zettelmarkup allows to include content from other zettel and to embed the result of a search query.
Zettelmarkup allows you to include content from other zettel and to embed the results of a search query.
Zettelmarkup might be seen as a proprietary markup language.
But if you want to use [[Markdown/CommonMark|00001008010000]] and you need support for footnotes or tables, you'll end up with proprietary extensions.
However, the Zettelstore supports CommonMark as a zettel syntax, so you can mix both Zettelmarkup zettel and CommonMark zettel in one store to get the best of both worlds.
* [[General principles|00001007010000]]
* [[Basic definitions|00001007020000]]
|
︙ | | |
Changes to docs/manual/00001007030000.zettel.
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
|
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
|
-
+
-
+
-
-
-
-
+
+
+
+
-
+
-
+
-
+
-
+
|
id: 00001007030000
title: Zettelmarkup: Block-Structured Elements
role: manual
tags: #manual #zettelmarkup #zettelstore
syntax: zmk
created: 20210126175322
modified: 20241212153023
modified: 20250627132222
Every markup for blocks-structured elements (""blocks"") begins at the very first position of a line.
There are five kinds of blocks: lists, one-line blocks, line-range blocks, tables, and paragraphs.
=== Lists
In Zettelmarkup, lists themselves are not specified, but list items.
A sequence of list items is considered as a list.
[[Description lists|00001007030100]] contain two different item types: the term to be described and the description itself.
These cannot be combined with other lists.
Ordered lists, unordered lists, and quotation lists can be combined into [[nested lists|00001007030200]].
=== One-line blocks
* [[Headings|00001007030300]] allow to structure the content of a zettel.
* [[Headings|00001007030300]] allow you to structure the content of a zettel.
* The [[horizontal rule|00001007030400]] signals a thematic break
* A [[transclusion|00001007031100]] embeds the content of one zettel into another.
=== Line-range blocks
This kind of blocks encompass at least two lines.
To be useful, they encompass more lines.
They begin with at least three identical characters at the first position of the beginning line.
They end at the line, that contains at least the same number of these identical characters, beginning at the first position of that line.
This kind of block encompasses at least two lines.
To be useful, it encompasses more lines.
It begins with at least three identical characters at the start of the first line.
It ends at the line that contains at least the same number of these identical characters, starting at the first position of that line.
This allows line-range blocks to be nested.
Additionally, all other blocks elements are allowed in line-range blocks.
Additionally, all other block elements are allowed within line-range blocks.
* [[Verbatim blocks|00001007030500]] do not interpret their content,
* [[Quotation blocks|00001007030600]] specify a block-length quotations,
* [[Verse blocks|00001007030700]] allow to enter poetry, lyrics and similar text, where line endings are important,
* [[Region blocks|00001007030800]] just mark regions, e.g. for common formatting,
* [[Comment blocks|00001007030900]] allow to enter text that will be ignored when rendered.
* [[Comment blocks|00001007030900]] allow you to enter text that will be ignored when rendered.
* [[Evaluation blocks|00001007031300]] specify some content to be evaluated by either Zettelstore or external software.
* [[Math-mode blocks|00001007031400]] can be used to enter mathematical formulas / equations.
* [[Inline-Zettel blocks|00001007031200]] provide a mechanism to specify zettel content with a new syntax without creating a new zettel.
=== Tables
Similar to lists are tables not specified explicitly.
Tables, similar to lists, are not specified explicitly.
A sequence of table rows is considered a [[table|00001007031000]].
A table row itself is a sequence of table cells.
=== Paragraphs
Any line that does not conform to another blocks-structured element begins a paragraph.
This has the implication that a mistyped syntax element for a block element will be part of the paragraph. For example:
```zmk
= Heading
Some text follows.
```
will be rendered in HTML as
:::example
= Heading
Some text follows.
:::
This is because headings need at least three equal sign character.
This is because headings require at least three equal sign characters.
A paragraph is essentially a sequence of [[inline-structured elements|00001007040000]].
Inline-structured elements can span more than one line.
Paragraphs are separated by empty lines.
If you want to specify a second paragraph inside a list item, or if you want to continue a paragraph on a second and more line within a list item, you must begin the paragraph with a certain number of space characters.
The number of space characters depends on the kind of a list and the relevant nesting level.
A line that begins with a space character and which is outside of a list or does not contain the right number of space characters is considered to be part of a paragraph.
|
Changes to docs/manual/00001007031200.zettel.
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
|
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
|
-
+
-
-
+
+
|
id: 00001007031200
title: Zettelmarkup: Inline-Zettel Block
role: manual
tags: #manual #zettelmarkup #zettelstore
syntax: zmk
created: 20220201142439
modified: 20250102183744
modified: 20250627131204
An inline-zettel block allows to specify some content with another syntax without creating a new zettel.
This is useful, for example, if you want to embed some [[Markdown|00001008010500]] content, because you are too lazy to translate Markdown into Zettelmarkup.
Another example is to specify HTML code to use it for some kind of web front-end framework.
Like all other [[line-range blocks|00001007030000#line-range-blocks]], an inline-zettel block begins with at least three identical characters, starting at the first position of a line.
For inline-zettel blocks, the at-sign character (""''@''"", U+0040) is used.
You can add some [[attributes|00001007050000]] to the beginning line of a verbatim block, following the initiating characters.
The inline-zettel block uses the attribute key ""syntax"" to specify the [[syntax|00001008000000]] of the inline-zettel.
Alternatively, you can use the generic attribute to specify the syntax value.
If no value is provided, ""[[text|00001008000000#text]]"" is assumed.
Any other character in this first line will be ignored.
Text following the beginning line will not be interpreted, until a line begins with at least the same number of the same at-sign characters given at the beginning line.
This allows to enter some at-sign characters in the text that should not be interpreted at this level.
Text following the beginning line will not be interpreted until a line starts with at least the same number of identical at-sign characters as those in the beginning line.
This allows entering at-sign characters in the text that should not be interpreted at this level.
Some examples:
```zmk
@@@markdown
A link to [this](00001007031200) zettel.
@@@
```
|
︙ | | |
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
-
-
+
+
|
:::example
@@@html
<h1>H1 Heading</h1>
Alea iacta est
@@@
:::
:::note
Please note: some HTML code will not be fully rendered because of possible security implications.
This include HTML lines that contain a ''<script>'' tag or an ''<iframe>'' tag.
Please note: some HTML code will not be fully rendered due to possible security implications.
This includes HTML lines that contain a ''<script>'' tag or an ''<iframe>'' tag.
:::
Of course, you do not need to switch the syntax and you are allowed to nest inline-zettel blocks:
```zmk
@@@@zmk
1st level inline
@@@zmk
2nd level inline
|
︙ | | |
Changes to docs/manual/00001007700000.zettel.
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
|
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
|
-
+
-
-
+
+
+
-
+
|
id: 00001007700000
title: Query Expression
role: manual
tags: #manual #search #zettelstore
syntax: zmk
created: 20220805150154
modified: 20230731161954
modified: 20250627150916
A query expression allows you to search for specific zettel and to perform some actions on them.
You may select zettel based on a list of [[zettel identifier|00001006050000]], based on a query directive, based on a full-text search, based on specific metadata values, or some or all of them.
A query expression allows you to search for specific zettel and perform actions on them.
You may select zettel based on a list of [[identifiers|00001006050000]], a query directive, a full-text search, specific metadata values, or any combination of these.
A query expression consists of an optional __[[zettel identifier list|00001007710000]]__, zero or more __[[query directives|00001007720000]]__, an optional __[[search expression|00001007701000]]__, and an optional __[[action list|00001007770000]]__.
The latter two are separated by a vertical bar character (""''|''"", U+007C).
A query expression follows a [[formal syntax|00001007780000]].
* [[List of zettel identifier|00001007710000]]
* [[Query directives|00001007720000]]
** [[Context directive|00001007720300]]
** [[Thread Directive|00001007720500]]
** [[Ident directive|00001007720600]]
** [[Items directive|00001007720900]]
** [[Unlinked directive|00001007721200]]
* [[Search expression|00001007701000]]
** [[Search term|00001007702000]]
** [[Search operator|00001007705000]]
** [[Search value|00001007706000]]
* [[Action list|00001007770000]]
Here are [[some examples|00001007790000]], which can be used to manage a Zettelstore:
Here are [[some examples|00001007790000]] thar can be used to manage a Zettelstore:
{{{00001007790000}}}
|
Changes to docs/manual/00001007720000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
+
|
id: 00001007720000
title: Query Directives
role: manual
tags: #manual #search #zettelstore
syntax: zmk
created: 20230707203135
modified: 20230731162002
modified: 20250624163147
A query directive transforms a list of zettel identifier into a list of zettel identifiert.
It is only valid if a list of zettel identifier is specified at the beginning of the query expression.
Otherwise the text of the directive is interpreted as a search expression.
For example, ''CONTEXT'' is interpreted as a full-text search for the word ""context"".
Every query directive therefore consumes a list of zettel, and it produces a list of zettel according to the specific directive.
* [[Context directive|00001007720300]]
* [[Thread Directive|00001007720500]]
* [[Ident directive|00001007720600]]
* [[Items directive|00001007720900]]
* [[Unlinked directive|00001007721200]]
|
Changes to docs/manual/00001007720300.zettel.
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
|
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
|
-
+
+
+
+
-
+
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
|
id: 00001007720300
title: Query: Context Directive
role: manual
tags: #manual #search #zettelstore
syntax: zmk
created: 20230707204706
modified: 20250202172633
modified: 20250626174255
A context directive calculates the __context__ of a list of zettel identifier.
It starts with the keyword ''CONTEXT''.
Optionally you may specify some context details, after the keyword ''CONTEXT'', separated by space characters.
These are:
* ''FULL'': additionally search for zettel with the same tags,
* ''BACKWARD'': search for context only though backward links,
* ''FORWARD'': search for context only through forward links,
* ''DIRECTED'': search for context only in the same direction as the one that led to the current zettel.
If the zettel is one of the initial zettel, search in both directions.
* ''COST'': one or more space characters, and a positive integer: set the maximum __cost__ (default: 17),
* ''MAX'': one or more space characters, and a positive integer: set the maximum number of context zettel (default: 200).
* ''MIN'': one or more space characters, and a positive integer: set the minimum number of context zettel (default: 0).
Takes precedence over ''COST'' and ''MAX''.
If neither ''BACKWARD'' nor ''FORWARD'' is specified, the search for context zettel will follow both backward and forward links.
If no ''BACKWARD'' and ''FORWARD'' is specified, a search for context zettel will be done though backward and forward links.
If both ''BACKWARD'' and ''FORWARD'' are specified, the search for context zettel will be performed as ''DIRECTED''.
Internally, ''DIRECTED'' is just a shorthand for specifying both ''BACKWARD'' and ''FORWARD''.
If any of the three direction specifiers ''BACKWARD'', ''FORWARD'', and ''DIRECTED'' is specified more than once, parsing of the thread directive is stopped.
All following text is then interpreted either as other directives or as a search term.
The cost of a context zettel is calculated iteratively:
* Each of the specified zettel hast a cost of one.
* A zettel found as a single folge zettel or single precursor zettel has the cost of the originating zettel, plus 0.1.
* A zettel found as a single sequel zettel or single prequel zettel has the cost of the originating zettel, plus 1.0.
* Each of the specified zettel has a cost of 1.0.
* Every zettel directly referenced by a specified zettel has a maximum cost of 4.0.
* A zettel found as a single folge zettel or single precursor zettel inherits the cost of the originating zettel, plus 0.2.
* A zettel found as a single sequel zettel or single prequel zettel inherits the cost of the originating zettel, plus 1.0.
* A zettel found as a single successor zettel or single predecessor zettel has the cost of the originating zettel, plus seven.
* A zettel found via another link without being part of a [[set of zettel identifier|00001006032500]], has the cost of the originating zettel, plus two.
* A zettel which is part of a set of zettel identifier, has the cost of the originating zettel, plus one of the four choices above and multiplied with roughly a linear-logarithmic value based on the size of the set.
* A zettel with the same tag, has the cost of the originating zettel, plus a linear-logarithmic number based on the number of zettel with this tag.
If a zettel belongs to more than one tag compared with the current zettel, there is a discount of 90% per additional tag.
This only applies if the ''FULL'' directive was specified.
* A zettel discovered via another type of link, without being part of a [[set of zettel identifiers|00001006032500]], inherits the cost of the originating zettel, plus 2.0.
* A zettel that is part of a set of zettel identifiers, inherits the cost of the originating zettel, plus one of the four costs above, multiplied by a value that grows roughly in a linear-logarithmic fashion based on the size of the set.
* A zettel sharing the same tag inherits the cost of the originating zettel, plus a linear-logarithmic value based on the number of zettel sharing that tag.
If a zettel shares more than one tag with the originating zettel, a 90% discount is applied per additional tag.
This rules applies only if the ''FULL'' directive has been specified.
The maximum cost is only checked for all zettel that are not directly reachable from the initial, specified list of zettel.
This ensures that initial zettel that have only a highly used tag, will also produce some context zettel.
Despite its possibly complicated structure, this algorithm ensures in practice that the zettel context is a list of zettel, where the first elements are ""near"" to the specified zettel and the last elements are more ""distant"" to the specified zettel.
It also penalties zettel that acts as a ""hub"" to other zettel, to make it more likely that only relevant zettel appear on the context list.
|
︙ | | |
Added docs/manual/00001007720500.zettel.
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
id: 00001007720500
title: Query: Thread Directive
role: manual
tags: #manual #search #zettelstore
syntax: zmk
created: 20250624162823
modified: 20250626174233
A thread directive is similar to the [[context directive|00001007720300]] by calculating zettel associated with a list of zettel identifier.
There are three thread directives, beginning with the keywords ''FOLGE'', ''SEQUEL'', and ''THREAD''.
With the help of the ''FOLGE'' directive, you can list all zettel that belong to the same ""train of thought"".
It is possible to list all folge zettel only or only precursor zettel.
Additionally, you can specify the maximum number to be listed.
A ''FOLGE'' directive considers only the metadata keys only metadata keys [[''folge''|00001006020000#folge]] and [[''precursor''|00001006020000#precursor]].
Similarly, the ''SEQUEL'' directive allows you to list all ""branching ideas""
You can limit the search to only sequel or only prequel zettel, and you can also restrict the result size.
The ''SEQUEL'' directive works exclusively with the metadata keys [[''sequel''|00001006020000#sequel]] and [[''prequel''|00001006020000#prequel]].
The ''THREAD'' directive combines the functionality of the other two directives.
It lists all zettel (or a subset) that can be reached via the four metadata keys mentioned.
Optionally yyou may specify additional parameters after the directive keyword, separated by space characters.
These include:
* ''BACKWARD'': Search for zettel only via backward links, i.e. ''precursor'' and/or ''prequel''
* ''FORWARD'': Search for zettel only via forward links, i.e. ''folge'' and/or ''sequel''
* ''DIRECTED'': Search for zettel only in the same direction as the one that led to the current zettel.
If the zettel is one of the initial zettel, search in both directions.
''DIRECTED'' is equivalent to ''FORWARD'' if the current zettel is a starting point of a ''folge'' and/or ''sequel'' sequence.
It is equivalent to ''BACKWARD'' at the end of such a sequence (or at the beginning of a ''precursor'' and/or ''prequel'' sequence).
* ''MAX'': Followed by one or more space characters and a positive integer, sets the maximum number of resulting Zettel (default: 0 = unlimited)
If neither BACKWARD nor FORWARD is specified, the search includes both backward and forward links.
If both ''BACKWARD'' and ''FORWARD'' are specified, the search for context zettel will be performed as ''DIRECTED''.
Internally, ''DIRECTED'' is just a shorthand for specifying both ''BACKWARD'' and ''FORWARD''.
If any of the three direction specifiers ''BACKWARD'', ''FORWARD'', and ''DIRECTED'' is specified more than once, parsing of the thread directive is stopped.
All following text is then interpreted either as other directives or as a search term.
The resulting list is sorted by the relative distance to the originating zettel.
These directives may be specified only once as a query directive.
A second occurence of ''FOLGE'', ''SEQUEL'' or ''THREAD'' is interpreted as a [[search expression|00001007701000]].
|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Changes to docs/manual/00001007780000.zettel.
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
|
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
|
-
+
-
+
+
+
+
+
+
+
+
|
id: 00001007780000
title: Formal syntax of query expressions
role: manual
tags: #manual #reference #search #zettelstore
syntax: zmk
created: 20220810144539
modified: 20250202164337
modified: 20250626172412
```
QueryExpression := ZettelList? QueryDirective* SearchExpression ActionExpression?
ZettelList := (ZID (SPACE+ ZID)*).
ZettelList := (ZID (SPACE+ ZID)*)?.
ZID := '0'+ ('1' .. '9'') DIGIT*
| ('1' .. '9') DIGIT*.
QueryDirective := ContextDirective
| ThreadDirective
| IdentDirective
| ItemsDirective
| UnlinkedDirective.
ContextDirective := "CONTEXT" (SPACE+ ContextDetail)*.
ContextDetail := "FULL"
| "BACKWARD"
| "FORWARD"
| "DIRECTED"
| "COST" SPACE+ PosInt
| "MAX" SPACE+ PosInt
| "MIN" SPACE+ PosInt.
ThreadDirective := ("FOLGE" | "SEQUEL" | "THREAD") (SPACE+ ThreadDetail)*.
ThreadDetail := "BACKWARD"
| "FORWARD"
| "DIRECTED"
| "MAX" SPACE+ PosInt.
IdentDirective := IDENT.
ItemsDirective := ITEMS.
UnlinkedDirective := UNLINKED (SPACE+ PHRASE SPACE+ Word)*.
SearchExpression := SearchTerm (SPACE+ SearchTerm)*.
SearchTerm := SearchOperator? SearchValue
| SearchKey SearchOperator SearchValue?
| SearchKey ExistOperator
|
︙ | | |
Changes to docs/manual/00001007790000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
-
+
-
+
+
|
id: 00001007790000
title: Useful query expressions
role: manual
tags: #example #manual #search #zettelstore
syntax: zmk
created: 20220810144539
modified: 20240216003702
modified: 20250627151159
|= Query Expression |= Meaning
| [[query:role:configuration]] | Zettel that contains some configuration data for the Zettelstore
| [[query:role:configuration]] | All zettel that contain configuration data for the Zettelstore
| [[query:ORDER REVERSE created LIMIT 40]] | 40 recently created zettel
| [[query:ORDER REVERSE published LIMIT 40]] | 40 recently updated zettel
| [[query:PICK 40]] | 40 random zettel, ordered by zettel identifier
| [[query:dead?]] | Zettel with invalid / dead links
| [[query:backward!? precursor!?]] | Zettel that are not referenced by other zettel
| [[query:tags!?]] | Zettel without tags
| [[query:expire? ORDER expire]] | Zettel with an expire date, ordered from the nearest to the latest
| [[query:expire? ORDER expire]] | All zettel with an expiration date, ordered from the nearest to the latest
| [[query:00001007700000 CONTEXT]] | Zettel within the context of the [[given zettel|00001007700000]]
| [[query:00001012051200 FOLGE]] | ""Train of thought"" of the [[given zettel|00001012051200]]
| [[query:PICK 1 | REDIRECT]] | Redirect to a random zettel
|
Changes to docs/manual/00001010040100.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
+
-
+
|
id: 00001010040100
title: Enable authentication
role: manual
tags: #authentication #configuration #manual #security #zettelstore
syntax: zmk
created: 20210126175322
modified: 20220419192817
modified: 20250627131456
show-back-links: close
To enable authentication, you must create a zettel that stores [[authentication data|00001010040200]] for the owner.
Then you must reference this zettel within the [[startup configuration|00001004010000#owner]] under the key ''owner''.
Once the startup configuration contains a valid [[zettel identifier|00001006050000]] under that key, authentication is enabled.
Please note that you must also set key ''secret'' of the [[startup configuration|00001004010000#secret]] to some random string data (minimum length is 16 bytes) to secure the data exchanged with a client system.
Please note that you must also set key ''secret'' of the [[startup configuration|00001004010000#secret]] to a random string (minimum length: 16 bytes) to secure the data exchanged with a client system.
|
Changes to docs/manual/00001010070200.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
-
+
-
+
-
-
+
+
|
id: 00001010070200
title: Visibility rules for zettel
role: manual
tags: #authorization #configuration #manual #security #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250102170611
modified: 20250627134004
For every zettel you can specify under which condition the zettel is visible to others.
This is controlled with the metadata key [[''visibility''|00001006020000#visibility]].
The following values are supported:
; [!public|""public""]
: The zettel is visible to everybody, even if the user is not authenticated.
; [!login|""login""]
: Only an authenticated user can access the zettel.
This is the default value for [[''default-visibility''|00001004020000#default-visibility]].
; [!creator|""creator""]
: Only an authenticated user that is allowed to create new zettel can access the zettel.
: Only an authenticated user who is allowed to create new zettel can access the zettel.
; [!owner|""owner""]
: Only the owner of the Zettelstore can access the zettel.
This is for zettel with sensitive content, e.g. the [[configuration zettel|00001004020000]] or the various zettel that contains the templates for rendering zettel in HTML.
; [!expert|""expert""]
: Only the owner of the Zettelstore can access the zettel, if runtime configuration [[''expert-mode''|00001004020000#expert-mode]] is set to a [[boolean true value|00001006030500]].
This is for zettel with sensitive content that might irritate the owner.
Computed zettel with internal runtime information are examples for such a zettel.
This applies to zettel with sensitive content that might irritate the owner.
Computed zettel containing internal runtime information are examples of such zettel.
When you install a Zettelstore, only [[some zettel|query:visibility:public]] have visibility ""public"".
One is the zettel that contains [[CSS|00000000020001]] for displaying the [[web user interface|00001014000000]].
This is to ensure that the web interface looks nice even for not authenticated users.
Another is the zettel containing the Zettelstore [[license|00000000000004]].
The [[default image|00000000040001]], used if an image reference is invalid, is also publicly visible.
|
︙ | | |
Changes to docs/manual/00001010090100.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
-
+
-
+
|
id: 00001010090100
title: External server to encrypt message transport
role: manual
tags: #configuration #encryption #manual #security #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250415165219
modified: 20250602182154
Since Zettelstore does not encrypt the messages it exchanges with its clients, you may need some additional software to enable encryption.
=== Public-key encryption
To enable encryption, you probably use some kind of encryption keys.
In most cases, you need to deploy a ""public-key encryption"" process, where your side publish a public encryption key that only works with a corresponding private decryption key.
In most cases, you need to deploy a ""public-key encryption"" process, where your side publishes a public encryption key that only works with a corresponding private decryption key.
Technically, this is not trivial.
Any client who wants to communicate with your Zettelstore must trust the public encryption key.
Otherwise the client cannot be sure that it is communication with your Zettelstore.
This problem is solved in part with [[Let's Encrypt|https://letsencrypt.org/]],
""a free, automated, and open certificate authority (CA), run for the public’s benefit.
It is a service provided by the [[Internet Security Research Group|https://www.abetterinternet.org/]]"".
|
︙ | | |
60
61
62
63
64
65
66
67
68
69
70
71
|
60
61
62
63
64
65
66
67
68
69
70
71
|
-
+
|
root /var/www/html
}
route /manual/* {
reverse_proxy localhost:23123
}
}
```
This will forwards requests with the prefix ""/manual/"" to the running Zettelstore.
This will forward requests with the prefix ""/manual/"" to the running Zettelstore.
All other requests will be handled by Caddy itself.
In this case you must specify the [[startup configuration key ''url-prefix''|00001004010000#url-prefix]] with the value ""/manual/"".
This is to allow Zettelstore to ignore the prefix while reading web requests and to give the correct URLs with the given prefix when sending a web response.
|
Changes to docs/manual/00001012000000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
+
-
+
|
id: 00001012000000
title: API
role: manual
tags: #api #manual #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250415154039
modified: 20250627124452
The API (short for ""**A**pplication **P**rogramming **I**nterface"") is the primary way to communicate with a running Zettelstore.
Most integration with other systems and services is done through the API.
Most integration with other systems and services is performed via the API.
The [[web user interface|00001014000000]] is just an alternative, secondary way of interacting with a Zettelstore.
=== Background
The API is HTTP-based and uses plain text and [[symbolic expressions|00001012930000]] as its main encoding formats for exchanging messages between a Zettelstore and its client software.
There is an [[overview zettel|00001012920000]] that shows the structure of the endpoints used by the API and gives an indication about its use.
|
︙ | | |
Changes to docs/manual/00001012051400.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001012051400
title: API: Query the list of all zettel
role: manual
tags: #api #manual #zettelstore
syntax: zmk
created: 20220912111111
modified: 20250224120055
modified: 20250602182055
precursor: 00001012051200
The [[endpoint|00001012920000]] ''/z'' also allows you to filter the list of all zettel[^If [[authentication is enabled|00001010040100]], you must include a valid [[access token|00001012050200]] in the ''Authorization'' header] and optionally specify some actions.
A [[query|00001007700000]] consists of an optional [[search expression|00001007700000#search-expression]] and an optional [[list of actions|00001007700000#action-list]] (described below).
If no search expression is provided, all zettel are selected.
Similarly, if no valid action is specified, or the action list is empty, the list of all selected zettel metadata is returned.
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
An implicit precondition is that the zettel must contain the given metadata key.
For a metadata key like [[''title''|00001006020000#title]], which have a default value, this precondition should always be true.
But the situation is different for a key like [[''url''|00001006020000#url]].
Both ``curl 'http://localhost:23123/z?q=url%3A'`` and ``curl 'http://localhost:23123/z?q=url%3A!'`` may result in an empty list.
As an example for a query action, to list all roles used in the Zettelstore, send a HTTP GET request to the endpoint ''/z?q=|role''.
As an example for a query action, to list all roles used in the Zettelstore, send an HTTP GET request to the endpoint ''/z?q=|role''.
```sh
# curl 'http://127.0.0.1:23123/z?q=|role'
configuration 00001000000100 00000000090002 00000000090000 00000000040001 00000000025001 00000000020001 00000000000100 00000000000092 00000000000090 00000000000006 00000000000005 00000000000004 00000000000001
manual 00001018000000 00001017000000 00001014000000 00001012921200 00001012921000 00001012920800 00001012920588 00001012920584 00001012920582 00001012920522 00001012920519 00001012920516 00001012920513 00001012920510 00001012920503 00001012920500 00001012920000 00001012080500 00001012080200 00001012080100 00001012070500 00001012054600 00001012054400 00001012054200 00001012054000 00001012053900 00001012053800 00001012053600 00001012053500 00001012053400 00001012053300 00001012053200 00001012051400 00001012051200 00001012050600 00001012050400 00001012050200 00001012000000 00001010090100 00001010070600 00001010070400 00001010070300 00001010070200 00001010040700 00001010040400 00001010040200 00001010040100 00001010000000 00001008050000 00001008010500 00001008010000 00001008000000 00001007990000 00001007906000 00001007903000 00001007900000 00001007800000 00001007790000 00001007780000 00001007706000 00001007705000 00001007702000 00001007700000 00001007050200 00001007050100 00001007050000 00001007040350 00001007040340 00001007040330 00001007040324 00001007040322 00001007040320 00001007040310 00001007040300 00001007040200 00001007040100 00001007040000 00001007031400 00001007031300 00001007031200 00001007031140 00001007031110 00001007031100 00001007031000 00001007030900 00001007030800 00001007030700 00001007030600 00001007030500 00001007030400 00001007030300 00001007030200 00001007030100 00001007030000 00001007020000 00001007010000 00001007000000 00001006055000 00001006050000 00001006036500 00001006036000 00001006035500 00001006035000 00001006034500 00001006034000 00001006033500 00001006033000 00001006032500 00001006032000 00001006031500 00001006031000 00001006030500 00001006030000 00001006020400 00001006020100 00001006020000 00001006010000 00001006000000 00001005090000 00001005000000 00001004101000 00001004100000 00001004059900 00001004059700 00001004051400 00001004051200 00001004051100 00001004051000 00001004050400 00001004050200 00001004050000 00001004020200 00001004020000 00001004011600 00001004011400 00001004011200 00001004010000 00001004000000 00001003600000 00001003315000 00001003310000 00001003305000 00001003300000 00001003000000 00001002000000 00001001000000 00001000000000
zettel 00010000000000 00000000090001
```
The result is a text file.
The first word, separated by a horizontal tab (U+0009) contains the role name.
The rest of the line consists of zettel identifier, where the corresponding zettel have this role.
Zettel identifier are separated by a space character (U+0020).
Zettel identifiers are separated by a space character (U+0020).
Please note that the list is **not** sorted by the role name, so the same request might result in a different order.
If you want a sorted list, you could sort it on the command line (``curl 'http://127.0.0.1:23123/z?q=|role' | sort``) or within the software that made the call to the Zettelstore.
Of course, this list can also be returned as a data object:
```sh
# curl 'http://127.0.0.1:23123/z?q=|role&enc=data'
(aggregate "role" (query "| role") (human "| role") ("zettel" 10000000000 90001) ("configuration" 6 100 1000000100 20001 90 25001 92 4 40001 1 90000 5 90002) ("manual" 1008050000 1007031110 1008000000 1012920513 1005000000 1012931800 1010040700 1012931000 1012053600 1006050000 1012050200 1012000000 1012070500 1012920522 1006032500 1006020100 1007906000 1007030300 1012051400 1007040350 1007040324 1007706000 1012931900 1006030500 1004050200 1012054400 1007700000 1004050000 1006020000 1007030400 1012080100 1012920510 1007790000 1010070400 1005090000 1004011400 1006033000 1012930500 1001000000 1007010000 1006020400 1007040300 1010070300 1008010000 1003305000 1006030000 1006034000 1012054200 1012080200 1004010000 1003300000 1006032000 1003310000 1004059700 1007031000 1003600000 1004000000 1007030700 1007000000 1006055000 1007050200 1006036000 1012050600 1006000000 1012053900 1012920500 1004050400 1007031100 1007040340 1007020000 1017000000 1012053200 1007030600 1007040320 1003315000 1012054000 1014000000 1007030800 1010000000 1007903000 1010070200 1004051200 1007040330 1004051100 1004051000 1007050100 1012080500 1012053400 1006035500 1012054600 1004100000 1010040200 1012920000 1012920525 1004051400 1006031500 1012921200 1008010500 1012921000 1018000000 1012051200 1010040100 1012931200 1012920516 1007040310 1007780000 1007030200 1004101000 1012920800 1007030100 1007040200 1012053500 1007040000 1007040322 1007031300 1007031140 1012931600 1012931400 1004059900 1003000000 1006036500 1004020200 1010040400 1006033500 1000000000 1012053300 1007990000 1010090100 1007900000 1007030500 1004011600 1012930000 1007030900 1004020000 1007030000 1010070600 1007040100 1007800000 1012050400 1006010000 1007705000 1007702000 1007050000 1002000000 1007031200 1006035000 1006031000 1006034500 1004011200 1007031400 1012920519))
```
The data object starts with the symbol ''aggregate'' to signal a different format compared to ''meta-list'' above.
Then a string follows, which specifies the key on which the aggregate was performed.
''query'' and ''human'' have the same meaning as above.
Then comes the result list of aggregates.
Each aggregate starts with a string of the aggregate value, in this case the role value, followed by a list of zettel identifier, denoting zettel which have the given role value.
Similar, to list all tags used in the Zettelstore, send a HTTP GET request to the endpoint ''/z?q=|tags''.
Similarly, to list all tags used in the Zettelstore, send an HTTP GET request to the endpoint ''/z?q=|tags''.
If successful, the output is a data object:
```sh
# curl 'http://127.0.0.1:23123/z?q=|tags&enc=data'
(aggregate "tags" (query "| tags") (human "| tags") ("#zettel" 1006034500 1006034000 1006031000 1006020400 1006033500 1006036500 1006032500 1006020100 1006031500 1006030500 1006035500 1006033000 1006020000 1006036000 1006030000 1006032000 1006035000) ("#reference" 1006034500 1006034000 1007800000 1012920500 1006031000 1012931000 1006020400 1012930000 1006033500 1012920513 1007050100 1012920800 1007780000 1012921000 1012920510 1007990000 1006036500 1006032500 1006020100 1012931400 1012931800 1012920516 1012931600 1012920525 1012931200 1006031500 1012931900 1012920000 1005090000 1012920522 1006030500 1007050200 1012921200 1006035500 1012920519 1006033000 1006020000 1006036000 1006030000 1006032000 1012930500 1006035000) ("#graphic" 1008050000) ("#search" 1007700000 1007705000 1007790000 1007780000 1007702000 1007706000 1007031140) ("#installation" 1003315000 1003310000 1003000000 1003305000 1003300000 1003600000) ("#zettelmarkup" 1007900000 1007030700 1007031300 1007030600 1007800000 1007000000 1007031400 1007040100 1007030300 1007031200 1007040350 1007030400 1007030900 1007050100 1007040000 1007030500 1007903000 1007040200 1007040330 1007990000 1007040320 1007050000 1007040310 1007031100 1007040340 1007020000 1007031110 1007031140 1007040324 1007030800 1007031000 1007030000 1007010000 1007906000 1007050200 1007030100 1007030200 1007040300 1007040322) ("#design" 1005000000 1006000000 1002000000 1006050000 1006055000) ("#markdown" 1008010000 1008010500) ("#goal" 1002000000) ("#syntax" 1006010000) ...
```
|
︙ | | |
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
-
+
|
__n__ must be a positive integer, ''MIN'' must be given in upper-case letters.
; ''MAXn'' (parameter)
: Emit only those values with at most __n__ aggregated values.
__n__ must be a positive integer, ''MAX'' must be given in upper-case letters.
; ''KEYS'' (aggregate)
: Emit a list of all metadata keys, together with the number of zettel having the key.
; ''REDIRECT'' (aggregate)
: Performs a HTTP redirect to the first selected zettel, using HTTP status code 302.
: Performs an HTTP redirect to the first selected zettel, using HTTP status code 302.
The zettel identifier is in the body.
; ''REINDEX'' (aggregate)
: Updates the internal search index for the selected zettel, roughly similar to the [[refresh|00001012080500]] API call.
It is not really an aggregate, since it is used only for its side effect.
It is allowed to specify another aggregate.
; Any [[metadata key|00001006020000]] of type [[Word|00001006035500]] or [[TagSet|00001006034000]] (aggregates)
: Emit an aggregate of the given metadata key.
|
︙ | | |
Changes to docs/manual/00001012054200.zettel.
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
|
-
+
-
-
+
+
-
+
-
+
|
id: 00001012054200
title: API: Update a zettel
role: manual
tags: #api #manual #zettelstore
syntax: zmk
created: 20210713150005
modified: 20231116110417
modified: 20250627125627
Updating metadata and content of a zettel is technically quite similar to [[creating a new zettel|00001012053200]].
In both cases you must provide the data for the new or updated zettel in the body of the HTTP request.
Updating metadata and content of a zettel is technically quite similar to [[creating a new one|00001012053200]].
In both cases, you must provide the data for the new or updated zettel in the body of the HTTP request.
One difference is the endpoint.
The [[endpoint|00001012920000]] to update a zettel is ''/z/{ID}'', where ''{ID}'' is a placeholder for the [[zettel identifier|00001006050000]].
You must send a HTTP PUT request to that endpoint.
You must send an HTTP PUT request to that endpoint.
The zettel must be encoded in a [[plain|00001006000000]] format: first comes the [[metadata|00001006010000]] and the following content is separated by an empty line.
This is the same format as used by storing zettel within a [[directory box|00001006010000]].
This is the same format used for storing zettel within a directory box. [[directory box|00001006010000]].
```
# curl -X PUT --data $'title: Updated Note\n\nUpdated content.' http://127.0.0.1:23123/z/00001012054200
```
=== Data input
Alternatively, you may encode the zettel as a parseable object / a [[symbolic expression|00001012930500]] by providing the query parameter ''enc=data''.
|
︙ | | |
33
34
35
36
37
38
39
40
|
33
34
35
36
37
38
39
40
|
-
+
|
; ''400''
: Request was not valid.
For example, the request body was not valid.
; ''403''
: You are not allowed to delete the given zettel.
; ''404''
: Zettel not found.
You probably used a zettel identifier that is not used in the Zettelstore.
You probably used a zettel identifier that does not exist in the Zettelstore.
|
Changes to docs/manual/00001012920000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
-
+
-
+
|
id: 00001012920000
title: Endpoints used by the API
role: manual
tags: #api #manual #reference #zettelstore
syntax: zmk
created: 20210126175322
modified: 20250415154347
modified: 20250627125818
All API endpoints conform to the pattern ''[PREFIX]LETTER[/ZETTEL-ID]'', where:
; ''PREFIX''
: is the URL prefix (default: ""/""), configured via the ''url-prefix'' [[startup configuration|00001004010000]],
; ''LETTER''
: is a single letter that specifies the resource type,
; ''ZETTEL-ID''
: is an optional 14 digits string that uniquely [[identify a zettel|00001006050000]].
: is an optional 14-digit string that uniquely [[identifies a zettel|00001006050000]].
The following letters are currently in use:
|= Letter:| Without zettel identifier | With [[zettel identifier|00001006050000]] | Mnemonic
| ''a'' | POST: [[client authentication|00001012050200]] | | **A**uthenticate
| | PUT: [[renew access token|00001012050400]] |
| ''r'' | | GET: [[references|00001012053800]] | **R**eference
|
︙ | | |
Changes to docs/manual/00001012921200.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001012921200
title: API: Encoding of Zettel Access Rights
role: manual
tags: #api #manual #reference #zettelstore
syntax: zmk
created: 20220201173115
modified: 20240711183931
modified: 20250602181802
Various API calls return a symbolic expression list ''(rights N)'', with ''N'' as a number, that encodes the access rights the user currently has.
''N'' is an integer number between 0 and 62.[^Not all values in this range are used.]
The value ""0"" signals that something went wrong internally while determining the access rights.
A value of ""1"" says, that the current user has no access right for the given zettel.
|
︙ | | |
36
37
38
39
40
41
42
43
44
45
46
|
36
37
38
39
40
41
42
43
44
45
46
|
-
+
|
# The next right is the right to update a zettel (16 > 10, but 8 < 10).
The new value of the rights value is now 2 (10-8).
# The last right is the right to create a new zettel.
The rights value is now zero, the algorithm ends.
In practice, not every rights value will occur.
A Zettelstore in [[read-only mode|00001010000000#read-only]] will always return the value 4.
Similar, a Zettelstore that you started with a [[double-click|00001003000000]] will return either the value ""6"" (reading and updating) or the value ""62"" (all operations are allowed).
Similarly, a Zettelstore that you started with a [[double-click|00001003000000]] will return either the value ""6"" (read and update) or the value ""62"" (all operations are allowed).
If you have added an additional [[user|00001010040200]] to your Zettelstore, this might change.
The access rights are calculated depending on [[enabled authentication|00001010040100]], on the [[user role|00001010070300]] of the current user, on [[visibility rules|00001010070200]] for a given zettel and on the [[read-only status|00001006020400]] for the zettel.
|
Changes to docs/manual/00001012930000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
+
-
+
|
id: 00001012930000
title: Symbolic Expression
role: manual
tags: #manual #reference #zettelstore
syntax: zmk
created: 20230403145644
modified: 20230403154010
modified: 20250627124639
A symbolic expression (also called __s-expression__) is a notation of a list-based tree.
Inner nodes are lists of arbitrary length, outer nodes are primitive values (also called __atoms__) or the empty list.
Inner nodes are lists of arbitrary length, while outer nodes are either primitive values (also called __atoms__) or the empty list.
A symbolic expression is either
* a primitive value (__atom__), or
* a list of the form __(E,,1,, E,,2,, … E,,n,,)__, where __E,,i,,__ is itself a symbolic expression, separated by space characters.
An atom is a number, a string, or a symbol.
|
︙ | | |
Changes to docs/manual/00001012930500.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
+
|
id: 00001012930500
title: Syntax of Symbolic Expressions
role: manual
tags: #manual #reference #zettelstore
syntax: zmk
created: 20230403151127
modified: 20250102175559
modified: 20250627124837
=== Syntax of lists
A list always starts with the left parenthesis (""''(''"", U+0028) and ends with a right parenthesis (""'')''"", U+0029).
A list may contain a possibly empty sequence of elements, i.e. lists and / or atoms.
Internally, lists are composed of __cells__.
A cell allows to store two values.
|
︙ | | |
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
-
+
|
v v v
+-------+ +-------+ +-------+
| Elem1 | | Elem2 | | ElemN |
+-------+ +-------+ +-------+
~~~
''V'' is a placeholder for a value, ''N'' is the reference to the next cell (also known as the rest / tail of the list).
Above list will be represented as an symbolic expression as ''(Elem1 Elem2 ... ElemN)''
The list above is represented as a symbolic expression in the form ''(Elem1 Elem2 ... ElemN)''
An improper list will have a non-__nil__ reference to an atom as the very last element
~~~draw
+---+---+ +---+---+ +---+---+
| V | N +-->| V | N +--> -->| V | V |
+-+-+---+ +-+-+---+ +-+-+-+-+
|
︙ | | |
Changes to docs/manual/00001017000000.zettel.
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
|
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
|
-
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
|
id: 00001017000000
title: Tips and Tricks
role: manual
tags: #manual #zettelstore
syntax: zmk
created: 20220803170112
modified: 20250102190553
modified: 20250627133713
=== Welcome Zettel
* **Problem:** You want to put your Zettelstore into the public and need a starting zettel for your users.
In addition, you still want a ""home zettel"", with all your references to internal, non-public zettel.
Zettelstore only allows specifying one [[''home-zettel''|00001004020000#home-zettel]].
* **Solution 1:**
*# Create a new zettel with all your references to internal, non-public zettel.
Let's assume this zettel receives the zettel identifier ''20220803182600''.
*# Create the zettel that should serve as the starting zettel for your users.
It must have syntax [[Zettelmarkup|00001008000000#zmk]], i.e. the syntax metadata must be set to ''zmk''.
If needed, set the runtime configuration [[''home-zettel''|00001004020000#home-zettel]] to the value of the identifier of this zettel.
*# Create the zettel that will serve as the starting zettel for your users.
It must use the [[Zettelmarkup|00001008000000#zmk]] syntax, i.e. the syntax metadata must be set to ''zmk''.
If needed, set the runtime configuration [[''home-zettel''|00001004020000#home-zettel]] to the identifier of this zettel.
*# At the beginning of the start zettel, add the following [[Zettelmarkup|00001007000000]] text in a separate paragraph: ``{{{20220803182600}}}`` (you have to adapt to the actual value of the zettel identifier for your non-public home zettel).
* **Discussion:** As stated in the description for a [[transclusion|00001007031100]], a transclusion will be ignored, if the transcluded zettel is not visible to the current user.
In effect, the transclusion statement (above paragraph that contained ''{{{...}}}'') is ignored when rendering the zettel.
* **Solution 2:** Set a user-specific value by adding metadata ''home-zettel'' to the [[user zettel|00001010040200]].
* **Discussion:** A value for ''home-zettel'' is first searched in the user zettel of the current authenticated user.
Only if it is not found, the value is looked up in the runtime configuration zettel.
If multiple user should use the same home zettel, its zettel identifier must be set in all relevant user zettel.
* **Discussion:** A value for ''home-zettel'' is first searched for in the user zettel of the currently authenticated user.
Only if it is not found there, the value is looked up in the runtime configuration zettel.
If multiple users should share the same home zettel, its zettel identifier must be set in all relevant user zettel.
=== Role-specific Layout of Zettel in Web User Interface (WebUI)
[!role-css]
* **Problem:** You want to add some CSS when displaying zettel of a specific [[role|00001006020000#role]].
For example, you might want to add a yellow background color for all [[configuration|00001006020100#configuration]] zettel.
Or you want a multi-column layout.
* **Solution:** If you enable [[''expert-mode''|00001004020000#expert-mode]], you will have access to a zettel called ""[[Zettelstore Sxn Start Code|00000000019000]]"" (its identifier is ''00000000019000'').
This zettel is the starting point for Sxn code, where you can place a definition for a variable named ""CSS-ROLE-map"".
But first, create a zettel containing the needed CSS: give it any title, its role is preferably ""configuration"" (but this is not a must).
Its [[''syntax''|00001006020000#syntax]] must be set to ""[[css|00001008000000#css]]"".
The content must contain the role-specific CSS code, for example ``body {background-color: #FFFFD0}``for a background in a light yellow color.
Let's assume, the newly created CSS zettel got the identifier ''20220825200100''.
Now, you have to map this freshly created zettel to a role, for example ""zettel"".
Since you have enabled ''expert-mode'', you are allowed to modify the zettel ""[[Zettelstore Sxn Start Code|00000000019000]]"".
Add the following code to the Sxn Start Code zettel: ``(set! CSS-ROLE-map '(("zettel" . "20220825200100")))``.
In general, the mapping must follow the pattern: ``(ROLE . ID)``, where ''ROLE'' is the placeholder for the role, and ''ID'' for the zettel identifier containing CSS code.
For example, if you also want the role ""configuration"" to be rendered using that CSS, the code should be something like ``(set! CSS-ROLE-map '(("zettel" . "20220825200100") ("configuration" . "20220825200100")))``.
* **Discussion:** you have to ensure that the CSS zettel is allowed to be read by the intended audience of the zettel with that given role.
For example, if you made zettel with a specific role public visible, the CSS zettel must also have a [[''visibility: public''|00001010070200]] metadata.
In general, the mapping must follow the pattern: ``(ROLE . ID)``, where ''ROLE'' is a placeholder for the role, and ''ID'' is the identifier of the zettel containing CSS code.
For example, if you also want the role ""configuration"" to be rendered using that CSS, the code should look like this: ``(set! CSS-ROLE-map '(("zettel" . "20220825200100") ("configuration" . "20220825200100")))``.
* **Discussion:** you have to ensure that the CSS zettel is accessible to the intended audience of the zettel with the given role.
For example, if you made a zettel with a specific role publicly visible, the CSS zettel must also have a [[''visibility: public''|00001010070200]] metadata entry.
=== Zettel synchronization with iCloud (Apple)
* **Problem:** You use Zettelstore on various macOS computers and you want to use the same set of zettel across all computers.
* **Solution:** Place your zettel in an iCloud folder.
To configure Zettelstore to use the folder, you must specify its location within your directory structure as [[''box-uri-X''|00001004010000#box-uri-x]] (replace ''X'' with an appropriate number).
Your iCloud folder is typically placed in the folder ''~/Library/Mobile Documents/com~apple~CloudDocs''.
|
︙ | | |
Changes to docs/manual/00001018000000.zettel.
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
|
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
|
-
+
-
+
|
id: 00001018000000
title: Troubleshooting
role: manual
tags: #manual #zettelstore
syntax: zmk
created: 20211027105921
modified: 20250415170405
modified: 20250627130445
This page lists some problems and their solutions that may occur when using your Zettelstore.
=== Installation
* **Problem:** When you double-click on the Zettelstore executable icon, macOS complains that Zettelstore is an application from an unknown developer.
Therefore, it will not start Zettelstore.
** **Solution:** Press the ''Ctrl'' key while opening the context menu of the Zettelstore executable with a right-click.
A dialog is then opened where you can acknowledge that you understand the possible risks when you start Zettelstore.
This dialog is only presented once for a given Zettelstore executable.
* **Problem:** When you double-click on the Zettelstore executable icon, Windows complains that Zettelstore is an application from an unknown developer.
** **Solution:** Windows displays a dialog where you can acknowledge possible risks and allow to start Zettelstore.
=== Authentication
* **Problem:** [[Authentication is enabled|00001010040100]] for a local running Zettelstore and there is a valid [[user zettel|00001010040200]] for the owner.
But entering user name and password at the [[web user interface|00001014000000]] seems to be ignored, while entering a wrong password will result in an error message.
** **Explanation:** A local running Zettelstore typically means, that you are accessing the Zettelstore using an URL with schema ''http://'', and not ''https://'', for example ''http://localhost:23123/''.
** **Explanation:** A local running Zettelstore typically means, that you are accessing the Zettelstore using a URL with schema ''http://'', and not ''https://'', for example ''http://localhost:23123/''.
The difference between these two is the missing encryption of user name / password and for the answer of the Zettelstore if you use the ''http://'' schema.
To be secure by default, the Zettelstore will not work in an insecure environment.
** **Solution 1:** If you are sure that your communication medium is safe, even if you use the ''http:/\/'' schema (for example, you are running the Zettelstore on the same computer you are working on, or if the Zettelstore is running on a computer in your protected local network), then you could add the entry ''insecure-cookie: true'' in your [[startup configuration|00001004010000#insecure-cookie]] file.
** **Solution 2:** If you are not sure about the security of your communication medium (for example, if unknown persons might use your local network), then you should run an [[external server|00001010090100]] in front of your Zettelstore to enable the use of the ''https://'' schema.
=== Working with Zettel Files
* **Problem:** When you delete a zettel file by removing it from the ""disk"", e.g. by dropping it into the trash folder, by dragging into another folder, or by removing it from the command line, Zettelstore sometimes does not detect the change.
|
︙ | | |
Changes to go.mod.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
|
module zettelstore.de/z
go 1.24
require (
github.com/fsnotify/fsnotify v1.9.0
github.com/yuin/goldmark v1.7.9
golang.org/x/crypto v0.37.0
golang.org/x/term v0.31.0
golang.org/x/text v0.24.0
t73f.de/r/sx v0.0.0-20250415161954-42ed9c4d6abc
t73f.de/r/sxwebs v0.0.0-20250415162443-110f49c5a1ae
t73f.de/r/webs v0.0.0-20250403120312-69ac186a05b5
t73f.de/r/zero v0.0.0-20250403120114-7d464a82f2e5
t73f.de/r/zsc v0.0.0-20250417145802-ef331b4f0cdd
t73f.de/r/zsx v0.0.0-20250415162540-fc13b286b6ce
github.com/yuin/goldmark v1.7.12
golang.org/x/crypto v0.39.0
golang.org/x/term v0.32.0
golang.org/x/text v0.26.0
t73f.de/r/sx v0.0.0-20250620141036-553aa22c59dc
t73f.de/r/sxwebs v0.0.0-20250621125212-c25706b6e4b3
t73f.de/r/webs v0.0.0-20250604132257-c12dbd1f7046
t73f.de/r/zero v0.0.0-20250604143210-ce1230735c4c
t73f.de/r/zsc v0.0.0-20250626125233-aeaaa36dabe9
t73f.de/r/zsx v0.0.0-20250526093914-c34f0bae8fd2
)
require golang.org/x/sys v0.32.0 // indirect
require golang.org/x/sys v0.33.0 // indirect
|
Changes to go.sum.
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/yuin/goldmark v1.7.9 h1:rVSeT+f7/lAM+bJHVm5YHGwNrnd40i1Ch2DEocEjHQ0=
github.com/yuin/goldmark v1.7.9/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
t73f.de/r/sx v0.0.0-20250415161954-42ed9c4d6abc h1:tlsP+47Rf8i9Zv1TqRnwfbQx3nN/F/92RkT6iCA6SVA=
t73f.de/r/sx v0.0.0-20250415161954-42ed9c4d6abc/go.mod h1:hzg05uSCMk3D/DWaL0pdlowfL2aWQeGIfD1S04vV+Xg=
t73f.de/r/sxwebs v0.0.0-20250415162443-110f49c5a1ae h1:K6nxN/bb0BCSiDffwNPGTF2uf5WcTdxcQXzByXNuJ7M=
t73f.de/r/sxwebs v0.0.0-20250415162443-110f49c5a1ae/go.mod h1:0LQ9T1svSg9ADY/6vQLKNUu6LqpPi8FGr7fd2qDT5H8=
t73f.de/r/webs v0.0.0-20250403120312-69ac186a05b5 h1:M2RMwkuBlMAKgNH70qLtEFqw7jtH+/rM0NTUPZObk6Y=
t73f.de/r/webs v0.0.0-20250403120312-69ac186a05b5/go.mod h1:zk92hSKB4iWyT290+163seNzu350TA9XLATC9kOldqo=
t73f.de/r/zero v0.0.0-20250403120114-7d464a82f2e5 h1:3FU6YUaqxJI20ZTXDc8xnPBzjajnWZ56XaPOfckEmJo=
t73f.de/r/zero v0.0.0-20250403120114-7d464a82f2e5/go.mod h1:T1vFcHoymUQcr7+vENBkS1yryZRZ3YB8uRtnMy8yRBA=
t73f.de/r/zsc v0.0.0-20250417145802-ef331b4f0cdd h1:InJjWI/Y2xAJBBs+SMXTS7rJaizk6/b/VW5CCno0vrg=
t73f.de/r/zsc v0.0.0-20250417145802-ef331b4f0cdd/go.mod h1:Mb3fLaOcSp5t6I7sRDKmOx4U1AnTb30F7Jh4e1L0mx8=
t73f.de/r/zsx v0.0.0-20250415162540-fc13b286b6ce h1:R9rtg4ecx4YYixsMmsh+wdcqLdY9GxoC5HZ9mMS33to=
t73f.de/r/zsx v0.0.0-20250415162540-fc13b286b6ce/go.mod h1:tXOlmsQBoY4mY7Plu0LCCMZNSJZJbng98fFarZXAWvM=
github.com/yuin/goldmark v1.7.12 h1:YwGP/rrea2/CnCtUHgjuolG/PnMxdQtPMO5PvaE2/nY=
github.com/yuin/goldmark v1.7.12/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
t73f.de/r/sx v0.0.0-20250620141036-553aa22c59dc h1:5s02C7lwQKjOXyY4ghR6oLo+0SagSBiEiC26ju3VG40=
t73f.de/r/sx v0.0.0-20250620141036-553aa22c59dc/go.mod h1:Ow4Btc5PCykSct4TrS1kbEB386msl/tmfmLSsEz6OAw=
t73f.de/r/sxwebs v0.0.0-20250621125212-c25706b6e4b3 h1:+tqWPX3z5BgsRZJDpMtReHmGUioUFP+LsPpXieZ2ZsY=
t73f.de/r/sxwebs v0.0.0-20250621125212-c25706b6e4b3/go.mod h1:zZBXrGeTfUqElkSMJhGUCuDDWNOUaZE0EH3IZwkW+RA=
t73f.de/r/webs v0.0.0-20250604132257-c12dbd1f7046 h1:BZWNT/wYlX5sHmEtClRG0rHzZnoh8J35NcRnTvXlqy0=
t73f.de/r/webs v0.0.0-20250604132257-c12dbd1f7046/go.mod h1:EVohQwCAlRK0kuVBEw5Gw+S44vj+6f6NU8eNJdAIK6s=
t73f.de/r/zero v0.0.0-20250604143210-ce1230735c4c h1:Zy7GaPv/uVSjKQY7t2c0OOIdSue36x+/0sXt+xoxlpQ=
t73f.de/r/zero v0.0.0-20250604143210-ce1230735c4c/go.mod h1:T1vFcHoymUQcr7+vENBkS1yryZRZ3YB8uRtnMy8yRBA=
t73f.de/r/zsc v0.0.0-20250626125233-aeaaa36dabe9 h1:zrUmOTkWhJt7YUARk8EPIqPe/kpSGMOlRCr3nIdn/TQ=
t73f.de/r/zsc v0.0.0-20250626125233-aeaaa36dabe9/go.mod h1:mxIDqZJD02ZD+pspYPa/VHdlMmUE+DBzE5J5dp+Vb/I=
t73f.de/r/zsx v0.0.0-20250526093914-c34f0bae8fd2 h1:GWLCd3n8mN6AGhiv8O7bhdjK0BqXQS5EExRlBdx3OPU=
t73f.de/r/zsx v0.0.0-20250526093914-c34f0bae8fd2/go.mod h1:IQdyC9JP1i6RK55+LJVGjP3hSA9H766yCyUt1AkOU9c=
|
Changes to internal/auth/impl/impl.go.
︙ | | |
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
-
+
-
+
|
kernel.CoreGoArch,
kernel.CoreVersion,
}
func calcSecret(extSecret string) []byte {
h := fnv.New128()
if extSecret != "" {
io.WriteString(h, extSecret)
_, _ = io.WriteString(h, extSecret)
}
for _, key := range configKeys {
io.WriteString(h, kernel.Main.GetConfig(kernel.CoreService, key).(string))
_, _ = io.WriteString(h, kernel.Main.GetConfig(kernel.CoreService, key).(string))
}
return h.Sum(nil)
}
// IsReadonly returns true, if the systems is configured to run in read-only-mode.
func (a *myAuth) IsReadonly() bool { return a.readonly }
|
︙ | | |
Changes to internal/auth/policy/box.go.
︙ | | |
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
+
-
|
"context"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/id/idset"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/auth/user"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/web/server"
"zettelstore.de/z/internal/zettel"
)
// BoxWithPolicy wraps the given box inside a policy box.
func BoxWithPolicy(manager auth.AuthzManager, box box.Box, authConfig config.AuthConfig) (box.Box, auth.Policy) {
pol := newPolicy(manager, authConfig)
return newBox(box, pol), pol
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
|
}
func (pp *polBox) CanCreateZettel(ctx context.Context) bool {
return pp.box.CanCreateZettel(ctx)
}
func (pp *polBox) CreateZettel(ctx context.Context, zettel zettel.Zettel) (id.Zid, error) {
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanCreate(user, zettel.Meta) {
return pp.box.CreateZettel(ctx, zettel)
}
return id.Invalid, box.NewErrNotAllowed("Create", user, id.Invalid)
}
func (pp *polBox) GetZettel(ctx context.Context, zid id.Zid) (zettel.Zettel, error) {
z, err := pp.box.GetZettel(ctx, zid)
if err != nil {
return zettel.Zettel{}, err
}
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanRead(user, z.Meta) {
return z, nil
}
return zettel.Zettel{}, box.NewErrNotAllowed("GetZettel", user, zid)
}
func (pp *polBox) GetAllZettel(ctx context.Context, zid id.Zid) ([]zettel.Zettel, error) {
return pp.box.GetAllZettel(ctx, zid)
}
func (pp *polBox) FetchZids(ctx context.Context) (*idset.Set, error) {
return nil, box.NewErrNotAllowed("fetch-zids", server.GetUser(ctx), id.Invalid)
return nil, box.NewErrNotAllowed("fetch-zids", user.GetCurrentUser(ctx), id.Invalid)
}
func (pp *polBox) GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) {
m, err := pp.box.GetMeta(ctx, zid)
if err != nil {
return nil, err
}
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanRead(user, m) {
return m, nil
}
return nil, box.NewErrNotAllowed("GetMeta", user, zid)
}
func (pp *polBox) SelectMeta(ctx context.Context, metaSeq []*meta.Meta, q *query.Query) ([]*meta.Meta, error) {
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
canRead := pp.policy.CanRead
q = q.SetPreMatch(func(m *meta.Meta) bool { return canRead(user, m) })
return pp.box.SelectMeta(ctx, metaSeq, q)
}
func (pp *polBox) CanUpdateZettel(ctx context.Context, zettel zettel.Zettel) bool {
return pp.box.CanUpdateZettel(ctx, zettel)
}
func (pp *polBox) UpdateZettel(ctx context.Context, zettel zettel.Zettel) error {
zid := zettel.Meta.Zid
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if !zid.IsValid() {
return box.ErrInvalidZid{Zid: zid.String()}
}
// Write existing zettel
oldZettel, err := pp.box.GetZettel(ctx, zid)
if err != nil {
return err
|
︙ | | |
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
|
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
-
+
-
+
-
+
|
}
func (pp *polBox) DeleteZettel(ctx context.Context, zid id.Zid) error {
z, err := pp.box.GetZettel(ctx, zid)
if err != nil {
return err
}
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanDelete(user, z.Meta) {
return pp.box.DeleteZettel(ctx, zid)
}
return box.NewErrNotAllowed("Delete", user, zid)
}
func (pp *polBox) Refresh(ctx context.Context) error {
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanRefresh(user) {
return pp.box.Refresh(ctx)
}
return box.NewErrNotAllowed("Refresh", user, id.Invalid)
}
func (pp *polBox) ReIndex(ctx context.Context, zid id.Zid) error {
user := server.GetUser(ctx)
user := user.GetCurrentUser(ctx)
if pp.policy.CanRefresh(user) {
// If a user is allowed to refresh all data, it it also allowed to re-index a zettel.
return pp.box.ReIndex(ctx, zid)
}
return box.NewErrNotAllowed("ReIndex", user, zid)
}
|
Added internal/auth/user/user.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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2025-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore 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: 2025-present Detlef Stern
//-----------------------------------------------------------------------------
// Package user provides services for working with user data.
package user
import (
"context"
"time"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/auth"
)
// AuthData stores all relevant authentication data for a context.
type AuthData struct {
User *meta.Meta
Token []byte
Now time.Time
Issued time.Time
Expires time.Time
}
// GetAuthData returns the full authentication data from the context.
func GetAuthData(ctx context.Context) *AuthData {
if ctx != nil {
if data, ok := ctx.Value(ctxKeyTypeSession{}).(*AuthData); ok {
return data
}
}
return nil
}
// GetCurrentUser returns the metadata of the current user, or nil if there is no one.
func GetCurrentUser(ctx context.Context) *meta.Meta {
if data := GetAuthData(ctx); data != nil {
return data.User
}
return nil
}
// ctxKeyTypeSession is just an additional type to make context value retrieval unambiguous.
type ctxKeyTypeSession struct{}
// UpdateContext enriches the given context with some data of the current user.
func UpdateContext(ctx context.Context, user *meta.Meta, data *auth.TokenData) context.Context {
if data == nil {
return context.WithValue(ctx, ctxKeyTypeSession{}, &AuthData{User: user})
}
return context.WithValue(
ctx,
ctxKeyTypeSession{},
&AuthData{
User: user,
Token: data.Token,
Now: data.Now,
Issued: data.Issued,
Expires: data.Expires,
})
}
|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Changes to internal/box/box.go.
︙ | | |
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
-
+
-
-
-
+
|
Enrich(ctx context.Context, m *meta.Meta, boxNumber int)
}
// NoEnrichContext will signal an enricher that nothing has to be done.
// This is useful for an Indexer, but also for some box.Box calls, when
// just the plain metadata is needed.
func NoEnrichContext(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxNoEnrichKey, &ctxNoEnrichKey)
return context.WithValue(ctx, ctxNoEnrichType{}, ctx)
}
type ctxNoEnrichType struct{}
var ctxNoEnrichKey ctxNoEnrichType
// DoEnrich determines if the context is not marked to not enrich metadata.
func DoEnrich(ctx context.Context) bool {
_, ok := ctx.Value(ctxNoEnrichKey).(*ctxNoEnrichType)
_, ok := ctx.Value(ctxNoEnrichType{}).(*ctxNoEnrichType)
return !ok
}
// NoEnrichQuery provides a context that signals not to enrich, if the query does not need this.
func NoEnrichQuery(ctx context.Context, q *query.Query) context.Context {
if q.EnrichNeeded() {
return ctx
|
︙ | | |
Changes to internal/box/compbox/compbox.go.
︙ | | |
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
|
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
|
+
-
+
-
+
|
//-----------------------------------------------------------------------------
// Package compbox provides zettel that have computed content.
package compbox
import (
"context"
"log/slog"
"net/url"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
func init() {
manager.Register(
" comp",
func(_ *url.URL, cdata *manager.ConnectData) (box.ManagedBox, error) {
return getCompBox(cdata.Number, cdata.Enricher), nil
})
}
type compBox struct {
log *logger.Logger
logger *slog.Logger
number int
enricher box.Enricher
}
var myConfig *meta.Meta
var myZettel = map[id.Zid]struct {
meta func(id.Zid) *meta.Meta
|
︙ | | |
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
|
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
|
-
+
-
-
+
-
+
-
+
-
+
-
+
|
id.ZidParser: {genParserM, genParserC},
id.ZidStartupConfiguration: {genConfigZettelM, genConfigZettelC},
}
// Get returns the one program box.
func getCompBox(boxNumber int, mf box.Enricher) *compBox {
return &compBox{
log: kernel.Main.GetLogger(kernel.BoxService).Clone().
logger: kernel.Main.GetLogger(kernel.BoxService).With("box", "comp", "boxnum", boxNumber),
Str("box", "comp").Int("boxnum", int64(boxNumber)).Child(),
number: boxNumber,
enricher: mf,
}
}
// Setup remembers important values.
func Setup(cfg *meta.Meta) { myConfig = cfg.Clone() }
func (*compBox) Location() string { return "" }
func (cb *compBox) GetZettel(ctx context.Context, zid id.Zid) (zettel.Zettel, error) {
if gen, ok := myZettel[zid]; ok && gen.meta != nil {
if m := gen.meta(zid); m != nil {
updateMeta(m)
if genContent := gen.content; genContent != nil {
cb.log.Trace().Msg("GetZettel/Content")
logging.LogTrace(cb.logger, "GetZettel/Content")
return zettel.Zettel{
Meta: m,
Content: zettel.NewContent(genContent(ctx, cb)),
}, nil
}
cb.log.Trace().Msg("GetZettel/NoContent")
logging.LogTrace(cb.logger, "GetZettel/NoContent")
return zettel.Zettel{Meta: m}, nil
}
}
err := box.ErrZettelNotFound{Zid: zid}
cb.log.Trace().Err(err).Msg("GetZettel/Err")
logging.LogTrace(cb.logger, "GetZettel/Err", "err", err)
return zettel.Zettel{}, err
}
func (*compBox) HasZettel(_ context.Context, zid id.Zid) bool {
_, found := myZettel[zid]
return found
}
func (cb *compBox) ApplyZid(_ context.Context, handle box.ZidFunc, constraint query.RetrievePredicate) error {
cb.log.Trace().Int("entries", int64(len(myZettel))).Msg("ApplyZid")
logging.LogTrace(cb.logger, "ApplyZid", "entries", len(myZettel))
for zid, gen := range myZettel {
if !constraint(zid) {
continue
}
if genMeta := gen.meta; genMeta != nil {
if genMeta(zid) != nil {
handle(zid)
}
}
}
return nil
}
func (cb *compBox) ApplyMeta(ctx context.Context, handle box.MetaFunc, constraint query.RetrievePredicate) error {
cb.log.Trace().Int("entries", int64(len(myZettel))).Msg("ApplyMeta")
logging.LogTrace(cb.logger, "ApplyMeta", "entries", len(myZettel))
for zid, gen := range myZettel {
if !constraint(zid) {
continue
}
if genMeta := gen.meta; genMeta != nil {
if m := genMeta(zid); m != nil {
updateMeta(m)
|
︙ | | |
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
-
+
-
+
|
func (cb *compBox) DeleteZettel(_ context.Context, zid id.Zid) (err error) {
if _, ok := myZettel[zid]; ok {
err = box.ErrReadOnly
} else {
err = box.ErrZettelNotFound{Zid: zid}
}
cb.log.Trace().Err(err).Msg("DeleteZettel")
logging.LogTrace(cb.logger, "DeleteZettel", "err", err)
return err
}
func (cb *compBox) ReadStats(st *box.ManagedBoxStats) {
st.ReadOnly = true
st.Zettel = len(myZettel)
cb.log.Trace().Int("zettel", int64(st.Zettel)).Msg("ReadStats")
logging.LogTrace(cb.logger, "ReadStats", "zettel", st.Zettel)
}
func getTitledMeta(zid id.Zid, title string) *meta.Meta {
m := meta.New(zid)
m.Set(meta.KeyTitle, meta.Value(title))
return m
}
|
︙ | | |
Changes to internal/box/compbox/log.go.
︙ | | |
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
|
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
|
+
-
+
+
+
|
import (
"bytes"
"context"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logging"
)
func genLogM(zid id.Zid) *meta.Meta {
m := getTitledMeta(zid, "Zettelstore Log")
m.Set(meta.KeySyntax, meta.ValueSyntaxText)
m.Set(meta.KeyModified, meta.Value(kernel.Main.GetLastLogTime().Local().Format(id.TimestampLayout)))
return m
}
func genLogC(context.Context, *compBox) []byte {
const tsFormat = "2006-01-02 15:04:05.999999"
entries := kernel.Main.RetrieveLogEntries()
var buf bytes.Buffer
for _, entry := range entries {
ts := entry.TS.Format(tsFormat)
buf.WriteString(ts)
for j := len(ts); j < len(tsFormat); j++ {
buf.WriteByte('0')
}
buf.WriteByte(' ')
buf.WriteString(entry.Level.Format())
buf.WriteString(logging.LevelStringPad(entry.Level))
buf.WriteByte(' ')
buf.WriteString(entry.Prefix)
buf.WriteByte(' ')
buf.WriteString(entry.Message)
buf.WriteByte(' ')
buf.WriteString(entry.Details)
buf.WriteByte('\n')
}
return buf.Bytes()
}
|
Changes to internal/box/compbox/sx.go.
︙ | | |
26
27
28
29
30
31
32
33
34
35
|
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
+
+
+
-
+
+
+
+
+
|
func genSxM(zid id.Zid) *meta.Meta {
return getTitledMeta(zid, "Zettelstore Sx Engine")
}
func genSxC(context.Context, *compBox) []byte {
var buf bytes.Buffer
buf.WriteString("|=Name|=Value>\n")
numSymbols := 0
for pkg := range sx.AllPackages() {
if size := pkg.Size(); size > 0 {
fmt.Fprintf(&buf, "|Symbols|%d\n", sx.MakeSymbol("NIL").Factory().Size())
fmt.Fprintf(&buf, "|Symbols in package %q|%d\n", pkg.Name(), size)
numSymbols += size
}
}
fmt.Fprintf(&buf, "|All symbols|%d\n", numSymbols)
return buf.Bytes()
}
|
Changes to internal/box/constbox/base.sxn.
︙ | | |
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
|
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
|
-
+
+
-
+
|
(meta (@ (charset "utf-8")))
(meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
(meta (@ (name "generator") (content "Zettelstore")))
(meta (@ (name "format-detection") (content "telephone=no")))
,@META-HEADER
(link (@ (rel "stylesheet") (href ,css-base-url)))
(link (@ (rel "stylesheet") (href ,css-user-url)))
,@(ROLE-DEFAULT-meta (current-binding))
,@(ROLE-DEFAULT-meta (current-frame))
,@(let* ((frame (current-frame))(rem (resolve-symbol 'ROLE-EXTRA-meta frame))) (if (defined? rem) (rem frame)))
(title ,title))
(body
(nav (@ (class "zs-menu"))
(a (@ (href ,home-url)) "Home")
,@(if with-auth
`((div (@ (class "zs-dropdown"))
(button "User")
(nav (@ (class "zs-dropdown-content"))
,@(if user-is-valid
`((a (@ (href ,user-zettel-url)) ,user-ident)
(a (@ (href ,logout-url)) "Logout"))
`((a (@ (href ,login-url)) "Login"))
)
)))
)
(div (@ (class "zs-dropdown"))
(button "Lists")
(nav (@ (class "zs-dropdown-content"))
,@list-urls
,@(if (bound? 'refresh-url) `((a (@ (href ,refresh-url)) "Refresh")))
,@(if (symbol-bound? 'refresh-url) `((a (@ (href ,refresh-url)) "Refresh")))
))
,@(if new-zettel-links
`((div (@ (class "zs-dropdown"))
(button "New")
(nav (@ (class "zs-dropdown-content"))
,@(map wui-link new-zettel-links)
)))
|
︙ | | |
Changes to internal/box/constbox/constbox.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
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
|
+
-
+
-
+
-
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
// Package constbox puts zettel inside the executable.
package constbox
import (
"context"
_ "embed" // Allow to embed file content
"log/slog"
"net/url"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
func init() {
manager.Register(
" const",
func(_ *url.URL, cdata *manager.ConnectData) (box.ManagedBox, error) {
return &constBox{
log: kernel.Main.GetLogger(kernel.BoxService).Clone().
logger: kernel.Main.GetLogger(kernel.BoxService).With("box", "const", "boxnum", cdata.Number),
Str("box", "const").Int("boxnum", int64(cdata.Number)).Child(),
number: cdata.Number,
zettel: constZettelMap,
enricher: cdata.Enricher,
}, nil
})
}
type constHeader map[string]string
type constZettel struct {
header constHeader
content zettel.Content
}
type constBox struct {
log *logger.Logger
logger *slog.Logger
number int
zettel map[id.Zid]constZettel
enricher box.Enricher
}
func (*constBox) Location() string { return "const:" }
func (cb *constBox) GetZettel(_ context.Context, zid id.Zid) (zettel.Zettel, error) {
if z, ok := cb.zettel[zid]; ok {
cb.log.Trace().Msg("GetZettel")
logging.LogTrace(cb.logger, "GetZettel")
return zettel.Zettel{Meta: meta.NewWithData(zid, z.header), Content: z.content}, nil
}
err := box.ErrZettelNotFound{Zid: zid}
cb.log.Trace().Err(err).Msg("GetZettel/Err")
logging.LogTrace(cb.logger, "GetZettel/Err", "err", err)
return zettel.Zettel{}, err
}
func (cb *constBox) HasZettel(_ context.Context, zid id.Zid) bool {
_, found := cb.zettel[zid]
return found
}
func (cb *constBox) ApplyZid(_ context.Context, handle box.ZidFunc, constraint query.RetrievePredicate) error {
cb.log.Trace().Int("entries", int64(len(cb.zettel))).Msg("ApplyZid")
logging.LogTrace(cb.logger, "ApplyZid", "entries", len(cb.zettel))
for zid := range cb.zettel {
if constraint(zid) {
handle(zid)
}
}
return nil
}
func (cb *constBox) ApplyMeta(ctx context.Context, handle box.MetaFunc, constraint query.RetrievePredicate) error {
cb.log.Trace().Int("entries", int64(len(cb.zettel))).Msg("ApplyMeta")
logging.LogTrace(cb.logger, "ApplyMeta", "entries", len(cb.zettel))
for zid, zettel := range cb.zettel {
if constraint(zid) {
m := meta.NewWithData(zid, zettel.header)
cb.enricher.Enrich(ctx, m, cb.number)
handle(m)
}
}
return nil
}
func (*constBox) CanDeleteZettel(context.Context, id.Zid) bool { return false }
func (cb *constBox) DeleteZettel(_ context.Context, zid id.Zid) (err error) {
if _, ok := cb.zettel[zid]; ok {
err = box.ErrReadOnly
} else {
err = box.ErrZettelNotFound{Zid: zid}
}
cb.log.Trace().Err(err).Msg("DeleteZettel")
logging.LogTrace(cb.logger, "DeleteZettel", logging.Err(err))
return err
}
func (cb *constBox) ReadStats(st *box.ManagedBoxStats) {
st.ReadOnly = true
st.Zettel = len(cb.zettel)
cb.log.Trace().Int("zettel", int64(st.Zettel)).Msg("ReadStats")
logging.LogTrace(cb.logger, "ReadStats", "zettel", st.Zettel)
}
var constZettelMap = map[id.Zid]constZettel{
id.ZidConfiguration: {
constHeader{
meta.KeyTitle: "Zettelstore Runtime Configuration",
meta.KeyRole: meta.ValueRoleConfiguration,
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
meta.KeyTitle: "Zettelstore Dependencies",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxZmk,
meta.KeyLang: meta.ValueLangEN,
meta.KeyReadOnly: meta.ValueTrue,
meta.KeyVisibility: meta.ValueVisibilityPublic,
meta.KeyCreated: "20210504135842",
meta.KeyModified: "20250312154700",
meta.KeyModified: "20250623150400",
},
zettel.NewContent(contentDependencies)},
id.ZidBaseTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Base HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20230510155100",
meta.KeyModified: "20241227212000",
meta.KeyModified: "20250623131400",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentBaseSxn)},
id.ZidLoginTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Login Form HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20200804111624",
meta.KeyModified: "20240219145200",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentLoginSxn)},
id.ZidZettelTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Zettel HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20230510155300",
meta.KeyModified: "20250214153100",
meta.KeyModified: "20250626113800",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentZettelSxn)},
id.ZidInfoTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Info HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20200804111624",
meta.KeyModified: "20241127170500",
meta.KeyModified: "20250624160000",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentInfoSxn)},
id.ZidFormTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Form HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20200804111624",
meta.KeyModified: "20240219145200",
meta.KeyModified: "20250612180300",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentFormSxn)},
id.ZidDeleteTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Delete HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20200804111624",
meta.KeyModified: "20241127170530",
meta.KeyModified: "20250612180200",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentDeleteSxn)},
id.ZidListTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore List Zettel HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20230704122100",
meta.KeyModified: "20240219145200",
meta.KeyModified: "20250612180200",
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentListZettelSxn)},
id.ZidErrorTemplate: {
constHeader{
meta.KeyTitle: "Zettelstore Error HTML Template",
meta.KeyRole: meta.ValueRoleConfiguration,
|
︙ | | |
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
|
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
zettel.NewContent(contentStartCodeSxn)},
id.ZidSxnBase: {
constHeader{
meta.KeyTitle: "Zettelstore Sxn Base Code",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20230619132800",
meta.KeyModified: "20241118173500",
meta.KeyModified: "20250624200200",
meta.KeyReadOnly: meta.ValueTrue,
meta.KeyVisibility: meta.ValueVisibilityExpert,
meta.KeyPrecursor: id.ZidSxnPrelude.String(),
},
zettel.NewContent(contentBaseCodeSxn)},
id.ZidSxnPrelude: {
constHeader{
meta.KeyTitle: "Zettelstore Sxn Prelude",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxSxn,
meta.KeyCreated: "20231006181700",
meta.KeyModified: "20240222121200",
meta.KeyReadOnly: meta.ValueTrue,
meta.KeyVisibility: meta.ValueVisibilityExpert,
},
zettel.NewContent(contentPreludeSxn)},
zettel.NewContent(contentBaseCodeSxn)},
id.ZidBaseCSS: {
constHeader{
meta.KeyTitle: "Zettelstore Base CSS",
meta.KeyRole: meta.ValueRoleConfiguration,
meta.KeySyntax: meta.ValueSyntaxCSS,
meta.KeyCreated: "20200804111624",
meta.KeyModified: "20240827143500",
|
︙ | | |
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
-
-
-
|
//go:embed start.sxn
var contentStartCodeSxn []byte
//go:embed wuicode.sxn
var contentBaseCodeSxn []byte
//go:embed prelude.sxn
var contentPreludeSxn []byte
//go:embed base.css
var contentBaseCSS []byte
//go:embed emoji_spin.gif
var contentEmoji []byte
//go:embed menu_lists.zettel
|
︙ | | |
Changes to internal/box/constbox/delete.sxn.
︙ | | |
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
-
+
|
,@(if incoming
`((div (@ (class "zs-warning"))
(h2 "Warning!")
(p "If you delete this zettel, incoming references from the following zettel will become invalid.")
(ul ,@(map wui-item-link incoming))
))
)
,@(if (and (bound? 'useless) useless)
,@(if (and (symbol-bound? 'useless) useless)
`((div (@ (class "zs-warning"))
(h2 "Warning!")
(p "Deleting this zettel will also delete the following files, so that they will not be interpreted as content for this zettel.")
(ul ,@(map wui-item useless))
))
)
,(wui-meta-desc metapairs)
|
︙ | | |
Changes to internal/box/constbox/dependencies.zettel.
︙ | | |
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
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
|
-
+
+
+
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
=== Sx, SxWebs, Webs, Zero, Zettelstore-Client
=== Sx, SxWebs, Webs, Zero, Zettelstore-Client, Zsx
These are companion projects, written by the main developer of Zettelstore.
They are published under the same license, [[EUPL v1.2, or later|00000000000004]].
; URL & Source Sx
: [[https://t73f.de/r/sx]]
; URL & Source SxWebs
: [[https://t73f.de/r/sxwebs]]
; URL & Source Webs
: [[https://t73f.de/r/webs]]
; URL & Source Zero
: [[https://t73f.de/r/zero]]
; URL & Source Zettelstore-Client
: [[https://t73f.de/r/zsc]]
; URL & Source Zsx
: [[https://t73f.de/r/zsx]]
; License:
: European Union Public License, version 1.2 (EUPL v1.2), or later.
|
Changes to internal/box/constbox/form.sxn.
︙ | | |
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
-
+
|
(input (@ (class "zs-input") (type "text") (pattern "\\w*") (id "zs-syntax") (name "syntax")
(title "Syntax/format of zettel content below, one word, letters and digits, no spaces.")
(placeholder "syntax..") (value ,meta-syntax) (dir "auto")
,@(if syntax-data '((list "zs-syntax-data")))
))
,@(wui-datalist "zs-syntax-data" syntax-data)
)
,@(if (bound? 'content)
,@(if (symbol-bound? 'content)
`((div
(label (@ (for "zs-content")) "Content " (a (@ (title "Content for this zettel, according to above syntax.")) (@H "ⓘ")))
(textarea (@ (class "zs-input zs-content") (id "zs-content") (name "content") (rows "20")
(title "Zettel content, according to the given syntax")
(placeholder "Zettel content..") (dir "auto")) ,content)
))
)
|
︙ | | |
Changes to internal/box/constbox/info.sxn.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
|
;;; SPDX-FileCopyrightText: 2023-present Detlef Stern
;;;----------------------------------------------------------------------------
`(article
(header (h1 "Information for Zettel " ,zid)
(p
(a (@ (href ,web-url)) "Web")
(@H " · ") (a (@ (href ,context-url)) "Context")
(@H " / ") (a (@ (href ,context-full-url)) "Full")
,@(if (bound? 'edit-url) `((@H " · ") (a (@ (href ,edit-url)) "Edit")))
,@(ROLE-DEFAULT-actions (current-binding))
,@(if (bound? 'reindex-url) `((@H " · ") (a (@ (href ,reindex-url)) "Reindex")))
,@(if (bound? 'delete-url) `((@H " · ") (a (@ (href ,delete-url)) "Delete")))
,@(if (symbol-bound? 'edit-url) `((@H " · ") (a (@ (href ,edit-url)) "Edit")))
(@H " · ") (a (@ (href ,context-full-url)) "Full Context")
,@(if (symbol-bound? 'thread-query-url)
`((@H " · [") (a (@ (href ,thread-query-url)) "Thread")
,@(if (symbol-bound? 'folge-query-url) `((@H ", ") (a (@ (href ,folge-query-url)) "Folge")))
,@(if (symbol-bound? 'sequel-query-url) `((@H ", ") (a (@ (href ,sequel-query-url)) "Sequel")))
(@H "]")))
,@(ROLE-DEFAULT-actions (current-frame))
,@(let* ((frame (current-frame))(rea (resolve-symbol 'ROLE-EXTRA-actions frame))) (if (defined? rea) (rea frame)))
,@(if (symbol-bound? 'reindex-url) `((@H " · ") (a (@ (href ,reindex-url)) "Reindex")))
,@(if (symbol-bound? 'delete-url) `((@H " · ") (a (@ (href ,delete-url)) "Delete")))
)
)
(h2 "Interpreted Metadata")
(table ,@(map wui-info-meta-table-row metadata))
(h2 "References")
,@(if local-links `((h3 "Local") (ul ,@(map wui-local-link local-links))))
,@(if query-links `((h3 "Queries") (ul ,@(map wui-item-link query-links))))
|
︙ | | |
Changes to internal/box/constbox/listzettel.sxn.
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
-
+
+
-
+
|
`(article
(header (h1 ,heading))
(search (form (@ (action ,search-url))
(input (@ (class "zs-input") (type "search") (inputmode "search") (name ,query-key-query)
(title "Contains the search that leads to the list below. You're allowed to modify it")
(placeholder "Search..") (value ,query-value) (dir "auto")))))
,@(if (bound? 'tag-zettel)
,@(if (symbol-bound? 'tag-zettel)
`((p (@ (class "zs-meta-zettel")) "Tag zettel: " ,@tag-zettel))
)
,@(if (bound? 'create-tag-zettel)
,@(if (symbol-bound? 'create-tag-zettel)
`((p (@ (class "zs-meta-zettel")) "Create tag zettel: " ,@create-tag-zettel))
)
,@(if (bound? 'role-zettel)
,@(if (symbol-bound? 'role-zettel)
`((p (@ (class "zs-meta-zettel")) "Role zettel: " ,@role-zettel))
)
,@(if (bound? 'create-role-zettel)
,@(if (symbol-bound? 'create-role-zettel)
`((p (@ (class "zs-meta-zettel")) "Create role zettel: " ,@create-role-zettel))
)
,@content
,@endnotes
(form (@ (action ,(if (bound? 'create-url) create-url)))
,(if (bound? 'data-url)
(form (@ (action ,(if (symbol-bound? 'create-url) create-url)))
,(if (symbol-bound? 'data-url)
`(@L "Other encodings"
,(if (> num-entries 3) `(@L " of these " ,num-entries " entries: ") ": ")
(a (@ (href ,data-url)) "data")
", "
(a (@ (href ,plain-url)) "plain")
)
)
,@(if (bound? 'create-url)
,@(if (symbol-bound? 'create-url)
`((input (@ (type "hidden") (name ,query-key-query) (value ,query-value)))
(input (@ (type "hidden") (name ,query-key-seed) (value ,seed)))
(input (@ (class "zs-primary") (type "submit") (value "Save As Zettel")))
)
)
)
)
|
Deleted internal/box/constbox/prelude.sxn.
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
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
;;;----------------------------------------------------------------------------
;;; Copyright (c) 2023-present Detlef Stern
;;;
;;; This file is part of Zettelstore.
;;;
;;; Zettelstore 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
;;;----------------------------------------------------------------------------
;;; This zettel contains sxn definitions that are independent of specific
;;; subsystems, such as WebUI, API, or other. It just contains generic code to
;;; be used in all places. It asumes that the symbols NIL and T are defined.
;; not macro
(defmacro not (x) `(if ,x NIL T))
;; not= macro, to negate an equivalence
(defmacro not= args `(not (= ,@args)))
;; let* macro
;;
;; (let* (BINDING ...) EXPR ...), where SYMBOL may occur in later bindings.
(defmacro let* (bindings . body)
(if (null? bindings)
`(begin ,@body)
`(let ((,(caar bindings) ,(cadar bindings)))
(let* ,(cdr bindings) ,@body))))
;; cond macro
;;
;; (cond ((COND EXPR) ...))
(defmacro cond clauses
(if (null? clauses)
()
(let* ((clause (car clauses))
(the-cond (car clause)))
(if (= the-cond T)
`(begin ,@(cdr clause))
`(if ,the-cond
(begin ,@(cdr clause))
(cond ,@(cdr clauses)))))))
;; and macro
;;
;; (and EXPR ...)
(defmacro and args
(cond ((null? args) T)
((null? (cdr args)) (car args))
(T `(if ,(car args) (and ,@(cdr args))))))
;; or macro
;;
;; (or EXPR ...)
(defmacro or args
(cond ((null? args) NIL)
((null? (cdr args)) (car args))
(T `(if ,(car args) T (or ,@(cdr args))))))
|
Changes to internal/box/constbox/wuicode.sxn.
︙ | | |
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
|
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
|
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
-
+
-
+
-
+
-
-
+
-
+
-
-
+
-
-
+
+
-
+
-
+
|
;; wui-enc-matrix returns the HTML table of all encodings and parts.
(defun wui-enc-matrix (matrix)
`(table
,@(map
(lambda (row) `(tr (th ,(car row)) ,@(map wui-tdata-link (cdr row))))
matrix)))
;; wui-optional-link puts the text into a link, if symbol is defined. Otherwise just return the text
(defun wui-optional-link (text url-sym)
(let ((url (resolve-symbol url-sym)))
(if (defined? url)
`(a (@ (href ,(resolve-symbol url-sym))) ,text)
text)))
;; CSS-ROLE-map is a mapping (pair list, assoc list) of role names to zettel
;; identifier. It is used in the base template to update the metadata of the
;; HTML page to include some role specific CSS code.
;; Referenced in function "ROLE-DEFAULT-meta".
(defvar CSS-ROLE-map '())
;; ROLE-DEFAULT-meta returns some metadata for the base template. Any role
;; specific code should include the returned list of this function.
(defun ROLE-DEFAULT-meta (binding)
`(,@(let* ((meta-role (binding-lookup 'meta-role binding))
(defun ROLE-DEFAULT-meta (frame)
`(,@(let* ((meta-role (resolve-symbol 'meta-role frame))
(entry (assoc CSS-ROLE-map meta-role)))
(if (pair? entry)
`((link (@ (rel "stylesheet") (href ,(zid-content-path (cdr entry))))))
)
)
)
)
;; ACTION-SEPARATOR defines a HTML value that separates actions links.
(defvar ACTION-SEPARATOR '(@H " · "))
;; ROLE-DEFAULT-actions returns the default text for actions.
(defun ROLE-DEFAULT-actions (binding)
`(,@(let ((copy-url (binding-lookup 'copy-url binding)))
(defun ROLE-DEFAULT-actions (frame)
`(,@(let ((copy-url (resolve-symbol 'copy-url frame)))
(if (defined? copy-url) `((@H " · ") (a (@ (href ,copy-url)) "Copy"))))
,@(let ((version-url (binding-lookup 'version-url binding)))
(if (defined? version-url) `((@H " · ") (a (@ (href ,version-url)) "Version"))))
,@(let ((sequel-url (binding-lookup 'sequel-url binding)))
,@(let ((sequel-url (resolve-symbol 'sequel-url frame)))
(if (defined? sequel-url) `((@H " · ") (a (@ (href ,sequel-url)) "Sequel"))))
,@(let ((folge-url (binding-lookup 'folge-url binding)))
,@(let ((folge-url (resolve-symbol 'folge-url frame)))
(if (defined? folge-url) `((@H " · ") (a (@ (href ,folge-url)) "Folge"))))
)
)
;; ROLE-tag-actions returns an additional action "Zettel" for zettel with role "tag".
(defun ROLE-tag-actions (binding)
(defun ROLE-tag-actions (frame)
`(,@(ROLE-DEFAULT-actions binding)
,@(let ((title (binding-lookup 'title binding)))
`(,@(let ((title (resolve-symbol 'title frame)))
(if (and (defined? title) title)
`(,ACTION-SEPARATOR (a (@ (href ,(query->url (concat "tags:" title)))) "Zettel"))
)
)
)
)
;; ROLE-role-actions returns an additional action "Zettel" for zettel with role "role".
(defun ROLE-role-actions (binding)
(defun ROLE-role-actions (frame)
`(,@(ROLE-DEFAULT-actions binding)
,@(let ((title (binding-lookup 'title binding)))
`(,@(let ((title (resolve-symbol 'title frame)))
(if (and (defined? title) title)
`(,ACTION-SEPARATOR (a (@ (href ,(query->url (concat "role:" title)))) "Zettel"))
)
)
)
)
;; ROLE-DEFAULT-heading returns the default text for headings, below the
;; references of a zettel. In most cases it should be called from an
;; overwriting function.
(defun ROLE-DEFAULT-heading (binding)
`(,@(let ((meta-url (binding-lookup 'meta-url binding)))
(defun ROLE-DEFAULT-heading (frame)
`(,@(let ((meta-url (resolve-symbol 'meta-url frame)))
(if (defined? meta-url) `((br) "URL: " ,(url-to-html meta-url))))
,@(let ((urls (binding-lookup 'urls binding)))
,@(let ((urls (resolve-symbol 'urls frame)))
(if (defined? urls)
(map (lambda (u) `(@L (br) ,(car u) ": " ,(url-to-html (cdr u)))) urls)
)
)
,@(let ((meta-author (binding-lookup 'meta-author binding)))
,@(let ((meta-author (resolve-symbol 'meta-author frame)))
(if (and (defined? meta-author) meta-author) `((br) "By " ,meta-author)))
)
)
|
Changes to internal/box/constbox/zettel.sxn.
︙ | | |
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
|
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
|
-
+
-
-
+
+
+
+
-
+
+
-
-
-
-
+
+
+
+
-
+
+
+
+
-
-
-
+
+
+
+
+
|
;;; SPDX-FileCopyrightText: 2023-present Detlef Stern
;;;----------------------------------------------------------------------------
`(article
(header
(h1 ,heading)
(div (@ (class "zs-meta"))
,@(if (bound? 'edit-url) `((a (@ (href ,edit-url)) "Edit") (@H " · ")))
,@(if (symbol-bound? 'edit-url) `((a (@ (href ,edit-url)) "Edit") (@H " · ")))
,zid (@H " · ")
(a (@ (href ,info-url)) "Info") (@H " · ")
"(" ,@(if (bound? 'role-url) `((a (@ (href ,role-url)) ,meta-role)))
,@(if (and (bound? 'folge-role-url) (bound? 'meta-folge-role))
"(" ,@(if (symbol-bound? 'role-url) `((a (@ (href ,role-url)) ,meta-role)))
,@(if (and (symbol-bound? 'folge-role-url) (symbol-bound? 'meta-folge-role))
`((@H " → ") (a (@ (href ,folge-role-url)) ,meta-folge-role)))
")"
,@(if tag-refs `((@H " · ") ,@tag-refs))
(@H " · ") (a (@ (href ,context-url)) "Context")
,@(if (symbol-bound? 'thread-query-url) `((@H " · ") (a (@ (href ,thread-query-url)) "Thread")))
,@(ROLE-DEFAULT-actions (current-binding))
,@(ROLE-DEFAULT-actions (current-frame))
,@(let* ((frame (current-frame))(rea (resolve-symbol 'ROLE-EXTRA-actions frame))) (if (defined? rea) (rea frame)))
,@(if superior-refs `((br) "Superior: " ,superior-refs))
,@(if predecessor-refs `((br) "Predecessor: " ,predecessor-refs))
,@(if prequel-refs `((br) "Prequel: " ,prequel-refs))
,@(if precursor-refs `((br) "Precursor: " ,precursor-refs))
,@(ROLE-DEFAULT-heading (current-binding))
,@(if prequel-refs `((br) ,(wui-optional-link "Prequel" 'sequel-query-url) ": " ,prequel-refs))
,@(if precursor-refs `((br) ,(wui-optional-link "Precursor" 'folge-query-url) ": " ,precursor-refs))
,@(ROLE-DEFAULT-heading (current-frame))
,@(let* ((frame (current-frame))(reh (resolve-symbol 'ROLE-EXTRA-heading frame))) (if (defined? reh) (reh frame)))
)
)
,@content
,endnotes
,@(if (or folge-links sequel-links back-links successor-links subordinate-links)
,@(if (or folge-links sequel-links back-links subordinate-links)
`((nav
,@(if folge-links
`((details (@ (,folge-open))
(summary ,(wui-optional-link "Folgezettel" 'folge-query-url))
,@(if folge-links `((details (@ (,folge-open)) (summary "Folgezettel") (ul ,@(map wui-item-link folge-links)))))
,@(if sequel-links `((details (@ (,sequel-open)) (summary "Sequel") (ul ,@(map wui-item-link sequel-links)))))
,@(if successor-links `((details (@ (,successor-open)) (summary "Successors") (ul ,@(map wui-item-link successor-links)))))
(ul ,@(map wui-item-link folge-links)))))
,@(if sequel-links
`((details (@ (,sequel-open))
(summary ,(wui-optional-link "Sequel" 'sequel-query-url))
(ul ,@(map wui-item-link sequel-links)))))
,@(if subordinate-links `((details (@ (,subordinate-open)) (summary "Subordinates") (ul ,@(map wui-item-link subordinate-links)))))
,@(if back-links `((details (@ (,back-open)) (summary "Incoming") (ul ,@(map wui-item-link back-links)))))
))
)
)
|
Changes to internal/box/dirbox/dirbox.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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
|
+
-
+
-
+
-
+
-
+
-
+
|
// Package dirbox provides a directory-based zettel box.
package dirbox
import (
"context"
"errors"
"log/slog"
"net/url"
"os"
"path/filepath"
"sync"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager"
"zettelstore.de/z/internal/box/notify"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
func init() {
manager.Register("dir", func(u *url.URL, cdata *manager.ConnectData) (box.ManagedBox, error) {
var log *logger.Logger
var logger *slog.Logger
if krnl := kernel.Main; krnl != nil {
log = krnl.GetLogger(kernel.BoxService).Clone().Str("box", "dir").Int("boxnum", int64(cdata.Number)).Child()
logger = krnl.GetLogger(kernel.BoxService).With("box", "dir", "boxnum", cdata.Number)
}
path := getDirPath(u)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil, err
}
dp := dirBox{
log: log,
logger: logger,
number: cdata.Number,
location: u.String(),
readonly: box.GetQueryBool(u, "readonly"),
cdata: *cdata,
dir: path,
notifySpec: getDirSrvInfo(log, u.Query().Get("type")),
notifySpec: getDirSrvInfo(logger, u.Query().Get("type")),
fSrvs: makePrime(uint32(box.GetQueryInt(u, "worker", 1, 7, 1499))),
}
return &dp, nil
})
}
func makePrime(n uint32) uint32 {
|
︙ | | |
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
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
-
+
-
+
-
+
|
const (
_ notifyTypeSpec = iota
dirNotifyAny
dirNotifySimple
dirNotifyFS
)
func getDirSrvInfo(log *logger.Logger, notifyType string) notifyTypeSpec {
func getDirSrvInfo(logger *slog.Logger, notifyType string) notifyTypeSpec {
for range 2 {
switch notifyType {
case kernel.BoxDirTypeNotify:
return dirNotifyFS
case kernel.BoxDirTypeSimple:
return dirNotifySimple
default:
notifyType = kernel.Main.GetConfig(kernel.BoxService, kernel.BoxDefaultDirType).(string)
}
}
log.Error().Str("notifyType", notifyType).Msg("Unable to set notify type, using a default")
logger.Error("Unable to set notify type, using a default", "notifyType", notifyType)
return dirNotifySimple
}
func getDirPath(u *url.URL) string {
if u.Opaque != "" {
return filepath.Clean(u.Opaque)
}
return filepath.Clean(u.Path)
}
// dirBox uses a directory to store zettel as files.
type dirBox struct {
log *logger.Logger
logger *slog.Logger
number int
location string
readonly bool
cdata manager.ConnectData
dir string
notifySpec notifyTypeSpec
dirSrv *notify.DirService
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
func (dp *dirBox) Start(context.Context) error {
dp.mxCmds.Lock()
defer dp.mxCmds.Unlock()
dp.fCmds = make([]chan fileCmd, 0, dp.fSrvs)
for i := range dp.fSrvs {
cc := make(chan fileCmd)
go fileService(i, dp.log.Clone().Str("sub", "file").Uint("fn", uint64(i)).Child(), dp.dir, cc)
go fileService(i, dp.logger.With("sub", "file", "fn", i), dp.dir, cc)
dp.fCmds = append(dp.fCmds, cc)
}
var notifier notify.Notifier
var err error
switch dp.notifySpec {
case dirNotifySimple:
notifier, err = notify.NewSimpleDirNotifier(dp.log.Clone().Str("notify", "simple").Child(), dp.dir)
notifier, err = notify.NewSimpleDirNotifier(dp.logger.With("notify", "simple"), dp.dir)
default:
notifier, err = notify.NewFSDirNotifier(dp.log.Clone().Str("notify", "fs").Child(), dp.dir)
notifier, err = notify.NewFSDirNotifier(dp.logger.With("notify", "fs"), dp.dir)
}
if err != nil {
dp.log.Error().Err(err).Msg("Unable to create directory supervisor")
dp.logger.Error("Unable to create directory supervisor", "err", err)
dp.stopFileServices()
return err
}
dp.dirSrv = notify.NewDirService(
dp,
dp.log.Clone().Str("sub", "dirsrv").Child(),
dp.logger.With("sub", "dirsrv"),
notifier,
dp.cdata.Notify,
)
dp.dirSrv.Start()
return nil
}
func (dp *dirBox) Refresh(_ context.Context) {
dp.dirSrv.Refresh()
dp.log.Trace().Msg("Refresh")
logging.LogTrace(dp.logger, "Refresh")
}
func (dp *dirBox) Stop(_ context.Context) {
dirSrv := dp.dirSrv
dp.dirSrv = nil
if dirSrv != nil {
dirSrv.Stop()
}
dp.stopFileServices()
}
func (dp *dirBox) stopFileServices() {
for _, c := range dp.fCmds {
close(c)
}
}
func (dp *dirBox) notifyChanged(zid id.Zid, reason box.UpdateReason) {
if notify := dp.cdata.Notify; notify != nil {
dp.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChanged")
logging.LogTrace(dp.logger, "notifyChanged", "zid", zid, "reason", reason)
notify(dp, zid, reason)
}
}
func (dp *dirBox) getFileChan(zid id.Zid) chan fileCmd {
// Based on https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
sum := 2166136261 ^ uint32(zid)
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
|
dp.updateEntryFromMetaContent(&entry, meta, zettel.Content)
err = dp.srvSetZettel(ctx, &entry, zettel)
if err == nil {
err = dp.dirSrv.UpdateDirEntry(&entry)
}
dp.notifyChanged(meta.Zid, box.OnZettel)
dp.log.Trace().Err(err).Zid(meta.Zid).Msg("CreateZettel")
logging.LogTrace(dp.logger, "CreateZettel", logging.Err(err), "zid", meta.Zid)
return meta.Zid, err
}
func (dp *dirBox) GetZettel(ctx context.Context, zid id.Zid) (zettel.Zettel, error) {
entry := dp.dirSrv.GetDirEntry(zid)
if !entry.IsValid() {
return zettel.Zettel{}, box.ErrZettelNotFound{Zid: zid}
}
m, c, err := dp.srvGetMetaContent(ctx, entry, zid)
if err != nil {
return zettel.Zettel{}, err
}
zettel := zettel.Zettel{Meta: m, Content: zettel.NewContent(c)}
dp.log.Trace().Zid(zid).Msg("GetZettel")
logging.LogTrace(dp.logger, "GetZettel", "zid", zid)
return zettel, nil
}
func (dp *dirBox) HasZettel(_ context.Context, zid id.Zid) bool {
return dp.dirSrv.GetDirEntry(zid).IsValid()
}
func (dp *dirBox) ApplyZid(_ context.Context, handle box.ZidFunc, constraint query.RetrievePredicate) error {
entries := dp.dirSrv.GetDirEntries(constraint)
dp.log.Trace().Int("entries", int64(len(entries))).Msg("ApplyZid")
logging.LogTrace(dp.logger, "ApplyZid", "entries", len(entries))
for _, entry := range entries {
handle(entry.Zid)
}
return nil
}
func (dp *dirBox) ApplyMeta(ctx context.Context, handle box.MetaFunc, constraint query.RetrievePredicate) error {
entries := dp.dirSrv.GetDirEntries(constraint)
dp.log.Trace().Int("entries", int64(len(entries))).Msg("ApplyMeta")
logging.LogTrace(dp.logger, "ApplyMeta", "entries", len(entries))
// The following loop could be parallelized if needed for performance.
for _, entry := range entries {
m, err := dp.srvGetMeta(ctx, entry, entry.Zid)
if err != nil {
dp.log.Trace().Err(err).Msg("ApplyMeta/getMeta")
logging.LogTrace(dp.logger, "ApplyMeta/getMeta", "err", err)
return err
}
dp.cdata.Enricher.Enrich(ctx, m, dp.number)
handle(m)
}
return nil
}
|
︙ | | |
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
-
-
+
+
+
+
+
-
+
|
}
entry := dp.dirSrv.GetDirEntry(zid)
if !entry.IsValid() {
// Existing zettel, but new in this box.
entry = ¬ify.DirEntry{Zid: zid}
}
dp.updateEntryFromMetaContent(entry, meta, zettel.Content)
dp.dirSrv.UpdateDirEntry(entry)
err := dp.srvSetZettel(ctx, entry, zettel)
err := dp.dirSrv.UpdateDirEntry(entry)
if err != nil {
return err
}
err = dp.srvSetZettel(ctx, entry, zettel)
if err == nil {
dp.notifyChanged(zid, box.OnZettel)
}
dp.log.Trace().Zid(zid).Err(err).Msg("UpdateZettel")
logging.LogTrace(dp.logger, "UpdateZettel", "zid", zid, logging.Err(err))
return err
}
func (dp *dirBox) updateEntryFromMetaContent(entry *notify.DirEntry, m *meta.Meta, content zettel.Content) {
entry.SetupFromMetaContent(m, content, dp.cdata.Config.GetZettelFileSyntax)
}
|
︙ | | |
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
-
+
-
+
|
if err != nil {
return nil
}
err = dp.srvDeleteZettel(ctx, entry, zid)
if err == nil {
dp.notifyChanged(zid, box.OnDelete)
}
dp.log.Trace().Zid(zid).Err(err).Msg("DeleteZettel")
logging.LogTrace(dp.logger, "DeleteZettel", "zid", zid, logging.Err(err))
return err
}
func (dp *dirBox) ReadStats(st *box.ManagedBoxStats) {
st.ReadOnly = dp.readonly
st.Zettel = dp.dirSrv.NumDirEntries()
dp.log.Trace().Int("zettel", int64(st.Zettel)).Msg("ReadStats")
logging.LogTrace(dp.logger, "ReadStats", "zettel", st.Zettel)
}
|
Changes to internal/box/dirbox/service.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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
|
+
-
-
+
-
+
-
+
-
+
|
package dirbox
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"time"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"t73f.de/r/zsx/input"
"zettelstore.de/z/internal/box/filebox"
"zettelstore.de/z/internal/box/notify"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/zettel"
)
func fileService(i uint32, log *logger.Logger, dirPath string, cmds <-chan fileCmd) {
func fileService(i uint32, logger *slog.Logger, dirPath string, cmds <-chan fileCmd) {
// Something may panic. Ensure a running service.
defer func() {
if ri := recover(); ri != nil {
kernel.Main.LogRecover("FileService", ri)
go fileService(i, log, dirPath, cmds)
go fileService(i, logger, dirPath, cmds)
}
}()
log.Debug().Uint("i", uint64(i)).Str("dirpath", dirPath).Msg("File service started")
logger.Debug("File service started", "i", i, "dirpath", dirPath)
for cmd := range cmds {
cmd.run(dirPath)
}
log.Debug().Uint("i", uint64(i)).Str("dirpath", dirPath).Msg("File service stopped")
logger.Debug("File service stopped", "i", i, "dirpath", dirPath)
}
type fileCmd interface {
run(string)
}
const serviceTimeout = 5 * time.Second // must be shorter than the web servers timeout values for reading+writing.
|
︙ | | |
Changes to internal/box/filebox/filebox.go.
︙ | | |
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
-
+
-
|
manager.Register("file", func(u *url.URL, cdata *manager.ConnectData) (box.ManagedBox, error) {
path := getFilepathFromURL(u)
ext := strings.ToLower(filepath.Ext(path))
if ext != ".zip" {
return nil, errors.New("unknown extension '" + ext + "' in box URL: " + u.String())
}
return &zipBox{
log: kernel.Main.GetLogger(kernel.BoxService).Clone().
logger: kernel.Main.GetLogger(kernel.BoxService).With("box", "zip", "boxnum", cdata.Number),
Str("box", "zip").Int("boxnum", int64(cdata.Number)).Child(),
number: cdata.Number,
name: path,
enricher: cdata.Enricher,
notify: cdata.Notify,
}, nil
})
}
|
︙ | | |
Changes to internal/box/filebox/zipbox.go.
︙ | | |
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
|
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
|
+
-
+
-
+
|
package filebox
import (
"archive/zip"
"context"
"fmt"
"io"
"log/slog"
"strings"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"t73f.de/r/zsx/input"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/notify"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
type zipBox struct {
log *logger.Logger
logger *slog.Logger
number int
name string
enricher box.Enricher
notify box.UpdateNotifier
dirSrv *notify.DirService
}
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
+
+
-
+
-
+
|
}
func (zb *zipBox) Start(context.Context) error {
reader, err := zip.OpenReader(zb.name)
if err != nil {
return err
}
reader.Close()
zipNotifier := notify.NewSimpleZipNotifier(zb.log, zb.name)
zb.dirSrv = notify.NewDirService(zb, zb.log, zipNotifier, zb.notify)
if err = reader.Close(); err != nil {
return err
}
zipNotifier := notify.NewSimpleZipNotifier(zb.logger, zb.name)
zb.dirSrv = notify.NewDirService(zb, zb.logger, zipNotifier, zb.notify)
zb.dirSrv.Start()
return nil
}
func (zb *zipBox) Refresh(_ context.Context) {
zb.dirSrv.Refresh()
zb.log.Trace().Msg("Refresh")
logging.LogTrace(zb.logger, "Refresh")
}
func (zb *zipBox) Stop(context.Context) {
zb.dirSrv.Stop()
zb.dirSrv = nil
}
func (zb *zipBox) GetZettel(_ context.Context, zid id.Zid) (zettel.Zettel, error) {
entry := zb.dirSrv.GetDirEntry(zid)
if !entry.IsValid() {
return zettel.Zettel{}, box.ErrZettelNotFound{Zid: zid}
}
reader, err := zip.OpenReader(zb.name)
if err != nil {
return zettel.Zettel{}, err
}
defer reader.Close()
defer func() { _ = reader.Close() }()
var m *meta.Meta
var src []byte
var inMeta bool
contentName := entry.ContentName
if metaName := entry.MetaName; metaName == "" {
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
+
-
+
-
+
-
+
|
if err != nil {
return zettel.Zettel{}, err
}
}
}
CleanupMeta(m, zid, entry.ContentExt, inMeta, entry.UselessFiles)
zb.log.Trace().Zid(zid).Msg("GetZettel")
logging.LogTrace(zb.logger, "GetZettel", "zid", zid)
return zettel.Zettel{Meta: m, Content: zettel.NewContent(src)}, nil
}
func (zb *zipBox) HasZettel(_ context.Context, zid id.Zid) bool {
return zb.dirSrv.GetDirEntry(zid).IsValid()
}
func (zb *zipBox) ApplyZid(_ context.Context, handle box.ZidFunc, constraint query.RetrievePredicate) error {
entries := zb.dirSrv.GetDirEntries(constraint)
zb.log.Trace().Int("entries", int64(len(entries))).Msg("ApplyZid")
logging.LogTrace(zb.logger, "ApplyZid", "entries", len(entries))
for _, entry := range entries {
handle(entry.Zid)
}
return nil
}
func (zb *zipBox) ApplyMeta(ctx context.Context, handle box.MetaFunc, constraint query.RetrievePredicate) error {
reader, err := zip.OpenReader(zb.name)
if err != nil {
return err
}
defer reader.Close()
entries := zb.dirSrv.GetDirEntries(constraint)
zb.log.Trace().Int("entries", int64(len(entries))).Msg("ApplyMeta")
logging.LogTrace(zb.logger, "ApplyMeta", "entries", len(entries))
for _, entry := range entries {
if !constraint(entry.Zid) {
continue
}
m, err2 := zb.readZipMeta(reader, entry.Zid, entry)
if err2 != nil {
continue
}
zb.enricher.Enrich(ctx, m, zb.number)
handle(m)
}
return nil
return reader.Close()
}
func (*zipBox) CanDeleteZettel(context.Context, id.Zid) bool { return false }
func (zb *zipBox) DeleteZettel(_ context.Context, zid id.Zid) error {
err := box.ErrReadOnly
entry := zb.dirSrv.GetDirEntry(zid)
if !entry.IsValid() {
err = box.ErrZettelNotFound{Zid: zid}
}
zb.log.Trace().Err(err).Msg("DeleteZettel")
logging.LogTrace(zb.logger, "DeleteZettel", "err", err)
return err
}
func (zb *zipBox) ReadStats(st *box.ManagedBoxStats) {
st.ReadOnly = true
st.Zettel = zb.dirSrv.NumDirEntries()
zb.log.Trace().Int("zettel", int64(st.Zettel)).Msg("ReadStats")
logging.LogTrace(zb.logger, "ReadStats", "zettel", st.Zettel)
}
func (zb *zipBox) readZipMeta(reader *zip.ReadCloser, zid id.Zid, entry *notify.DirEntry) (m *meta.Meta, err error) {
var inMeta bool
if metaName := entry.MetaName; metaName == "" {
contentName := entry.ContentName
contentExt := entry.ContentExt
|
︙ | | |
222
223
224
225
226
227
228
229
230
231
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
+
-
-
+
+
+
+
+
|
}
func readZipFileContent(reader *zip.ReadCloser, name string) ([]byte, error) {
f, err := reader.Open(name)
if err != nil {
return nil, err
}
data, err := io.ReadAll(f)
defer f.Close()
return io.ReadAll(f)
err2 := f.Close()
if err == nil {
err = err2
}
return data, err
}
|
Changes to internal/box/manager/box.go.
︙ | | |
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
+
|
"strings"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/id/idset"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
// Conatains all box.Box related functions
// Location returns some information where the box is located.
|
︙ | | |
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
|
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
|
-
+
-
+
|
return box.CanCreateZettel(ctx)
}
return false
}
// CreateZettel creates a new zettel.
func (mgr *Manager) CreateZettel(ctx context.Context, ztl zettel.Zettel) (id.Zid, error) {
mgr.mgrLog.Debug().Msg("CreateZettel")
mgr.mgrLogger.Debug("CreateZettel")
if err := mgr.checkContinue(ctx); err != nil {
return id.Invalid, err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
if box, isWriteBox := mgr.boxes[0].(box.WriteBox); isWriteBox {
ztl.Meta = mgr.cleanMetaProperties(ztl.Meta)
zid, err := box.CreateZettel(ctx, ztl)
if err == nil {
mgr.idxUpdateZettel(ctx, ztl)
}
return zid, err
}
return id.Invalid, box.ErrReadOnly
}
// GetZettel retrieves a specific zettel.
func (mgr *Manager) GetZettel(ctx context.Context, zid id.Zid) (zettel.Zettel, error) {
mgr.mgrLog.Debug().Zid(zid).Msg("GetZettel")
mgr.mgrLogger.Debug("GetZettel", "zid", zid)
if err := mgr.checkContinue(ctx); err != nil {
return zettel.Zettel{}, err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
return mgr.getZettel(ctx, zid)
}
|
︙ | | |
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
|
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
|
-
+
-
+
|
}
}
return zettel.Zettel{}, box.ErrZettelNotFound{Zid: zid}
}
// GetAllZettel retrieves a specific zettel from all managed boxes.
func (mgr *Manager) GetAllZettel(ctx context.Context, zid id.Zid) ([]zettel.Zettel, error) {
mgr.mgrLog.Debug().Zid(zid).Msg("GetAllZettel")
mgr.mgrLogger.Debug("GetAllZettel", "zid", zid)
if err := mgr.checkContinue(ctx); err != nil {
return nil, err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
var result []zettel.Zettel
for i, p := range mgr.boxes {
if z, err := p.GetZettel(ctx, zid); err == nil {
mgr.Enrich(ctx, z.Meta, i+1)
result = append(result, z)
}
}
return result, nil
}
// FetchZids returns the set of all zettel identifer managed by the box.
func (mgr *Manager) FetchZids(ctx context.Context) (*idset.Set, error) {
mgr.mgrLog.Debug().Msg("FetchZids")
mgr.mgrLogger.Debug("FetchZids")
if err := mgr.checkContinue(ctx); err != nil {
return nil, err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
return mgr.fetchZids(ctx)
}
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
+
-
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
return nil, err
}
}
return result, nil
}
func (mgr *Manager) hasZettel(ctx context.Context, zid id.Zid) bool {
mgr.mgrLog.Debug().Zid(zid).Msg("HasZettel")
mgr.mgrLogger.Debug("HasZettel", "zid", zid)
if err := mgr.checkContinue(ctx); err != nil {
return false
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
for _, bx := range mgr.boxes {
if bx.HasZettel(ctx, zid) {
return true
}
}
return false
}
// GetMeta returns just the metadata of the zettel with the given identifier.
func (mgr *Manager) GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) {
mgr.mgrLog.Debug().Zid(zid).Msg("GetMeta")
mgr.mgrLogger.Debug("GetMeta", "zid", zid)
if err := mgr.checkContinue(ctx); err != nil {
return nil, err
}
m, err := mgr.idxStore.GetMeta(ctx, zid)
if err != nil {
// TODO: Call GetZettel and return just metadata, in case the index is not complete.
return nil, err
}
mgr.Enrich(ctx, m, 0)
return m, nil
}
// SelectMeta returns all zettel meta data that match the selection
// criteria. The result is ordered by descending zettel id.
func (mgr *Manager) SelectMeta(ctx context.Context, metaSeq []*meta.Meta, q *query.Query) ([]*meta.Meta, error) {
if msg := mgr.mgrLog.Debug(); msg.Enabled() {
msg.Str("query", q.String()).Msg("SelectMeta")
mgr.mgrLogger.Debug("SelectMeta", "query", q)
}
if err := mgr.checkContinue(ctx); err != nil {
return nil, err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
compSearch := q.RetrieveAndCompile(ctx, mgr, metaSeq)
if result := compSearch.Result(); result != nil {
mgr.mgrLog.Trace().Int("count", int64(len(result))).Msg("found without ApplyMeta")
logging.LogTrace(mgr.mgrLogger, "found without ApplyMeta", "count", len(result))
return result, nil
}
selected := map[id.Zid]*meta.Meta{}
for _, term := range compSearch.Terms {
rejected := idset.New()
handleMeta := func(m *meta.Meta) {
zid := m.Zid
if rejected.Contains(zid) {
mgr.mgrLog.Trace().Zid(zid).Msg("SelectMeta/alreadyRejected")
logging.LogTrace(mgr.mgrLogger, "SelectMeta/alreadyRejected", "zid", zid)
return
}
if _, ok := selected[zid]; ok {
mgr.mgrLog.Trace().Zid(zid).Msg("SelectMeta/alreadySelected")
logging.LogTrace(mgr.mgrLogger, "SelectMeta/alreadySelected", "zid", zid)
return
}
if compSearch.PreMatch(m) && term.Match(m) {
selected[zid] = m
mgr.mgrLog.Trace().Zid(zid).Msg("SelectMeta/match")
logging.LogTrace(mgr.mgrLogger, "SelectMeta/match", "zid", zid)
} else {
rejected.Add(zid)
mgr.mgrLog.Trace().Zid(zid).Msg("SelectMeta/reject")
logging.LogTrace(mgr.mgrLogger, "SelectMeta/reject", "zid", zid)
}
}
for _, p := range mgr.boxes {
if err2 := p.ApplyMeta(ctx, handleMeta, term.Retrieve); err2 != nil {
return nil, err2
}
}
}
result := make([]*meta.Meta, 0, len(selected))
for _, m := range selected {
result = append(result, m)
}
result = compSearch.AfterSearch(result)
mgr.mgrLog.Trace().Int("count", int64(len(result))).Msg("found with ApplyMeta")
logging.LogTrace(mgr.mgrLogger, "found with ApplyMeta", "count", len(result))
return result, nil
}
// CanUpdateZettel returns true, if box could possibly update the given zettel.
func (mgr *Manager) CanUpdateZettel(ctx context.Context, zettel zettel.Zettel) bool {
if err := mgr.checkContinue(ctx); err != nil {
return false
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
if box, isWriteBox := mgr.boxes[0].(box.WriteBox); isWriteBox {
return box.CanUpdateZettel(ctx, zettel)
}
return false
}
// UpdateZettel updates an existing zettel.
func (mgr *Manager) UpdateZettel(ctx context.Context, zettel zettel.Zettel) error {
mgr.mgrLog.Debug().Zid(zettel.Meta.Zid).Msg("UpdateZettel")
mgr.mgrLogger.Debug("UpdateZettel", "zid", zettel.Meta.Zid)
if err := mgr.checkContinue(ctx); err != nil {
return err
}
return mgr.updateZettel(ctx, zettel)
}
func (mgr *Manager) updateZettel(ctx context.Context, zettel zettel.Zettel) error {
if box, isWriteBox := mgr.boxes[0].(box.WriteBox); isWriteBox {
|
︙ | | |
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
-
+
|
}
}
return false
}
// DeleteZettel removes the zettel from the box.
func (mgr *Manager) DeleteZettel(ctx context.Context, zid id.Zid) error {
mgr.mgrLog.Debug().Zid(zid).Msg("DeleteZettel")
mgr.mgrLogger.Debug("DeleteZettel", "zid", zid)
if err := mgr.checkContinue(ctx); err != nil {
return err
}
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
for _, p := range mgr.boxes {
err := p.DeleteZettel(ctx, zid)
|
︙ | | |
Changes to internal/box/manager/indexer.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
|
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
|
-
+
-
-
+
+
-
-
-
-
+
+
-
-
-
-
+
+
-
-
-
-
+
+
-
-
|
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package manager
import (
"context"
"fmt"
"net/url"
"time"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/id/idset"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager/store"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/parser"
"zettelstore.de/z/internal/zettel"
"zettelstore.de/z/strfun"
)
// SearchEqual returns all zettel that contains the given exact word.
// The word must be normalized through Unicode NKFD, trimmed and not empty.
func (mgr *Manager) SearchEqual(word string) *idset.Set {
found := mgr.idxStore.SearchEqual(word)
mgr.idxLog.Debug().Str("word", word).Int("found", int64(found.Length())).Msg("SearchEqual")
if msg := mgr.idxLog.Trace(); msg.Enabled() {
mgr.idxLogger.Debug("SearchEqual", "word", word, "found", found.Length())
logging.LogTrace(mgr.idxLogger, "IDs", "ids", found)
msg.Str("ids", fmt.Sprint(found)).Msg("IDs")
}
return found
}
// SearchPrefix returns all zettel that have a word with the given prefix.
// The prefix must be normalized through Unicode NKFD, trimmed and not empty.
func (mgr *Manager) SearchPrefix(prefix string) *idset.Set {
found := mgr.idxStore.SearchPrefix(prefix)
mgr.idxLog.Debug().Str("prefix", prefix).Int("found", int64(found.Length())).Msg("SearchPrefix")
if msg := mgr.idxLog.Trace(); msg.Enabled() {
mgr.idxLogger.Debug("SearchPrefix", "prefix", prefix, "found", found.Length())
logging.LogTrace(mgr.idxLogger, "IDs", "ids", found)
msg.Str("ids", fmt.Sprint(found)).Msg("IDs")
}
return found
}
// SearchSuffix returns all zettel that have a word with the given suffix.
// The suffix must be normalized through Unicode NKFD, trimmed and not empty.
func (mgr *Manager) SearchSuffix(suffix string) *idset.Set {
found := mgr.idxStore.SearchSuffix(suffix)
mgr.idxLog.Debug().Str("suffix", suffix).Int("found", int64(found.Length())).Msg("SearchSuffix")
if msg := mgr.idxLog.Trace(); msg.Enabled() {
mgr.idxLogger.Debug("SearchSuffix", "suffix", suffix, "found", found.Length())
logging.LogTrace(mgr.idxLogger, "IDs", "ids", found)
msg.Str("ids", fmt.Sprint(found)).Msg("IDs")
}
return found
}
// SearchContains returns all zettel that contains the given string.
// The string must be normalized through Unicode NKFD, trimmed and not empty.
func (mgr *Manager) SearchContains(s string) *idset.Set {
found := mgr.idxStore.SearchContains(s)
mgr.idxLog.Debug().Str("s", s).Int("found", int64(found.Length())).Msg("SearchContains")
if msg := mgr.idxLog.Trace(); msg.Enabled() {
mgr.idxLogger.Debug("SearchContains", "s", s, "found", found.Length())
logging.LogTrace(mgr.idxLogger, "IDs", "ids", found)
msg.Str("ids", fmt.Sprint(found)).Msg("IDs")
}
return found
}
// idxIndexer runs in the background and updates the index data structures.
// This is the main service of the idxIndexer.
func (mgr *Manager) idxIndexer() {
// Something may panic. Ensure a running indexer.
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
func (mgr *Manager) idxWorkService(ctx context.Context) {
var start time.Time
for {
switch action, zid, lastReload := mgr.idxAr.Dequeue(); action {
case arNothing:
return
case arReload:
mgr.idxLog.Debug().Msg("reload")
mgr.idxLogger.Debug("reload")
zids, err := mgr.FetchZids(ctx)
if err == nil {
start = time.Now()
mgr.idxAr.Reload(zids)
mgr.idxMx.Lock()
mgr.idxLastReload = time.Now().Local()
mgr.idxSinceReload = 0
mgr.idxMx.Unlock()
}
case arZettel:
mgr.idxLog.Debug().Zid(zid).Msg("zettel")
mgr.idxLogger.Debug("zettel", "zid", zid)
zettel, err := mgr.GetZettel(ctx, zid)
if err != nil {
// Zettel was deleted or is not accessible b/c of other reasons
mgr.idxLog.Trace().Zid(zid).Msg("delete")
logging.LogTrace(mgr.idxLogger, "delete", "zid", zid)
mgr.idxDeleteZettel(ctx, zid)
continue
}
mgr.idxLog.Trace().Zid(zid).Msg("update")
logging.LogTrace(mgr.idxLogger, "update", "zid", zid)
mgr.idxUpdateZettel(ctx, zettel)
mgr.idxMx.Lock()
if lastReload {
mgr.idxDurReload = time.Since(start)
}
mgr.idxSinceReload++
mgr.idxMx.Unlock()
|
︙ | | |
Changes to internal/box/manager/manager.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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
|
+
-
+
|
// Package manager coordinates the various boxes and indexes of a Zettelstore.
package manager
import (
"context"
"io"
"log/slog"
"net/url"
"sync"
"time"
"t73f.de/r/zero/set"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager/mapstore"
"zettelstore.de/z/internal/box/manager/store"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
)
// ConnectData contains all administration related values.
type ConnectData struct {
Number int // number of the box, starting with 1.
Config config.Config
Enricher box.Enricher
|
︙ | | |
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
|
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
|
-
+
-
-
-
-
+
+
+
+
|
panic(scheme)
}
registry[scheme] = create
}
// Manager is a coordinating box.
type Manager struct {
mgrLog *logger.Logger
mgrLogger *slog.Logger
stateMx sync.RWMutex
state box.StartState
mgrMx sync.RWMutex
rtConfig config.Config
boxes []box.ManagedBox
observers []box.UpdateFunc
mxObserver sync.RWMutex
done chan struct{}
infos chan box.UpdateInfo
propertyKeys *set.Set[string] // Set of property key names
// Indexer data
idxLog *logger.Logger
idxStore store.Store
idxAr *anteroomQueue
idxReady chan struct{} // Signal a non-empty anteroom to background task
idxLogger *slog.Logger
idxStore store.Store
idxAr *anteroomQueue
idxReady chan struct{} // Signal a non-empty anteroom to background task
// Indexer stats data
idxMx sync.RWMutex
idxLastReload time.Time
idxDurReload time.Duration
idxSinceReload uint64
}
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
-
-
+
+
+
+
|
descrs := meta.GetSortedKeyDescriptions()
propertyKeys := set.New[string]()
for _, kd := range descrs {
if kd.IsProperty() {
propertyKeys.Add(kd.Name)
}
}
boxLog := kernel.Main.GetLogger(kernel.BoxService)
boxLogger := kernel.Main.GetLogger(kernel.BoxService)
mgr := &Manager{
mgrLog: boxLog.Clone().Str("box", "manager").Child(),
mgrLogger: boxLogger.With("box", "manager"),
rtConfig: rtConfig,
infos: make(chan box.UpdateInfo, len(boxURIs)*10),
propertyKeys: propertyKeys,
idxLog: boxLog.Clone().Str("box", "index").Child(),
idxStore: createIdxStore(rtConfig),
idxAr: newAnteroomQueue(1000),
idxReady: make(chan struct{}, 1),
idxLogger: boxLogger.With("box", "index"),
idxStore: createIdxStore(rtConfig),
idxAr: newAnteroomQueue(1000),
idxReady: make(chan struct{}, 1),
}
cdata := ConnectData{Number: 1, Config: rtConfig, Enricher: mgr, Notify: mgr.notifyChanged}
boxes := make([]box.ManagedBox, 0, len(boxURIs)+2)
for _, uri := range boxURIs {
p, err := Connect(uri, authManager, &cdata)
if err != nil {
|
︙ | | |
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
-
+
-
+
-
+
|
for {
select {
case ci, ok := <-mgr.infos:
if ok {
now := time.Now()
if len(cache) > 1 && tsLastEvent.Add(10*time.Second).Before(now) {
// Cache contains entries and is definitely outdated
mgr.mgrLog.Trace().Msg("clean destutter cache")
logging.LogTrace(mgr.mgrLogger, "clean destutter cache")
cache = destutterCache{}
}
tsLastEvent = now
reason, zid := ci.Reason, ci.Zid
mgr.mgrLog.Debug().Uint("reason", uint64(reason)).Zid(zid).Msg("notifier")
mgr.mgrLogger.Debug("notifier", "reason", reason, "zid", zid)
if ignoreUpdate(cache, now, reason, zid) {
mgr.mgrLog.Trace().Uint("reason", uint64(reason)).Zid(zid).Msg("notifier ignored")
logging.LogTrace(mgr.mgrLogger, "notifier ignored", "reason", reason, "zid", zid)
continue
}
isStarted := mgr.State() == box.StartStateStarted
mgr.idxEnqueue(reason, zid)
if ci.Box == nil {
ci.Box = mgr
|
︙ | | |
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
-
+
|
case box.OnReload:
mgr.idxAr.Reset()
case box.OnZettel:
mgr.idxAr.EnqueueZettel(zid)
case box.OnDelete:
mgr.idxAr.EnqueueZettel(zid)
default:
mgr.mgrLog.Error().Uint("reason", uint64(reason)).Zid(zid).Msg("Unknown notification reason")
mgr.mgrLogger.Error("Unknown notification reason", "reason", reason, "zid", zid)
return
}
select {
case mgr.idxReady <- struct{}{}:
default:
}
}
|
︙ | | |
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
-
+
-
+
|
func (mgr *Manager) waitBoxesAreStarted() {
const waitTime = 10 * time.Millisecond
const waitLoop = int(1 * time.Second / waitTime)
for i := 1; !mgr.allBoxesStarted(); i++ {
if i%waitLoop == 0 {
if time.Duration(i)*waitTime > time.Minute {
mgr.mgrLog.Info().Msg("Waiting for more than one minute to start")
mgr.mgrLogger.Info("Waiting for more than one minute to start")
} else {
mgr.mgrLog.Trace().Msg("Wait for boxes to start")
logging.LogTrace(mgr.mgrLogger, "Wait for boxes to start")
}
}
time.Sleep(waitTime)
}
}
func (mgr *Manager) allBoxesStarted() bool {
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
}
}
mgr.setState(box.StartStateStopped)
}
// Refresh internal box data.
func (mgr *Manager) Refresh(ctx context.Context) error {
mgr.mgrLog.Debug().Msg("Refresh")
mgr.mgrLogger.Debug("Refresh")
if err := mgr.checkContinue(ctx); err != nil {
return err
}
mgr.infos <- box.UpdateInfo{Reason: box.OnReload, Zid: id.Invalid}
mgr.mgrMx.Lock()
defer mgr.mgrMx.Unlock()
for _, bx := range mgr.boxes {
if rb, ok := bx.(box.Refresher); ok {
rb.Refresh(ctx)
}
}
return nil
}
// ReIndex data of the given zettel.
func (mgr *Manager) ReIndex(ctx context.Context, zid id.Zid) error {
mgr.mgrLog.Debug().Msg("ReIndex")
mgr.mgrLogger.Debug("ReIndex")
if err := mgr.checkContinue(ctx); err != nil {
return err
}
mgr.infos <- box.UpdateInfo{Box: mgr, Reason: box.OnZettel, Zid: zid}
return nil
}
// ReadStats populates st with box statistics.
func (mgr *Manager) ReadStats(st *box.Stats) {
mgr.mgrLog.Debug().Msg("ReadStats")
mgr.mgrLogger.Debug("ReadStats")
mgr.mgrMx.RLock()
defer mgr.mgrMx.RUnlock()
subStats := make([]box.ManagedBoxStats, len(mgr.boxes))
for i, p := range mgr.boxes {
p.ReadStats(&subStats[i])
}
|
︙ | | |
Changes to internal/box/manager/mapstore/mapstore.go.
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
-
+
+
|
ms.mxStats.Unlock()
}
func (ms *mapStore) Dump(w io.Writer) {
ms.mx.RLock()
defer ms.mx.RUnlock()
io.WriteString(w, "=== Dump\n")
_, _ = io.WriteString(w, "=== Dump\n")
ms.dumpIndex(w)
ms.dumpDead(w)
dumpStringRefs(w, "Words", "", "", ms.words)
dumpStringRefs(w, "URLs", "[[", "]]", ms.urls)
}
func (ms *mapStore) dumpIndex(w io.Writer) {
if len(ms.idx) == 0 {
return
}
io.WriteString(w, "==== Zettel Index\n")
_, _ = io.WriteString(w, "==== Zettel Index\n")
zids := make([]id.Zid, 0, len(ms.idx))
for id := range ms.idx {
zids = append(zids, id)
}
slices.Sort(zids)
for _, id := range zids {
fmt.Fprintln(w, "=====", id)
_, _ = fmt.Fprintln(w, "=====", id)
zi := ms.idx[id]
if !zi.dead.IsEmpty() {
fmt.Fprintln(w, "* Dead:", zi.dead)
_, _ = fmt.Fprintln(w, "* Dead:", zi.dead)
}
dumpSet(w, "* Forward:", zi.forward)
dumpSet(w, "* Backward:", zi.backward)
otherRefs := make([]string, 0, len(zi.otherRefs))
for k := range zi.otherRefs {
otherRefs = append(otherRefs, k)
}
slices.Sort(otherRefs)
for _, k := range otherRefs {
fmt.Fprintln(w, "* Meta", k)
_, _ = fmt.Fprintln(w, "* Meta", k)
dumpSet(w, "** Forward:", zi.otherRefs[k].forward)
dumpSet(w, "** Backward:", zi.otherRefs[k].backward)
}
dumpStrings(w, "* Words", "", "", zi.words)
dumpStrings(w, "* URLs", "[[", "]]", zi.urls)
}
}
func (ms *mapStore) dumpDead(w io.Writer) {
if len(ms.dead) == 0 {
return
}
fmt.Fprintf(w, "==== Dead References\n")
_, _ = fmt.Fprintf(w, "==== Dead References\n")
zids := make([]id.Zid, 0, len(ms.dead))
for id := range ms.dead {
zids = append(zids, id)
}
slices.Sort(zids)
for _, id := range zids {
fmt.Fprintln(w, ";", id)
fmt.Fprintln(w, ":", ms.dead[id])
_, _ = fmt.Fprintln(w, ";", id)
_, _ = fmt.Fprintln(w, ":", ms.dead[id])
}
}
func dumpSet(w io.Writer, prefix string, s *idset.Set) {
if !s.IsEmpty() {
io.WriteString(w, prefix)
_, _ = io.WriteString(w, prefix)
s.ForEach(func(zid id.Zid) {
io.WriteString(w, " ")
w.Write(zid.Bytes())
_, _ = io.WriteString(w, " ")
_, _ = w.Write(zid.Bytes())
})
fmt.Fprintln(w)
_, _ = fmt.Fprintln(w)
}
}
func dumpStrings(w io.Writer, title, preString, postString string, slice []string) {
if len(slice) > 0 {
sl := make([]string, len(slice))
copy(sl, slice)
slices.Sort(sl)
fmt.Fprintln(w, title)
_, _ = fmt.Fprintln(w, title)
for _, s := range sl {
fmt.Fprintf(w, "** %s%s%s\n", preString, s, postString)
_, _ = fmt.Fprintf(w, "** %s%s%s\n", preString, s, postString)
}
}
}
func dumpStringRefs(w io.Writer, title, preString, postString string, srefs stringRefs) {
if len(srefs) == 0 {
return
}
fmt.Fprintln(w, "====", title)
_, _ = fmt.Fprintln(w, "====", title)
for _, s := range slices.Sorted(maps.Keys(srefs)) {
fmt.Fprintf(w, "; %s%s%s\n", preString, s, postString)
fmt.Fprintln(w, ":", srefs[s])
_, _ = fmt.Fprintf(w, "; %s%s%s\n", preString, s, postString)
_, _ = fmt.Fprintln(w, ":", srefs[s])
}
}
|
Changes to internal/box/membox/membox.go.
︙ | | |
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
|
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
|
+
-
+
-
+
-
-
+
|
//-----------------------------------------------------------------------------
// Package membox stores zettel volatile in main memory.
package membox
import (
"context"
"log/slog"
"net/url"
"sync"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/box/manager"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/query"
"zettelstore.de/z/internal/zettel"
)
func init() {
manager.Register(
"mem",
func(u *url.URL, cdata *manager.ConnectData) (box.ManagedBox, error) {
return &memBox{
log: kernel.Main.GetLogger(kernel.BoxService).Clone().
logger: kernel.Main.GetLogger(kernel.BoxService).With("box", "mem", "boxnum", cdata.Number),
Str("box", "mem").Int("boxnum", int64(cdata.Number)).Child(),
u: u,
cdata: *cdata,
maxZettel: box.GetQueryInt(u, "max-zettel", 0, 127, 65535),
maxBytes: box.GetQueryInt(u, "max-bytes", 0, 65535, (1024*1024*1024)-1),
}, nil
})
}
type memBox struct {
log *logger.Logger
logger *slog.Logger
u *url.URL
cdata manager.ConnectData
maxZettel int
maxBytes int
mx sync.RWMutex // Protects the following fields
zettel map[id.Zid]zettel.Zettel
curBytes int
|
︙ | | |
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
-
+
|
}
func (mb *memBox) Start(context.Context) error {
mb.mx.Lock()
mb.zettel = make(map[id.Zid]zettel.Zettel)
mb.curBytes = 0
mb.mx.Unlock()
mb.log.Trace().Int("max-zettel", int64(mb.maxZettel)).Int("max-bytes", int64(mb.maxBytes)).Msg("Start Box")
logging.LogTrace(mb.logger, "Start box", "max-zettel", mb.maxZettel, "max-bytes", mb.maxBytes)
return nil
}
func (mb *memBox) Stop(context.Context) {
mb.mx.Lock()
mb.zettel = nil
mb.mx.Unlock()
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
meta.Zid = zid
zettel.Meta = meta
mb.zettel[zid] = zettel
mb.curBytes = newBytes
mb.mx.Unlock()
mb.notifyChanged(zid, box.OnZettel)
mb.log.Trace().Zid(zid).Msg("CreateZettel")
logging.LogTrace(mb.logger, "CreateZettel", "zid", zid)
return zid, nil
}
func (mb *memBox) GetZettel(_ context.Context, zid id.Zid) (zettel.Zettel, error) {
mb.mx.RLock()
z, ok := mb.zettel[zid]
mb.mx.RUnlock()
if !ok {
return zettel.Zettel{}, box.ErrZettelNotFound{Zid: zid}
}
z.Meta = z.Meta.Clone()
mb.log.Trace().Msg("GetZettel")
logging.LogTrace(mb.logger, "GetZettel")
return z, nil
}
func (mb *memBox) HasZettel(_ context.Context, zid id.Zid) bool {
mb.mx.RLock()
_, found := mb.zettel[zid]
mb.mx.RUnlock()
return found
}
func (mb *memBox) ApplyZid(_ context.Context, handle box.ZidFunc, constraint query.RetrievePredicate) error {
mb.mx.RLock()
defer mb.mx.RUnlock()
mb.log.Trace().Int("entries", int64(len(mb.zettel))).Msg("ApplyZid")
logging.LogTrace(mb.logger, "ApplyZid", "entries", len(mb.zettel))
for zid := range mb.zettel {
if constraint(zid) {
handle(zid)
}
}
return nil
}
func (mb *memBox) ApplyMeta(ctx context.Context, handle box.MetaFunc, constraint query.RetrievePredicate) error {
mb.mx.RLock()
defer mb.mx.RUnlock()
mb.log.Trace().Int("entries", int64(len(mb.zettel))).Msg("ApplyMeta")
logging.LogTrace(mb.logger, "ApplyMeta", "entries", len(mb.zettel))
for zid, zettel := range mb.zettel {
if constraint(zid) {
m := zettel.Meta.Clone()
mb.cdata.Enricher.Enrich(ctx, m, mb.cdata.Number)
handle(m)
}
}
|
︙ | | |
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
-
+
|
}
zettel.Meta = m
mb.zettel[m.Zid] = zettel
mb.curBytes = newBytes
mb.mx.Unlock()
mb.notifyChanged(m.Zid, box.OnZettel)
mb.log.Trace().Msg("UpdateZettel")
logging.LogTrace(mb.logger, "UpdateZettel")
return nil
}
func (mb *memBox) CanDeleteZettel(_ context.Context, zid id.Zid) bool {
mb.mx.RLock()
_, ok := mb.zettel[zid]
mb.mx.RUnlock()
|
︙ | | |
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
-
+
-
+
|
mb.mx.Unlock()
return box.ErrZettelNotFound{Zid: zid}
}
delete(mb.zettel, zid)
mb.curBytes -= oldZettel.ByteSize()
mb.mx.Unlock()
mb.notifyChanged(zid, box.OnDelete)
mb.log.Trace().Msg("DeleteZettel")
logging.LogTrace(mb.logger, "DeleteZettel")
return nil
}
func (mb *memBox) ReadStats(st *box.ManagedBoxStats) {
st.ReadOnly = false
mb.mx.RLock()
st.Zettel = len(mb.zettel)
mb.mx.RUnlock()
mb.log.Trace().Int("zettel", int64(st.Zettel)).Msg("ReadStats")
logging.LogTrace(mb.logger, "ReadStats", "zettel", st.Zettel)
}
|
Changes to internal/box/notify/directory.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
|
-
+
-
+
|
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------
package notify
import (
"errors"
"fmt"
"log/slog"
"path/filepath"
"regexp"
"slices"
"sync"
"t73f.de/r/zero/set"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/kernel"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/parser"
"zettelstore.de/z/internal/query"
)
type entrySet map[id.Zid]*DirEntry
// DirServiceState signal the internal state of the service.
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
DsMissing // Directory is missing
DsStopping // Service is shut down
)
// DirService specifies a directory service for file based zettel.
type DirService struct {
box box.ManagedBox
log *logger.Logger
logger *slog.Logger
dirPath string
notifier Notifier
infos box.UpdateNotifier
mx sync.RWMutex // protects status, entries
state DirServiceState
entries entrySet
}
// ErrNoDirectory signals missing directory data.
var ErrNoDirectory = errors.New("unable to retrieve zettel directory information")
// NewDirService creates a new directory service.
func NewDirService(box box.ManagedBox, log *logger.Logger, notifier Notifier, notify box.UpdateNotifier) *DirService {
func NewDirService(box box.ManagedBox, logger *slog.Logger, notifier Notifier, notify box.UpdateNotifier) *DirService {
return &DirService{
box: box,
log: log,
logger: logger,
notifier: notifier,
infos: notify,
state: DsCreated,
}
}
// State the current service state.
|
︙ | | |
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
-
+
|
ds.state = DsStopping
ds.mx.Unlock()
ds.notifier.Close()
}
func (ds *DirService) logMissingEntry(action string) error {
err := ErrNoDirectory
ds.log.Info().Err(err).Str("action", action).Msg("Unable to get directory information")
ds.logger.Info("Unable to get directory information", "err", err, "action", action)
return err
}
// NumDirEntries returns the number of entries in the directory.
func (ds *DirService) NumDirEntries() int {
ds.mx.RLock()
defer ds.mx.RUnlock()
|
︙ | | |
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
|
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
|
-
-
+
-
-
+
-
+
-
+
-
+
|
}
}
func (ds *DirService) handleEvent(ev Event, newEntries entrySet) (entrySet, bool) {
ds.mx.RLock()
state := ds.state
ds.mx.RUnlock()
if msg := ds.log.Trace(); msg.Enabled() {
msg.Uint("state", uint64(state)).Str("op", ev.Op.String()).Str("name", ev.Name).Msg("notifyEvent")
logging.LogTrace(ds.logger, "notifyEvent", "state", state, "op", ev.Op, "name", ev.Name)
}
if state == DsStopping {
return nil, false
}
switch ev.Op {
case Error:
newEntries = nil
if state != DsMissing {
ds.log.Error().Err(ev.Err).Msg("Notifier confused")
ds.logger.Error("Notifier confused", "state", state, logging.Err(ev.Err))
}
case Make:
newEntries = make(entrySet)
case List:
if ev.Name == "" {
zids := getNewZids(newEntries)
ds.mx.Lock()
fromMissing := ds.state == DsMissing
prevEntries := ds.entries
ds.entries = newEntries
ds.state = DsWorking
ds.mx.Unlock()
ds.onCreateDirectory(zids, prevEntries)
if fromMissing {
ds.log.Info().Str("path", ds.dirPath).Msg("Zettel directory found")
ds.logger.Info("Zettel directory found", "path", ds.dirPath)
}
return nil, true
}
if newEntries != nil {
ds.onUpdateFileEvent(newEntries, ev.Name)
}
case Destroy:
ds.onDestroyDirectory()
ds.log.Error().Str("path", ds.dirPath).Msg("Zettel directory missing")
ds.logger.Error("Zettel directory missing", "path", ds.dirPath)
return nil, true
case Update:
ds.mx.Lock()
zid := ds.onUpdateFileEvent(ds.entries, ev.Name)
ds.mx.Unlock()
if zid != id.Invalid {
ds.notifyChange(zid, box.OnZettel)
}
case Delete:
ds.mx.Lock()
zid := ds.onDeleteFileEvent(ds.entries, ev.Name)
ds.mx.Unlock()
if zid != id.Invalid {
ds.notifyChange(zid, box.OnDelete)
}
default:
ds.log.Error().Str("event", fmt.Sprintf("%v", ev)).Msg("Unknown zettel notification event")
ds.logger.Error("Unknown zettel notification", "event", ev)
}
return newEntries, true
}
func getNewZids(entries entrySet) []id.Zid {
zids := make([]id.Zid, 0, len(entries))
for zid := range entries {
|
︙ | | |
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
-
+
-
+
|
zid := seekZid(name)
if zid == id.Invalid {
return id.Invalid
}
entry := fetchdirEntry(entries, zid)
dupName1, dupName2 := ds.updateEntry(entry, name)
if dupName1 != "" {
ds.log.Info().Str("name", dupName1).Msg("Duplicate content (is ignored)")
ds.logger.Info("Duplicate content (is ignored)", "name", dupName1)
if dupName2 != "" {
ds.log.Info().Str("name", dupName2).Msg("Duplicate content (is ignored)")
ds.logger.Info("Duplicate content (is ignored)", "name", dupName2)
}
return id.Invalid
}
return zid
}
func (ds *DirService) onDeleteFileEvent(entries entrySet, name string) id.Zid {
|
︙ | | |
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
-
+
|
loop:
for _, prevName := range uselessFiles {
for _, newName := range entry.UselessFiles {
if prevName == newName {
continue loop
}
}
ds.log.Info().Str("name", prevName).Msg("Previous duplicate file becomes useful")
ds.logger.Info("Previous duplicate file becomes useful", "name", prevName)
}
}
func (ds *DirService) updateEntry(entry *DirEntry, name string) (string, string) {
ext := onlyExt(name)
if !extIsMetaAndContent(entry.ContentExt) {
if ext == "" {
|
︙ | | |
573
574
575
576
577
578
579
580
581
582
583
|
571
572
573
574
575
576
577
578
579
580
581
|
-
+
|
return newLen < oldLen
}
return newExt < oldExt
}
func (ds *DirService) notifyChange(zid id.Zid, reason box.UpdateReason) {
if notify := ds.infos; notify != nil {
ds.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChange")
logging.LogTrace(ds.logger, "notifychange", "zid", zid, "reason", reason)
notify(ds.box, zid, reason)
}
}
|
Changes to internal/box/notify/fsdir.go.
︙ | | |
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
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
|
+
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
+
-
+
|
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package notify
import (
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
)
type fsdirNotifier struct {
log *logger.Logger
logger *slog.Logger
events chan Event
done chan struct{}
refresh chan struct{}
base *fsnotify.Watcher
path string
fetcher EntryFetcher
parent string
}
// NewFSDirNotifier creates a directory based notifier that receives notifications
// from the file system.
func NewFSDirNotifier(log *logger.Logger, path string) (Notifier, error) {
func NewFSDirNotifier(logger *slog.Logger, path string) (Notifier, error) {
absPath, err := filepath.Abs(path)
if err != nil {
log.Debug().Err(err).Str("path", path).Msg("Unable to create absolute path")
logger.Debug("Unable to create absolute path", "err", err, "path", path)
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Debug().Err(err).Str("absPath", absPath).Msg("Unable to create watcher")
logger.Debug("Unable to create watcher", "err", err, "absPath", absPath)
return nil, err
}
absParentDir := filepath.Dir(absPath)
errParent := watcher.Add(absParentDir)
err = watcher.Add(absPath)
if errParent != nil {
if err != nil {
log.Error().
Str("parentDir", absParentDir).Err(errParent).
Str("path", absPath).Err(err).
Msg("Unable to access Zettel directory and its parent directory")
watcher.Close()
logger.Error("Unable to access Zettel directory and its parent directory",
"parentDir", absParentDir, "errParent", errParent, "path", absPath, "err", err)
_ = watcher.Close()
return nil, err
}
log.Info().Str("parentDir", absParentDir).Err(errParent).
Msg("Parent of Zettel directory cannot be supervised")
log.Info().Str("path", absPath).
Msg("Zettelstore might not detect a deletion or movement of the Zettel directory")
logger.Info("Parent of Zettel directory cannot be supervised",
"parentDir", absParentDir, "err", errParent)
logger.Info("Zettelstore might not detect a deletion or movement of the Zettel directory",
"path", absPath)
} else if err != nil {
// Not a problem, if container is not available. It might become available later.
log.Info().Err(err).Str("path", absPath).Msg("Zettel directory currently not available")
logger.Info("Zettel directory currently not available", "err", err, "path", absPath)
}
fsdn := &fsdirNotifier{
log: log,
logger: logger,
events: make(chan Event),
refresh: make(chan struct{}),
done: make(chan struct{}),
base: watcher,
path: absPath,
fetcher: newDirPathFetcher(absPath),
parent: absParentDir,
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
}
func (fsdn *fsdirNotifier) Refresh() {
fsdn.refresh <- struct{}{}
}
func (fsdn *fsdirNotifier) eventLoop() {
defer fsdn.base.Close()
defer func() { _ = fsdn.base.Close() }()
defer close(fsdn.events)
defer close(fsdn.refresh)
if !listDirElements(fsdn.log, fsdn.fetcher, fsdn.events, fsdn.done) {
if !listDirElements(fsdn.logger, fsdn.fetcher, fsdn.events, fsdn.done) {
return
}
for fsdn.readAndProcessEvent() {
}
}
func (fsdn *fsdirNotifier) readAndProcessEvent() bool {
select {
case <-fsdn.done:
fsdn.traceDone(1)
return false
default:
}
select {
case <-fsdn.done:
fsdn.traceDone(2)
return false
case <-fsdn.refresh:
fsdn.log.Trace().Msg("refresh")
listDirElements(fsdn.log, fsdn.fetcher, fsdn.events, fsdn.done)
logging.LogTrace(fsdn.logger, "refresh")
listDirElements(fsdn.logger, fsdn.fetcher, fsdn.events, fsdn.done)
case err, ok := <-fsdn.base.Errors:
fsdn.log.Trace().Err(err).Bool("ok", ok).Msg("got errors")
logging.LogTrace(fsdn.logger, "got errors", "err", err, "ok", ok)
if !ok {
return false
}
select {
case fsdn.events <- Event{Op: Error, Err: err}:
case <-fsdn.done:
fsdn.traceDone(3)
return false
}
case ev, ok := <-fsdn.base.Events:
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Bool("ok", ok).Msg("file event")
logging.LogTrace(fsdn.logger, "file event", "name", ev.Name, "op", ev.Op, "ok", ok)
if !ok {
return false
}
if !fsdn.processEvent(&ev) {
return false
}
}
return true
}
func (fsdn *fsdirNotifier) traceDone(pos int64) {
fsdn.log.Trace().Int("i", pos).Msg("done with read and process events")
logging.LogTrace(fsdn.logger, "done with read and process events", "i", pos)
}
func (fsdn *fsdirNotifier) processEvent(ev *fsnotify.Event) bool {
if strings.HasPrefix(ev.Name, fsdn.path) {
if len(ev.Name) == len(fsdn.path) {
return fsdn.processDirEvent(ev)
}
return fsdn.processFileEvent(ev)
}
fsdn.log.Trace().Str("path", fsdn.path).Str("name", ev.Name).Str("op", ev.Op.String()).Msg("event does not match")
logging.LogTrace(fsdn.logger, "event does not match", "path", fsdn.path, "name", ev.Name, "op", ev.Op)
return true
}
func (fsdn *fsdirNotifier) processDirEvent(ev *fsnotify.Event) bool {
if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
fsdn.log.Debug().Str("name", fsdn.path).Msg("Directory removed")
fsdn.base.Remove(fsdn.path)
fsdn.logger.Debug("Directory removed", "name", fsdn.path)
_ = fsdn.base.Remove(fsdn.path)
select {
case fsdn.events <- Event{Op: Destroy}:
case <-fsdn.done:
fsdn.log.Trace().Int("i", 1).Msg("done dir event processing")
logging.LogTrace(fsdn.logger, "done dir event processing", "i", 1)
return false
}
return true
}
if ev.Has(fsnotify.Create) {
err := fsdn.base.Add(fsdn.path)
if err != nil {
fsdn.log.Error().Err(err).Str("name", fsdn.path).Msg("Unable to add directory")
fsdn.logger.Error("Unable to add directory", "err", err, "name", fsdn.path)
select {
case fsdn.events <- Event{Op: Error, Err: err}:
case <-fsdn.done:
fsdn.log.Trace().Int("i", 2).Msg("done dir event processing")
logging.LogTrace(fsdn.logger, "done dir event processing", "i", 2)
return false
}
}
fsdn.log.Debug().Str("name", fsdn.path).Msg("Directory added")
return listDirElements(fsdn.log, fsdn.fetcher, fsdn.events, fsdn.done)
fsdn.logger.Debug("Directory added", "name", fsdn.path)
return listDirElements(fsdn.logger, fsdn.fetcher, fsdn.events, fsdn.done)
}
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("Directory processed")
logging.LogTrace(fsdn.logger, "Directory processed", "name", ev.Name, "op", ev.Op)
return true
}
func (fsdn *fsdirNotifier) processFileEvent(ev *fsnotify.Event) bool {
if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Write) {
if fi, err := os.Lstat(ev.Name); err != nil || !fi.Mode().IsRegular() {
regular := err == nil && fi.Mode().IsRegular()
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Err(err).Bool("regular", regular).Msg("error with file")
logging.LogTrace(fsdn.logger, "error with file",
"name", ev.Name, "op", ev.Op, logging.Err(err), "regular", regular)
return true
}
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("File updated")
logging.LogTrace(fsdn.logger, "File updated", "name", ev.Name, "op", ev.Op)
return fsdn.sendEvent(Update, filepath.Base(ev.Name))
}
if ev.Has(fsnotify.Rename) {
fi, err := os.Lstat(ev.Name)
if err != nil {
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("File deleted")
logging.LogTrace(fsdn.logger, "File deleted", "name", ev.Name, "op", ev.Op)
return fsdn.sendEvent(Delete, filepath.Base(ev.Name))
}
if fi.Mode().IsRegular() {
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("File updated")
logging.LogTrace(fsdn.logger, "File updated", "name", ev.Name, "op", ev.Op)
return fsdn.sendEvent(Update, filepath.Base(ev.Name))
}
fsdn.log.Trace().Str("name", ev.Name).Msg("File not regular")
logging.LogTrace(fsdn.logger, "File not regular", "name", ev.Name)
return true
}
if ev.Has(fsnotify.Remove) {
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("File deleted")
logging.LogTrace(fsdn.logger, "File deleted", "name", ev.Name, "op", ev.Op)
return fsdn.sendEvent(Delete, filepath.Base(ev.Name))
}
fsdn.log.Trace().Str("name", ev.Name).Str("op", ev.Op.String()).Msg("File processed")
logging.LogTrace(fsdn.logger, "File processed", "name", ev.Name, "op", ev.Op)
return true
}
func (fsdn *fsdirNotifier) sendEvent(op EventOp, filename string) bool {
select {
case fsdn.events <- Event{Op: op, Name: filename}:
case <-fsdn.done:
fsdn.log.Trace().Msg("done file event processing")
logging.LogTrace(fsdn.logger, "done file event processing")
return false
}
return true
}
func (fsdn *fsdirNotifier) Close() {
close(fsdn.done)
}
|
Changes to internal/box/notify/helper.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
+
-
+
|
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package notify
import (
"archive/zip"
"log/slog"
"os"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
)
// EntryFetcher return a list of (file) names of an directory.
type EntryFetcher interface {
Fetch() ([]string, error)
}
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
func newZipPathFetcher(zipPath string) EntryFetcher { return &zipPathFetcher{zipPath} }
func (zpf *zipPathFetcher) Fetch() ([]string, error) {
reader, err := zip.OpenReader(zpf.zipPath)
if err != nil {
return nil, err
}
defer reader.Close()
result := make([]string, 0, len(reader.File))
for _, f := range reader.File {
result = append(result, f.Name)
}
err = reader.Close()
return result, nil
return result, err
}
// listDirElements write all files within the directory path as events.
func listDirElements(log *logger.Logger, fetcher EntryFetcher, events chan<- Event, done <-chan struct{}) bool {
func listDirElements(logger *slog.Logger, fetcher EntryFetcher, events chan<- Event, done <-chan struct{}) bool {
select {
case events <- Event{Op: Make}:
case <-done:
return false
}
entries, err := fetcher.Fetch()
if err != nil {
select {
case events <- Event{Op: Error, Err: err}:
case <-done:
return false
}
}
for _, name := range entries {
log.Trace().Str("name", name).Msg("File listed")
logging.LogTrace(logger, "File listed", "name", name)
select {
case events <- Event{Op: List, Name: name}:
case <-done:
return false
}
}
|
︙ | | |
Changes to internal/box/notify/simpledir.go.
︙ | | |
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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
|
+
-
-
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package notify
import (
"log/slog"
"path/filepath"
"zettelstore.de/z/internal/logger"
)
type simpleDirNotifier struct {
log *logger.Logger
logger *slog.Logger
events chan Event
done chan struct{}
refresh chan struct{}
fetcher EntryFetcher
}
// NewSimpleDirNotifier creates a directory based notifier that will not receive
// any notifications from the operating system.
func NewSimpleDirNotifier(log *logger.Logger, path string) (Notifier, error) {
func NewSimpleDirNotifier(logger *slog.Logger, path string) (Notifier, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
sdn := &simpleDirNotifier{
log: log,
logger: logger,
events: make(chan Event),
done: make(chan struct{}),
refresh: make(chan struct{}),
fetcher: newDirPathFetcher(absPath),
}
go sdn.eventLoop()
return sdn, nil
}
// NewSimpleZipNotifier creates a zip-file based notifier that will not receive
// any notifications from the operating system.
func NewSimpleZipNotifier(log *logger.Logger, zipPath string) Notifier {
func NewSimpleZipNotifier(logger *slog.Logger, zipPath string) Notifier {
sdn := &simpleDirNotifier{
log: log,
logger: logger,
events: make(chan Event),
done: make(chan struct{}),
refresh: make(chan struct{}),
fetcher: newZipPathFetcher(zipPath),
}
go sdn.eventLoop()
return sdn
}
func (sdn *simpleDirNotifier) Events() <-chan Event {
return sdn.events
}
func (sdn *simpleDirNotifier) Refresh() {
sdn.refresh <- struct{}{}
}
func (sdn *simpleDirNotifier) eventLoop() {
defer close(sdn.events)
defer close(sdn.refresh)
if !listDirElements(sdn.log, sdn.fetcher, sdn.events, sdn.done) {
if !listDirElements(sdn.logger, sdn.fetcher, sdn.events, sdn.done) {
return
}
for {
select {
case <-sdn.done:
return
case <-sdn.refresh:
listDirElements(sdn.log, sdn.fetcher, sdn.events, sdn.done)
listDirElements(sdn.logger, sdn.fetcher, sdn.events, sdn.done)
}
}
}
func (sdn *simpleDirNotifier) Close() {
close(sdn.done)
}
|
Changes to internal/config/config.go.
︙ | | |
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-
|
KeyFooterZettel = "footer-zettel"
KeyHomeZettel = "home-zettel"
KeyListsMenuZettel = "lists-menu-zettel"
KeyShowBackLinks = "show-back-links"
KeyShowFolgeLinks = "show-folge-links"
KeyShowSequelLinks = "show-sequel-links"
KeyShowSubordinateLinks = "show-subordinate-links"
KeyShowSuccessorLinks = "show-successor-links"
// api.KeyLang
)
// Config allows to retrieve all defined configuration values that can be changed during runtime.
type Config interface {
AuthConfig
|
︙ | | |
Changes to internal/encoder/htmlenc.go.
︙ | | |
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
-
+
|
head.AddN(
shtml.SymHead,
sx.Nil().Cons(sx.Nil().Cons(sx.Cons(sx.MakeSymbol("charset"), sx.MakeString("utf-8"))).Cons(sxhtml.SymAttr)).Cons(shtml.SymMeta),
)
head.ExtendBang(hm)
var sb strings.Builder
if hasTitle {
he.textEnc.WriteInlines(&sb, &isTitle)
_, _ = he.textEnc.WriteInlines(&sb, &isTitle)
} else {
sb.Write(zn.Meta.Zid.Bytes())
}
head.Add(sx.MakeList(shtml.SymAttrTitle, sx.MakeString(sb.String())))
var body sx.ListBuilder
body.Add(shtml.SymBody)
|
︙ | | |
Changes to internal/encoder/mdenc.go.
︙ | | |
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
-
+
|
// WriteZettel writes the encoded zettel to the writer.
func (me *mdEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode) (int, error) {
v := newMDVisitor(w, me.lang)
v.acceptMeta(zn.InhMeta)
if zn.InhMeta.YamlSep {
v.b.WriteString("---\n")
} else {
v.b.WriteByte('\n')
v.b.WriteLn()
}
ast.Walk(&v, &zn.BlocksAST)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data as markdown.
|
︙ | | |
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
|
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
|
-
+
-
+
|
return
}
v.writeSpaces(4)
lcm1 := lc - 1
for i := 0; i < lc; i++ {
b := vn.Content[i]
if b != '\n' && b != '\r' {
v.b.WriteByte(b)
_ = v.b.WriteByte(b)
continue
}
j := i + 1
for ; j < lc; j++ {
c := vn.Content[j]
if c != '\n' && c != '\r' {
break
}
}
if j >= lcm1 {
break
}
v.b.WriteByte('\n')
v.b.WriteLn()
v.writeSpaces(4)
i = j - 1
}
}
func (v *mdVisitor) visitRegion(rn *ast.RegionNode) {
if rn.Kind != ast.RegionQuote {
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
|
func (v *mdVisitor) writeNestedList(ln *ast.NestedListNode, enum string) {
v.listInfo = append(v.listInfo, len(enum))
regIndent := 4*len(v.listInfo) - 4
paraIndent := regIndent + len(enum)
for i, item := range ln.Items {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.writeSpaces(regIndent)
v.b.WriteString(enum)
for j, in := range item {
if j > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
if _, ok := in.(*ast.ParaNode); ok {
v.writeSpaces(paraIndent)
}
}
ast.Walk(v, in)
}
}
}
func (v *mdVisitor) writeListQuote(ln *ast.NestedListNode) {
v.listInfo = append(v.listInfo, 0)
if len(v.listInfo) > 1 {
return
}
prefix := v.listPrefix
v.listPrefix = "> "
for i, item := range ln.Items {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.b.WriteString(v.listPrefix)
for j, in := range item {
if j > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
if _, ok := in.(*ast.ParaNode); ok {
v.b.WriteString(v.listPrefix)
}
}
ast.Walk(v, in)
}
}
v.listPrefix = prefix
}
func (v *mdVisitor) visitBreak(bn *ast.BreakNode) {
if bn.Hard {
v.b.WriteString("\\\n")
} else {
v.b.WriteByte('\n')
v.b.WriteLn()
}
if l := len(v.listInfo); l > 0 {
if v.listPrefix == "" {
v.writeSpaces(4*l - 4 + v.listInfo[l-1])
} else {
v.writeSpaces(4*l - 4)
v.b.WriteString(v.listPrefix)
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
v.writeReference(ln.Ref, ln.Inlines)
}
func (v *mdVisitor) visitEmbedRef(en *ast.EmbedRefNode) {
v.pushAttributes(en.Attrs)
defer v.popAttributes()
v.b.WriteByte('!')
_ = v.b.WriteByte('!')
v.writeReference(en.Ref, en.Inlines)
}
func (v *mdVisitor) writeReference(ref *ast.Reference, is ast.InlineSlice) {
if ref.State == ast.RefStateQuery {
ast.Walk(v, &is)
} else if len(is) > 0 {
v.b.WriteByte('[')
_ = v.b.WriteByte('[')
ast.Walk(v, &is)
v.b.WriteStrings("](", ref.String())
v.b.WriteByte(')')
_ = v.b.WriteByte(')')
} else if isAutoLinkable(ref) {
v.b.WriteByte('<')
_ = v.b.WriteByte('<')
v.b.WriteString(ref.String())
v.b.WriteByte('>')
_ = v.b.WriteByte('>')
} else {
s := ref.String()
v.b.WriteStrings("[", s, "](", s, ")")
}
}
func isAutoLinkable(ref *ast.Reference) bool {
if ref.State != ast.RefStateExternal || ref.URL == nil {
return false
}
return ref.URL.Scheme != ""
}
func (v *mdVisitor) visitFormat(fn *ast.FormatNode) {
v.pushAttributes(fn.Attrs)
defer v.popAttributes()
switch fn.Kind {
case ast.FormatEmph:
v.b.WriteByte('*')
_ = v.b.WriteByte('*')
ast.Walk(v, &fn.Inlines)
v.b.WriteByte('*')
_ = v.b.WriteByte('*')
case ast.FormatStrong:
v.b.WriteString("__")
ast.Walk(v, &fn.Inlines)
v.b.WriteString("__")
case ast.FormatQuote:
v.writeQuote(fn)
case ast.FormatMark:
|
︙ | | |
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
-
-
-
+
+
+
-
+
-
+
|
}
v.b.WriteString(rightQ)
}
func (v *mdVisitor) visitLiteral(ln *ast.LiteralNode) {
switch ln.Kind {
case ast.LiteralCode, ast.LiteralInput, ast.LiteralOutput:
v.b.WriteByte('`')
v.b.Write(ln.Content)
v.b.WriteByte('`')
_ = v.b.WriteByte('`')
_, _ = v.b.Write(ln.Content)
_ = v.b.WriteByte('`')
case ast.LiteralComment: // ignore everything
default:
v.b.Write(ln.Content)
_, _ = v.b.Write(ln.Content)
}
}
func (v *mdVisitor) writeSpaces(n int) {
for range n {
v.b.WriteByte(' ')
v.b.WriteSpace()
}
}
|
Changes to internal/encoder/textenc.go.
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
// TextEncoder encodes just the text and ignores any formatting.
type TextEncoder struct{}
// WriteZettel writes metadata and content.
func (te *TextEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode) (int, error) {
v := newTextVisitor(w)
te.WriteMeta(&v.b, zn.InhMeta)
_, _ = te.WriteMeta(&v.b, zn.InhMeta)
v.visitBlockSlice(&zn.BlocksAST)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes metadata as text.
func (te *TextEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
buf := newEncWriter(w)
for key, val := range m.Computed() {
if meta.Type(key) == meta.TypeTagSet {
writeTagSet(&buf, val.Elems())
} else {
buf.WriteString(string(val))
}
buf.WriteByte('\n')
buf.WriteLn()
}
length, err := buf.Flush()
return length, err
}
func writeTagSet(buf *encWriter, tags iter.Seq[meta.Value]) {
first := true
for tag := range tags {
if !first {
buf.WriteByte(' ')
buf.WriteSpace()
}
first = false
buf.WriteString(string(tag.CleanTag()))
}
}
|
︙ | | |
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
-
+
|
return nil
case *ast.VerbatimNode:
v.visitVerbatim(n)
return nil
case *ast.RegionNode:
v.visitBlockSlice(&n.Blocks)
if len(n.Inlines) > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
ast.Walk(v, &n.Inlines)
}
return nil
case *ast.NestedListNode:
v.visitNestedList(n)
return nil
case *ast.DescriptionListNode:
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
-
+
|
case *ast.BLOBNode:
return nil
case *ast.TextNode:
v.visitText(n.Text)
return nil
case *ast.BreakNode:
if n.Hard {
v.b.WriteByte('\n')
v.b.WriteLn()
} else {
v.b.WriteByte(' ')
v.b.WriteSpace()
}
return nil
case *ast.LinkNode:
if len(n.Inlines) > 0 {
ast.Walk(v, &n.Inlines)
}
return nil
case *ast.MarkNode:
if len(n.Inlines) > 0 {
ast.Walk(v, &n.Inlines)
}
return nil
case *ast.FootnoteNode:
if v.inlinePos > 0 {
v.b.WriteByte(' ')
v.b.WriteSpace()
}
// No 'return nil' to write text
case *ast.LiteralNode:
if n.Kind != ast.LiteralComment {
v.b.Write(n.Content)
_, _ = v.b.Write(n.Content)
}
}
return v
}
func (v *textVisitor) visitVerbatim(vn *ast.VerbatimNode) {
if vn.Kind == ast.VerbatimComment {
return
if vn.Kind != ast.VerbatimComment {
_, _ = v.b.Write(vn.Content)
}
v.b.Write(vn.Content)
}
func (v *textVisitor) visitNestedList(ln *ast.NestedListNode) {
for i, item := range ln.Items {
v.writePosChar(i, '\n')
for j, it := range item {
v.writePosChar(j, '\n')
ast.Walk(v, it)
}
}
}
func (v *textVisitor) visitDescriptionList(dl *ast.DescriptionListNode) {
for i, descr := range dl.Descriptions {
v.writePosChar(i, '\n')
ast.Walk(v, &descr.Term)
for _, b := range descr.Descriptions {
v.b.WriteByte('\n')
v.b.WriteLn()
for k, d := range b {
v.writePosChar(k, '\n')
ast.Walk(v, d)
}
}
}
}
func (v *textVisitor) visitTable(tn *ast.TableNode) {
if len(tn.Header) > 0 {
v.writeRow(tn.Header)
v.b.WriteByte('\n')
v.b.WriteLn()
}
for i, row := range tn.Rows {
v.writePosChar(i, '\n')
v.writeRow(row)
}
}
|
︙ | | |
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
-
+
-
+
|
}
func (v *textVisitor) visitText(s string) {
spaceFound := false
for _, ch := range s {
if input.IsSpace(ch) {
if !spaceFound {
v.b.WriteByte(' ')
v.b.WriteSpace()
spaceFound = true
}
continue
}
spaceFound = false
v.b.WriteString(string(ch))
}
}
func (v *textVisitor) writePosChar(pos int, ch byte) {
if pos > 0 {
v.b.WriteByte(ch)
_ = v.b.WriteByte(ch)
}
}
|
Changes to internal/encoder/write.go.
︙ | | |
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
-
+
-
-
|
type encWriter struct {
w io.Writer // The io.Writer to write to
err error // Collect error
length int // Collected length
}
// newEncWriter creates a new encWriter
func newEncWriter(w io.Writer) encWriter {
func newEncWriter(w io.Writer) encWriter { return encWriter{w: w} }
return encWriter{w: w}
}
// Write writes the content of p.
func (w *encWriter) Write(p []byte) (l int, err error) {
if w.err != nil {
return 0, w.err
}
l, w.err = w.w.Write(p)
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
var l int
l, w.err = w.Write([]byte{b})
w.length += l
return w.err
}
// WriteBytes writes the content of bs.
func (w *encWriter) WriteBytes(bs ...byte) {
func (w *encWriter) WriteBytes(bs ...byte) { _, _ = w.Write(bs) }
w.Write(bs)
}
// WriteBase64 writes the content of p, encoded with base64.
func (w *encWriter) WriteBase64(p []byte) {
if w.err == nil {
encoder := base64.NewEncoder(base64.StdEncoding, w.w)
var l int
l, w.err = encoder.Write(p)
w.length += l
err1 := encoder.Close()
if w.err == nil {
w.err = err1
}
}
}
// WriteLn writes a new line character.
func (w *encWriter) WriteLn() {
if w.err == nil {
w.err = w.WriteByte('\n')
}
}
// WriteLn writes a space character.
func (w *encWriter) WriteSpace() {
if w.err == nil {
w.err = w.WriteByte(' ')
}
}
// Flush returns the collected length and error.
func (w *encWriter) Flush() (int, error) { return w.length, w.err }
|
Changes to internal/encoder/zmkenc.go.
︙ | | |
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
-
+
|
// WriteZettel writes the encoded zettel to the writer.
func (*zmkEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode) (int, error) {
v := newZmkVisitor(w)
v.acceptMeta(zn.InhMeta)
if zn.InhMeta.YamlSep {
v.b.WriteString("---\n")
} else {
v.b.WriteByte('\n')
v.b.WriteLn()
}
ast.Walk(&v, &zn.BlocksAST)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data as zmk.
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
case *ast.EmbedBLOBNode:
v.visitEmbedBLOB(n)
case *ast.CiteNode:
v.visitCite(n)
case *ast.FootnoteNode:
v.b.WriteString("[^")
ast.Walk(v, &n.Inlines)
v.b.WriteByte(']')
_ = v.b.WriteByte(']')
v.visitAttributes(n.Attrs)
case *ast.MarkNode:
v.visitMark(n)
case *ast.FormatNode:
v.visitFormat(n)
case *ast.LiteralNode:
v.visitLiteral(n)
default:
return v
}
return nil
}
func (v *zmkVisitor) visitBlockSlice(bs *ast.BlockSlice) {
var lastWasParagraph bool
for i, bn := range *bs {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
if lastWasParagraph && !v.inVerse {
if _, ok := bn.(*ast.ParaNode); ok {
v.b.WriteByte('\n')
v.b.WriteLn()
}
}
}
ast.Walk(v, bn)
_, lastWasParagraph = bn.(*ast.ParaNode)
}
}
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
-
+
-
+
-
+
|
if vn.Kind == ast.VerbatimHTML {
attrs = syntaxToHTML(attrs)
}
// TODO: scan cn.Lines to find embedded kind[0]s at beginning
v.b.WriteString(kind)
v.visitAttributes(attrs)
v.b.WriteByte('\n')
v.b.Write(vn.Content)
v.b.WriteByte('\n')
v.b.WriteLn()
_, _ = v.b.Write(vn.Content)
v.b.WriteLn()
v.b.WriteString(kind)
}
var mapRegionKind = map[ast.RegionKind]string{
ast.RegionSpan: ":::",
ast.RegionQuote: "<<<",
ast.RegionVerse: "\"\"\"",
}
func (v *zmkVisitor) visitRegion(rn *ast.RegionNode) {
// Scan rn.Blocks for embedded regions to adjust length of regionCode
kind, ok := mapRegionKind[rn.Kind]
if !ok {
panic(fmt.Sprintf("Unknown region kind %d", rn.Kind))
}
v.b.WriteString(kind)
v.visitAttributes(rn.Attrs)
v.b.WriteByte('\n')
v.b.WriteLn()
saveInVerse := v.inVerse
v.inVerse = rn.Kind == ast.RegionVerse
ast.Walk(v, &rn.Blocks)
v.inVerse = saveInVerse
v.b.WriteByte('\n')
v.b.WriteLn()
v.b.WriteString(kind)
if len(rn.Inlines) > 0 {
v.b.WriteByte(' ')
v.b.WriteSpace()
ast.Walk(v, &rn.Inlines)
}
}
func (v *zmkVisitor) visitHeading(hn *ast.HeadingNode) {
const headingSigns = "========= "
v.b.WriteString(headingSigns[len(headingSigns)-hn.Level-3:])
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
+
-
+
-
+
|
ast.NestedListQuote: '>',
}
func (v *zmkVisitor) visitNestedList(ln *ast.NestedListNode) {
v.prefix = append(v.prefix, mapNestedListKind[ln.Kind])
for i, item := range ln.Items {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.b.Write(v.prefix)
v.b.WriteByte(' ')
_, _ = v.b.Write(v.prefix)
v.b.WriteSpace()
for j, in := range item {
if j > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
if _, ok := in.(*ast.ParaNode); ok {
v.writePrefixSpaces()
}
}
ast.Walk(v, in)
}
}
v.prefix = v.prefix[:len(v.prefix)-1]
}
func (v *zmkVisitor) writePrefixSpaces() {
if prefixLen := len(v.prefix); prefixLen > 0 {
for i := 0; i <= prefixLen; i++ {
v.b.WriteByte(' ')
v.b.WriteSpace()
}
}
}
func (v *zmkVisitor) visitDescriptionList(dn *ast.DescriptionListNode) {
for i, descr := range dn.Descriptions {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.b.WriteString("; ")
ast.Walk(v, &descr.Term)
for _, b := range descr.Descriptions {
v.b.WriteString("\n: ")
for jj, dn := range b {
|
︙ | | |
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
-
+
-
+
|
ast.AlignCenter: ":",
ast.AlignRight: ">",
}
func (v *zmkVisitor) visitTable(tn *ast.TableNode) {
if header := tn.Header; len(header) > 0 {
v.writeTableHeader(header, tn.Align)
v.b.WriteByte('\n')
v.b.WriteLn()
}
for i, row := range tn.Rows {
if i > 0 {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.writeTableRow(row, tn.Align)
}
}
func (v *zmkVisitor) writeTableHeader(header ast.TableRow, align []ast.Alignment) {
for pos, cell := range header {
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
v.b.WriteString(alignCode[colAlign])
}
}
}
func (v *zmkVisitor) writeTableRow(row ast.TableRow, align []ast.Alignment) {
for pos, cell := range row {
v.b.WriteByte('|')
_ = v.b.WriteByte('|')
if cell.Align != align[pos] {
v.b.WriteString(alignCode[cell.Align])
}
ast.Walk(v, &cell.Inlines)
}
}
func (v *zmkVisitor) visitBLOB(bn *ast.BLOBNode) {
if bn.Syntax == meta.ValueSyntaxSVG {
v.b.WriteStrings("@@@", bn.Syntax, "\n")
v.b.Write(bn.Blob)
_, _ = v.b.Write(bn.Blob)
v.b.WriteString("\n@@@\n")
return
}
var sb strings.Builder
v.textEnc.WriteInlines(&sb, &bn.Description)
_, _ = v.textEnc.WriteInlines(&sb, &bn.Description)
v.b.WriteStrings("%% Unable to display BLOB with description '", sb.String(), "' and syntax '", bn.Syntax, "'.")
}
var escapeSeqs = set.New(
"\\", "__", "**", "~~", "^^", ",,", ">>", `""`, "::", "''", "``", "++", "==", "##",
)
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
|
v.b.WriteString(tn.Text[last:])
}
func (v *zmkVisitor) visitBreak(bn *ast.BreakNode) {
if bn.Hard {
v.b.WriteString("\\\n")
} else {
v.b.WriteByte('\n')
v.b.WriteLn()
}
v.writePrefixSpaces()
}
func (v *zmkVisitor) visitLink(ln *ast.LinkNode) {
v.b.WriteString("[[")
if len(ln.Inlines) > 0 {
ast.Walk(v, &ln.Inlines)
v.b.WriteByte('|')
_ = v.b.WriteByte('|')
}
if ln.Ref.State == ast.RefStateBased {
v.b.WriteByte('/')
_ = v.b.WriteByte('/')
}
v.b.WriteStrings(ln.Ref.String(), "]]")
}
func (v *zmkVisitor) visitEmbedRef(en *ast.EmbedRefNode) {
v.b.WriteString("{{")
if len(en.Inlines) > 0 {
ast.Walk(v, &en.Inlines)
v.b.WriteByte('|')
_ = v.b.WriteByte('|')
}
v.b.WriteStrings(en.Ref.String(), "}}")
}
func (v *zmkVisitor) visitEmbedBLOB(en *ast.EmbedBLOBNode) {
if en.Syntax == meta.ValueSyntaxSVG {
v.b.WriteString("@@")
v.b.Write(en.Blob)
_, _ = v.b.Write(en.Blob)
v.b.WriteStrings("@@{=", en.Syntax, "}")
return
}
v.b.WriteString("{{TODO: display inline BLOB}}")
}
func (v *zmkVisitor) visitCite(cn *ast.CiteNode) {
v.b.WriteStrings("[@", cn.Key)
if len(cn.Inlines) > 0 {
v.b.WriteByte(' ')
v.b.WriteSpace()
ast.Walk(v, &cn.Inlines)
}
v.b.WriteByte(']')
_ = v.b.WriteByte(']')
v.visitAttributes(cn.Attrs)
}
func (v *zmkVisitor) visitMark(mn *ast.MarkNode) {
v.b.WriteStrings("[!", mn.Mark)
if len(mn.Inlines) > 0 {
v.b.WriteByte('|')
_ = v.b.WriteByte('|')
ast.Walk(v, &mn.Inlines)
}
v.b.WriteByte(']')
_ = v.b.WriteByte(']')
}
var mapFormatKind = map[ast.FormatKind][]byte{
ast.FormatEmph: []byte("__"),
ast.FormatStrong: []byte("**"),
ast.FormatInsert: []byte(">>"),
ast.FormatDelete: []byte("~~"),
ast.FormatSuper: []byte("^^"),
ast.FormatSub: []byte(",,"),
ast.FormatQuote: []byte(`""`),
ast.FormatMark: []byte("##"),
ast.FormatSpan: []byte("::"),
}
func (v *zmkVisitor) visitFormat(fn *ast.FormatNode) {
kind, ok := mapFormatKind[fn.Kind]
if !ok {
panic(fmt.Sprintf("Unknown format kind %d", fn.Kind))
}
v.b.Write(kind)
_, _ = v.b.Write(kind)
ast.Walk(v, &fn.Inlines)
v.b.Write(kind)
_, _ = v.b.Write(kind)
v.visitAttributes(fn.Attrs)
}
func (v *zmkVisitor) visitLiteral(ln *ast.LiteralNode) {
switch ln.Kind {
case ast.LiteralCode:
v.writeLiteral('`', ln.Attrs, ln.Content)
case ast.LiteralMath:
v.b.WriteStrings("$$", string(ln.Content), "$$")
v.visitAttributes(ln.Attrs)
case ast.LiteralInput:
v.writeLiteral('\'', ln.Attrs, ln.Content)
case ast.LiteralOutput:
v.writeLiteral('=', ln.Attrs, ln.Content)
case ast.LiteralComment:
v.b.WriteString("%%")
v.visitAttributes(ln.Attrs)
v.b.WriteByte(' ')
v.b.Write(ln.Content)
v.b.WriteSpace()
_, _ = v.b.Write(ln.Content)
default:
panic(fmt.Sprintf("Unknown literal kind %v", ln.Kind))
}
}
func (v *zmkVisitor) writeLiteral(code byte, a zsx.Attributes, content []byte) {
v.b.WriteBytes(code, code)
v.writeEscaped(string(content), code)
v.b.WriteBytes(code, code)
v.visitAttributes(a)
}
// visitAttributes write HTML attributes
func (v *zmkVisitor) visitAttributes(a zsx.Attributes) {
if a.IsEmpty() {
return
}
v.b.WriteByte('{')
_ = v.b.WriteByte('{')
for i, k := range a.Keys() {
if i > 0 {
v.b.WriteByte(' ')
v.b.WriteSpace()
}
if k == "-" {
v.b.WriteByte('-')
_ = v.b.WriteByte('-')
continue
}
v.b.WriteString(k)
if vl := a[k]; len(vl) > 0 {
v.b.WriteStrings("=\"", vl)
v.b.WriteByte('"')
_ = v.b.WriteByte('"')
}
}
v.b.WriteByte('}')
_ = v.b.WriteByte('}')
}
func (v *zmkVisitor) writeEscaped(s string, toEscape byte) {
last := 0
for i := range len(s) {
if b := s[i]; b == toEscape || b == '\\' {
v.b.WriteString(s[last:i])
|
︙ | | |
Changes to internal/evaluator/evaluator.go.
︙ | | |
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
-
+
|
if vn, isVerbatim := bs[0].(*ast.VerbatimNode); isVerbatim && vn.Kind == ast.VerbatimCode {
if classAttr, hasClass := vn.Attrs.Get(""); hasClass && classAttr == meta.ValueSyntaxSxn {
rd := sxreader.MakeReader(bytes.NewReader(vn.Content))
if objs, err := rd.ReadAll(); err == nil {
result := make(ast.BlockSlice, len(objs))
for i, obj := range objs {
var buf bytes.Buffer
sxbuiltins.Print(&buf, obj)
_, _ = sxbuiltins.Print(&buf, obj)
result[i] = &ast.VerbatimNode{
Kind: ast.VerbatimCode,
Attrs: zsx.Attributes{"": classAttr},
Content: buf.Bytes(),
}
}
return result
|
︙ | | |
Changes to internal/kernel/auth.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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
|
+
-
-
+
+
|
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package kernel
import (
"errors"
"log/slog"
"sync"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/logger"
)
type authService struct {
srvConfig
mxService sync.RWMutex
manager auth.Manager
createManager CreateAuthManagerFunc
}
var errAlreadySetOwner = errors.New("changing an existing owner not allowed")
var errAlreadyROMode = errors.New("system in readonly mode cannot change this mode")
func (as *authService) Initialize(logger *logger.Logger) {
func (as *authService) Initialize(levelVar *slog.LevelVar, logger *slog.Logger) {
as.logLevelVar = levelVar
as.logger = logger
as.descr = descriptionMap{
AuthOwner: {
"Owner's zettel id",
func(val string) (any, error) {
if owner := as.cur[AuthOwner]; owner != nil && owner != id.Invalid {
return nil, errAlreadySetOwner
|
︙ | | |
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
|
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
|
-
+
+
+
-
+
-
+
-
+
|
}
as.next = interfaceMap{
AuthOwner: id.Invalid,
AuthReadonly: false,
}
}
func (as *authService) GetLogger() *logger.Logger { return as.logger }
func (as *authService) GetLogger() *slog.Logger { return as.logger }
func (as *authService) GetLevel() slog.Level { return as.logLevelVar.Level() }
func (as *authService) SetLevel(l slog.Level) { as.logLevelVar.Set(l) }
func (as *authService) Start(*Kernel) error {
as.mxService.Lock()
defer as.mxService.Unlock()
readonlyMode := as.GetNextConfig(AuthReadonly).(bool)
owner := as.GetNextConfig(AuthOwner).(id.Zid)
authMgr, err := as.createManager(readonlyMode, owner)
if err != nil {
as.logger.Error().Err(err).Msg("Unable to create manager")
as.logger.Error("Unable to create manager", "err", err)
return err
}
as.logger.Info().Msg("Start Manager")
as.logger.Info("Start Manager")
as.manager = authMgr
return nil
}
func (as *authService) IsStarted() bool {
as.mxService.RLock()
defer as.mxService.RUnlock()
return as.manager != nil
}
func (as *authService) Stop(*Kernel) {
as.logger.Info().Msg("Stop Manager")
as.logger.Info("Stop Manager")
as.mxService.Lock()
as.manager = nil
as.mxService.Unlock()
}
func (*authService) GetStatistics() []KeyValue { return nil }
|
Changes to internal/kernel/box.go.
︙ | | |
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
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
+
-
-
+
+
|
package kernel
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/url"
"strconv"
"sync"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/logger"
)
type boxService struct {
srvConfig
mxService sync.RWMutex
manager box.Manager
createManager CreateBoxManagerFunc
}
var errInvalidDirType = errors.New("invalid directory type")
func (ps *boxService) Initialize(logger *logger.Logger) {
func (ps *boxService) Initialize(levelVar *slog.LevelVar, logger *slog.Logger) {
ps.logLevelVar = levelVar
ps.logger = logger
ps.descr = descriptionMap{
BoxDefaultDirType: {
"Default directory box type",
ps.noFrozen(func(val string) (any, error) {
switch val {
case BoxDirTypeNotify, BoxDirTypeSimple:
|
︙ | | |
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
|
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
|
-
+
+
+
-
+
-
+
-
+
-
+
|
},
}
ps.next = interfaceMap{
BoxDefaultDirType: BoxDirTypeNotify,
}
}
func (ps *boxService) GetLogger() *logger.Logger { return ps.logger }
func (ps *boxService) GetLogger() *slog.Logger { return ps.logger }
func (ps *boxService) GetLevel() slog.Level { return ps.logLevelVar.Level() }
func (ps *boxService) SetLevel(l slog.Level) { ps.logLevelVar.Set(l) }
func (ps *boxService) Start(kern *Kernel) error {
boxURIs := make([]*url.URL, 0, 4)
for i := 1; ; i++ {
u := ps.GetNextConfig(BoxURIs + strconv.Itoa(i))
if u == nil {
break
}
boxURIs = append(boxURIs, u.(*url.URL))
}
ps.mxService.Lock()
defer ps.mxService.Unlock()
mgr, err := ps.createManager(boxURIs, kern.auth.manager, &kern.cfg)
if err != nil {
ps.logger.Error().Err(err).Msg("Unable to create manager")
ps.logger.Error("Unable to create manager", "err", err)
return err
}
ps.logger.Info().Str("location", mgr.Location()).Msg("Start Manager")
ps.logger.Info("Start Manager", "location", mgr.Location())
if err = mgr.Start(context.Background()); err != nil {
ps.logger.Error().Err(err).Msg("Unable to start manager")
ps.logger.Error("Unable to start manager", "err", err)
return err
}
kern.cfg.setBox(mgr)
ps.manager = mgr
return nil
}
func (ps *boxService) IsStarted() bool {
ps.mxService.RLock()
defer ps.mxService.RUnlock()
return ps.manager != nil
}
func (ps *boxService) Stop(*Kernel) {
ps.logger.Info().Msg("Stop Manager")
ps.logger.Info("Stop Manager")
ps.mxService.RLock()
mgr := ps.manager
ps.mxService.RUnlock()
mgr.Stop(context.Background())
ps.mxService.Lock()
ps.manager = nil
ps.mxService.Unlock()
|
︙ | | |
Changes to internal/kernel/cfg.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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
|
+
-
-
-
-
+
+
+
+
|
package kernel
import (
"context"
"errors"
"fmt"
"log/slog"
"strconv"
"strings"
"sync"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/web/server"
"zettelstore.de/z/internal/auth/user"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/logging"
)
type configService struct {
srvConfig
mxService sync.RWMutex
orig *meta.Meta
manager box.Manager
|
︙ | | |
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
-
+
+
|
keySiteName = "site-name"
keyYAMLHeader = "yaml-header"
keyZettelFileSyntax = "zettel-file-syntax"
)
var errUnknownVisibility = errors.New("unknown visibility")
func (cs *configService) Initialize(logger *logger.Logger) {
func (cs *configService) Initialize(levelVar *slog.LevelVar, logger *slog.Logger) {
cs.logLevelVar = levelVar
cs.logger = logger
cs.descr = descriptionMap{
keyDefaultCopyright: {"Default copyright", parseString, true},
keyDefaultLicense: {"Default license", parseString, true},
keyDefaultVisibility: {
"Default zettel visibility",
func(val string) (any, error) {
|
︙ | | |
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
|
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
|
-
+
-
-
+
-
+
-
+
+
+
-
+
-
+
-
+
+
-
+
-
+
+
+
+
-
+
-
+
+
-
+
-
+
+
+
+
-
+
|
return config.ZettelmarkupHTML, nil
}
return config.NoHTML, nil
}),
true,
},
meta.KeyLang: {"Language", parseString, true},
keyMaxTransclusions: {"Maximum transclusions", parseInt64, true},
keyMaxTransclusions: {"Maximum transclusions", parseInt, true},
keySiteName: {"Site name", parseString, true},
keyYAMLHeader: {"YAML header", parseBool, true},
keyZettelFileSyntax: {
"Zettel file syntax",
func(val string) (any, error) { return strings.Fields(val), nil },
true,
},
ConfigSimpleMode: {"Simple mode", cs.noFrozen(parseBool), true},
config.KeyListsMenuZettel: {"Lists menu", parseZid, true},
config.KeyShowBackLinks: {"Show back links", parseString, true},
config.KeyShowFolgeLinks: {"Show folge links", parseString, true},
config.KeyShowSequelLinks: {"Show sequel links", parseString, true},
config.KeyShowSubordinateLinks: {"Show subordinate links", parseString, true},
config.KeyShowSuccessorLinks: {"Show successor links", parseString, true},
}
cs.next = interfaceMap{
keyDefaultCopyright: "",
keyDefaultLicense: "",
keyDefaultVisibility: meta.VisibilityLogin,
keyExpertMode: false,
config.KeyFooterZettel: id.Invalid,
config.KeyHomeZettel: id.ZidDefaultHome,
ConfigInsecureHTML: config.NoHTML,
meta.KeyLang: meta.ValueLangEN,
keyMaxTransclusions: int64(1024),
keyMaxTransclusions: 1024,
keySiteName: "Zettelstore",
keyYAMLHeader: false,
keyZettelFileSyntax: nil,
ConfigSimpleMode: false,
config.KeyListsMenuZettel: id.ZidTOCListsMenu,
config.KeyShowBackLinks: "",
config.KeyShowFolgeLinks: "",
config.KeyShowSequelLinks: "",
config.KeyShowSubordinateLinks: "",
config.KeyShowSuccessorLinks: "",
}
}
func (cs *configService) GetLogger() *logger.Logger { return cs.logger }
func (cs *configService) GetLogger() *slog.Logger { return cs.logger }
func (cs *configService) GetLevel() slog.Level { return cs.logLevelVar.Level() }
func (cs *configService) SetLevel(l slog.Level) { cs.logLevelVar.Set(l) }
func (cs *configService) Start(*Kernel) error {
cs.logger.Info().Msg("Start Service")
cs.logger.Info("Start Service")
data := meta.New(id.ZidConfiguration)
for _, kv := range cs.GetNextConfigList() {
data.Set(kv.Key, meta.Value(kv.Value))
}
cs.mxService.Lock()
cs.orig = data
cs.mxService.Unlock()
return nil
}
func (cs *configService) IsStarted() bool {
cs.mxService.RLock()
defer cs.mxService.RUnlock()
return cs.orig != nil
}
func (cs *configService) Stop(*Kernel) {
cs.logger.Info().Msg("Stop Service")
cs.logger.Info("Stop Service")
cs.mxService.Lock()
cs.orig = nil
cs.manager = nil
cs.mxService.Unlock()
}
func (*configService) GetStatistics() []KeyValue {
return nil
}
func (cs *configService) setBox(mgr box.Manager) {
cs.mxService.Lock()
cs.manager = mgr
cs.mxService.Unlock()
mgr.RegisterObserver(cs.observe)
cs.observe(box.UpdateInfo{Box: mgr, Reason: box.OnZettel, Zid: id.ZidConfiguration})
}
func (cs *configService) doUpdate(p box.BaseBox) error {
z, err := p.GetZettel(context.Background(), id.ZidConfiguration)
cs.logger.Trace().Err(err).Msg("got config meta")
logging.LogTrace(cs.logger, "got config meta", logging.Err(err))
if err != nil {
return err
}
m := z.Meta
cs.mxService.Lock()
for key := range cs.orig.All() {
var setErr error
if val, ok := m.Get(key); ok {
cs.SetConfig(key, string(val))
setErr = cs.SetConfig(key, string(val))
} else if defVal, defFound := cs.orig.Get(key); defFound {
cs.SetConfig(key, string(defVal))
setErr = cs.SetConfig(key, string(defVal))
}
if err == nil && setErr != nil && setErr != errAlreadyFrozen {
err = fmt.Errorf("%w (key=%q)", setErr, key)
}
}
cs.mxService.Unlock()
cs.SwitchNextToCur() // Poor man's restart
return nil
return err
}
func (cs *configService) observe(ci box.UpdateInfo) {
if (ci.Reason != box.OnZettel && ci.Reason != box.OnDelete) || ci.Zid == id.ZidConfiguration {
cs.logger.Debug().Uint("reason", uint64(ci.Reason)).Zid(ci.Zid).Msg("observe")
cs.logger.Debug("observe", "reason", ci.Reason, "zid", ci.Zid)
go func() {
cs.mxService.RLock()
mgr := cs.manager
cs.mxService.RUnlock()
var err error
if mgr != nil {
cs.doUpdate(mgr)
err = cs.doUpdate(mgr)
} else {
cs.doUpdate(ci.Box)
err = cs.doUpdate(ci.Box)
}
if err != nil {
cs.logger.Error("update config", "err", err)
}
}()
}
}
// --- config.Config
func (cs *configService) Get(ctx context.Context, m *meta.Meta, key string) string {
if m != nil {
if val, found := m.Get(key); found {
return string(val)
}
}
if user := server.GetUser(ctx); user != nil {
if user := user.GetCurrentUser(ctx); user != nil {
if val, found := user.Get(key); found {
return string(val)
}
}
result := cs.GetCurConfig(key)
if result == nil {
return ""
|
︙ | | |
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
-
+
|
}
// GetSiteName returns the current value of the "site-name" key.
func (cs *configService) GetSiteName() string { return cs.GetCurConfig(keySiteName).(string) }
// GetMaxTransclusions return the maximum number of indirect transclusions.
func (cs *configService) GetMaxTransclusions() int {
return int(cs.GetCurConfig(keyMaxTransclusions).(int64))
return int(cs.GetCurConfig(keyMaxTransclusions).(int))
}
// GetYAMLHeader returns the current value of the "yaml-header" key.
func (cs *configService) GetYAMLHeader() bool { return cs.GetCurConfig(keyYAMLHeader).(bool) }
// GetZettelFileSyntax returns the current value of the "zettel-file-syntax" key.
func (cs *configService) GetZettelFileSyntax() []meta.Value {
|
︙ | | |
Changes to internal/kernel/cmd.go.
︙ | | |
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
+
-
+
|
//-----------------------------------------------------------------------------
package kernel
import (
"fmt"
"io"
"log/slog"
"maps"
"os"
"runtime/metrics"
"slices"
"strconv"
"strings"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/strfun"
)
type cmdSession struct {
w io.Writer
kern *Kernel
echo bool
|
︙ | | |
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
-
+
-
-
+
+
-
+
|
sess.println("Unknown command:", cmd, strings.Join(args, " "))
sess.println("-- Enter 'help' go get a list of valid commands.")
return true
}
func (sess *cmdSession) println(args ...string) {
if len(args) > 0 {
io.WriteString(sess.w, args[0])
_, _ = io.WriteString(sess.w, args[0])
for _, arg := range args[1:] {
io.WriteString(sess.w, " ")
io.WriteString(sess.w, arg)
_, _ = io.WriteString(sess.w, " ")
_, _ = io.WriteString(sess.w, arg)
}
}
sess.w.Write(sess.eol)
_, _ = sess.w.Write(sess.eol)
}
func (sess *cmdSession) usage(cmd, val string) {
sess.println("Usage:", cmd, val)
}
func (sess *cmdSession) printTable(table [][]string) {
|
︙ | | |
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
-
+
-
+
-
+
|
}
}
return maxLen
}
func (sess *cmdSession) printRow(row []string, maxLen []int, prefix, delim string, pad rune) {
for colno, column := range row {
io.WriteString(sess.w, prefix)
_, _ = io.WriteString(sess.w, prefix)
prefix = delim
io.WriteString(sess.w, strfun.JustifyLeft(column, maxLen[colno], pad))
_, _ = io.WriteString(sess.w, strfun.JustifyLeft(column, maxLen[colno], pad))
}
sess.w.Write(sess.eol)
_, _ = sess.w.Write(sess.eol)
}
func splitLine(line string) (string, []string) {
s := strings.Fields(line)
if len(s) == 0 {
return "", nil
}
|
︙ | | |
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
-
+
|
srvD, found := getService(sess, args[0])
if !found {
return true
}
key := args[1]
newValue := strings.Join(args[2:], " ")
if err := srvD.srv.SetConfig(key, newValue); err == nil {
sess.kern.logger.Mandatory().Str("key", key).Str("value", newValue).Msg("Update system configuration")
logging.LogMandatory(sess.kern.logger, "Update system configuration", "key", key, "value", newValue)
} else {
sess.println("Unable to set key", args[1], "to value", newValue, "because:", err.Error())
}
return true
}
func cmdServices(sess *cmdSession, _ string, _ []string) bool {
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
+
-
-
+
+
|
return true
}
func cmdLogLevel(sess *cmdSession, _ string, args []string) bool {
kern := sess.kern
if len(args) == 0 {
// Write log levels
level := kern.logger.Level()
level := kern.GetKernelLogLevel()
table := [][]string{
{"Service", "Level", "Name"},
{"kernel", strconv.Itoa(int(level)), level.String()},
{"(kernel)", strconv.Itoa(int(level)), logging.LevelString(level)},
}
for _, name := range sortedServiceNames(sess) {
level = kern.srvNames[name].srv.GetLogger().Level()
table = append(table, []string{name, strconv.Itoa(int(level)), level.String()})
level = kern.srvNames[name].srv.GetLevel()
table = append(table, []string{name, strconv.Itoa(int(level.Level())), logging.LevelString(level)})
}
sess.printTable(table)
return true
}
var l *logger.Logger
name := args[0]
if name == "kernel" {
l = kern.logger
} else {
srvD, ok := getService(sess, name)
if !ok {
return true
}
l = srvD.srv.GetLogger()
}
srvD, ok := getService(sess, args[0])
if !ok {
return true
}
srv := srvD.srv
if len(args) == 1 {
level := l.Level()
sess.println(strconv.Itoa(int(level)), level.String())
lvl := srv.GetLevel()
sess.println(strconv.Itoa(int(lvl)), lvl.String())
return true
}
level := args[1]
uval, err := strconv.ParseUint(level, 10, 8)
lv := logger.Level(uval)
if err != nil || !lv.IsValid() {
lv = logger.ParseLevel(level)
levelString := args[1]
var lvl slog.Level
if val, err := strconv.ParseInt(levelString, 10, 8); err == nil {
lvl = slog.Level(val)
} else {
lvl = logging.ParseLevel(levelString)
}
if !lv.IsValid() {
sess.println("Invalid level:", level)
if lvl <= logging.LevelMissing || logging.LevelMandatory < lvl {
sess.println("Invalid level: ", levelString)
return true
}
logging.LogMandatory(kern.logger,
kern.logger.Mandatory().Str("name", name).Str("level", lv.String()).Msg("Update log level")
l.SetLevel(lv)
"Update log level", "name", args[0], "level", logging.LevelString(lvl))
srv.SetLevel(lvl)
return true
}
func lookupService(sess *cmdSession, cmd string, args []string) (Service, bool) {
if len(args) == 0 {
sess.usage(cmd, "SERVICE")
return 0, false
|
︙ | | |
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
|
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
|
-
+
-
+
|
} else {
fileName = args[1]
}
kern := sess.kern
if err := kern.doStartProfiling(profileName, fileName); err != nil {
sess.println("Error:", err.Error())
} else {
kern.logger.Mandatory().Str("profile", profileName).Str("file", fileName).Msg("Start profiling")
logging.LogMandatory(sess.kern.logger, "Start profiling", "profile", profileName, "file", fileName)
}
return true
}
func cmdEndProfile(sess *cmdSession, _ string, _ []string) bool {
kern := sess.kern
err := kern.doStopProfiling()
if err != nil {
sess.println("Error:", err.Error())
}
kern.logger.Mandatory().Err(err).Msg("Stop profiling")
logging.LogMandatory(sess.kern.logger, "Stop profiling", logging.Err(err))
return true
}
func cmdMetrics(sess *cmdSession, _ string, _ []string) bool {
var samples []metrics.Sample
all := metrics.All()
for _, d := range all {
|
︙ | | |
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
-
-
+
+
+
+
|
func cmdDumpIndex(sess *cmdSession, _ string, _ []string) bool {
sess.kern.dumpIndex(sess.w)
return true
}
func cmdRefresh(sess *cmdSession, _ string, _ []string) bool {
kern := sess.kern
kern.logger.Mandatory().Msg("Refresh")
kern.box.Refresh()
logging.LogMandatory(sess.kern.logger, "Refresh")
if err := kern.box.Refresh(); err != nil {
logging.LogMandatory(sess.kern.logger, "refresh", "err", err)
}
return true
}
func cmdDumpRecover(sess *cmdSession, cmd string, args []string) bool {
if len(args) == 0 {
sess.usage(cmd, "RECOVER")
sess.println("-- A valid value for RECOVER can be obtained via 'stat core'.")
|
︙ | | |
Changes to internal/kernel/config.go.
︙ | | |
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
|
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
|
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
|
//-----------------------------------------------------------------------------
package kernel
import (
"errors"
"fmt"
"log/slog"
"maps"
"slices"
"strconv"
"strings"
"sync"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/logger"
)
type parseFunc func(string) (any, error)
type configDescription struct {
text string
parse parseFunc
canList bool
}
type descriptionMap map[string]configDescription
type interfaceMap map[string]any
func (m interfaceMap) Clone() interfaceMap { return maps.Clone(m) }
type srvConfig struct {
logLevelVar *slog.LevelVar
logger *logger.Logger
mxConfig sync.RWMutex
frozen bool
descr descriptionMap
cur interfaceMap
next interfaceMap
logger *slog.Logger
mxConfig sync.RWMutex
frozen bool
descr descriptionMap
cur interfaceMap
next interfaceMap
}
func (cfg *srvConfig) ConfigDescriptions() []serviceConfigDescription {
cfg.mxConfig.RLock()
defer cfg.mxConfig.RUnlock()
keys := slices.Sorted(maps.Keys(cfg.descr))
result := make([]serviceConfigDescription, 0, len(keys))
for _, k := range keys {
text := cfg.descr[k].text
if strings.HasSuffix(k, "-") {
text = text + " (list)"
}
result = append(result, serviceConfigDescription{Key: k, Descr: text})
}
return result
}
var errAlreadyFrozen = errors.New("value not allowed to be set")
var errAlreadyFrozen = errors.New("value frozen")
func (cfg *srvConfig) noFrozen(parse parseFunc) parseFunc {
return func(val string) (any, error) {
if cfg.frozen {
return nil, errAlreadyFrozen
}
return parse(val)
|
︙ | | |
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
|
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
|
-
-
-
-
+
+
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
|
func (cfg *srvConfig) SwitchNextToCur() {
cfg.mxConfig.Lock()
defer cfg.mxConfig.Unlock()
cfg.cur = cfg.next.Clone()
}
func parseString(val string) (any, error) { return val, nil }
var errNoBoolean = errors.New("no boolean value")
func parseBool(val string) (any, error) {
if val == "" {
return false, errNoBoolean
}
switch val[0] {
case '0', 'f', 'F', 'n', 'N':
return false, nil
}
return true, nil
}
func parseInt64(val string) (any, error) {
u64, err := strconv.ParseInt(val, 10, 64)
func parseString(val string) (any, error) { return val, nil }
func parseInt(val string) (any, error) { return strconv.Atoi(val) }
if err == nil {
return u64, nil
}
return nil, err
}
func parseZid(val string) (any, error) {
func parseInt64(val string) (any, error) { return strconv.ParseInt(val, 10, 64) }
func parseZid(val string) (any, error) { return id.Parse(val) }
zid, err := id.Parse(val)
if err == nil {
return zid, nil
}
return id.Invalid, err
}
func parseInvalidZid(val string) (any, error) {
zid, _ := id.Parse(val)
return zid, nil
}
|
Changes to internal/kernel/core.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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
|
+
-
-
+
+
|
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package kernel
import (
"fmt"
"log/slog"
"maps"
"net"
"os"
"runtime"
"slices"
"sync"
"time"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/strfun"
)
type coreService struct {
srvConfig
started bool
mxRecover sync.RWMutex
mapRecover map[string]recoverInfo
}
type recoverInfo struct {
count uint64
ts time.Time
info any
stack []byte
}
func (cs *coreService) Initialize(logger *logger.Logger) {
func (cs *coreService) Initialize(levelVar *slog.LevelVar, logger *slog.Logger) {
cs.logLevelVar = levelVar
cs.logger = logger
cs.mapRecover = make(map[string]recoverInfo)
cs.descr = descriptionMap{
CoreDebug: {"Debug mode", parseBool, false},
CoreGoArch: {"Go processor architecture", nil, false},
CoreGoOS: {"Go Operating System", nil, false},
CoreGoVersion: {"Go Version", nil, false},
|
︙ | | |
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
-
+
+
+
|
CoreVerbose: false,
}
if hn, err := os.Hostname(); err == nil {
cs.next[CoreHostname] = hn
}
}
func (cs *coreService) GetLogger() *logger.Logger { return cs.logger }
func (cs *coreService) GetLogger() *slog.Logger { return cs.logger }
func (cs *coreService) GetLevel() slog.Level { return cs.logLevelVar.Level() }
func (cs *coreService) SetLevel(l slog.Level) { cs.logLevelVar.Set(l) }
func (cs *coreService) Start(*Kernel) error {
cs.started = true
return nil
}
func (cs *coreService) IsStarted() bool { return cs.started }
func (cs *coreService) Stop(*Kernel) {
|
︙ | | |
Changes to internal/kernel/kernel.go.
︙ | | |
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
|
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
|
+
-
+
+
+
-
+
-
-
-
-
+
+
+
-
+
-
-
-
+
+
+
+
-
+
-
+
-
-
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
+
+
+
+
-
-
-
+
+
+
|
// Package kernel provides the main kernel service.
package kernel
import (
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/url"
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/pprof"
"strconv"
"strings"
"sync"
"syscall"
"time"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/auth"
"zettelstore.de/z/internal/box"
"zettelstore.de/z/internal/config"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/web/server"
)
// Main references the main kernel.
var Main *Kernel
// Kernel is the main internal kernel.
type Kernel struct {
logLevelVar slog.LevelVar
logger *slog.Logger
logWriter *kernelLogWriter
dlogWriter *kernelLogWriter
logger *logger.Logger
wg sync.WaitGroup
mx sync.RWMutex
interrupt chan os.Signal
wg sync.WaitGroup
mx sync.RWMutex
interrupt chan os.Signal
profileName string
fileName string
profileFile *os.File
profile *pprof.Profile
self kernelService
core coreService
cfg configService
auth authService
box boxService
web webService
srvs map[Service]serviceDescr
srvs map[Service]*serviceDescr
srvNames map[string]serviceData
depStart serviceDependency
depStop serviceDependency // reverse of depStart
}
type serviceDescr struct {
srv service
name string
logLevel logger.Level
srv service
name string
logLevel slog.Level
logLevelVar slog.LevelVar
}
type serviceData struct {
srv service
srvnum Service
}
type serviceDependency map[Service][]Service
// create a new kernel.
func init() {
Main = createKernel()
}
// create a new
func createKernel() *Kernel {
lw := newKernelLogWriter(8192)
lw := newKernelLogWriter(os.Stdout, 8192)
kern := &Kernel{
logWriter: lw,
dlogWriter: lw,
logger: logger.New(lw, "").SetLevel(defaultNormalLogLevel),
interrupt: make(chan os.Signal, 5),
interrupt: make(chan os.Signal, 5),
}
kern.logLevelVar.Set(defaultNormalLogLevel)
kern.logger = slog.New(newKernelLogHandler(lw, &kern.logLevelVar))
kern.self.kernel = kern
kern.srvs = map[Service]serviceDescr{
KernelService: {&kern.self, "kernel", defaultNormalLogLevel},
CoreService: {&kern.core, "core", defaultNormalLogLevel},
ConfigService: {&kern.cfg, "config", defaultNormalLogLevel},
AuthService: {&kern.auth, "auth", defaultNormalLogLevel},
BoxService: {&kern.box, "box", defaultNormalLogLevel},
WebService: {&kern.web, "web", defaultNormalLogLevel},
kern.srvs = map[Service]*serviceDescr{
KernelService: {srv: &kern.self, name: "kernel", logLevel: defaultNormalLogLevel},
CoreService: {srv: &kern.core, name: "core", logLevel: defaultNormalLogLevel},
ConfigService: {srv: &kern.cfg, name: "config", logLevel: defaultNormalLogLevel},
AuthService: {srv: &kern.auth, name: "auth", logLevel: defaultNormalLogLevel},
BoxService: {srv: &kern.box, name: "box", logLevel: defaultNormalLogLevel},
WebService: {srv: &kern.web, name: "web", logLevel: defaultNormalLogLevel},
}
kern.srvNames = make(map[string]serviceData, len(kern.srvs))
for key, srvD := range kern.srvs {
if _, ok := kern.srvNames[srvD.name]; ok {
kern.logger.Error().Str("service", srvD.name).Msg("Service data already set, ignore")
kern.logger.Error("Service data already set, ignored", "service", srvD.name)
}
kern.srvNames[srvD.name] = serviceData{srvD.srv, key}
srvD.logLevelVar.Set(srvD.logLevel)
srvLogger := slog.New(newKernelLogHandler(lw, &srvD.logLevelVar)).With(
l := logger.New(lw, strings.ToUpper(srvD.name)).SetLevel(srvD.logLevel)
kern.logger.Debug().Str("service", srvD.name).Msg("Initialize")
srvD.srv.Initialize(l)
"system", strings.ToUpper(srvD.name))
kern.logger.Debug("Initialize", "service", srvD.name)
srvD.srv.Initialize(&srvD.logLevelVar, srvLogger)
}
kern.depStart = serviceDependency{
KernelService: nil,
CoreService: {KernelService},
ConfigService: {CoreService},
AuthService: {CoreService},
BoxService: {CoreService, ConfigService, AuthService},
|
︙ | | |
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
|
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
|
+
+
+
-
+
-
+
+
|
)
// Constants for web service keys.
const (
WebAssetDir = "asset-dir"
WebBaseURL = "base-url"
WebListenAddress = "listen"
WebLoopbackIdent = "loopback-ident"
WebLoopbackZid = "loopback-zid"
WebPersistentCookie = "persistent"
WebProfiling = "profiling"
WebMaxRequestSize = "max-request-size"
WebSecureCookie = "secure"
WebSxMaxNesting = "sx-max-nesting"
WebTokenLifetimeAPI = "api-lifetime"
WebTokenLifetimeHTML = "html-lifetime"
WebURLPrefix = "prefix"
)
// KeyDescrValue is a triple of config data.
type KeyDescrValue struct{ Key, Descr, Value string }
// KeyValue is a pair of key and value.
type KeyValue struct{ Key, Value string }
// LogEntry stores values of one log line written by a logger.Logger
// LogEntry stores values of one log line written by a logger.
type LogEntry struct {
Level logger.Level
Level slog.Level
TS time.Time
Prefix string
Message string
Details string
}
// CreateAuthManagerFunc is called to create a new auth manager.
type CreateAuthManagerFunc func(readonly bool, owner id.Zid) (auth.Manager, error)
// CreateBoxManagerFunc is called to create a new box manager.
type CreateBoxManagerFunc func(
|
︙ | | |
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
|
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
|
-
-
+
+
-
-
-
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
+
+
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
+
-
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
|
webServer server.Server,
boxManager box.Manager,
authManager auth.Manager,
rtConfig config.Config,
) error
const (
defaultNormalLogLevel = logger.InfoLevel
defaultSimpleLogLevel = logger.ErrorLevel
defaultNormalLogLevel = slog.LevelInfo
defaultSimpleLogLevel = slog.LevelError
)
// Setup sets the most basic data of a software: its name, its version,
// and when the version was created.
func (kern *Kernel) Setup(progname, version string, versionTime time.Time) {
kern.SetConfig(CoreService, CoreProgname, progname)
kern.SetConfig(CoreService, CoreVersion, version)
kern.SetConfig(CoreService, CoreVTime, versionTime.Local().Format(id.TimestampLayout))
_ = kern.SetConfig(CoreService, CoreProgname, progname)
_ = kern.SetConfig(CoreService, CoreVersion, version)
_ = kern.SetConfig(CoreService, CoreVTime, versionTime.Local().Format(id.TimestampLayout))
}
// Start the service.
func (kern *Kernel) Start(headline, lineServer bool, configFilename string) {
for _, srvD := range kern.srvs {
srvD.srv.Freeze()
}
if kern.cfg.GetCurConfig(ConfigSimpleMode).(bool) {
kern.SetLogLevel(defaultSimpleLogLevel.String())
kern.logLevelVar.Set(defaultSimpleLogLevel)
}
kern.wg.Add(1)
signal.Notify(kern.interrupt, os.Interrupt, syscall.SIGTERM)
go func() {
// Wait for interrupt.
sig := <-kern.interrupt
if strSig := sig.String(); strSig != "" {
kern.logger.Info().Str("signal", strSig).Msg("Shut down Zettelstore")
kern.logger.Info("Shut down Zettelstore", "signal", strSig)
}
kern.doShutdown()
kern.wg.Done()
}()
kern.StartService(KernelService)
_ = kern.StartService(KernelService)
if headline {
logger := kern.logger
logger.Mandatory().Msg(fmt.Sprintf(
logging.LogMandatory(logger, fmt.Sprintf(
"%v %v (%v@%v/%v)",
kern.core.GetCurConfig(CoreProgname),
kern.core.GetCurConfig(CoreVersion),
kern.core.GetCurConfig(CoreGoVersion),
kern.core.GetCurConfig(CoreGoOS),
kern.core.GetCurConfig(CoreGoArch),
))
logger.Mandatory().Msg("Licensed under the latest version of the EUPL (European Union Public License)")
logging.LogMandatory(logger, "Licensed under the latest version of the EUPL (European Union Public License)")
if configFilename != "" {
logger.Mandatory().Str("filename", configFilename).Msg("Configuration file found")
logging.LogMandatory(logger, "Configuration file found", "filename", configFilename)
} else {
logger.Mandatory().Msg("No configuration file found / used")
logging.LogMandatory(logger, "No configuration file found / used")
}
if kern.core.GetCurConfig(CoreDebug).(bool) {
logger.Info().Msg("----------------------------------------")
logger.Info().Msg("DEBUG MODE, DO NO USE THIS IN PRODUCTION")
logger.Info().Msg("----------------------------------------")
logger.Info("----------------------------------------")
logger.Info("DEBUG MODE, DO NO USE THIS IN PRODUCTION")
logger.Info("----------------------------------------")
}
if kern.auth.GetCurConfig(AuthReadonly).(bool) {
logger.Info().Msg("Read-only mode")
logger.Info("Read-only mode")
}
}
if lineServer {
port := kern.core.GetNextConfig(CorePort).(int)
if port > 0 {
listenAddr := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
startLineServer(kern, listenAddr)
_ = startLineServer(kern, listenAddr)
}
}
}
func (kern *Kernel) doShutdown() {
kern.stopService(KernelService) // Will stop all other services.
}
// WaitForShutdown blocks the call until Shutdown is called.
func (kern *Kernel) WaitForShutdown() {
kern.wg.Wait()
kern.doStopProfiling()
_ = kern.doStopProfiling()
}
// --- Shutdown operation ----------------------------------------------------
// Shutdown the service. Waits for all concurrent activities to stop.
func (kern *Kernel) Shutdown(silent bool) {
kern.logger.Trace().Msg("Shutdown")
logging.LogTrace(kern.logger, "Shutdown")
kern.interrupt <- &shutdownSignal{silent: silent}
}
type shutdownSignal struct{ silent bool }
func (s *shutdownSignal) String() string {
if s.silent {
return ""
}
return "shutdown"
}
func (*shutdownSignal) Signal() { /* Just a signal */ }
// --- Log operation ---------------------------------------------------------
// GetKernelLogger returns the kernel logger.
func (kern *Kernel) GetKernelLogger() *logger.Logger {
func (kern *Kernel) GetKernelLogger() *slog.Logger { return kern.logger }
return kern.logger
}
// GetKernelLogLevel return the logging level of the kernel logger.
func (kern *Kernel) GetKernelLogLevel() slog.Level { return kern.logLevelVar.Level() }
// SetLogLevel sets the logging level for logger maintained by the kernel.
//
// Its syntax is: (SERVICE ":")? LEVEL (";" (SERICE ":")? LEVEL)*.
// Its syntax is: (SERVICE ":")? LEVEL (";" (SERVICE ":")? LEVEL)*.
func (kern *Kernel) SetLogLevel(logLevel string) {
defaultLevel, srvLevel := kern.parseLogLevel(logLevel)
kern.mx.RLock()
defer kern.mx.RUnlock()
for srvN, srvD := range kern.srvs {
if lvl, found := srvLevel[srvN]; found {
srvD.srv.GetLogger().SetLevel(lvl)
} else if defaultLevel != logger.NoLevel {
srvD.srv.GetLogger().SetLevel(defaultLevel)
srvD.srv.SetLevel(lvl)
} else if defaultLevel != logging.LevelMissing {
srvD.srv.SetLevel(defaultLevel)
}
}
}
func (kern *Kernel) parseLogLevel(logLevel string) (logger.Level, map[Service]logger.Level) {
defaultLevel := logger.NoLevel
srvLevel := map[Service]logger.Level{}
func (kern *Kernel) parseLogLevel(logLevel string) (slog.Level, map[Service]slog.Level) {
defaultLevel := logging.LevelMissing
srvLevel := map[Service]slog.Level{}
for spec := range strings.SplitSeq(logLevel, ";") {
vals := cleanLogSpec(strings.Split(spec, ":"))
switch len(vals) {
case 0:
case 1:
if lvl := logger.ParseLevel(vals[0]); lvl.IsValid() {
if lvl := logging.ParseLevel(vals[0]); lvl != logging.LevelMissing {
defaultLevel = lvl
}
default:
serviceText, levelText := vals[0], vals[1]
if srv, found := kern.srvNames[serviceText]; found {
if lvl := logger.ParseLevel(levelText); lvl.IsValid() {
if lvl := logging.ParseLevel(levelText); lvl != logging.LevelMissing {
srvLevel[srv.srvnum] = lvl
}
}
}
}
return defaultLevel, srvLevel
}
|
︙ | | |
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
-
+
-
+
-
+
|
}
}
return vals
}
// RetrieveLogEntries returns all buffered log entries.
func (kern *Kernel) RetrieveLogEntries() []LogEntry {
return kern.logWriter.retrieveLogEntries()
return kern.dlogWriter.retrieveLogEntries()
}
// GetLastLogTime returns the time when the last logging with level > DEBUG happened.
func (kern *Kernel) GetLastLogTime() time.Time { return kern.logWriter.getLastLogTime() }
func (kern *Kernel) GetLastLogTime() time.Time { return kern.dlogWriter.getLastLogTime() }
// LogRecover outputs some information about the previous panic.
func (kern *Kernel) LogRecover(name string, recoverInfo any) {
stack := debug.Stack()
kern.logger.Error().Str("recovered_from", fmt.Sprint(recoverInfo)).Bytes("stack", stack).Msg(name)
kern.logger.Error(name, "recovered_from", recoverInfo, "stack", stack)
kern.core.updateRecoverInfo(name, recoverInfo, stack)
}
// --- Profiling ---------------------------------------------------------
var errProfileInWork = errors.New("already profiling")
var errProfileNotFound = errors.New("profile not found")
|
︙ | | |
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
|
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
|
-
+
-
-
+
-
+
-
|
return errProfileInWork
}
if profileName == ProfileCPU {
f, err := os.Create(fileName)
if err != nil {
return err
}
err = pprof.StartCPUProfile(f)
if err = pprof.StartCPUProfile(f); err != nil {
if err != nil {
f.Close()
_ = f.Close()
return err
}
kern.profileName = profileName
kern.fileName = fileName
kern.profileFile = f
return nil
}
profile := pprof.Lookup(profileName)
if profile == nil {
return errProfileNotFound
}
f, err := os.Create(fileName)
if err != nil {
return err
}
kern.profileName = profileName
kern.fileName = fileName
kern.profile = profile
kern.profileFile = f
runtime.GC() // get up-to-date statistics
profile.WriteTo(f, 0)
return profile.WriteTo(f, 0)
return nil
}
// StopProfiling stops the current profiling and writes the result to
// the file, which was named during StartProfiling().
// It will always be called before the software stops its operations.
func (kern *Kernel) StopProfiling() error {
kern.mx.Lock()
|
︙ | | |
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
-
+
|
if srvD, ok := kern.srvs[srvnum]; ok {
return srvD.srv.GetStatistics()
}
return nil
}
// GetLogger returns a logger for the given service.
func (kern *Kernel) GetLogger(srvnum Service) *logger.Logger {
func (kern *Kernel) GetLogger(srvnum Service) *slog.Logger {
kern.mx.RLock()
defer kern.mx.RUnlock()
if srvD, ok := kern.srvs[srvnum]; ok {
return srvD.srv.GetLogger()
}
return kern.GetKernelLogger()
}
|
︙ | | |
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
-
+
-
+
+
+
+
+
+
+
|
}
// dumpIndex writes some data about the internal index into a writer.
func (kern *Kernel) dumpIndex(w io.Writer) { kern.box.dumpIndex(w) }
type service interface {
// Initialize the data for the service.
Initialize(*logger.Logger)
Initialize(*slog.LevelVar, *slog.Logger)
// Get service logger.
GetLogger() *logger.Logger
GetLogger() *slog.Logger
// Get the log level.
GetLevel() slog.Level
// Set the service log level.
SetLevel(slog.Level)
// ConfigDescriptions returns a sorted list of configuration descriptions.
ConfigDescriptions() []serviceConfigDescription
// SetConfig stores a configuration value.
SetConfig(key, value string) error
|
︙ | | |
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
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
|
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
|
// --- The kernel as a service -------------------------------------------
type kernelService struct {
kernel *Kernel
}
func (*kernelService) Initialize(*logger.Logger) {}
func (ks *kernelService) GetLogger() *logger.Logger { return ks.kernel.logger }
func (*kernelService) Initialize(*slog.LevelVar, *slog.Logger) {}
func (ks *kernelService) GetLogger() *slog.Logger { return ks.kernel.logger }
func (ks *kernelService) GetLevel() slog.Level { return ks.kernel.logLevelVar.Level() }
func (ks *kernelService) SetLevel(lvl slog.Level) { ks.kernel.logLevelVar.Set(lvl) }
func (*kernelService) ConfigDescriptions() []serviceConfigDescription { return nil }
func (*kernelService) SetConfig(string, string) error { return errAlreadyFrozen }
func (*kernelService) GetCurConfig(string) any { return nil }
func (*kernelService) GetNextConfig(string) any { return nil }
func (*kernelService) GetCurConfigList(bool) []KeyDescrValue { return nil }
func (*kernelService) GetNextConfigList() []KeyDescrValue { return nil }
func (*kernelService) GetStatistics() []KeyValue { return nil }
func (*kernelService) Freeze() {}
func (*kernelService) Start(*Kernel) error { return nil }
func (*kernelService) SwitchNextToCur() {}
func (*kernelService) IsStarted() bool { return true }
func (*kernelService) Stop(*Kernel) {}
func (*kernelService) GetStatistics() []KeyValue { return nil }
func (*kernelService) Freeze() {}
func (*kernelService) Start(*Kernel) error { return nil }
func (*kernelService) SwitchNextToCur() {}
func (*kernelService) IsStarted() bool { return true }
func (*kernelService) Stop(*Kernel) {}
|
Changes to internal/kernel/log.go.
︙ | | |
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
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
|
+
+
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
+
-
-
+
+
+
+
-
+
+
-
+
|
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package kernel
import (
"bytes"
"context"
"os"
"io"
"log/slog"
"slices"
"sync"
"time"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
)
type kernelLogHandler struct {
klw *kernelLogWriter
level slog.Leveler
system string
attrs string
}
func newKernelLogHandler(klw *kernelLogWriter, level slog.Leveler) *kernelLogHandler {
return &kernelLogHandler{
klw: klw,
level: level,
system: "",
attrs: "",
}
}
func (klh *kernelLogHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= klh.level.Level()
}
func (klh *kernelLogHandler) Handle(_ context.Context, rec slog.Record) error {
var buf bytes.Buffer
buf.WriteString(klh.attrs)
rec.Attrs(func(attr slog.Attr) bool {
if !attr.Equal(slog.Attr{}) {
buf.WriteByte(' ')
buf.WriteString(attr.Key)
buf.WriteByte('=')
buf.WriteString(attr.Value.Resolve().String())
}
return true
})
return klh.klw.writeMessage(rec.Level, rec.Time, klh.system, rec.Message, buf.Bytes())
}
func (klh *kernelLogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
h := newKernelLogHandler(klh.klw, klh.level)
for _, attr := range attrs {
if attr.Equal(slog.Attr{}) {
continue
}
if attr.Key == "system" {
system := attr.Value.String()
if len(system) < 6 {
system += " "[:6-len(system)]
}
h.system = system
continue
}
h.attrs += attr.String()
}
return h
}
func (klh *kernelLogHandler) WithGroup(name string) slog.Handler {
if name == "" {
return klh
}
return klh
// panic("kernelLogHandler.WithGroup(name) not implemented")
}
// kernelLogWriter adapts an io.Writer to a LogWriter
type kernelLogWriter struct {
w io.Writer
mx sync.RWMutex // protects buf, serializes w.Write and retrieveLogEntries
lastLog time.Time
buf []byte
writePos int
data []logEntry
full bool
}
// newKernelLogWriter creates a new LogWriter for kernel logging.
func newKernelLogWriter(capacity int) *kernelLogWriter {
func newKernelLogWriter(w io.Writer, capacity int) *kernelLogWriter {
if capacity < 1 {
capacity = 1
}
return &kernelLogWriter{
w: w,
lastLog: time.Now(),
buf: make([]byte, 0, 500),
data: make([]logEntry, capacity),
}
}
func (klw *kernelLogWriter) WriteMessage(level logger.Level, ts time.Time, prefix, msg string, details []byte) error {
func (klw *kernelLogWriter) writeMessage(level slog.Level, ts time.Time, prefix, msg string, details []byte) error {
details = bytes.TrimSpace(details)
klw.mx.Lock()
if level > logger.DebugLevel {
klw.lastLog = ts
if level > slog.LevelDebug {
if !ts.IsZero() {
klw.lastLog = ts
}
klw.data[klw.writePos] = logEntry{
level: level,
ts: ts,
prefix: prefix,
msg: msg,
details: slices.Clone(details),
}
klw.writePos++
if klw.writePos >= cap(klw.data) {
klw.writePos = 0
klw.full = true
}
}
klw.buf = klw.buf[:0]
buf := klw.buf
addTimestamp(&buf, ts)
buf = append(buf, ' ')
buf = append(buf, level.Format()...)
buf = append(buf, logging.LevelStringPad(level)...)
buf = append(buf, ' ')
if prefix != "" {
buf = append(buf, prefix...)
buf = append(buf, ' ')
}
buf = append(buf, msg...)
buf = append(buf, ' ')
buf = append(buf, details...)
buf = append(buf, '\n')
_, err := os.Stdout.Write(buf)
_, err := klw.w.Write(buf)
klw.mx.Unlock()
return err
}
func addTimestamp(buf *[]byte, ts time.Time) {
year, month, day := ts.Date()
|
︙ | | |
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
-
+
|
b[bp] = byte('0' + i - q*10)
i = q
}
*buf = append(*buf, b[:wid]...)
}
type logEntry struct {
level logger.Level
level slog.Level
ts time.Time
prefix string
msg string
details []byte
}
func (klw *kernelLogWriter) retrieveLogEntries() []LogEntry {
|
︙ | | |
150
151
152
153
154
155
156
157
158
|
221
222
223
224
225
226
227
228
229
230
|
-
+
+
|
return klw.lastLog
}
func copyE2E(result *LogEntry, origin *logEntry) {
result.Level = origin.level
result.TS = origin.ts
result.Prefix = origin.prefix
result.Message = origin.msg + string(origin.details)
result.Message = origin.msg
result.Details = string(origin.details)
}
|
Added internal/kernel/log_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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2025-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore 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: 2025-present Detlef Stern
//-----------------------------------------------------------------------------
package kernel
import (
"fmt"
"log/slog"
"strings"
"testing"
"testing/slogtest"
)
func TestLogHand(t *testing.T) {
t.SkipNow() // Handler does not implement Groups
var sb strings.Builder
logWriter := newKernelLogWriter(&sb, 1000)
h := newKernelLogHandler(logWriter, slog.LevelInfo)
results := func() []map[string]any {
var ms []map[string]any
for _, entry := range logWriter.retrieveLogEntries() {
m := map[string]any{
slog.LevelKey: entry.Level,
slog.MessageKey: entry.Message,
}
if ts := entry.TS; !ts.IsZero() {
m[slog.TimeKey] = ts
}
details := entry.Details
fmt.Printf("%q\n", details)
for len(details) > 0 {
pos := strings.Index(details, "=")
if pos <= 0 {
break
}
key := details[:pos]
details = details[pos+1:]
if details == "" || details[0] == '[' {
break
}
pos = strings.Index(details, " ")
var val string
if pos <= 0 {
val = details
details = ""
} else {
val = details[:pos]
details = details[pos+1:]
}
fmt.Printf("key %q, val %q\n", key, val)
m[key] = val
}
ms = append(ms, m)
}
return ms
}
err := slogtest.TestHandler(h, results)
if err != nil {
t.Error(err)
}
t.Error(sb.String())
}
|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Changes to internal/kernel/server.go.
︙ | | |
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
|
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
|
+
+
-
+
-
+
-
+
-
+
-
+
-
+
|
//-----------------------------------------------------------------------------
package kernel
import (
"bufio"
"net"
"zettelstore.de/z/internal/logging"
)
func startLineServer(kern *Kernel, listenAddr string) error {
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
kern.logger.Error().Err(err).Msg("Unable to start administration console")
kern.logger.Error("Unable to start administration console", "err", err)
return err
}
kern.logger.Mandatory().Str("listen", listenAddr).Msg("Start administration console")
logging.LogMandatory(kern.logger, "Start administration console", "listen", listenAddr)
go func() { lineServer(ln, kern) }()
return nil
}
func lineServer(ln net.Listener, kern *Kernel) {
// Something may panic. Ensure a running line service.
defer func() {
if ri := recover(); ri != nil {
kern.LogRecover("Line", ri)
go lineServer(ln, kern)
}
}()
for {
conn, err := ln.Accept()
if err != nil {
// handle error
kern.logger.Error().Err(err).Msg("Unable to accept connection")
kern.logger.Error("Unable to accept connection", "err", err)
break
}
go handleLineConnection(conn, kern)
}
ln.Close()
_ = ln.Close()
}
func handleLineConnection(conn net.Conn, kern *Kernel) {
// Something may panic. Ensure a running connection.
defer func() {
if ri := recover(); ri != nil {
kern.LogRecover("LineConn", ri)
go handleLineConnection(conn, kern)
}
}()
kern.logger.Mandatory().Str("from", conn.RemoteAddr().String()).Msg("Start session on administration console")
logging.LogMandatory(kern.logger, "Start session on administration console", "from", conn.RemoteAddr().String())
cmds := cmdSession{}
cmds.initialize(conn, kern)
s := bufio.NewScanner(conn)
for s.Scan() {
line := s.Text()
if !cmds.executeLine(line) {
break
}
}
conn.Close()
_ = conn.Close()
}
|
Changes to internal/kernel/web.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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
|
+
+
+
-
+
-
+
+
|
// SPDX-FileCopyrightText: 2021-present Detlef Stern
//-----------------------------------------------------------------------------
package kernel
import (
"errors"
"log/slog"
"net"
"net/netip"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"t73f.de/r/zsc/domain/id"
"zettelstore.de/z/internal/logger"
"zettelstore.de/z/internal/logging"
"zettelstore.de/z/internal/web/server"
)
type webService struct {
srvConfig
mxService sync.RWMutex
srvw server.Server
setupServer SetupWebServerFunc
}
var errURLPrefixSyntax = errors.New("must not be empty and must start with '//'")
func (ws *webService) Initialize(logger *logger.Logger) {
func (ws *webService) Initialize(levelVar *slog.LevelVar, logger *slog.Logger) {
ws.logLevelVar = levelVar
ws.logger = logger
ws.descr = descriptionMap{
WebAssetDir: {
"Asset file directory",
func(val string) (any, error) {
val = filepath.Clean(val)
finfo, err := os.Stat(val)
|
︙ | | |
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
+
+
+
|
ap, err := netip.ParseAddrPort(val)
if err != nil {
return "", err
}
return ap.String(), nil
},
true},
WebLoopbackIdent: {"Loopback user ident", ws.noFrozen(parseString), true},
WebLoopbackZid: {"Loopback user zettel identifier", ws.noFrozen(parseInvalidZid), true},
WebMaxRequestSize: {"Max Request Size", parseInt64, true},
WebPersistentCookie: {"Persistent cookie", parseBool, true},
WebProfiling: {"Runtime profiling", parseBool, true},
WebSecureCookie: {"Secure cookie", parseBool, true},
WebSxMaxNesting: {"Max nesting of Sx calls", parseInt, true},
WebTokenLifetimeAPI: {
"Token lifetime API",
makeDurationParser(10*time.Minute, 0, 1*time.Hour),
true,
},
WebTokenLifetimeHTML: {
"Token lifetime HTML",
|
︙ | | |
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
+
+
+
-
+
|
true,
},
}
ws.next = interfaceMap{
WebAssetDir: "",
WebBaseURL: "http://127.0.0.1:23123/",
WebListenAddress: "127.0.0.1:23123",
WebLoopbackIdent: "",
WebLoopbackZid: id.Invalid,
WebMaxRequestSize: int64(16 * 1024 * 1024),
WebPersistentCookie: false,
WebProfiling: false,
WebSecureCookie: true,
WebProfiling: false,
WebSxMaxNesting: 32 * 1024,
WebTokenLifetimeAPI: 1 * time.Hour,
WebTokenLifetimeHTML: 10 * time.Minute,
WebURLPrefix: "/",
}
}
func makeDurationParser(defDur, minDur, maxDur time.Duration) parseFunc {
|
︙ | | |
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
|
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
|
-
+
+
+
+
+
-
-
+
-
+
+
+
-
+
-
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
|
}
return defDur, nil
}
}
var errWrongBasePrefix = errors.New(WebURLPrefix + " does not match " + WebBaseURL)
func (ws *webService) GetLogger() *logger.Logger { return ws.logger }
func (ws *webService) GetLogger() *slog.Logger { return ws.logger }
func (ws *webService) GetLevel() slog.Level { return ws.logLevelVar.Level() }
func (ws *webService) SetLevel(l slog.Level) { ws.logLevelVar.Set(l) }
func (ws *webService) Start(kern *Kernel) error {
baseURL := ws.GetNextConfig(WebBaseURL).(string)
listenAddr := ws.GetNextConfig(WebListenAddress).(string)
loopbackIdent := ws.GetNextConfig(WebLoopbackIdent).(string)
loopbackZid := ws.GetNextConfig(WebLoopbackZid).(id.Zid)
urlPrefix := ws.GetNextConfig(WebURLPrefix).(string)
persistentCookie := ws.GetNextConfig(WebPersistentCookie).(bool)
secureCookie := ws.GetNextConfig(WebSecureCookie).(bool)
profile := ws.GetNextConfig(WebProfiling).(bool)
maxRequestSize := max(ws.GetNextConfig(WebMaxRequestSize).(int64), 1024)
if !strings.HasSuffix(baseURL, urlPrefix) {
ws.logger.Error().Str("base-url", baseURL).Str("url-prefix", urlPrefix).Msg(
"url-prefix is not a suffix of base-url")
ws.logger.Error("url-prefix is not a suffix of base-url", "base-url", baseURL, "url-prefix", urlPrefix)
return errWrongBasePrefix
}
if lap := netip.MustParseAddrPort(listenAddr); !kern.auth.manager.WithAuth() && !lap.Addr().IsLoopback() {
ws.logger.Info().Str("listen", listenAddr).Msg("service may be reached from outside, but authentication is not enabled")
ws.logger.Info("service may be reached from outside, but authentication is not enabled", "listen", listenAddr)
}
sd := server.ConfigData{
Log: ws.logger,
ListenAddr: listenAddr,
BaseURL: baseURL,
URLPrefix: urlPrefix,
MaxRequestSize: maxRequestSize,
Auth: kern.auth.manager,
LoopbackIdent: loopbackIdent,
LoopbackZid: loopbackZid,
PersistentCookie: persistentCookie,
SecureCookie: secureCookie,
Profiling: profile,
}
srvw := server.New(sd)
err := kern.web.setupServer(srvw, kern.box.manager, kern.auth.manager, &kern.cfg)
if err != nil {
ws.logger.Error().Err(err).Msg("Unable to create")
ws.logger.Error("Unable to create", "err", err)
return err
}
if err = srvw.Run(); err != nil {
ws.logger.Error().Err(err).Msg("Unable to start")
ws.logger.Error("Unable to start", "err", err)
return err
}
ws.logger.Info().Str("listen", listenAddr).Str("base-url", baseURL).Msg("Start Service")
ws.logger.Info("Start Service", "listen", listenAddr, "base-url", baseURL)
ws.mxService.Lock()
ws.srvw = srvw
ws.mxService.Unlock()
if kern.cfg.GetCurConfig(ConfigSimpleMode).(bool) {
listenAddr := ws.GetNextConfig(WebListenAddress).(string)
if idx := strings.LastIndexByte(listenAddr, ':'); idx >= 0 {
ws.logger.Mandatory().Msg(strings.Repeat("--------------------", 3))
ws.logger.Mandatory().Msg("Open your browser and enter the following URL:")
ws.logger.Mandatory().Msg(" http://localhost" + listenAddr[idx:])
ws.logger.Mandatory().Msg("")
ws.logger.Mandatory().Msg("If this does not work, try:")
ws.logger.Mandatory().Msg(" http://127.0.0.1" + listenAddr[idx:])
logging.LogMandatory(ws.logger, strings.Repeat("--------------------", 3))
logging.LogMandatory(ws.logger, "Open your browser and enter the following URL:")
logging.LogMandatory(ws.logger, " http://localhost"+listenAddr[idx:])
logging.LogMandatory(ws.logger, "")
logging.LogMandatory(ws.logger, "If this does not work, try:")
logging.LogMandatory(ws.logger, " http://127.0.0.1"+listenAddr[idx:])
}
}
return nil
}
func (ws *webService) IsStarted() bool {
ws.mxService.RLock()
defer ws.mxService.RUnlock()
return ws.srvw != nil
}
func (ws *webService) Stop(*Kernel) {
ws.logger.Info().Msg("Stop Service")
ws.logger.Info("Stop Service")
ws.srvw.Stop()
ws.mxService.Lock()
ws.srvw = nil
ws.mxService.Unlock()
}
func (*webService) GetStatistics() []KeyValue {
return nil
}
|
Deleted internal/logger/logger.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore 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 logger implements a logging package for use in the Zettelstore.
package logger
import (
"context"
"strconv"
"strings"
"sync/atomic"
"time"
"t73f.de/r/zsc/domain/meta"
)
// Level defines the possible log levels
type Level uint8
// Constants for Level
const (
NoLevel Level = iota // the absent log level
TraceLevel // Log most internal activities
DebugLevel // Log most data updates
InfoLevel // Log normal activities
ErrorLevel // Log (persistent) errors
MandatoryLevel // Log only mandatory events
NeverLevel // Logging is disabled
)
var logLevel = [...]string{
" ",
"TRACE",
"DEBUG",
"INFO ",
"ERROR",
">>>>>",
"NEVER",
}
var strLevel = [...]string{
"",
"trace",
"debug",
"info",
"error",
"mandatory",
"disabled",
}
// IsValid returns true, if the level is a valid level
func (l Level) IsValid() bool { return TraceLevel <= l && l <= NeverLevel }
func (l Level) String() string {
if l.IsValid() {
return strLevel[l]
}
return strconv.Itoa(int(l))
}
// Format returns a string representation suitable for logging.
func (l Level) Format() string {
if l.IsValid() {
return logLevel[l]
}
return strconv.Itoa(int(l))
}
// ParseLevel returns the recognized level.
func ParseLevel(text string) Level {
for lv := TraceLevel; lv <= NeverLevel; lv++ {
if len(text) > 2 && strings.HasPrefix(strLevel[lv], text) {
return lv
}
}
return NoLevel
}
// Logger represents an objects that emits logging messages.
type Logger struct {
lw LogWriter
levelVal uint32
prefix string
context []byte
topParent *Logger
uProvider UserProvider
}
// LogWriter writes log messages to their specified destinations.
type LogWriter interface {
WriteMessage(level Level, ts time.Time, prefix, msg string, details []byte) error
}
// New creates a new logger for the given service.
//
// This function must only be called from a kernel implementation, not from
// code that tries to log something.
func New(lw LogWriter, prefix string) *Logger {
if prefix != "" && len(prefix) < 6 {
prefix = (prefix + " ")[:6]
}
result := &Logger{
lw: lw,
levelVal: uint32(InfoLevel),
prefix: prefix,
context: nil,
uProvider: nil,
}
result.topParent = result
return result
}
func newFromMessage(msg *Message) *Logger {
if msg == nil {
return nil
}
logger := msg.logger
context := make([]byte, 0, len(msg.buf))
context = append(context, msg.buf...)
return &Logger{
lw: nil,
levelVal: 0,
prefix: logger.prefix,
context: context,
topParent: logger.topParent,
uProvider: nil,
}
}
// SetLevel sets the level of the logger.
func (l *Logger) SetLevel(newLevel Level) *Logger {
if l != nil {
if l.topParent != l {
panic("try to set level for child logger")
}
atomic.StoreUint32(&l.levelVal, uint32(newLevel))
}
return l
}
// Level returns the current level of the given logger
func (l *Logger) Level() Level {
if l != nil {
return Level(atomic.LoadUint32(&l.levelVal))
}
return NeverLevel
}
// Trace creates a tracing message.
func (l *Logger) Trace() *Message { return newMessage(l, TraceLevel) }
// Debug creates a debug message.
func (l *Logger) Debug() *Message { return newMessage(l, DebugLevel) }
// Info creates a message suitable for information data.
func (l *Logger) Info() *Message { return newMessage(l, InfoLevel) }
// Error creates a message suitable for errors.
func (l *Logger) Error() *Message { return newMessage(l, ErrorLevel) }
// Mandatory creates a message that will always logged, except when logging
// is disabled.
func (l *Logger) Mandatory() *Message { return newMessage(l, MandatoryLevel) }
// Clone creates a message to clone the logger.
func (l *Logger) Clone() *Message {
msg := newMessage(l, NeverLevel)
if msg != nil {
msg.level = NoLevel
}
return msg
}
// UserProvider allows to retrieve an user metadata from a context.
type UserProvider interface {
GetUser(ctx context.Context) *meta.Meta
}
// WithUser creates a derivied logger that allows to retrieve and log user identifer.
func (l *Logger) WithUser(up UserProvider) *Logger {
return &Logger{
lw: nil,
levelVal: 0,
prefix: l.prefix,
context: l.context,
topParent: l.topParent,
uProvider: up,
}
}
func (l *Logger) writeMessage(level Level, msg string, details []byte) error {
return l.topParent.lw.WriteMessage(level, time.Now().Local(), l.prefix, msg, details)
}
|
Deleted internal/logger/logger_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
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 2021-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore 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 logger_test
import (
"fmt"
"os"
"testing"
"time"
"zettelstore.de/z/internal/logger"
)
func TestParseLevel(t *testing.T) {
testcases := []struct {
text string
exp logger.Level
}{
{"tra", logger.TraceLevel},
{"deb", logger.DebugLevel},
{"info", logger.InfoLevel},
{"err", logger.ErrorLevel},
{"manda", logger.MandatoryLevel},
{"dis", logger.NeverLevel},
{"d", logger.Level(0)},
}
for i, tc := range testcases {
got := logger.ParseLevel(tc.text)
if got != tc.exp {
t.Errorf("%d: ParseLevel(%q) == %q, but got %q", i, tc.text, tc.exp, got)
}
}
}
func BenchmarkDisabled(b *testing.B) {
log := logger.New(&stderrLogWriter{}, "").SetLevel(logger.NeverLevel)
for b.Loop() {
log.Info().Str("key", "val").Msg("Benchmark")
}
}
type stderrLogWriter struct{}
func (*stderrLogWriter) WriteMessage(level logger.Level, ts time.Time, prefix, msg string, details []byte) error {
fmt.Fprintf(os.Stderr, "%v %v %v %v %v\n", level.Format(), ts, prefix, msg, string(details))
return nil
}
type testLogWriter struct{}
func (*testLogWriter) WriteMessage(logger.Level, time.Time, string, string, []byte) error {
return nil
}
func BenchmarkStrMessage(b *testing.B) {
log := logger.New(&testLogWriter{}, "")
for b.Loop() {
log.Info().Str("key", "val").Msg("Benchmark")
}
}
func BenchmarkMessage(b *testing.B) {
log := logger.New(&testLogWriter{}, "")
for b.Loop() {
log.Info().Msg("Benchmark")
}
}
func BenchmarkCloneStrMessage(b *testing.B) {
log := logger.New(&testLogWriter{}, "").Clone().Str("sss", "ttt").Child()
for b.Loop() {
log.Info().Msg("123456789")
}
}
|
Deleted internal/logger/message.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
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 2022-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore 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 logger
import (
"context"
"net/http"
"strconv"
"sync"
"t73f.de/r/zsc/domain/id"
"t73f.de/r/zsc/domain/meta"
)
// Message presents a message to log.
type Message struct {
logger *Logger
level Level
buf []byte
}
func newMessage(logger *Logger, level Level) *Message {
if logger != nil {
if logger.topParent.Level() <= level {
m := messagePool.Get().(*Message)
m.logger = logger
m.level = level
m.buf = append(m.buf[:0], logger.context...)
return m
}
}
return nil
}
func recycleMessage(m *Message) {
messagePool.Put(m)
}
var messagePool = &sync.Pool{
New: func() any {
return &Message{
buf: make([]byte, 0, 500),
}
},
}
// Enabled returns whether the message will log or not.
func (m *Message) Enabled() bool {
return m != nil && m.level != NeverLevel
}
// Str adds a string value to the full message
func (m *Message) Str(text, val string) *Message {
if m.Enabled() {
buf := append(m.buf, ',', ' ')
buf = append(buf, text...)
buf = append(buf, '=')
m.buf = append(buf, val...)
}
return m
}
// Bool adds a boolean value to the full message
func (m *Message) Bool(text string, val bool) *Message {
if val {
m.Str(text, "true")
} else {
m.Str(text, "false")
}
return m
}
// Bytes adds a byte slice value to the full message
func (m *Message) Bytes(text string, val []byte) *Message {
if m.Enabled() {
buf := append(m.buf, ',', ' ')
buf = append(buf, text...)
buf = append(buf, '=')
m.buf = append(buf, val...)
}
return m
}
// Err adds an error value to the full message
func (m *Message) Err(err error) *Message {
if err != nil {
return m.Str("error", err.Error())
}
return m
}
// Int adds an integer to the full message
func (m *Message) Int(text string, i int64) *Message {
return m.Str(text, strconv.FormatInt(i, 10))
}
// Uint adds an unsigned integer to the full message
func (m *Message) Uint(text string, u uint64) *Message {
return m.Str(text, strconv.FormatUint(u, 10))
}
// User adds the user-id field of the given user to the message.
func (m *Message) User(ctx context.Context) *Message {
if m.Enabled() {
if up := m.logger.uProvider; up != nil {
if user := up.GetUser(ctx); user != nil {
m.buf = append(m.buf, ", user="...)
if userID, found := user.Get(meta.KeyUserID); found {
m.buf = append(m.buf, userID...)
} else {
m.buf = append(m.buf, user.Zid.Bytes()...)
}
}
}
}
return m
}
// HTTPIP adds the IP address of a HTTP request to the message.
func (m *Message) HTTPIP(r *http.Request) *Message {
if r == nil {
return m
}
if from := r.Header.Get("X-Forwarded-For"); from != "" {
return m.Str("ip", from)
}
return m.Str("IP", r.RemoteAddr)
}
// Zid adds a zettel identifier to the full message
func (m *Message) Zid(zid id.Zid) *Message {
return m.Bytes("zid", zid.Bytes())
}
// Msg add the given text to the message and writes it to the log.
func (m *Message) Msg(text string) {
if m.Enabled() {
m.logger.writeMessage(m.level, text, m.buf)
recycleMessage(m)
}
}
// Child creates a child logger with context of this message.
func (m *Message) Child() *Logger {
return newFromMessage(m)
}
|
Added internal/logging/logging.go.