ADDED .deepsource.toml Index: .deepsource.toml ================================================================== --- /dev/null +++ .deepsource.toml @@ -0,0 +1,8 @@ +version = 1 + +[[analyzers]] +name = "go" +enabled = true + + [analyzers.meta] +import_paths = ["github.com/zettelstore/zettelstore"] Index: Makefile ================================================================== --- Makefile +++ Makefile @@ -1,46 +1,25 @@ -## Copyright (c) 2020 Detlef Stern +## Copyright (c) 2020-2021 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. -.PHONY: test check validate race run build build-dev release clean - -PACKAGE := zettelstore.de/z/cmd/zettelstore - -GO_LDFLAG_VERSION := -X main.buildVersion=$(shell go run tools/version.go || echo unknown) -GOFLAGS_DEVELOP := -ldflags "$(GO_LDFLAG_VERSION)" -tags osusergo,netgo -GOFLAGS_RELEASE := -ldflags "$(GO_LDFLAG_VERSION) -w" -tags osusergo,netgo - -test: - go test ./... +.PHONY: check build release clean check: - go vet ./... - ~/go/bin/golint ./... - -validate: test check - -race: - go test -race ./... - -build-dev: - mkdir -p bin - go build $(GOFLAGS_DEVELOP) -o bin/zettelstore $(PACKAGE) + go run tools/build.go check + +version: + @echo $(shell go run tools/build.go version) build: - mkdir -p bin - go build $(GOFLAGS_RELEASE) -o bin/zettelstore $(PACKAGE) + go run tools/build.go build release: - mkdir -p releases - GOARCH=amd64 GOOS=linux go build $(GOFLAGS_RELEASE) -o releases/zettelstore $(PACKAGE) - GOARCH=arm GOARM=6 GOOS=linux go build $(GOFLAGS_RELEASE) -o releases/zettelstore-arm6 $(PACKAGE) - GOARCH=amd64 GOOS=darwin go build $(GOFLAGS_RELEASE) -o releases/iZettelstore $(PACKAGE) - GOARCH=amd64 GOOS=windows go build $(GOFLAGS_RELEASE) -o releases/zettelstore.exe $(PACKAGE) + go run tools/build.go release clean: - rm -rf bin releases + go run tools/build.go clean Index: VERSION ================================================================== --- VERSION +++ VERSION @@ -1,1 +1,1 @@ -0.0.9 +0.0.10 Index: ast/ast.go ================================================================== --- ast/ast.go +++ ast/ast.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -80,13 +80,14 @@ // RefState indicates the state of the reference. type RefState int // Constants for RefState const ( - RefStateInvalid RefState = iota // Invalid URL - RefStateZettel // Valid reference to an internal zettel - RefStateZettelSelf // Valid reference to same zettel with a fragment - RefStateZettelFound // Valid reference to an existing internal zettel - RefStateZettelBroken // Valid reference to a non-existing internal zettel - RefStateLocal // Valid reference to a non-zettel, but local hosted - RefStateExternal // Valid reference to external material + RefStateInvalid RefState = iota // Invalid Referende + RefStateZettel // Reference to an internal zettel + RefStateSelf // Reference to same zettel with a fragment + RefStateFound // Reference to an existing internal zettel + RefStateBroken // Reference to a non-existing internal zettel + RefStateHosted // Reference to local hosted non-Zettel, without URL change + RefStateBased // Reference to local non-Zettel, to be prefixed + RefStateExternal // Reference to external material ) Index: ast/attr.go ================================================================== --- ast/attr.go +++ ast/attr.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -54,11 +54,11 @@ } return &Attributes{attrs} } // Set changes the attribute that a given key has now a given value. -func (a *Attributes) Set(key string, value string) *Attributes { +func (a *Attributes) Set(key, value string) *Attributes { if a == nil { return &Attributes{map[string]string{key: value}} } if a.Attrs == nil { a.Attrs = make(map[string]string) Index: ast/ref.go ================================================================== --- ast/ref.go +++ ast/ref.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -17,12 +17,21 @@ "zettelstore.de/z/domain/id" ) // ParseReference parses a string and returns a reference. func ParseReference(s string) *Reference { - if len(s) == 0 { + if s == "" { return &Reference{URL: nil, Value: s, State: RefStateInvalid} + } + if state, ok := localState(s); ok { + if state == RefStateBased { + s = s[1:] + } + u, err := url.Parse(s) + if err == nil { + return &Reference{URL: u, Value: s, State: state} + } } u, err := url.Parse(s) if err != nil { return &Reference{URL: nil, Value: s, State: RefStateInvalid} } @@ -29,30 +38,30 @@ if len(u.Scheme)+len(u.Opaque)+len(u.Host) == 0 && u.User == nil { if _, err := id.Parse(u.Path); err == nil { return &Reference{URL: u, Value: s, State: RefStateZettel} } if u.Path == "" && u.Fragment != "" { - return &Reference{URL: u, Value: s, State: RefStateZettelSelf} - } - if isLocalPath(u.Path) { - return &Reference{URL: u, Value: s, State: RefStateLocal} + return &Reference{URL: u, Value: s, State: RefStateSelf} } } return &Reference{URL: u, Value: s, State: RefStateExternal} } -func isLocalPath(path string) bool { +func localState(path string) (RefState, bool) { if len(path) > 0 && path[0] == '/' { - return true + if len(path) > 1 && path[1] == '/' { + return RefStateBased, true + } + return RefStateHosted, true } if len(path) > 1 && path[0] == '.' { if len(path) > 2 && path[1] == '.' && path[2] == '/' { - return true + return RefStateHosted, true } - return path[1] == '/' + return RefStateHosted, path[1] == '/' } - return false + return RefStateInvalid, false } // String returns the string representation of a reference. func (r Reference) String() string { if r.URL != nil { @@ -65,16 +74,18 @@ func (r *Reference) IsValid() bool { return r.State != RefStateInvalid } // IsZettel returns true if it is a referencen to a local zettel. func (r *Reference) IsZettel() bool { switch r.State { - case RefStateZettel, RefStateZettelSelf, RefStateZettelFound, RefStateZettelBroken: + case RefStateZettel, RefStateSelf, RefStateFound, RefStateBroken: return true } return false } // IsLocal returns true if reference is local -func (r *Reference) IsLocal() bool { return r.State == RefStateLocal } +func (r *Reference) IsLocal() bool { + return r.State == RefStateHosted || r.State == RefStateBased +} // IsExternal returns true if it is a referencen to external material. func (r *Reference) IsExternal() bool { return r.State == RefStateExternal } Index: ast/ref_test.go ================================================================== --- ast/ref_test.go +++ ast/ref_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -53,10 +53,11 @@ {"12345678901234#local", true, false, false}, {"http://12345678901234", false, true, false}, {"http://zettelstore.de/z/12345678901234", false, true, false}, {"http://zettelstore.de/12345678901234", false, true, false}, {"/12345678901234", false, false, true}, + {"//12345678901234", false, false, true}, {"./12345678901234", false, false, true}, {"../12345678901234", false, false, true}, {".../12345678901234", false, true, false}, } Index: auth/cred/cred.go ================================================================== --- auth/cred/cred.go +++ auth/cred/cred.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -17,11 +17,11 @@ "golang.org/x/crypto/bcrypt" "zettelstore.de/z/domain/id" ) // HashCredential returns a hashed vesion of the given credential -func HashCredential(zid id.Zid, ident string, credential string) (string, error) { +func HashCredential(zid id.Zid, ident, credential string) (string, error) { fullCredential := createFullCredential(zid, ident, credential) res, err := bcrypt.GenerateFromPassword(fullCredential, bcrypt.DefaultCost) if err != nil { return "", err } @@ -28,11 +28,11 @@ return string(res), nil } // CompareHashAndCredential checks, whether the hashed credential is a possible // value when hashing the credential. -func CompareHashAndCredential(hashed string, zid id.Zid, ident string, credential string) (bool, error) { +func CompareHashAndCredential(hashed string, zid id.Zid, ident, credential string) (bool, error) { fullCredential := createFullCredential(zid, ident, credential) err := bcrypt.CompareHashAndPassword([]byte(hashed), fullCredential) if err == nil { return true, nil } @@ -40,14 +40,14 @@ return false, nil } return false, err } -func createFullCredential(zid id.Zid, ident string, credential string) []byte { +func createFullCredential(zid id.Zid, ident, credential string) []byte { var buf bytes.Buffer buf.WriteString(zid.String()) buf.WriteByte(' ') buf.WriteString(ident) buf.WriteByte(' ') buf.WriteString(credential) return buf.Bytes() } Index: auth/policy/anon.go ================================================================== --- auth/policy/anon.go +++ auth/policy/anon.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -20,31 +20,27 @@ expertMode func() bool getVisibility func(*meta.Meta) meta.Visibility pre Policy } -func (ap *anonPolicy) CanReload(user *meta.Meta) bool { - return ap.pre.CanReload(user) -} - -func (ap *anonPolicy) CanCreate(user *meta.Meta, newMeta *meta.Meta) bool { +func (ap *anonPolicy) CanCreate(user, newMeta *meta.Meta) bool { return ap.pre.CanCreate(user, newMeta) } -func (ap *anonPolicy) CanRead(user *meta.Meta, m *meta.Meta) bool { +func (ap *anonPolicy) CanRead(user, m *meta.Meta) bool { return ap.pre.CanRead(user, m) && ap.checkVisibility(m) } -func (ap *anonPolicy) CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool { +func (ap *anonPolicy) CanWrite(user, oldMeta, newMeta *meta.Meta) bool { return ap.pre.CanWrite(user, oldMeta, newMeta) && ap.checkVisibility(oldMeta) } -func (ap *anonPolicy) CanRename(user *meta.Meta, m *meta.Meta) bool { +func (ap *anonPolicy) CanRename(user, m *meta.Meta) bool { return ap.pre.CanRename(user, m) && ap.checkVisibility(m) } -func (ap *anonPolicy) CanDelete(user *meta.Meta, m *meta.Meta) bool { +func (ap *anonPolicy) CanDelete(user, m *meta.Meta) bool { return ap.pre.CanDelete(user, m) && ap.checkVisibility(m) } func (ap *anonPolicy) checkVisibility(m *meta.Meta) bool { switch ap.getVisibility(m) { Index: auth/policy/default.go ================================================================== --- auth/policy/default.go +++ auth/policy/default.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -16,35 +16,19 @@ "zettelstore.de/z/domain/meta" ) type defaultPolicy struct{} -func (d *defaultPolicy) CanReload(user *meta.Meta) bool { - return true -} - -func (d *defaultPolicy) CanCreate(user *meta.Meta, newMeta *meta.Meta) bool { - return true -} - -func (d *defaultPolicy) CanRead(user *meta.Meta, m *meta.Meta) bool { - return true -} - -func (d *defaultPolicy) CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool { +func (d *defaultPolicy) CanCreate(user, newMeta *meta.Meta) bool { return true } +func (d *defaultPolicy) CanRead(user, m *meta.Meta) bool { return true } +func (d *defaultPolicy) CanWrite(user, oldMeta, newMeta *meta.Meta) bool { return d.canChange(user, oldMeta) } - -func (d *defaultPolicy) CanRename(user *meta.Meta, m *meta.Meta) bool { - return d.canChange(user, m) -} - -func (d *defaultPolicy) CanDelete(user *meta.Meta, m *meta.Meta) bool { - return d.canChange(user, m) -} - -func (d *defaultPolicy) canChange(user *meta.Meta, m *meta.Meta) bool { +func (d *defaultPolicy) CanRename(user, m *meta.Meta) bool { return d.canChange(user, m) } +func (d *defaultPolicy) CanDelete(user, m *meta.Meta) bool { return d.canChange(user, m) } + +func (d *defaultPolicy) canChange(user, m *meta.Meta) bool { metaRo, ok := m.Get(meta.KeyReadOnly) if !ok { return true } if user == nil { Index: auth/policy/owner.go ================================================================== --- auth/policy/owner.go +++ auth/policy/owner.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -22,46 +22,38 @@ isOwner func(id.Zid) bool getVisibility func(*meta.Meta) meta.Visibility pre Policy } -func (o *ownerPolicy) CanReload(user *meta.Meta) bool { - // No need to call o.pre.CanReload(user), because it will always return true. - // Both the default and the readonly policy allow to reload a place. - - // Only the owner is allowed to reload a place - return o.userIsOwner(user) -} - -func (o *ownerPolicy) CanCreate(user *meta.Meta, newMeta *meta.Meta) bool { +func (o *ownerPolicy) CanCreate(user, newMeta *meta.Meta) bool { if user == nil || !o.pre.CanCreate(user, newMeta) { return false } return o.userIsOwner(user) || o.userCanCreate(user, newMeta) } -func (o *ownerPolicy) userCanCreate(user *meta.Meta, newMeta *meta.Meta) bool { +func (o *ownerPolicy) userCanCreate(user, newMeta *meta.Meta) bool { if runtime.GetUserRole(user) == meta.UserRoleReader { return false } if role, ok := newMeta.Get(meta.KeyRole); ok && role == meta.ValueRoleUser { return false } return true } -func (o *ownerPolicy) CanRead(user *meta.Meta, m *meta.Meta) bool { +func (o *ownerPolicy) CanRead(user, m *meta.Meta) bool { // No need to call o.pre.CanRead(user, meta), because it will always return true. // Both the default and the readonly policy allow to read a zettel. vis := o.getVisibility(m) if res, ok := o.checkVisibility(user, vis); ok { return res } return o.userIsOwner(user) || o.userCanRead(user, m, vis) } -func (o *ownerPolicy) userCanRead(user *meta.Meta, m *meta.Meta, vis meta.Visibility) bool { +func (o *ownerPolicy) userCanRead(user, m *meta.Meta, vis meta.Visibility) bool { switch vis { case meta.VisibilityOwner, meta.VisibilitySimple, meta.VisibilityExpert: return false case meta.VisibilityPublic: return true @@ -81,11 +73,11 @@ meta.KeyRole, meta.KeyUserID, meta.KeyUserRole, } -func (o *ownerPolicy) CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool { +func (o *ownerPolicy) CanWrite(user, oldMeta, newMeta *meta.Meta) bool { if user == nil || !o.pre.CanWrite(user, oldMeta, newMeta) { return false } vis := o.getVisibility(oldMeta) if res, ok := o.checkVisibility(user, vis); ok { @@ -111,21 +103,21 @@ return false } return o.userCanCreate(user, newMeta) } -func (o *ownerPolicy) CanRename(user *meta.Meta, m *meta.Meta) bool { +func (o *ownerPolicy) CanRename(user, m *meta.Meta) bool { if user == nil || !o.pre.CanRename(user, m) { return false } if res, ok := o.checkVisibility(user, o.getVisibility(m)); ok { return res } return o.userIsOwner(user) } -func (o *ownerPolicy) CanDelete(user *meta.Meta, m *meta.Meta) bool { +func (o *ownerPolicy) CanDelete(user, m *meta.Meta) bool { if user == nil || !o.pre.CanDelete(user, m) { return false } if res, ok := o.checkVisibility(user, o.getVisibility(m)); ok { return res Index: auth/policy/place.go ================================================================== --- auth/policy/place.go +++ auth/policy/place.go @@ -55,12 +55,11 @@ func (pp *polPlace) CanCreateZettel(ctx context.Context) bool { return pp.place.CanCreateZettel(ctx) } -func (pp *polPlace) CreateZettel( - ctx context.Context, zettel domain.Zettel) (id.Zid, error) { +func (pp *polPlace) CreateZettel(ctx context.Context, zettel domain.Zettel) (id.Zid, error) { user := session.GetUser(ctx) if pp.policy.CanCreate(user, zettel.Meta) { return pp.place.CreateZettel(ctx, zettel) } return id.Invalid, place.NewErrNotAllowed("Create", user, id.Invalid) @@ -88,11 +87,11 @@ return m, nil } return nil, place.NewErrNotAllowed("GetMeta", user, zid) } -func (pp *polPlace) FetchZids(ctx context.Context) (map[id.Zid]bool, error) { +func (pp *polPlace) FetchZids(ctx context.Context) (id.Set, error) { return nil, place.NewErrNotAllowed("fetch-zids", session.GetUser(ctx), id.Invalid) } func (pp *polPlace) SelectMeta( ctx context.Context, f *place.Filter, s *place.Sorter) ([]*meta.Meta, error) { @@ -163,15 +162,8 @@ return pp.place.DeleteZettel(ctx, zid) } return place.NewErrNotAllowed("Delete", user, zid) } -func (pp *polPlace) Reload(ctx context.Context) error { - user := session.GetUser(ctx) - if pp.policy.CanReload(user) { - return pp.place.Reload(ctx) - } - return place.NewErrNotAllowed("Reload", user, id.Invalid) -} func (pp *polPlace) ReadStats(st *place.Stats) { pp.place.ReadStats(st) } Index: auth/policy/policy.go ================================================================== --- auth/policy/policy.go +++ auth/policy/policy.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -16,27 +16,24 @@ "zettelstore.de/z/domain/meta" ) // Policy is an interface for checking access authorization. type Policy interface { - // User is allowed to reload a place. - CanReload(user *meta.Meta) bool - // User is allowed to create a new zettel. - CanCreate(user *meta.Meta, newMeta *meta.Meta) bool + CanCreate(user, newMeta *meta.Meta) bool // User is allowed to read zettel - CanRead(user *meta.Meta, m *meta.Meta) bool + CanRead(user, m *meta.Meta) bool // User is allowed to write zettel. - CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool + CanWrite(user, oldMeta, newMeta *meta.Meta) bool // User is allowed to rename zettel - CanRename(user *meta.Meta, m *meta.Meta) bool + CanRename(user, m *meta.Meta) bool // User is allowed to delete zettel - CanDelete(user *meta.Meta, m *meta.Meta) bool + CanDelete(user, m *meta.Meta) bool } // newPolicy creates a policy based on given constraints. func newPolicy( simpleMode bool, @@ -72,29 +69,25 @@ type prePolicy struct { post Policy } -func (p *prePolicy) CanReload(user *meta.Meta) bool { - return p.post.CanReload(user) -} - -func (p *prePolicy) CanCreate(user *meta.Meta, newMeta *meta.Meta) bool { +func (p *prePolicy) CanCreate(user, newMeta *meta.Meta) bool { return newMeta != nil && p.post.CanCreate(user, newMeta) } -func (p *prePolicy) CanRead(user *meta.Meta, m *meta.Meta) bool { +func (p *prePolicy) CanRead(user, m *meta.Meta) bool { return m != nil && p.post.CanRead(user, m) } -func (p *prePolicy) CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool { +func (p *prePolicy) CanWrite(user, oldMeta, newMeta *meta.Meta) bool { return oldMeta != nil && newMeta != nil && oldMeta.Zid == newMeta.Zid && p.post.CanWrite(user, oldMeta, newMeta) } -func (p *prePolicy) CanRename(user *meta.Meta, m *meta.Meta) bool { +func (p *prePolicy) CanRename(user, m *meta.Meta) bool { return m != nil && p.post.CanRename(user, m) } -func (p *prePolicy) CanDelete(user *meta.Meta, m *meta.Meta) bool { +func (p *prePolicy) CanDelete(user, m *meta.Meta) bool { return m != nil && p.post.CanDelete(user, m) } Index: auth/policy/policy_test.go ================================================================== --- auth/policy/policy_test.go +++ auth/policy/policy_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -58,11 +58,10 @@ } pol := newPolicy(ts.simple, authFunc, ts.readonly, expertFunc, isOwner, getVisibility) name := fmt.Sprintf("simple=%v/readonly=%v/withauth=%v/expert=%v", ts.simple, ts.readonly, ts.withAuth, ts.expert) t.Run(name, func(tt *testing.T) { - testReload(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) testCreate(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) testRead(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) testWrite(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) testRename(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) testDelete(tt, pol, ts.simple, ts.withAuth, ts.readonly, ts.expert) @@ -89,32 +88,11 @@ } } return meta.VisibilityLogin } -func testReload(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, isExpert bool) { - t.Helper() - testCases := []struct { - user *meta.Meta - exp bool - }{ - {newAnon(), !withAuth}, - {newReader(), !withAuth}, - {newWriter(), !withAuth}, - {newOwner(), true}, - } - for _, tc := range testCases { - t.Run("Reload", func(tt *testing.T) { - got := pol.CanReload(tc.user) - if tc.exp != got { - tt.Errorf("exp=%v, but got=%v", tc.exp, got) - } - }) - } -} - -func testCreate(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, isExpert bool) { +func testCreate(t *testing.T, pol Policy, simple, withAuth, readonly, isExpert bool) { t.Helper() anonUser := newAnon() reader := newReader() writer := newWriter() owner := newOwner() @@ -153,11 +131,11 @@ } }) } } -func testRead(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, expert bool) { +func testRead(t *testing.T, pol Policy, simple, withAuth, readonly, expert bool) { t.Helper() anonUser := newAnon() reader := newReader() writer := newWriter() owner := newOwner() @@ -238,11 +216,11 @@ } }) } } -func testWrite(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, expert bool) { +func testWrite(t *testing.T, pol Policy, simple, withAuth, readonly, expert bool) { t.Helper() anonUser := newAnon() reader := newReader() writer := newWriter() owner := newOwner() @@ -379,11 +357,11 @@ } }) } } -func testRename(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, expert bool) { +func testRename(t *testing.T, pol Policy, simple, withAuth, readonly, expert bool) { t.Helper() anonUser := newAnon() reader := newReader() writer := newWriter() owner := newOwner() @@ -464,11 +442,11 @@ } }) } } -func testDelete(t *testing.T, pol Policy, simple bool, withAuth bool, readonly bool, expert bool) { +func testDelete(t *testing.T, pol Policy, simple, withAuth, readonly, expert bool) { t.Helper() anonUser := newAnon() reader := newReader() writer := newWriter() owner := newOwner() Index: auth/policy/readonly.go ================================================================== --- auth/policy/readonly.go +++ auth/policy/readonly.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -13,28 +13,10 @@ import "zettelstore.de/z/domain/meta" type roPolicy struct{} -func (p *roPolicy) CanReload(user *meta.Meta) bool { - return true -} - -func (p *roPolicy) CanCreate(user *meta.Meta, newMeta *meta.Meta) bool { - return false -} - -func (p *roPolicy) CanRead(user *meta.Meta, m *meta.Meta) bool { - return true -} - -func (p *roPolicy) CanWrite(user *meta.Meta, oldMeta, newMeta *meta.Meta) bool { - return false -} - -func (p *roPolicy) CanRename(user *meta.Meta, m *meta.Meta) bool { - return false -} - -func (p *roPolicy) CanDelete(user *meta.Meta, m *meta.Meta) bool { - return false -} +func (p *roPolicy) CanCreate(user, newMeta *meta.Meta) bool { return false } +func (p *roPolicy) CanRead(user, m *meta.Meta) bool { return true } +func (p *roPolicy) CanWrite(user, oldMeta, newMeta *meta.Meta) bool { return false } +func (p *roPolicy) CanRename(user, m *meta.Meta) bool { return false } +func (p *roPolicy) CanDelete(user, m *meta.Meta) bool { return false } Index: auth/token/token.go ================================================================== --- auth/token/token.go +++ auth/token/token.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -50,11 +50,11 @@ func GetToken(ident *meta.Meta, d time.Duration, kind Kind) ([]byte, error) { if role, ok := ident.Get(meta.KeyRole); !ok || role != meta.ValueRoleUser { return nil, ErrNoUser } subject, ok := ident.Get(meta.KeyUserID) - if !ok || len(subject) == 0 { + if !ok || subject == "" { return nil, ErrNoIdent } now := time.Now().Round(time.Second) claims := jwt.Claims{ @@ -102,11 +102,11 @@ expires := claims.Expires.Time() if expires.Before(now) { return Data{}, ErrTokenExpired } ident := claims.Subject - if len(ident) == 0 { + if ident == "" { return Data{}, ErrNoIdent } if zidS, ok := claims.Set["zid"].(string); ok { if zid, err := id.Parse(zidS); err == nil { if kind, ok := claims.Set["_tk"].(float64); ok { Index: cmd/cmd_run.go ================================================================== --- cmd/cmd_run.go +++ cmd/cmd_run.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -81,25 +81,25 @@ ucGetZettel := usecase.NewGetZettel(pp) ucParseZettel := usecase.NewParseZettel(ucGetZettel) ucListMeta := usecase.NewListMeta(pp) ucListRoles := usecase.NewListRole(pp) ucListTags := usecase.NewListTags(pp) - listHTMLMetaHandler := webui.MakeListHTMLMetaHandler(te, ucListMeta) - getHTMLZettelHandler := webui.MakeGetHTMLZettelHandler(te, ucParseZettel, ucGetMeta) + ucZettelContext := usecase.NewZettelContext(pp) router := router.NewRouter() - router.Handle("/", webui.MakeGetRootHandler( - pp, listHTMLMetaHandler, getHTMLZettelHandler)) + router.Handle("/", webui.MakeGetRootHandler(pp)) router.AddListRoute('a', http.MethodGet, webui.MakeGetLoginHandler(te)) router.AddListRoute('a', http.MethodPost, adapter.MakePostLoginHandler( api.MakePostLoginHandlerAPI(ucAuthenticate), webui.MakePostLoginHandlerHTML(te, ucAuthenticate))) router.AddListRoute('a', http.MethodPut, api.MakeRenewAuthHandler()) router.AddZettelRoute('a', http.MethodGet, webui.MakeGetLogoutHandler()) - router.AddListRoute('c', http.MethodGet, adapter.MakeReloadHandler( - usecase.NewReload(pp), api.ReloadHandlerAPI, webui.ReloadHandlerHTML)) if !readonlyMode { + router.AddZettelRoute('b', http.MethodGet, webui.MakeGetRenameZettelHandler( + te, ucGetMeta)) + router.AddZettelRoute('b', http.MethodPost, webui.MakePostRenameZettelHandler( + usecase.NewRenameZettel(pp))) router.AddZettelRoute('c', http.MethodGet, webui.MakeGetCopyZettelHandler( te, ucGetZettel, usecase.NewCopyZettel())) router.AddZettelRoute('c', http.MethodPost, webui.MakePostCreateZettelHandler( usecase.NewCreateZettel(pp))) router.AddZettelRoute('d', http.MethodGet, webui.MakeGetDeleteZettelHandler( @@ -112,35 +112,32 @@ usecase.NewUpdateZettel(pp))) router.AddZettelRoute('f', http.MethodGet, webui.MakeGetFolgeZettelHandler( te, ucGetZettel, usecase.NewFolgeZettel())) router.AddZettelRoute('f', http.MethodPost, webui.MakePostCreateZettelHandler( usecase.NewCreateZettel(pp))) - } - router.AddListRoute('h', http.MethodGet, listHTMLMetaHandler) - router.AddZettelRoute('h', http.MethodGet, getHTMLZettelHandler) - router.AddZettelRoute('i', http.MethodGet, webui.MakeGetInfoHandler( - te, ucParseZettel, ucGetMeta)) - router.AddZettelRoute('k', http.MethodGet, webui.MakeWebUIListsHandler( - te, ucListMeta, ucListRoles, ucListTags)) - router.AddZettelRoute('l', http.MethodGet, api.MakeGetLinksHandler(ucParseZettel)) - if !readonlyMode { - router.AddZettelRoute('n', http.MethodGet, webui.MakeGetNewZettelHandler( + router.AddZettelRoute('g', http.MethodGet, webui.MakeGetNewZettelHandler( te, ucGetZettel, usecase.NewNewZettel())) - router.AddZettelRoute('n', http.MethodPost, webui.MakePostCreateZettelHandler( + router.AddZettelRoute('g', http.MethodPost, webui.MakePostCreateZettelHandler( usecase.NewCreateZettel(pp))) } + router.AddListRoute('f', http.MethodGet, webui.MakeSearchHandler( + te, usecase.NewSearch(pp), ucGetMeta, ucGetZettel)) + router.AddListRoute('h', http.MethodGet, webui.MakeListHTMLMetaHandler( + te, ucListMeta, ucListRoles, ucListTags)) + router.AddZettelRoute('h', http.MethodGet, webui.MakeGetHTMLZettelHandler( + te, ucParseZettel, ucGetMeta)) + router.AddZettelRoute('i', http.MethodGet, webui.MakeGetInfoHandler( + te, ucParseZettel, ucGetMeta)) + router.AddZettelRoute('j', http.MethodGet, webui.MakeZettelContextHandler(te, ucZettelContext)) + + router.AddZettelRoute('l', http.MethodGet, api.MakeGetLinksHandler(ucParseZettel)) + router.AddZettelRoute('o', http.MethodGet, api.MakeGetOrderHandler( + usecase.NewZettelOrder(pp, ucParseZettel))) router.AddListRoute('r', http.MethodGet, api.MakeListRoleHandler(ucListRoles)) - if !readonlyMode { - router.AddZettelRoute('r', http.MethodGet, webui.MakeGetRenameZettelHandler( - te, ucGetMeta)) - router.AddZettelRoute('r', http.MethodPost, webui.MakePostRenameZettelHandler( - usecase.NewRenameZettel(pp))) - } router.AddListRoute('t', http.MethodGet, api.MakeListTagsHandler(ucListTags)) - router.AddListRoute('s', http.MethodGet, webui.MakeSearchHandler( - te, usecase.NewSearch(pp), ucGetMeta, ucGetZettel)) + router.AddZettelRoute('y', http.MethodGet, api.MakeZettelContextHandler(ucZettelContext)) router.AddListRoute('z', http.MethodGet, api.MakeListMetaHandler( usecase.NewListMeta(pp), ucGetMeta, ucParseZettel)) router.AddZettelRoute('z', http.MethodGet, api.MakeGetZettelHandler( ucParseZettel, ucGetMeta)) return session.NewHandler(router, usecase.NewGetUserByZid(up)) } Index: cmd/cmd_run_simple.go ================================================================== --- cmd/cmd_run_simple.go +++ cmd/cmd_run_simple.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -9,38 +9,25 @@ //----------------------------------------------------------------------------- package cmd import ( - "context" "flag" "fmt" "log" "os" "strings" - "zettelstore.de/z/domain" - "zettelstore.de/z/place" - "zettelstore.de/z/web/server" - "zettelstore.de/z/config/startup" - "zettelstore.de/z/domain/id" - "zettelstore.de/z/domain/meta" + "zettelstore.de/z/web/server" ) func flgSimpleRun(fs *flag.FlagSet) { fs.String("d", "", "zettel directory") } func runSimpleFunc(*flag.FlagSet) (int, error) { - p := startup.PlaceManager() - if _, err := p.GetMeta(context.Background(), id.WelcomeZid); err != nil { - if err == place.ErrNotFound { - updateWelcomeZettel(p) - } - } - listenAddr := startup.ListenAddress() readonlyMode := startup.IsReadOnlyMode() logBeforeRun(listenAddr, readonlyMode) if idx := strings.LastIndexByte(listenAddr, ':'); idx >= 0 { log.Println() @@ -60,74 +47,11 @@ // runSimple is called, when the user just starts the software via a double click // or via a simple call ``./zettelstore`` on the command line. func runSimple() { dir := "./zettel" - if err := os.MkdirAll(dir, 0755); err != nil { + if err := os.MkdirAll(dir, 0750); err != nil { fmt.Fprintf(os.Stderr, "Unable to create zettel directory %q (%s)\n", dir, err) os.Exit(1) } executeCommand("run-simple", "-d", dir) } - -func updateWelcomeZettel(p place.Place) { - m := meta.New(id.WelcomeZid) - m.Set(meta.KeyTitle, "Welcome to Zettelstore") - m.Set(meta.KeyRole, meta.ValueRoleZettel) - m.Set(meta.KeySyntax, meta.ValueSyntaxZmk) - zid, err := p.CreateZettel( - context.Background(), - domain.Zettel{Meta: m, Content: domain.NewContent(welcomeZettelContent)}, - ) - if err == nil { - p.RenameZettel(context.Background(), zid, id.WelcomeZid) - } -} - -var welcomeZettelContent = `Thank you for using Zettelstore! - -You will find the lastest information about Zettelstore at [[https://zettelstore.de]]. -Check that website regulary for [[upgrades|https://zettelstore.de/home/doc/trunk/www/download.wiki]] to the latest version. -You should consult the [[change log|https://zettelstore.de/home/doc/trunk/www/changes.wiki]] before upgrading. -Sometimes, you have to edit some of your Zettelstore-related zettel before upgrading. -Since Zettelstore is currently in a development state, every upgrade might fix some of your problems. -To check for versions, there is a zettel with the [[current version|00000000000001]] of your Zettelstore. - -If you have problems concerning Zettelstore, -do not hesitate to get in [[contact with the main developer|mailto:ds@zettelstore.de]]. - -=== Reporting errors -If you have encountered an error, please include the content of the following zettel in your mail: -* [[Zettelstore Version|00000000000001]] -* [[Zettelstore Operating System|00000000000003]] -* [[Zettelstore Startup Configuration|00000000000096]] -* [[Zettelstore Startup Values|00000000000098]] -* [[Zettelstore Runtime Configuration|00000000000100]] - -Additionally, you have to describe, what you have done before that error occurs -and what you have expected instead. -Please do not forget to include the error message, if there is one. - -Some of above Zettelstore zettel can only be retrieved if you enabled ""expert mode"". -Otherwise, only some zettel are linked. -To enable expert mode, edit the zettel [[Zettelstore Runtime Configuration|00000000000100]]: -please set the metadata value of the key ''expert-mode'' to true. -To do you, enter the string ''expert-mode:true'' inside the edit view of the metadata. - -=== Information about this zettel -This zettel was generated automatically. -Every time you start Zettelstore by double clicking in your graphical user interface, -or by just starting it in a command line via something like ''zettelstore'', and this zettel -does not exist, it will be generated. -This allows you to edit this zettel for your own needs. - -If you don't need it anymore, you can delete this zettel by clicking on ""Info"" and then -on ""Delete"". -However, by starting Zettelstore as described above, the original version of this zettel -will be restored. - -If you start Zettelstore with the ''run'' command, e.g. as a service or via command line, -this zettel will not be generated. -But if it exists before, it will not be deleted. -In this case, Zettelstore assumes that you have enough knowledge and that you do not need -zettel. -` Index: cmd/main.go ================================================================== --- cmd/main.go +++ cmd/main.go @@ -122,11 +122,11 @@ } }) return cfg } -func setupOperations(cfg *meta.Meta, withPlaces bool, simple bool) error { +func setupOperations(cfg *meta.Meta, withPlaces, simple bool) error { var mgr place.Manager var idx index.Indexer if withPlaces { idx = indexer.New() filter := index.NewMetaFilter(idx) Index: cmd/zettelstore/main.go ================================================================== --- cmd/zettelstore/main.go +++ cmd/zettelstore/main.go @@ -14,10 +14,10 @@ import ( "zettelstore.de/z/cmd" ) // Version variable. Will be filled by build process. -var buildVersion string = "" +var version string = "" func main() { - cmd.Main("Zettelstore", buildVersion) + cmd.Main("Zettelstore", version) } Index: collect/collect.go ================================================================== --- collect/collect.go +++ collect/collect.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 Index: collect/collect_test.go ================================================================== --- collect/collect_test.go +++ collect/collect_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 ADDED collect/order.go Index: collect/order.go ================================================================== --- /dev/null +++ collect/order.go @@ -0,0 +1,69 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package collect provides functions to collect items from a syntax tree. +package collect + +import "zettelstore.de/z/ast" + +// Order of internal reference within the given zettel. +func Order(zn *ast.ZettelNode) (result []*ast.Reference) { + for _, bn := range zn.Ast { + if ln, ok := bn.(*ast.NestedListNode); ok { + switch ln.Code { + case ast.NestedListOrdered, ast.NestedListUnordered: + for _, is := range ln.Items { + if ref := firstItemZettelReference(is); ref != nil { + result = append(result, ref) + } + } + } + } + } + return result +} + +func firstItemZettelReference(is ast.ItemSlice) *ast.Reference { + for _, in := range is { + if pn, ok := in.(*ast.ParaNode); ok { + if ref := firstInlineZettelReference(pn.Inlines); ref != nil { + return ref + } + } + } + return nil +} + +func firstInlineZettelReference(ins ast.InlineSlice) (result *ast.Reference) { + for _, inl := range ins { + switch in := inl.(type) { + case *ast.LinkNode: + if ref := in.Ref; ref.IsZettel() { + return ref + } + result = firstInlineZettelReference(in.Inlines) + case *ast.ImageNode: + result = firstInlineZettelReference(in.Inlines) + case *ast.CiteNode: + result = firstInlineZettelReference(in.Inlines) + case *ast.FootnoteNode: + // Ignore references in footnotes + continue + case *ast.FormatNode: + result = firstInlineZettelReference(in.Inlines) + default: + continue + } + if result != nil { + return result + } + } + return nil +} Index: collect/split.go ================================================================== --- collect/split.go +++ collect/split.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -9,13 +9,11 @@ //----------------------------------------------------------------------------- // Package collect provides functions to collect items from a syntax tree. package collect -import ( - "zettelstore.de/z/ast" -) +import "zettelstore.de/z/ast" // DivideReferences divides the given list of rederences into zettel, local, and external References. func DivideReferences(all []*ast.Reference, duplicates bool) (zettel, local, external []*ast.Reference) { if len(all) == 0 { return nil, nil, nil @@ -23,40 +21,37 @@ mapZettel := make(map[string]bool) mapLocal := make(map[string]bool) mapExternal := make(map[string]bool) for _, ref := range all { - if ref.State == ast.RefStateZettelSelf { + if ref.State == ast.RefStateSelf { continue } - s := ref.String() if ref.IsZettel() { - if duplicates { - zettel = append(zettel, ref) - } else { - if _, ok := mapZettel[s]; !ok { - zettel = append(zettel, ref) - mapZettel[s] = true - } - } + zettel = appendRefToList(zettel, mapZettel, ref, duplicates) } else if ref.IsExternal() { - if duplicates { - external = append(external, ref) - } else { - if _, ok := mapExternal[s]; !ok { - external = append(external, ref) - mapExternal[s] = true - } - } - } else { - if duplicates { - local = append(local, ref) - } else { - if _, ok := mapLocal[s]; !ok { - local = append(local, ref) - mapLocal[s] = true - } - } + external = appendRefToList(external, mapExternal, ref, duplicates) + } else { + local = appendRefToList(local, mapLocal, ref, duplicates) } } return zettel, local, external } + +func appendRefToList( + reflist []*ast.Reference, + refSet map[string]bool, + ref *ast.Reference, + duplicates bool, +) []*ast.Reference { + if duplicates { + reflist = append(reflist, ref) + } else { + s := ref.String() + if _, ok := refSet[s]; !ok { + reflist = append(reflist, ref) + refSet[s] = true + } + } + + return reflist +} Index: config/runtime/runtime.go ================================================================== --- config/runtime/runtime.go +++ config/runtime/runtime.go @@ -10,12 +10,10 @@ // Package runtime provides functions to retrieve runtime configuration data. package runtime import ( - "strconv" - "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" "zettelstore.de/z/place" "zettelstore.de/z/place/stock" ) @@ -132,20 +130,20 @@ } } return "Zettelstore" } -// GetStart returns the value of the "start" key. -func GetStart() id.Zid { +// GetHomeZettel returns the value of the "home-zettel" key. +func GetHomeZettel() id.Zid { if config := getConfigurationMeta(); config != nil { - if start, ok := config.Get(meta.KeyStart); ok { + if start, ok := config.Get(meta.KeyHomeZettel); ok { if startID, err := id.Parse(start); err == nil { return startID } } } - return id.Invalid + return id.DefaultHomeZid } // GetDefaultVisibility returns the default value for zettel visibility. func GetDefaultVisibility() meta.Visibility { if config := getConfigurationMeta(); config != nil { @@ -179,11 +177,11 @@ if config := getConfigurationMeta(); config != nil { if html, ok := config.Get(meta.KeyMarkerExternal); ok { return html } } - return "↗︎" + return "➚" } // GetFooterHTML returns HTML code that should be embedded into the footer // of each WebUI page. func GetFooterHTML() string { @@ -197,13 +195,11 @@ // GetListPageSize returns the maximum length of a list to be returned in WebUI. // A value less or equal to zero signals no limit. func GetListPageSize() int { if config := getConfigurationMeta(); config != nil { - if data, ok := config.Get(meta.KeyListPageSize); ok { - if value, err := strconv.Atoi(data); err == nil { - return value - } + if value, ok := config.GetNumber(meta.KeyListPageSize); ok && value > 0 { + return value } } return 0 } Index: config/startup/startup.go ================================================================== --- config/startup/startup.go +++ config/startup/startup.go @@ -102,11 +102,10 @@ io.WriteString(h, secret) } io.WriteString(h, version.Prog) io.WriteString(h, version.Build) io.WriteString(h, version.Hostname) - io.WriteString(h, version.GoVersion) io.WriteString(h, version.Os) io.WriteString(h, version.Arch) return h.Sum(nil) } Index: docs/manual/00000000000100.zettel ================================================================== --- docs/manual/00000000000100.zettel +++ docs/manual/00000000000100.zettel @@ -4,10 +4,8 @@ syntax: none default-copyright: (c) 2020-2021 by Detlef Stern default-license: EUPL-1.2-or-later default-visibility: public footer-html:

Imprint / Privacy

-modified: 20210111182407 site-name: Zettelstore Manual -start: 00001000000000 visibility: owner DELETED docs/manual/00001000000000.zettel Index: docs/manual/00001000000000.zettel ================================================================== --- docs/manual/00001000000000.zettel +++ /dev/null @@ -1,22 +0,0 @@ -id: 00001000000000 -title: Zettelstore Manual -role: manual -tags: #manual #zettelstore -syntax: zmk -modified: 20210126174156 - -* [[Introduction|00001001000000]] -* [[Design goals|00001002000000]] -* [[Installation|00001003000000]] -* [[Configuration|00001004000000]] -* [[Structure of Zettelstore|00001005000000]] -* [[Layout of a zettel|00001006000000]] -* [[Zettelmarkup|00001007000000]] -* [[Other markup languages|00001008000000]] -* [[Security|00001010000000]] -* [[API|00001012000000]] -* [[Web user interface|00001014000000]] -* Troubleshooting -* Frequently asked questions - -Licensed under the EUPL-1.2-or-later. Index: docs/manual/00001001000000.zettel ================================================================== --- docs/manual/00001001000000.zettel +++ docs/manual/00001001000000.zettel @@ -1,11 +1,10 @@ id: 00001001000000 title: Introduction to the Zettelstore role: manual tags: #introduction #manual #zettelstore syntax: zmk -modified: 20210126170856 [[Personal knowledge management|https://en.wikipedia.org/wiki/Personal_knowledge_management]] is about collecting, classifying, storing, searching, retrieving, assessing, evaluating, and sharing knowledge as a daily activity. Personal knowledge Index: docs/manual/00001002000000.zettel ================================================================== --- docs/manual/00001002000000.zettel +++ docs/manual/00001002000000.zettel @@ -1,5 +1,6 @@ +id: 00001002000000 title: Design goals for the Zettelstore tags: #design #goal #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001003000000.zettel ================================================================== --- docs/manual/00001003000000.zettel +++ docs/manual/00001003000000.zettel @@ -1,10 +1,10 @@ +id: 00001003000000 title: Installation of the Zettelstore software role: manual tags: #installation #manual #zettelstore syntax: zmk -modified: 20201221142822 === The curious user You just want to check out the Zettelstore software * Grab the appropriate executable and copy it into any directory @@ -11,13 +11,13 @@ * Start the Zettelstore software, e.g. with a double click * A sub-directory ""zettel"" will be created in the directory where you placed the executable. It will contain your future zettel. * Open the URI [[http://localhost:23123]] with your web browser. It will present you a mostly empty Zettelstore. - There will be a zettel titled ""Welcome to Zettelstore"" that contains some helpful information. + There will be a zettel titled ""[[Home|00010000000000]]"" that contains some helpful information. * Please read the instructions for the web-based user interface and learn about the various ways to write zettel. -* If you restart your device, please make sure to start your Zettelstore. +* If you restart your device, please make sure to start your Zettelstore again. === The intermediate user You already tried the Zettelstore software and now you want to use it permanently. * Grab the appropriate executable and copy it into the appropriate directory Index: docs/manual/00001004000000.zettel ================================================================== --- docs/manual/00001004000000.zettel +++ docs/manual/00001004000000.zettel @@ -1,11 +1,10 @@ id: 00001004000000 title: Configuration of Zettelstore role: manual tags: #configuration #manual #zettelstore syntax: zmk -modified: 20210125195740 There are two levels to change the behavior and/or the appearance of Zettelstore. The first level is the configuration that is needed to start the services provided by Zettelstore. For example, this includes the URI under which your Zettelstore is accessible. * [[Zettelstore start-up configuration|00001004010000]] Index: docs/manual/00001004010000.zettel ================================================================== --- docs/manual/00001004010000.zettel +++ docs/manual/00001004010000.zettel @@ -1,11 +1,10 @@ id: 00001004010000 title: Zettelstore start-up configuration role: manual tags: #configuration #manual #zettelstore syntax: zmk -modified: 20201226183537 The configuration file, as specified by the ''-c CONFIGFILE'' [[command line option|00001004051000]], allows you to specify some start-up options. These options cannot be stored in a [[configuration zettel|00001004020000]] because either they are needed before Zettelstore can start or because of security reasons. For example, Zettelstore need to know in advance, on which network address is must listen or where zettel are placed. An attacker that is able to change the owner can do anything. @@ -40,11 +39,11 @@ Its lifetime exceeds the lifetime of the authentication token (see option ''token-lifetime-html'') by 30 seconds. Default: ''false'' ; [!place-X-uri]''place-//X//-uri'', where //X// is a number greater or equal to one : Specifies a [[place|00001004011200]] where zettel are stored. - During startup //X// is counted, starting with one, until no key is found. + During start-up //X// is counted, starting with one, until no key is found. This allows to configure more than one place. If no ''place-1-uri'' key is given, the overall effect will be the same as if only ''place-1-uri'' was specified with the value ''dir://.zettel''. In this case, even a key ''place-2-uri'' will be ignored. ; [!read-only-mode]''read-only-mode'' @@ -61,14 +60,14 @@ ''token-lifetime-html'' specifies the lifetime for the HTML views. Default: 60. It is automatically extended, when a new HTML view is rendered. ; [!url-prefix]''url-prefix'' : Add the given string as a prefix to the local part of a Zettelstore local URL/URI when rendering zettel representations. - Must start and end with a slash character (""''/''"", ''U+002F''). + Must begin and end with a slash character (""''/''"", ''U+002F''). Default: ''"/"''. This allows to use a forwarding proxy [[server|00001010090100]] in front of the Zettelstore. ; ''verbose'' : Be more verbose inf logging data. Default: false Other keys will be ignored. Index: docs/manual/00001004011200.zettel ================================================================== --- docs/manual/00001004011200.zettel +++ docs/manual/00001004011200.zettel @@ -1,5 +1,6 @@ +id: 00001004011200 title: Zettelstore places tags: #configuration #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001004011400.zettel ================================================================== --- docs/manual/00001004011400.zettel +++ docs/manual/00001004011400.zettel @@ -1,5 +1,6 @@ +id: 00001004011400 title: Configure file directory places tags: #configuration #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001004020000.zettel ================================================================== --- docs/manual/00001004020000.zettel +++ docs/manual/00001004020000.zettel @@ -1,11 +1,10 @@ id: 00001004020000 title: Configure the running Zettelstore role: manual tags: #configuration #manual #zettelstore syntax: zmk -modified: 20201231131204 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: @@ -47,24 +46,24 @@ ; [!footer-html]''footer-html'' : Contains some HTML code that will be included into the footer of each Zettelstore web page. It only affects the [[web user interface|00001014000000]]. Zettel content, delivered via the [[API|00001012000000]] as JSON, etc. is not affected. Default: (the empty string). +; [!home-zettel]''home-zettel'' +: Specifies the identifier of the zettel, that should be presented for the default view / home view. + If not given or if the identifier does not identify a zettel, the zettel with the identifier ''00010000000000'' is shown. ; [!marker-external]''marker-external'' : Some HTML code that is displayed after a reference to external material. - Default: ''&\#8599;&\#xfe0e;'', to display a ""↗︎"" sign[^The string ''&\#xfe0e;'' is needed to enforce the sign on all platforms.]. + Default: ''&\#10138;'', to display a ""➚"" sign. ; [!list-page-size]''list-page-size'' : If set to a value greater than zero, specifies the number of items shown in WebUI lists. Basically, this is the list of all zettel (possibly restricted) and the list of search results. Default: ''0''. ; [!site-name]''site-name'' : Name of the Zettelstore instance. Will be used when displaying some lists. Default: ''Zettelstore''. -; [!start]''start'' -: Specifies the ID of the zettel, that should be presented for the default view. - If not given or if the ID does not identify a zettel, the list of all zettel is shown. ; [!yaml-header]''yaml-header'' : If true, metadata and content will be separated by ''-\--\\n'' instead of an empty line (''\\n\\n''). Default: ''false''. You will probably use this key, if you are working with another software Index: docs/manual/00001004050000.zettel ================================================================== --- docs/manual/00001004050000.zettel +++ docs/manual/00001004050000.zettel @@ -1,11 +1,10 @@ id: 00001004050000 title: Command line parameters role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115555 Zettelstore is not just a web service that provides services of a zettelkasten. It allows to 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. Index: docs/manual/00001004050200.zettel ================================================================== --- docs/manual/00001004050200.zettel +++ docs/manual/00001004050200.zettel @@ -1,11 +1,10 @@ id: 00001004050200 title: The ''help'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115646 precursor: 00001004050000 Lists all implemented sub-commands. Example: Index: docs/manual/00001004050400.zettel ================================================================== --- docs/manual/00001004050400.zettel +++ docs/manual/00001004050400.zettel @@ -1,11 +1,10 @@ id: 00001004050400 title: The ''version'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115659 precursor: 00001004050000 Emits some information about the Zettelstore's version. This allows you to check, whether your installed Zettelstore is Index: docs/manual/00001004050600.zettel ================================================================== --- docs/manual/00001004050600.zettel +++ docs/manual/00001004050600.zettel @@ -1,11 +1,10 @@ id: 00001004050600 title: The ''config'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115712 precursor: 00001004050000 Shows the Zettelstore configuration, for debugging purposes. Currently, only the [[start-up configuration|00001004010000]] is shown. Index: docs/manual/00001004051000.zettel ================================================================== --- docs/manual/00001004051000.zettel +++ docs/manual/00001004051000.zettel @@ -1,11 +1,10 @@ id: 00001004051000 title: The ''run'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115719 precursor: 00001004050000 === ``zettelstore run`` This starts the web service. @@ -40,8 +39,8 @@ No changes are possible via the web interface / via the API. This allows to publish your content without any risks of unauthorized changes. ; ''-v'' : Be more verbose in writing logs. - Writes the startup configuration to stderr. + Writes the start-up configuration to stderr. Command line options take precedence over configuration file options. Index: docs/manual/00001004051100.zettel ================================================================== --- docs/manual/00001004051100.zettel +++ docs/manual/00001004051100.zettel @@ -1,11 +1,10 @@ id: 00001004051100 title: The ''run-simple'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115448 precursor: 00001004050000 === ``zettelstore run-simple`` This sub-command is implicitly called, when an user starts Zettelstore by double-clicking on its GUI icon. It is s simplified variant of the [[''run'' sub-command|00001004051000]]. Index: docs/manual/00001004051200.zettel ================================================================== --- docs/manual/00001004051200.zettel +++ docs/manual/00001004051200.zettel @@ -1,11 +1,10 @@ id: 00001004051200 title: The ''file'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115726 precursor: 00001004050000 Reads zettel data from a file (or from standard input / stdin) and renders it to standard output / stdout. This allows Zettelstore to render files manually. ``` Index: docs/manual/00001004051400.zettel ================================================================== --- docs/manual/00001004051400.zettel +++ docs/manual/00001004051400.zettel @@ -1,11 +1,10 @@ id: 00001004051400 title: The ''password'' sub-command role: manual tags: #command #configuration #manual #zettelstore syntax: zmk -modified: 20210104115737 precursor: 00001004050000 This sub-command is used to create a hashed password for to be authenticated users. It reads a password from standard input (two times, both must be equal) and writes the hashed password to standard output. Index: docs/manual/00001005000000.zettel ================================================================== --- docs/manual/00001005000000.zettel +++ docs/manual/00001005000000.zettel @@ -1,11 +1,10 @@ id: 00001005000000 title: Structure of Zettelstore role: manual tags: #design #manual #zettelstore syntax: zmk -modified: 20210125195908 Zettelstore is a software that manages your zettel. Since every zettel must be readable without any special tool, most zettel has to be stored as ordinary files within specific directories. Typically, file names and file content must comply to 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. @@ -21,14 +20,14 @@ === Where zettel are stored Your zettel are stored as files in a specific directory. If you have not explicitly specified the directory, a default directory will be used. -The directory has to be specified at [[startup time|00001004010000]]. +The directory has to be specified at [[start-up 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 starts with 14 digits (0-9), the [[zettel identifier|00001006050000]]. +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 interface or the API, 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. The [[configuration zettel|00001004020000]] is one prominent example, as well as these manual zettel. @@ -51,11 +50,11 @@ In case of some textual zettel content you do not want to store the metadata and the zettel content in two different files. Here the ''.zettel'' extension will signal that the metadata and the zettel content will be placed 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 to work properly. +Zettelstore contains some [[predefined zettel|00001005090000]] to work properly. The [[configuration zettel|00001004020000]] is one example. To render the builtin web interface, some templates are used, as well as a layout specification in CSS. The icon that visualizes an external link is a predefined SVG image. All of these are visible to the Zettelstore as zettel. Index: docs/manual/00001005090000.zettel ================================================================== --- docs/manual/00001005090000.zettel +++ docs/manual/00001005090000.zettel @@ -1,11 +1,10 @@ id: 00001005090000 title: List of predefined zettel role: manual tags: #manual #reference #zettelstore syntax: zmk -modified: 20210126114739 The following table lists all predefined zettel with their purpose. |= Identifier :|= Title | Purpose | [[00000000000001]] | Zettelstore Version | Contains the version string of the running Zettelstore @@ -14,12 +13,12 @@ | [[00000000000006]] | Zettelstore Environment Values | Contains environmental data of Zettelstore executable | [[00000000000008]] | Zettelstore Runtime Values | Contains values that reflect the inner working; see [[here|https://golang.org/pkg/runtime/]] for a technical description of these values | [[00000000000018]] | Zettelstore Indexer | Provides some statistics about the index process | [[00000000000020]] | Zettelstore Place Manager | Contains some statistics about zettel places | [[00000000000090]] | Zettelstore Supported Metadata Keys | Contains all supported metadata keys, their [[types|00001006030000]], and more -| [[00000000000096]] | Zettelstore Startup Configuration | Contains the effective values of the [[startup configuration|00001004010000]] -| [[00000000000098]] | Zettelstore Startup Values | Contains all values computed from the [[startup configuration|00001004010000]] +| [[00000000000096]] | Zettelstore Start-up Configuration | Contains the effective values of the [[start-up configuration|00001004010000]] +| [[00000000000098]] | Zettelstore Start-up Values | Contains all values computed from the [[start-up configuration|00001004010000]] | [[00000000000100]] | Zettelstore Runtime Configuration | Allows 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 Meta HTML Template | Used when displaying a list of zettel | [[00000000010401]] | Zettelstore Detail HTML Template | Layout for the HTML detail view of one zettel @@ -28,11 +27,13 @@ | [[00000000010404]] | Zettelstore Rename Form HTML Template | View that is displayed to change the [[zettel identifier|00001006050000]] | [[00000000010405]] | Zettelstore Delete HTML Template | View to confirm the deletion of a zettel | [[00000000010500]] | Zettelstore List Roles HTML Template | Layout for listing all roles | [[00000000010600]] | Zettelstore List Tags HTML Template | Layout of tags lists | [[00000000020001]] | Zettelstore Base CSS | CSS file that is included by the [[Base HTML Template|00000000010100]] -| [[00000000091001]] | New Zettel | Template for a new zettel with role ""[[zettel|00001006020100]]"" -| [[00000000096001]] | New User | Template for a new zettel with role ""[[user|00001006020100#user]]"" +| [[00000000090000]] | New Menu | Contains items that should contain in the zettel template menu +| [[00000000090001]] | New Zettel | Template for a new zettel with role ""[[zettel|00001006020100]]"" +| [[00000000090002]] | New User | Template for a new zettel with role ""[[user|00001006020100#user]]"" +| [[00010000000000]] | Home | Default home zettel, contains some welcome information If a zettel is not linked, it is not accessible for the current user. **Important:** The identifier may change until a stable version of the software is released. Index: docs/manual/00001006000000.zettel ================================================================== --- docs/manual/00001006000000.zettel +++ docs/manual/00001006000000.zettel @@ -1,5 +1,6 @@ +id: 00001006000000 title: Layout of a Zettel tags: #design #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001006010000.zettel ================================================================== --- docs/manual/00001006010000.zettel +++ docs/manual/00001006010000.zettel @@ -1,23 +1,24 @@ +id: 00001006010000 title: Syntax of Metadata tags: #manual #syntax #zettelstore syntax: zmk role: manual The metadata of a zettel is a collection of key-value pairs. The syntax roughly resembles the internal header of an email ([[RFC5322|https://tools.ietf.org/html/rfc5322]]). The key is a sequence of alphanumeric characters, a hyphen-minus character (""''-''"") is also allowed. -It starts at a new line. +It begins at the first position of a new line. A key is separated from its value either by -* a colon character (""'':''""), -* a non-empty sequence of space characters, +* a colon character (""'':''""), +* a non-empty sequence of space characters, * a sequence of space characters, followed by a colon, followed by a sequence of space characters. A Value is a sequence of printable characters. -If the value should be continued in the following line, that following line (//continuation line//) must start with a non-empty sequence of space characters. +If the value should be continued in the following line, that following line (//continuation line//) must begin with a non-empty sequence of space characters. The rest of the following line will be interpreted as the next part of the value. There can be more than one continuation line for a value. A non-continuation line that contains a possibly empty sequence of characters, followed by the percent sign character (""''%''"") is treated as a comment line. It will be ignored. Index: docs/manual/00001006020000.zettel ================================================================== --- docs/manual/00001006020000.zettel +++ docs/manual/00001006020000.zettel @@ -1,11 +1,10 @@ id: 00001006020000 title: Supported Metadata Keys role: manual tags: #manual #meta #reference #zettel #zettelstore syntax: zmk -modified: 20210123223645 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]]. @@ -74,11 +73,11 @@ : Specifies the syntax that should be used for interpreting the zettel. The zettel about [[other markup languages|00001008000000]] defines supported values. If not given, the value ''default-syntax'' from the [[configuration zettel|00001004020000#default-syntax]] will be used. ; [!tags]''tags'' : Contains a space separated list of tags to describe the zettel further. - Each Tag must start with the number sign character (""''#''"", ''U+0023''). + Each Tag must begin with the number sign character (""''#''"", ''U+0023''). ; [!title]''title'' : Specifies the title of the zettel. If not given, the value ''default-title'' from the [[configuration zettel|00001004020000#default-title]] will be used. You can use all [[inline-structured elements|00001007040000]] of Zettelmarkup. @@ -99,11 +98,5 @@ ; [!visibility]''visibility'' : When you work with authentication, you can give every zettel a value to decide, who can see the zettel. Its default value can be set with [[''default-visibility''|00001004020000#default-visibility]] of the configuration zettel. See [[visibility rules for zettel|00001010070200]] for more details. - ---- -Not yet supported, but planned: - -; [!folge]''folge'' -: The IDs of zettel that acts as a [[Folgezettel|https://zettelkasten.de/posts/tags/folgezettel/]]. Index: docs/manual/00001006020100.zettel ================================================================== --- docs/manual/00001006020100.zettel +++ docs/manual/00001006020100.zettel @@ -1,20 +1,18 @@ +id: 00001006020100 title: Supported Zettel Roles +role: manual tags: #manual #meta #reference #zettel #zettelstore syntax: zmk -role: manual The [[''role'' key|00001006020000#role]] defines what kind of zettel you are writing. -The following values are used by Zettelstore: +The following values are used internally by Zettelstore and must exist: -; [!new-template]''new-template'' -: Zettel with this role are used as templates for creating new zettel. - Within such a zettel, the metadata key [[''new-role''|00001006020000#new-role]] is used to specify the role of the new zettel. ; [!user]''user'' : If you want to use [[authentication|00001010000000]], all zettel that identify users of the zettel store must have this role. -Beside this, you are free to define your own roles. +Beside of this, you are free to define your own roles. The role ''zettel'' is predefined as the default role, but you can [[change this|00001004020000#default-role]]. Some roles are defined for technical reasons: Index: docs/manual/00001006020400.zettel ================================================================== --- docs/manual/00001006020400.zettel +++ docs/manual/00001006020400.zettel @@ -1,30 +1,40 @@ +id: 00001006020400 title: Supported values for metadata key ''read-only'' role: manual tags: #manual #meta #reference #zettel #zettelstore syntax: zmk -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]]. +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 -If there is no metadata value for key ''read-only'' or if its [[boolean value|00001006030000]] is interpreted as ""false"", anybody can modify the zettel. +If there is no metadata value for key ''read-only'' or if its [[boolean value|00001006030500]] +is interpreted as ""false"", anybody can modify the zettel. -If the metadata value is something else (the value ""true"" is recommended), the user cannot modify the zettel through the web interface. -However, if the zettel is stored as a file in a [[directory place|00001004011400]], the zettel could be modified using an external editor. +If the metadata value is something else (the value ""true"" is recommended), +the user cannot modify the zettel through the web interface. +However, if the zettel is stored as a file in a [[directory place|00001004011400]], +the zettel could be modified using an external editor. === Authentication enabled -If there is no metadata value for key ''read-only'' or if its [[boolean value|00001006030000]] is interpreted as ""false"", anybody can modify the zettel. +If there is no metadata value for key ''read-only'' or if its [[boolean value|00001006030500]] +is interpreted as ""false"", anybody can modify the zettel. -If the metadata value is the same as an explicit [[user role|00001010070300]], user with that role (or below) are not allowed to modify the zettel. +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'' +; ""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'' +; ""writer"" : 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 (the value ""owner"" is recommended), no user is allowed modify the zettel through the web interface. -However, if the zettel is accessible as a file in a [[directory place|00001004011400]], the zettel could be modified using an external editor. -Typically the owner of a Zettelstore should have such an access. +If the metadata value is something else (one of the values ""true"" or ""owner"" is recommended), +no user is allowed modify the zettel through the web interface. +However, if the zettel is accessible as a file in a [[directory place|00001004011400]], +the zettel could be modified using an external editor. +Typically the owner of a Zettelstore have such an access. Index: docs/manual/00001006030000.zettel ================================================================== --- docs/manual/00001006030000.zettel +++ docs/manual/00001006030000.zettel @@ -1,31 +1,29 @@ id: 00001006030000 title: Supported Key Types role: manual tags: #manual #meta #reference #zettel #zettelstore syntax: zmk -modified: 20210108184053 Most [[supported metadata keys|00001006020000]] conform to a type. Every metadata key should conform to a type. -Every key type is specified by a letter. -User-defined types are normally strings (type ''e''). - -Every key type has an associated validation rule to check values of the given -type. There is also a rule how values are matched, e.g. against a search term -when selecting some zettel. And there is a rule, how values compare for -sorting. - -|= Name | Meaning | Match | Sorting -| Boolean | Boolean value, False if value starts with ""''0''"", ""''F''"", ""''N''"", ""''f''"", or ""''n''"" | Boolean match | False < True -| Credential | Value is a credential, e.g. an encrypted password (planned) | Never matches | Uses zettel identifier for sorting -| Timestamp | Timestamp value YYYYMMDDHHmmSS | prefix match | by number -| EString | Any string, possibly empty | case-insensitive contains | case-sensitive -| Identifier | Value is a [[zettel identifier|00001006050000]] | prefix match | by number -| Number | Integer value | exact match | by number -| String | Any string, must not be empty | case-insensitive contains | case-sensitive -| TagSet | Value is a space-separated list of tags | exact match for one tag | case sensitive by sorted tags -| Word | Alfanumeric word, case-insensitive | case-insensitive equality | case-sensitive -| WordSet | Space-separated list of alfanumeric words, case-insensitive | case-insensitive match for one word | case-sensitive by sorted words -| URL | URL / URI | case-insensitive contains | case-sensitive -| Zettelmarkup | Any string, must not be empty, formatted in [[Zettelmarkup|00001007000000]] | case-insensitive contains | case-sensitive +User-defined metadata keys are of type EString. +The name of the metadata key is bound to the key type + +Every key type has an associated validation rule to check values of the given type. +There is also a rule how values are matched, e.g. against a search term when selecting some zettel. +And there is a rule, how values compare for sorting. + +* [[Boolean|00001006030500]] +* [[Credential|00001006031000]] +* [[EString|00001006031500]] +* [[Identifier|00001006032000]] +* [[IdentifierSet|00001006032500]] +* [[Number|00001006033000]] +* [[String|00001006033500]] +* [[TagSet|00001006034000]] +* [[Timestamp|00001006034500]] +* [[URL|00001006035000]] +* [[Word|00001006035500]] +* [[WordSet|00001006036000]] +* [[Zettelmarkup|00001006036500]] ADDED docs/manual/00001006030500.zettel Index: docs/manual/00001006030500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006030500.zettel @@ -0,0 +1,21 @@ +id: 00001006030500 +title: Boolean Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a truth value. + +=== Allowed values +Every character sequence that begins with a ""''0''"", ""''F''"", ""''N''"", ""''f''"", or a ""''n''"" is interpreted as the ""false"" boolean value. +All other metadata value is interpreted as the ""true"" boolean value. + +=== Match operator +The match operator is the equals operator, i.e. +* ``(true == true) == true`` +* ``(false == false) == true`` +* ``(true == false) == false`` +* ``(false == true) == false`` + +=== Sorting +The ""false"" value is less than the ""true"" value: ``false < true`` ADDED docs/manual/00001006031000.zettel Index: docs/manual/00001006031000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006031000.zettel @@ -0,0 +1,17 @@ +id: 00001006031000 +title: Credential Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a credential value, e.g. an encrypted password. + +=== Allowed values +All printable characters are allowed. +Since a credential contains some kind of secret, the sequence of characters might have some hidden syntax to be interpreted by other parts of Zettelstore. + +=== Match operator +A credential never matches to any other value. + +=== Sorting +If a list of zettel should be sorted based on a credential value, the identifier of the respective zettel is used instead. ADDED docs/manual/00001006031500.zettel Index: docs/manual/00001006031500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006031500.zettel @@ -0,0 +1,26 @@ +id: 00001006031500 +title: EString Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type are just a sequence of character, possibly an empty sequence. + +An EString is the most general metadata key type, as it places no restrictions to the character sequence.[^Well, there are some minor restrictions that follow from the [[metadata syntax|00001006010000]].] + +=== Allowed values +All printable characters are allowed. + +=== Match operator +A value matches an EString value, if the first value is part of the EString value. +This check is done case-insensitive. + +For example, ""hell"" matches ""Hello"". + +=== Sorting +To sort two values, the underlying encoding is used to determine which value is less than the other. + +Uppercase letters are typically interpreted as less than their corresponding lowercase letters, i.e. ``A < a``. + +Comparison is done character-wise by finding the first difference in the respective character sequence. +For example, ``abc > aBc``. ADDED docs/manual/00001006032000.zettel Index: docs/manual/00001006032000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006032000.zettel @@ -0,0 +1,20 @@ +id: 00001006032000 +title: Identifier Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a [[zettel identifier|00001006050000]]. + +=== Allowed values +Must be a sequence of 14 digits (""0""--""9""). + +=== Match operator +A value matches an identifier value, if the first value is the prefix of the identifier value. + +For example, ""000010"" matches ""[[00001006032000]]"". + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. + +If both values are identifiers, this works well because both have the same length. ADDED docs/manual/00001006032500.zettel Index: docs/manual/00001006032500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006032500.zettel @@ -0,0 +1,20 @@ +id: 00001006032500 +title: IdentifierSet Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a (sorted) set of [[zettel identifier|00001006050000]]. + +A set is different to a list, as no duplicates are allowed. + +=== Allowed values +Must be at least one sequence of 14 digits (""0""--""9""), separated by space characters. + +=== Match operator +A value matches an identifier set value, if the first value is a prefix of one of the identifier value. + +For example, ""000010"" matches ""[[00001006032000]] [[00001006032500]]"". + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. ADDED docs/manual/00001006033000.zettel Index: docs/manual/00001006033000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006033000.zettel @@ -0,0 +1,18 @@ +id: 00001006033000 +title: Number Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a numeric integer value. + +=== Allowed values +Must be a sequence of digits (""0""--""9""), optionally prefixed with a ""-"" or a ""+"" character. + +=== Match operator +The match operator is the equals operator, i.e. two values must be numeric equal to match. + +This includes that ""+12"" is equal to ""12"", therefore both values match. + +=== Sorting +Sorting is done by comparing the numeric values. ADDED docs/manual/00001006033500.zettel Index: docs/manual/00001006033500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006033500.zettel @@ -0,0 +1,25 @@ +id: 00001006033500 +title: String Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type are just a sequence of character, but not an empty sequence. + +=== Allowed values +All printable characters are allowed. +There must be at least one such character. + +=== Match operator +A value matches a String value, if the first value is part of the String value. +This check is done case-insensitive. + +For example, ""hell"" matches ""Hello"". + +=== Sorting +To sort two values, the underlying encoding is used to determine which value is less than the other. + +Uppercase letters are typically interpreted as less than their corresponding lowercase letters, i.e. ``A < a``. + +Comparison is done character-wise by finding the first difference in the respective character sequence. +For example, ``abc > aBc``. ADDED docs/manual/00001006034000.zettel Index: docs/manual/00001006034000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006034000.zettel @@ -0,0 +1,19 @@ +id: 00001006034000 +title: TagSet Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a (sorted) set of tags. + +A set is different to a list, as no duplicates are allowed. + +=== Allowed values +Every tag must must begin with the number sign character (""''#''"", ''U+0023''), followed by at least one printable character. +Tags are separated by space characters. + +=== Match operator +A value matches a tag set value, if the first value is equal to at least one tag in the tag set. + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. ADDED docs/manual/00001006034500.zettel Index: docs/manual/00001006034500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006034500.zettel @@ -0,0 +1,27 @@ +id: 00001006034500 +title: Timestamp Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a point in time. + +=== Allowed values +Must be a sequence of 14 digits (""0""--""9"") (same as an [[Identifier|00001006032000]]), with the restriction that is conforms to the pattern ""YYYYMMDDhhmmss"". + +* YYYY is the year, +* MM is the month, +* DD is the day, +* hh is the hour, +* mm is the minute, +* ss is the second. + +=== Match operator +A value matches an timestampvalue, if the first value is the prefix of the timestamp value. + +For example, ""202102"" matches ""20210212143200"". + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. + +If both values are timestamp values, this works well because both have the same length. ADDED docs/manual/00001006035000.zettel Index: docs/manual/00001006035000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006035000.zettel @@ -0,0 +1,19 @@ +id: 00001006035000 +title: URL Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote an URL. + +=== Allowed values +All characters of an URL / URI are allowed. + +=== Match operator +A value matches a URL value, if the first value is part of the URL value. +This check is done case-insensitive. + +For example, ""hell"" matches ""http://example.com/Hello"". + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. ADDED docs/manual/00001006035500.zettel Index: docs/manual/00001006035500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006035500.zettel @@ -0,0 +1,16 @@ +id: 00001006035500 +title: Word Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a single word. + +=== Allowed values +Must be a non-empty sequence of characters, but without the space character. + +=== Match operator +A value matches a word value, if both value are character-wise equal. + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. ADDED docs/manual/00001006036000.zettel Index: docs/manual/00001006036000.zettel ================================================================== --- /dev/null +++ docs/manual/00001006036000.zettel @@ -0,0 +1,18 @@ +id: 00001006036000 +title: WordSet Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type denote a (sorted) set of [[words|00001006035500]]. + +A set is different to a list, as no duplicates are allowed. + +=== Allowed values +Must be a sequence of at least one word, separated by space characters. + +=== Match operator +A value matches an wordset value, if the first value is equal to one of the word values in the word set. + +=== Sorting +Sorting is done by comparing the [[String|00001006033500]] values. ADDED docs/manual/00001006036500.zettel Index: docs/manual/00001006036500.zettel ================================================================== --- /dev/null +++ docs/manual/00001006036500.zettel @@ -0,0 +1,25 @@ +id: 00001006036500 +title: Zettelmarkup Key Type +role: manual +tags: #manual #meta #reference #zettel #zettelstore +syntax: zmk + +Values of this type are [[String|00001006033500]] values, interpreted as [[Zettelmarkup|00001007000000]]. + +=== Allowed values +All printable characters are allowed. +There must be at least one such character. + +=== Match operator +A value matches a String value, if the first value is part of the String value. +This check is done case-insensitive. + +For example, ""hell"" matches ""Hello"". + +=== Sorting +To sort two values, the underlying encoding is used to determine which value is less than the other. + +Uppercase letters are typically interpreted as less than their corresponding lowercase letters, i.e. ``A < a``. + +Comparison is done character-wise by finding the first difference in the respective character sequence. +For example, ``abc > aBc``. Index: docs/manual/00001006050000.zettel ================================================================== --- docs/manual/00001006050000.zettel +++ docs/manual/00001006050000.zettel @@ -1,5 +1,6 @@ +id: 00001006050000 title: Zettel identifier tags: #design #manual #zettelstore syntax: zmk role: manual @@ -6,17 +7,21 @@ 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, following by day, hour, minute, and second. +They resemble a timestamp: the first four digits could represent the year, the +next two represent the month, following by day, hour, minute, and second. This allows to order zettel 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. -In fact, all identifiers of zettel initially provided by an empty Zettelstore begin with ""000000"". -The identifiers of zettel if this manual have be chosen to start with ""000010"". +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. +In fact, all identifiers of zettel initially provided by an empty Zettelstore +begin with ""000000"", except the home zettel ''00010000000000''. +The identifiers of zettel if this manual have be 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. +A zettel can have any identifier that contains 14 digits and that is not in use +by another zettel managed by the same Zettelstore. Index: docs/manual/00001007000000.zettel ================================================================== --- docs/manual/00001007000000.zettel +++ docs/manual/00001007000000.zettel @@ -1,5 +1,6 @@ +id: 00001007000000 title: Zettelmarkup tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007010000.zettel ================================================================== --- docs/manual/00001007010000.zettel +++ docs/manual/00001007010000.zettel @@ -1,56 +1,57 @@ +id: 00001007010000 title: Zettelmarkup: General Principles tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Any document can be thought as a sequence of paragraphs and other blocks-structural elements (""blocks""), such as headings, lists, quotations, and code blocks. Some of these blocks can contain other blocks, for example lists may contain other lists or paragraphs. Other blocks contain inline-structural elements (""inlines""), such as text, links, emphasized text, and images. -With the exception of lists and tables, the markup for blocks always starts at the first position of a line and starts with three or more identical characters. -List blocks also starts at the first position of a line, but may need one or more character, plus a space character. -Table blocks starts at the first position of a line with the character ""``|``"". +With the exception of lists and tables, the markup for blocks always begins at the first position of a line with three or more identical characters. +List blocks also begins at the first position of a line, but may need one or more character, plus a space character. +Table blocks begins at the first position of a line with the character ""``|``"". Non-list blocks are either fully specified on that line or they span multiple lines and are delimited with the same three or more character. It depends on the block kind, whether blocks are specified on one line or on at least two lines. -If a line does not start with an explicit block element. the line is treated as a (implicit) paragraph block element that contains inline elements. +If a line does not begin with an explicit block element. the line is treated as a (implicit) paragraph block element that contains inline elements. This paragraph ends when a block element is detected at the beginning of a next line or when an empty line occurs. Some blocks may also contain inline elements, e.g. a heading. -Inline elements mostly starts with two non-space, often identical characters. -With some exceptions, two identical non-space characters starts a formatting range that is ended with the same two characters. +Inline elements mostly begins with two non-space, often identical characters. +With some exceptions, two identical non-space characters begins a formatting range that is ended with the same two characters. Exceptions are: links, images, edits, comments, and both the ""en-dash"" and the ""horizontal ellipsis"". A link is given with ``[[...]]``{=zmk}, an images with ``{{...}}``{=zmk}, and an edit formatting with ``((...))``{=zmk}. -An inline comment, starting with the sequence ``%%``{=zmk}, always ends at the end of the line where it begins. +An inline comment, beginning with the sequence ``%%``{=zmk}, always ends at the end of the line where it begins. The ""en-dash"" (""--"") is specified as ``--``{=zmk}, the ""horizontal ellipsis"" (""..."") as ``...``{=zmk}[^If placed at the end of non-space text.]. Some inline elements do not follow the rule of two identical character, especially to specify footnotes, citation keys, and local marks. -These elements start with one opening square bracket (""``[``""), use a character for specifying the kind of the inline, typically allow to specify some content, and end with one closing square bracket (""``]``""). +These elements begin with one opening square bracket (""``[``""), use a character for specifying the kind of the inline, typically allow to specify some content, and end with one closing square bracket (""``]``""). -One inline element that does not start with two characters is the ""entity"". +One inline element that does not begin with two characters is the ""entity"". It allows to specify any Unicode character. The specification of that character is placed between an ampersand character and a semicolon: ``&...;``{=zmk}. For exmple, an ""n-dash"" could also be specified as ``–``{==zmk}. The backslash character (""``\\``"") possibly gives the next character a special meaning. This allows to resolve some left ambiguities. -For example, list of depth 2 will start a line with ``** Item 2.2``{=zmk}. -An inline element to strongly emphasize some text start with a space will be specified as ``** Text**``{=zmk}. -To force the inline element formatting at the start of a line, ``**\\ Text**``{=zmk} should better be specified. +For example, a list of depth 2 will begin a line with ``** Item 2.2``{=zmk}. +An inline element to strongly emphasize some text begin with a space will be specified as ``** Text**``{=zmk}. +To force the inline element formatting at the beginning of a line, ``**\\ Text**``{=zmk} should better be specified. Many block and inline elements can be refined by additional attributes. Attributes resemble roughly HTML attributes and are placed near the corresponding elements by using the syntax ``{...}``{=zmk}. One example is to make space characters visible inside a inline literal element: ``1 + 2 = 3``{-} was specified by using the default attribute: ``\`\`1 + 2 = 3\`\`{-}``. To summarize: -* With some exceptions, blocks-structural elements starts at the for position of a line with three identical characters. +* With some exceptions, blocks-structural elements begins at the for position of a line with three identical characters. * The most important exception to this rule is the specification of lists. * If no block element is found, a paragraph with inline elements is assumed. -* With some exceptions, inline-structural elements starts with two characters, quite often the same two characters. +* With some exceptions, inline-structural elements begins with two characters, quite often the same two characters. * The most important exceptions are links. * The backslash character can help to resolve possible ambiguities. * Attributes refine some block and inline elements. * Block elements have a higher priority than inline elements. Index: docs/manual/00001007020000.zettel ================================================================== --- docs/manual/00001007020000.zettel +++ docs/manual/00001007020000.zettel @@ -1,5 +1,6 @@ +id: 00001007020000 title: Zettelmarkup: Basic Definitions tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007030000.zettel ================================================================== --- docs/manual/00001007030000.zettel +++ docs/manual/00001007030000.zettel @@ -1,11 +1,12 @@ +id: 00001007030000 title: Zettelmarkup: Blocks-Structured Elements tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual -Every markup for blocks-structured elements (""blocks"") starts at the very first position of a line. +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 @@ -24,12 +25,12 @@ === Line-range blocks This kind of blocks encompass at least two lines. To be useful, they encompass more lines. -They start 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, starting at the first position of that line. +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 allows line-range blocks to be nested. Additionally, all other blocks elements are allowed in line-range blocks. * [[Verbatim blocks|00001007030500]] do not interpret their content, * [[Quotation blocks|00001007030600]] specify a block-length quotation, @@ -43,11 +44,11 @@ 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 starts a paragraph. +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. ``` @@ -60,9 +61,9 @@ A paragraph is essentially a sequence of [[inline-structured elements|00001007040000]]. Inline-structured elements cam 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 start the paragraph with a certain number of space characters. +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 starts 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. +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. Index: docs/manual/00001007030100.zettel ================================================================== --- docs/manual/00001007030100.zettel +++ docs/manual/00001007030100.zettel @@ -1,18 +1,19 @@ +id: 00001007030100 title: Zettelmarkup: Description Lists tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual A description list is a sequence of terms to be described together with the descriptions of each term. Every term can described in multiple ways. A description term (short: //term//) is specified with one semicolon (""'';''"", ''U+003B'') at the first position, followed by a space character and the described term, specified as a sequence of line elements. -If the following lines should also be part of the term, exactly two spaces must be given at the start the each following line. +If the following lines should also be part of the term, exactly two spaces must be given at the beginning of each following line. The description of a term is given with one colon (""'':''"", ''U+003A'') at the first position, followed by a space character and the description itself, specified as a sequence of inline elements. -Similar to terms, following lines can also be part of the actual description, if they start at each line with exactly two space characters. +Similar to terms, following lines can also be part of the actual description, if they begin at each line with exactly two space characters. In contrast to terms, the actual descriptions are merged into a paragraph. This is because, an actual description can contain more than one paragraph. As usual, paragraphs are separated by an empty line. Every following paragraph of an actual description must be indented by two space characters. Index: docs/manual/00001007030200.zettel ================================================================== --- docs/manual/00001007030200.zettel +++ docs/manual/00001007030200.zettel @@ -1,5 +1,6 @@ +id: 00001007030200 title: Zettelmarkup: Nested Lists tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual @@ -9,11 +10,12 @@ Let's call these three characters //list characters//. Any nested list item is specified by a non-empty sequence of list characters, followed by a space character and a sequence of inline elements. In case of a quotation list as the last list character, the space character followed by a sequence of inline elements is optional. The number / count of list characters gives the nesting of the lists. -If the following lines should also be part of the list item, exactly the same number of spaces must be given at the start the each following line as it is the lists are nested, plus one additional space character. In other words: the inline elements must start at the same column as it was on the previous line. +If the following lines should also be part of the list item, exactly the same number of spaces must be given at the beginning of each of the following lines as it is the lists are nested, plus one additional space character. +In other words: the inline elements must begin at the same column as it was on the previous line. The resulting sequence on inline elements is merged into a paragraph. Appropriately indented paragraphs can specified after the first one. Since each blocks-structured element has to be specified at the first position of a line, none of the nested list items may contain anything else than paragraphs. Index: docs/manual/00001007030300.zettel ================================================================== --- docs/manual/00001007030300.zettel +++ docs/manual/00001007030300.zettel @@ -1,12 +1,13 @@ +id: 00001007030300 title: Zettelmarkup: Headings tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual To specify a (sub-) section of a zettel, you should use the headings syntax: at -the start of a new line type at least three equal signs (""''=''"", ''U+003D''), plus at least one +the beginning of a new line type at least three equal signs (""''=''"", ''U+003D''), plus at least one space and enter the text of the heading as inline elements. ```zmk === Level 1 Heading ==== Level 2 Heading Index: docs/manual/00001007030400.zettel ================================================================== --- docs/manual/00001007030400.zettel +++ docs/manual/00001007030400.zettel @@ -1,5 +1,6 @@ +id: 00001007030400 title: Zettelmarkup: Horizontal Rule tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007030500.zettel ================================================================== --- docs/manual/00001007030500.zettel +++ docs/manual/00001007030500.zettel @@ -1,22 +1,23 @@ +id: 00001007030500 title: Zettelmarkup: Verbatim Blocks tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Verbatim blocks are used to enter text that should not be interpreted. -They start with at least three grave accent characters (""''`''"", ''U+0060'') at the first position of a line. +They begin with at least three grave accent characters (""''`''"", ''U+0060'') at the first position of a line. Alternatively, a modifier letter grave accent (""''Ë‹''"", ''U+02CB'') is also allowed[^On some devices, such as an iPhone / iPad, a grave accent character is harder to enter and is often confused with a modifier letter grave accent.]. -You can add some [[attributes|00001007050000]] on the start line of a verbatim block, following the initiating characters. +You can add some [[attributes|00001007050000]] on the beginning line of a verbatim block, following the initiating characters. The verbatim block supports the default attribute: when given, all spaces in the text are rendered in HTML as open box characters (""''␣''"", ''U+2423''). If you want to give only one attribute and this attribute is the generic attribute, you can omit the most of the attribute syntax and just specify the value. It will be interpreted as a (programming) language to support colourizing the text when rendered in HTML. Any other character in this line will be ignored -Text following the starting line will not be interpreted, until a line starts with at least the same number of the same characters given at the starting line. +Text following the beginning line will not be interpreted, until a line begins with at least the same number of the same characters given at the beginning line. This allows to enter some grave accent characters in the text that should not be interpreted. For example: `````zmk ````zmk Index: docs/manual/00001007030600.zettel ================================================================== --- docs/manual/00001007030600.zettel +++ docs/manual/00001007030600.zettel @@ -1,21 +1,22 @@ +id: 00001007030600 title: Zettelmarkup: Quotation Blocks tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual A simple way to enter a quotation is to use the [[quotation list|00001007030200]]. A quotation list loosely follows the convention of quoting text within emails. However, if you want to attribute the quotation to seomeone, a quotation block is more appropriately. -This kind of line-range block starts with at least three less-than characters (""''<''"", ''U+003C'') at the first position of a line. -You can add some [[attributes|00001007050000]] on the start line of a quotation block, following the initiating characters. +This kind of line-range block begins with at least three less-than characters (""''<''"", ''U+003C'') at the first position of a line. +You can add some [[attributes|00001007050000]] on the beginning line of a quotation block, following the initiating characters. The quotation does not support the default attribute, nor the generic attribute. Attributes are interpreted on HTML rendering. Any other character in this line will be ignored -Text following the starting line will be interpreted, until a line starts with at least the same number of the same characters given at the starting line. +Text following the beginning line will be interpreted, until a line begins with at least the same number of the same characters given at the beginning line. This allows to enter a quotation block within a quotation block. At the ending line, you can enter some [[inline elements|00001007040000]] after the less-than characters. These will interpreted as some attribution text. For example: Index: docs/manual/00001007030700.zettel ================================================================== --- docs/manual/00001007030700.zettel +++ docs/manual/00001007030700.zettel @@ -1,5 +1,6 @@ +id: 00001007030700 title: Zettelmarkup: Verse Blocks tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual @@ -6,17 +7,17 @@ Sometimes, you want to enter text with significant space characters at the beginning of each line and with significant line endings. Poetry is one typical example. Of course, you could help yourself with hard space characters and hard line breaks, by entering a backslash character before a space character and at the end of each line. Using a verse block might be easier. -This kind of line-range block starts with at least three quotation mark characters (""''"''"", ''U+0022'') at the first position of a line. -You can add some [[attributes|00001007050000]] on the start line of a verse block, following the initiating characters. +This kind of line-range block begins with at least three quotation mark characters (""''"''"", ''U+0022'') at the first position of a line. +You can add some [[attributes|00001007050000]] on the beginning line of a verse block, following the initiating characters. The verse block does not support the default attribute, nor the generic attribute. Attributes are interpreted on HTML rendering. Any other character in this line will be ignored. -Text following the starting line will be interpreted, until a line starts with at least the same number of the same characters given at the starting line. +Text following the beginning line will be interpreted, until a line begins with at least the same number of the same characters given at the beginning line. This allows to enter a verse block within a verse block. At the ending line, you can enter some [[inline elements|00001007040000]] after the quotation mark characters. These will interpreted as some attribution text. For example: Index: docs/manual/00001007030800.zettel ================================================================== --- docs/manual/00001007030800.zettel +++ docs/manual/00001007030800.zettel @@ -1,5 +1,6 @@ +id: 00001007030800 title: Zettelmarkup: Region Blocks tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual @@ -6,18 +7,18 @@ Region blocks does not directly have a visual representation. They just group a range of lines. You can use region blocks to enter [[attributes|00001007050000]] that apply only to this range of lines. One example is to enter a multi-line warning that should be visible. -This kind of line-range block starts with at least three colon characters (""'':''"", ''U+003A'') at the first position of a line[^Since a [[description text|00001007030100]] only use exactly one colon character at the first position of a line, there is no possible ambiguity between these elements.]. -You can add some [[attributes|00001007050000]] on the start line of a verse block, following the initiating characters. +This kind of line-range block begins with at least three colon characters (""'':''"", ''U+003A'') at the first position of a line[^Since a [[description text|00001007030100]] only use exactly one colon character at the first position of a line, there is no possible ambiguity between these elements.]. +You can add some [[attributes|00001007050000]] on the beginning line of a verse block, following the initiating characters. The region block does not support the default attribute, but it supports the generic attribute. Some generic attributes, like ``=note``, ``=warning`` will be rendered special. Attributes are interpreted on HTML rendering. Any other character in this line will be ignored. -Text following the starting line will be interpreted, until a line starts with at least the same number of the same characters given at the starting line. +Text following the beginning line will be interpreted, until a line begins with at least the same number of the same characters given at the beginning line. This allows to enter a region block within a region block. At the ending line, you can enter some [[inline elements|00001007040000]] after the colon characters. These will interpreted as some attribution text. For example: Index: docs/manual/00001007030900.zettel ================================================================== --- docs/manual/00001007030900.zettel +++ docs/manual/00001007030900.zettel @@ -1,23 +1,24 @@ +id: 00001007030900 title: Zettelmarkup: Comment Blocks tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Comment blocks are quite similar to [[verbatim blocks|00001007030500]]: both are used to enter text that should not be interpreted. While the text entered inside a verbatim block will be processed somehow, text inside a comment block will be ignored[^Well, not completely ignored: text is read, but it will typically not rendered visible.]. Comment blocks are typically used to give some internal comments, e.g. the license of a text or some internal remarks. -Comment blocks start with at least three percent sign characters (""''%''"", ''U+0025'') at the first position of a line. -You can add some [[attributes|00001007050000]] on the start line of a comment block, following the initiating characters. +Comment blocks begin with at least three percent sign characters (""''%''"", ''U+0025'') at the first position of a line. +You can add some [[attributes|00001007050000]] on the beginning line of a comment block, following the initiating characters. The comment block supports the default attribute: when given, the text will be rendered, e.g. as an HTML comment. When rendered to JSON, the comment block will not be ignored but it will output some JSON text. Same for other renderers. Any other character in this line will be ignored -Text following the starting line will not be interpreted, until a line starts with at least the same number of the same characters given at the starting line. +Text following the beginning line will not be interpreted, until a line begins with at least the same number of the same characters given at the beginning line. This allows to enter some percent sign characters in the text that should not be interpreted. For example: ```zmk %%% Index: docs/manual/00001007031000.zettel ================================================================== --- docs/manual/00001007031000.zettel +++ docs/manual/00001007031000.zettel @@ -1,5 +1,6 @@ +id: 00001007031000 title: Zettelmarkup: Tables tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual @@ -7,12 +8,12 @@ In zettelmarkup, table are not specified explicitly, but by entering //table rows//. Therefore, a table can be seen as a sequence of table rows. A table row is nothing as a sequence of //table cells//. The length of a table is the number of table rows, the width of a table is the maximum length of its rows. -The first cell of a row must start with the vertical bar character (""''|''"", ''U+007C'') at the first position of a line. -The other cells of a row start with the same vertical bar character at later positions in that line. +The first cell of a row must begin with the vertical bar character (""''|''"", ''U+007C'') at the first position of a line. +The other cells of a row begin with the same vertical bar character at later positions in that line. A cell is delimited by the vertical bar character of the next cell or by the end of the current line. A vertical bar character as the last character of a line will not result in a table cell. It will be ignored. Inside a cell, you can specify any [[inline elements|00001007040000]]. Index: docs/manual/00001007040000.zettel ================================================================== --- docs/manual/00001007040000.zettel +++ docs/manual/00001007040000.zettel @@ -1,5 +1,6 @@ +id: 00001007040000 title: Zettelmarkup: Inline-Structured Elements tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual @@ -7,11 +8,11 @@ The content of a zettel contains is many cases just ordinary text, lightly formatted. Inline-structured elements allow to format your text and add some helpful links or images. Sometimes, you want to enter characters that have no representation on your keyboard. === Text formatting -Every [[text formatting|00001007040100]] element starts with two same characters at the beginning. +Every [[text formatting|00001007040100]] element begins with two same characters at the beginning. It lasts until the same two characters occurred the second time. Some of these elements explicitly support [[attributes|00001007050000]]. === Literal-like formatting Sometime you want to render the text as it is. @@ -25,29 +26,29 @@ An important aspect of all knowledge work is to reference others work, e.g. with citation keys. All these elements can be subsumed under [[reference-like text|00001007040300]]. === Other inline elements ==== Comments -A comment is started with two consecutive percent sign characters (""''%''"", ''U+0025''). -It ends at the end of the line where it started. +A comment begins with two consecutive percent sign characters (""''%''"", ''U+0025''). +It ends at the end of the line where it begins. ==== Backslash The backslash character (""''\\''"", ''U+005C'') gives the next character another meaning. * If a space character follows, it is converted in a non-breaking space (''U+00A0''). * If a line ending follows the backslash character, the line break is converted from a //soft break// into a //hard break//. * Every other character is taken as itself, but without the interpretation of a Zettelmarkup element. For example, if you want to enter a ""'']''"" into a footnote text, you should escape it with a backslash. ==== Tag -Any text that starts with a number sign character (""''#''"", ''U+0023''), followed by a non-empty sequence of Unicode letters, Unicode digits, the hyphen-minus character (""''-''"", ''U+002D''), or the low line character (""''_''"", ''U+005F'') is interpreted as an //inline tag//. +Any text that begins with a number sign character (""''#''"", ''U+0023''), followed by a non-empty sequence of Unicode letters, Unicode digits, the hyphen-minus character (""''-''"", ''U+002D''), or the low line character (""''_''"", ''U+005F'') is interpreted as an //inline tag//. They will be considered equivalent to tags in metadata. ==== Entities & more Sometimes it is not easy to enter special characters. If you know the Unicode code point of that character, or its name according to the [[HTML standard|https://html.spec.whatwg.org/multipage/named-characters.html]], you can enter it by number or by name. -Regardless which method you use, an entity always starts with an ampersand character (""''&''"", ''U+0026'') and ends with a semicolon character (""'';''"", ''U+003B''). +Regardless which method you use, an entity always begins with an ampersand character (""''&''"", ''U+0026'') and ends with a semicolon character (""'';''"", ''U+003B''). If you know the HTML name of the character you want to enter, place it between these two character. Example: ``&`` is rendered as ::&::{=example}. If you want to enter its numeric code point, a number sign character must follow the ampersand character, followed by digits to base 10. Example: ``&`` is rendered in HTML as ::&::{=example}. Index: docs/manual/00001007040100.zettel ================================================================== --- docs/manual/00001007040100.zettel +++ docs/manual/00001007040100.zettel @@ -1,18 +1,19 @@ +id: 00001007040100 title: Zettelmarkup: Text Formatting tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Text formatting is the way to make your text visually different. -Every text formatting element starts with two same characters. +Every text formatting element begins with two same characters. It ends when these two same characters occur the second time. It is possible that some [[attributes|00001007050000]] follow immediately, without any separating character. Text formatting can be nested, up to a reasonable limit. -The following characters start a text formatting: +The following characters begin a text formatting: * The slash character (""''/''"", ''U+002F'') emphasizes its text. Often, such text is rendered in italics. If the default attribute is specified, the emphasized text is not just rendered as such, but also internally marked as emphasized. ** Example: ``abc //def// ghi`` is rendered in HTML as: ::abc //def// ghi::{=example}. ** Example: ``abc //def//{-} ghi`` is rendered in HTML as: ::abc //def//{-} ghi::{=example}. * The asterisk character (""''*''"", ''U+002A'') strongly emphasized its enclosed text. The text is often rendered in bold. Again, the default attribute will force a explicit semantic meaning of strong emphasizing. Index: docs/manual/00001007040200.zettel ================================================================== --- docs/manual/00001007040200.zettel +++ docs/manual/00001007040200.zettel @@ -1,5 +1,6 @@ +id: 00001007040200 title: Zettelmarkup: Literal-like formatting tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007040300.zettel ================================================================== --- docs/manual/00001007040300.zettel +++ docs/manual/00001007040300.zettel @@ -1,5 +1,6 @@ +id: 00001007040300 title: Zettelmarkup: Reference-like text role: manual tags: #manual #zettelmarkup #zettelstore syntax: zmk @@ -14,11 +15,11 @@ * Reference via a citation key. * Put a mark within your zettel that you can reference later with a link. === Links There are two kinds of links, regardless of links to (internal) other zettel or to (external) material. -Both kinds starts with two consecutive left square bracket characters (""''[''"", ''U+005B'') and ends with two consecutive right square bracket characters (""'']''"", ''U+005D''). +Both kinds begin with two consecutive left square bracket characters (""''[''"", ''U+005B'') and ends with two consecutive right square bracket characters (""'']''"", ''U+005D''). The first form provides some text plus the link specification, delimited by a vertical bar character (""''|''"", ''U+007C''): ``[[text|linkspecification]]``. The second form just provides a link specification between the square brackets. Its text is derived from the link specification, e.g. by interpreting the link specification as text: ``[[linkspecification]]``. @@ -26,21 +27,22 @@ The link specification for another zettel within the same Zettelstore is just the [[zettel identifier|00001006050000]]. To reference some content within a zettel, you can append a number sign character (""''#''"", ''U+0023'') and the name of the mark to the zettel identifier. The resulting reference is called ""zettel reference"". To specify some material outside the Zettelstore, just use an normal Uniform Resource Identifier (URI) as defined by [[RFC\ 3986|https://tools.ietf.org/html/rfc3986]]. -If the URL starts with the slash character (""/"", ''U+002F''), i.e. without scheme, user info, and host name, or if it starts with ""./"" or with ""../"", the reference will be treated as a ""local reference"", otherwise as an ""external reference"". +If the URL begins with the slash character (""/"", ''U+002F''), or if it begins with ""./"" or with ""../"", i.e. without scheme, user info, and host name, the reference will be treated as a ""local reference"", otherwise as an ""external reference"". +If the URL begins with two slash characters, it will be interpreted relative to the value of [[''url-prefix''|00001004010000#url-prefix]]. The text in the second form is just a sequence of inline elements. === Images To some degree, an image specification is conceptually not too far away from a link specification. Both contain a link specification and optionally some text. In contrast to a link, the link specification of an image must resolve to actual graphical image data. That data is read when rendered as HTML, and is embedded inside the zettel as an inline image. -An image specification starts with two consecutive left curly bracket characters (""''{''"", ''U+007B'') and ends with two consecutive right curly bracket characters (""''}''"", ''U+007D''). +An image specification begins with two consecutive left curly bracket characters (""''{''"", ''U+007B'') and ends with two consecutive right curly bracket characters (""''}''"", ''U+007D''). The curly brackets delimits either a link specification or some text, a vertical bar character and the link specification, similar to a link. One difference to a link: if the text was not given, an empty string is assumed. The link specification must reference a graphical image representation if the image is about to be rendered. @@ -61,11 +63,11 @@ %%``{{External link|00000000030001}}{title=External width=30}`` is rendered as ::{{External link|00000000030001}}{title=External width=30}::{=example}. === Footnotes -A footnote starts with a left square bracket, followed by a circumflex accent (""''^''"", ''U+005E''), followed by some text, and ends with a right square bracket. +A footnote begins with a left square bracket, followed by a circumflex accent (""''^''"", ''U+005E''), followed by some text, and ends with a right square bracket. Example: ``Main text[^Footnote text.].`` is rendered in HTML as: ::Main text[^Footnote text.].::{=example}. @@ -73,22 +75,22 @@ A citation key references some external material that is part of a bibliografical collection. Currently, Zettelstore implements this only partially, it is ""work in progress"". -However, the syntax is: starting with a left square bracket and followed by an at sign character (""''@''"", ''U+0040''), a the citation key is given. +However, the syntax is: beginning with a left square bracket and followed by an at sign character (""''@''"", ''U+0040''), a the citation key is given. The key is typically a sequence of letters and digits. If a comma character (""'',''"", ''U+002C'') or a vertical bar character is given, the following is interpreted as inline elements. A right square bracket ends the text and the citation key element. === Mark A mark allows to name a point within a zettel. This is useful if you want to reference some content in a bigger-sized zettel[^Other uses of marks will be given, if Zettelmarkup is extended by a concept called //transclusion//.]. -A mark starts with a left square bracket, followed by an exclamation mark character (""''!''"", ''U+0021''). +A mark begins with a left square bracket, followed by an exclamation mark character (""''!''"", ''U+0021''). Now the optional mark name follows. It is a (possibly empty) sequence of Unicode letters, Unicode digits, the hyphen-minus character (""''-''"", ''U+002D''), or the low-line character (""''_''"", ''U+005F''). The mark element ends with a right square bracket. Examples: * ``[!]`` is a mark without a name, the empty mark. * ``[!mark]`` is a mark with the name ""mark"". Index: docs/manual/00001007050000.zettel ================================================================== --- docs/manual/00001007050000.zettel +++ docs/manual/00001007050000.zettel @@ -1,5 +1,6 @@ +id: 00001007050000 title: Zettelmarkup: Attributes tags: #manual #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007050100.zettel ================================================================== --- docs/manual/00001007050100.zettel +++ docs/manual/00001007050100.zettel @@ -1,5 +1,6 @@ +id: 00001007050100 title: Zettelmarkup: Supported Attribute Values for Natural Languages tags: #manual #reference #zettelmarkup #zettelstore syntax: zmk role: manual @@ -16,13 +17,13 @@ * ``{lang=de}`` for the german language * ``{lang=de-at}`` for the german language dialect spoken in Austria * ``{lang=de-de}`` for the german language dialect spoken in Germany The actual [[typographic quotations marks|00001007040100]] (``""...""``) are derived from the current language. -The language of a zettel (meta key ''lang'') or of the whole Zettelstore (''default-lang'' of the [[configuration zettel|00001004020000]]) can be overwritten by an attribute: ``""...""{lang=fr}``{=zmk}. +The language of a zettel (meta key ''lang'') or of the whole Zettelstore (''default-lang'' of the [[configuration zettel|00001004020000#default-lang]]) can be overwritten by an attribute: ``""...""{lang=fr}``{=zmk}. Currently, Zettelstore supports the following primary languages: * ''de'' * ''en'' * ''fr'' These are used, even if a dialect was specified. Index: docs/manual/00001007050200.zettel ================================================================== --- docs/manual/00001007050200.zettel +++ docs/manual/00001007050200.zettel @@ -1,5 +1,6 @@ +id: 00001007050200 title: Zettelmarkup: Supported Attribute Values for Programming Languages tags: #manual #reference #zettelmarkup #zettelstore syntax: zmk role: manual Index: docs/manual/00001007060000.zettel ================================================================== --- docs/manual/00001007060000.zettel +++ docs/manual/00001007060000.zettel @@ -1,11 +1,12 @@ +id: 00001007060000 title: Zettelmarkup: Summary of Formatting Characters tags: #manual #reference #zettelmarkup #zettelstore syntax: zmk role: manual -The following table gives an overview about the use of all characters that start a markup element. +The following table gives an overview about the use of all characters that begin a markup element. |= Character :|= Blocks <|= Inlines < | ''!'' | (free) | (free) | ''"'' | [[Verse block|00001007030700]] | [[Typographic quotation mark|00001007040100]] | ''#'' | [[Ordered list|00001007030200]] | [[Tag|00001007040000]] Index: docs/manual/00001008000000.zettel ================================================================== --- docs/manual/00001008000000.zettel +++ docs/manual/00001008000000.zettel @@ -1,11 +1,10 @@ id: 00001008000000 title: Other Markup Languages role: manual tags: #manual #zettelstore syntax: zmk -modified: 20210111182215 [[Zettelmarkup|00001007000000]] is not the only markup language you can use to define your content. Zettelstore is quite agnostic with respect to markup languages. Of course, Zettelmarkup plays an important role. However, with the exception of zettel titles, you can use any (markup) language that is supported: @@ -15,11 +14,11 @@ * CSS * HTML template data * Plain text, not further interpreted The [[metadata key|00001006020000#syntax]] ""''syntax''"" specifies which language should be used. -If it is not given, the key ""''default-syntax''"" will be used (specified in the [[configuration zettel|00001004020000]]). +If it is not given, the key ""''default-syntax''"" will be used (specified in the [[configuration zettel|00001004020000#default-syntax]]). The following syntax values are supported: ; [!css]''css'' : A [[Cascading Style Sheet|https://www.w3.org/Style/CSS/]], to be used when rendering a zettel as HTML. ; [!gif]''gif''; [!jpeg]''jpeg''; [!jpg]''jpg''; [!png]''png'' Index: docs/manual/00001008010000.zettel ================================================================== --- docs/manual/00001008010000.zettel +++ docs/manual/00001008010000.zettel @@ -1,5 +1,6 @@ +id: 00001008010000 title: Use Markdown as the main markup language of Zettelstore tags: #manual #markdown #zettelstore syntax: zmk role: manual Index: docs/manual/00001010000000.zettel ================================================================== --- docs/manual/00001010000000.zettel +++ docs/manual/00001010000000.zettel @@ -1,5 +1,6 @@ +id: 00001010000000 title: Security tags: #configuration #manual #security #zettelstore syntax: zmk role: manual Index: docs/manual/00001010040100.zettel ================================================================== --- docs/manual/00001010040100.zettel +++ docs/manual/00001010040100.zettel @@ -1,8 +1,9 @@ +id: 00001010040100 title: Enable authentication tags: #authentication #configuration #manual #security #zettelstore syntax: zmk role: manual To enable authentication, you must create a zettel that stores [[authentication data|00001010040200]] for the owner. -Then you must reference this zettel within the [[start-up configuration|00001004010000]] under the key ''owner''. +Then you must reference this zettel within the [[start-up configuration|00001004010000#owner]] under the key ''owner''. Once the start-up configuration contains a valid [[zettel identifier|00001006050000]] under that key, authentication is enabled. Index: docs/manual/00001010040200.zettel ================================================================== --- docs/manual/00001010040200.zettel +++ docs/manual/00001010040200.zettel @@ -1,5 +1,6 @@ +id: 00001010040200 title: Creating an user zettel tags: #authentication #configuration #manual #security #zettelstore syntax: zmk role: manual Index: docs/manual/00001010040400.zettel ================================================================== --- docs/manual/00001010040400.zettel +++ docs/manual/00001010040400.zettel @@ -1,5 +1,6 @@ +id: 00001010040400 title: Authentication process tags: #authentication #configuration #manual #security #zettelstore syntax: zmk role: manual Index: docs/manual/00001010040700.zettel ================================================================== --- docs/manual/00001010040700.zettel +++ docs/manual/00001010040700.zettel @@ -1,5 +1,6 @@ +id: 00001010040700 title: Access token tags: #authentication #configuration #manual #security #zettelstore syntax: zmk role: manual @@ -6,19 +7,19 @@ If an user is authenticated, an ""access token"" is created that must be sent with every request to prove the identity of the caller. Otherwise the user will not be recognized by Zettelstore. If the user was authenticated via the web interface, the access token is stored in a [[""session cookie""|https://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie]]. When the web browser is closed, theses cookies are not saved. -If you want web browser to store the cookie as long as lifetime of that token, the owner must set ''persistent-cookie'' of the [[startup configuration|00001004010000]] to ''true''. +If you want web browser to store the cookie as long as lifetime of that token, the owner must set ''persistent-cookie'' of the [[start-up configuration|00001004010000]] to ''true''. If the web browser remains inactive for a period, the user will be automatically logged off, because each access token has a limited lifetime. -The maximum length of this period is specified by the ''token-lifetime-html'' value of the startup configuration. +The maximum length of this period is specified by the ''token-lifetime-html'' value of the start-up configuration. Every time a web page is displayed, a fresh token is created and stored inside the cookie. If the user was authenticated via the API, the access token will be returned as the content of the response. Typically, the lifetime of this token is more short term, e.g. 10 minutes. -It is specified by the ''token-timeout-api'' value of the startup configuration. +It is specified by the ''token-timeout-api'' value of the start-up configuration. If you need more time, you can either [[re-authenticate|00001012050200]] the user or use an API call to [[renew the access token|00001012050400]]. -If you remotely access your Zettelstore via HTTP (not via HTTPS, which allows encrypted communication), your must set the ''insecure-cookie'' value of the startup configuration to ''true''. +If you remotely access your Zettelstore via HTTP (not via HTTPS, which allows encrypted communication), your must set the ''insecure-cookie'' value of the start-up configuration to ''true''. In most cases, such a scenario is not recommended, because user name and password will be transferred as plain text. You could make use of such scenario if you know all parties that access the local network where you access the Zettelstore. Index: docs/manual/00001010070200.zettel ================================================================== --- docs/manual/00001010070200.zettel +++ docs/manual/00001010070200.zettel @@ -1,10 +1,10 @@ +id: 00001010070200 title: Visibility rules for zettel role: manual tags: #authorization #configuration #manual #security #zettelstore syntax: zmk -modified: 20201221174224 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: @@ -22,11 +22,11 @@ : 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. This is for zettel with sensitive content that might irritate the owner. Computed zettel with internal runtime information are examples for such a zettel. ; [!simple-expert]""simple-expert"" -: The owner of the Zettelstore cab access the zettel, if expert mode is enabled, or if authentication is disabled and the Zettelstore is started without any command. +: The owner of the Zettelstore can access the zettel, if expert mode is enabled, or if authentication is disabled and the Zettelstore is started without any command. The reason for this is to show all computed zettel to an user that started the Zettestore in simple mode. Many computed zettel should be given in error reporting and a new user might not be able to enable expert mode. When you install a Zettelstore, only two zettel have visibility ""public"". @@ -35,7 +35,7 @@ The other zettel is the zettel containing the [[version|00000000000001]] of the Zettelstore. Please note: if authentication is not enabled, every user has the same rights as the owner of a Zettelstore. This is also true, if the Zettelstore runs additionally in [[read-only mode|00001004010000#read-only-mode]]. In this case, the [[runtime configuration zettel|00001004020000]] is shown (its visibility is ""owner""). -The [[startup configuration|00001004010000]] is not shown, because the associated computed zettel with identifier ''00000000000099'' is stored with the visibility ""expert"". +The [[start-up configuration|00001004010000]] is not shown, because the associated computed zettel with identifier ''00000000000099'' is stored with the visibility ""expert"". If you want to show such a zettel, you must set ''expert-mode'' to true. Index: docs/manual/00001010070300.zettel ================================================================== --- docs/manual/00001010070300.zettel +++ docs/manual/00001010070300.zettel @@ -1,5 +1,6 @@ +id: 00001010070300 title: User roles role: manual tags: #authorization #configuration #manual #security #zettelstore syntax: zmk Index: docs/manual/00001010070400.zettel ================================================================== --- docs/manual/00001010070400.zettel +++ docs/manual/00001010070400.zettel @@ -1,5 +1,6 @@ +id: 00001010070400 title: Authorization and read-only mode tags: #authorization #configuration #manual #security #zettelstore syntax: zmk role: manual Index: docs/manual/00001010070600.zettel ================================================================== --- docs/manual/00001010070600.zettel +++ docs/manual/00001010070600.zettel @@ -1,5 +1,6 @@ +id: 00001010070600 title: Access rules tags: #authorization #configuration #manual #security #zettelstore syntax: zmk role: manual @@ -44,9 +45,5 @@ Only the owner of the Zettelstore is currently allowed to give a new identifier for a zettel. * Delete a zettel ** Reject the access. Only the owner of the Zettelstore is allowed to delete a zettel. This may change in the future. -* Reload internal values -** Reject the access. - Only the owner of the Zettelstore is allowed to perform a reload operation. - This may change in the future. Index: docs/manual/00001010090100.zettel ================================================================== --- docs/manual/00001010090100.zettel +++ docs/manual/00001010090100.zettel @@ -1,11 +1,10 @@ id: 00001010090100 title: External server to encrypt message transport role: manual tags: #configuration #encryption #manual #security #zettelstore syntax: zmk -modified: 20210125195546 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. @@ -65,7 +64,7 @@ } ``` This will forwards 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 start-tp configuration key ''url-prefix'' with the value ""/manual"". +In this case you must specify the start-up configuration key ''url-prefix'' with the value ""/manual"". This is to allow the Zettelstore to give you the correct URLs with the given prefix. Index: docs/manual/00001012000000.zettel ================================================================== --- docs/manual/00001012000000.zettel +++ docs/manual/00001012000000.zettel @@ -1,11 +1,10 @@ id: 00001012000000 title: API role: manual tags: #api #manual #zettelstore syntax: zmk -modified: 20210112113014 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. The [[web user interface|00001014000000]] is just an alternative, secondary way of interacting with a Zettelstore. @@ -31,15 +30,17 @@ * [[List metadata of all zettel|00001012051200]] * [[List all zettel, but in different encoding formats|00001012051400]] * [[List all zettel, but include different parts of a zettel|00001012051600]] * [[Shape the list of zettel metadata with filter options|00001012051800]] * [[Sort the list of zettel metadata|00001012052000]] -* List all [[tags|00001006020000]] used in a Zettelstore -* List all [[roles|00001006020100]] used in a Zettelstore. +* [[List all tags|00001012052200]] +* [[List all roles|00001012052400]] === Working with zettel * Create a new zettel * [[Retrieve metadata and content of an existing zettel|00001012053400]] * [[Retrieve references of an existing zettel|00001012053600]] +* [[Retrieve context of an existing zettel|00001012053800]] +* [[Retrieve zettel order within an existing zettel|00001012054000]] * Update metadata and content of a zettel * Rename a zettel * Delete a zettel Index: docs/manual/00001012050200.zettel ================================================================== --- docs/manual/00001012050200.zettel +++ docs/manual/00001012050200.zettel @@ -1,17 +1,16 @@ id: 00001012050200 title: API: Authenticate a client role: manual tags: #api #manual #zettelstore syntax: zmk -modified: 20210111190943 Authentication for future API calls is done by sending a [[user identification|00001010040200]] and a password to the Zettelstore to obtain an [[access token|00001010040700]]. This token has to be used for other API calls. It is valid for a relatively short amount of time, as configured with the key ''token-timeout-api'' of the [[start-up configuration|00001004010000]] (typically 10 minutes). -The simplest way is to send user identification (''IDENT'') and password (''PASSWORD'') via [[HTTP Basic Authentication|https://tools.ietf.org/html/rfc7617]] and send them to the endpoint ''/a'' with a POST request: +The simplest way is to send user identification (''IDENT'') and password (''PASSWORD'') via [[HTTP Basic Authentication|https://tools.ietf.org/html/rfc7617]] and send them to the [[endpoint|00001012920000]] ''/a'' with a POST request: ```sh # curl -X POST -u IDENT:PASSWORD http://127.0.0.1:23123/a {"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJfdGsiOjEsImV4cCI6MTYwMTczMTI3NSwiaWF0IjoxNjAxNzMwNjc1LCJzdWIiOiJhYmMiLCJ6aWQiOiIyMDIwMTAwMzE1MDEwMCJ9.ekhXkvn146P2bMKFQcU-bNlvgbeO6sS39hs6U5EKfjIqnSInkuHYjYAIfUqf_clYRfr6YBlX5izii8XfxV8jhg","token_type":"Bearer","expires_in":600} ``` Index: docs/manual/00001012050400.zettel ================================================================== --- docs/manual/00001012050400.zettel +++ docs/manual/00001012050400.zettel @@ -1,14 +1,15 @@ +id: 00001012050400 title: API: Renew an access token +role: manual tags: #api #manual #zettelstore syntax: zmk -role: manual An access token is only valid for a certain duration. Since the [[authentication process|00001012050200]] will need some processing time, there is a way to renew the token without providing full authentication data. -Send a HTTP PUT request to the endpoint ''/a'' and include the current access token in the ''Authorization'' header: +Send a HTTP PUT request to the [[endpoint|00001012920000]] ''/a'' and include the current access token in the ''Authorization'' header: ```sh # curl -X PUT -H 'Authorization: Bearer TOKEN' http://127.0.0.1:23123/a {"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJfdGsiOjEsImV4cCI6MTYwMTczMTI3NSwiaWF0IjoxNjAxNzMwNjc1LCJzdWIiOiJhYmMiLCJ6aWQiOiIyMDIwMTAwMzE1MDEwMCJ9.ekhXkvn146P2bMKFQcU-bNlvgbeO6sS39hs6U5EKfjIqnSInkuHYjYAIfUqf_clYRfr6YBlX5izii8XfxV8jhg","token_type":"Bearer","expires_in":456} ``` Index: docs/manual/00001012050600.zettel ================================================================== --- docs/manual/00001012050600.zettel +++ docs/manual/00001012050600.zettel @@ -1,5 +1,6 @@ +id: 00001012050600 title: API: Provide an access token tags: #api #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001012051200.zettel ================================================================== --- docs/manual/00001012051200.zettel +++ docs/manual/00001012051200.zettel @@ -1,11 +1,12 @@ +id: 00001012051200 title: API: List metadata of all zettel +role: manual tags: #api #manual #zettelstore syntax: zmk -role: manual -To list the metadata of all zettel just send a HTTP GET request to the endpoint ''/z''[^If [[authentication is enabled|00001010040100]], you must include the a valid [[access token|00001012050200]] in the ''Authorization'' header]. +To list the metadata of all zettel just send a HTTP GET request to the [[endpoint|00001012920000]] ''/z''[^If [[authentication is enabled|00001010040100]], you must include the a valid [[access token|00001012050200]] in the ''Authorization'' header]. If successful, the output is a JSON object: ```sh # curl http://127.0.0.1:23123/z {"list":[{"id":"00001012051200","url":"/z/00001012051200","meta":{"title":"API: Renew an access token","tags":"#api #manual #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012050600","url":"/z/00001012050600","meta":{"title":"API: Provide an access token","tags":"#api #manual #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012050400","url":"/z/00001012050400","meta":{"title":"API: Renew an access token","tags":"#api #manual #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012050200","url":"/z/00001012050200","meta":{"title":"API: Authenticate a client","tags":"#api #manual #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012000000","url":"/z/00001012000000","meta":{"title":"API","tags":"#api #manual #zettelstore","syntax":"zmk","role":"manual"}}]} Index: docs/manual/00001012051400.zettel ================================================================== --- docs/manual/00001012051400.zettel +++ docs/manual/00001012051400.zettel @@ -1,5 +1,6 @@ +id: 00001012051400 title: API: List all zettel, but in different encoding formats tags: #api #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001012051600.zettel ================================================================== --- docs/manual/00001012051600.zettel +++ docs/manual/00001012051600.zettel @@ -1,5 +1,6 @@ +id: 00001012051600 title: API: List all zettel, but include different parts of a zettel tags: #api #manual #zettelstore syntax: zmk role: manual Index: docs/manual/00001012051800.zettel ================================================================== --- docs/manual/00001012051800.zettel +++ docs/manual/00001012051800.zettel @@ -1,18 +1,17 @@ id: 00001012051800 title: API: Shape the list of zettel metadata with filter options role: manual tags: #api #manual #zettelstore syntax: zmk -modified: 20210112114019 In most cases, it is not essential to list //all// zettel. Typically, you are interested only in a subset of the zettel maintained by your Zettelstore. This is done by adding some query parameters to the general ''GET /z'' request. === Filter -Every query parameter that does //not// start with the low line character (""_"", ''U+005F'') is treated as the name of a [[metadata|00001006010000]] key. +Every query parameter that does //not// begin with the low line character (""_"", ''U+005F'') is treated as the name of a [[metadata|00001006010000]] key. According to the [[type|00001006030000]] of a metadata key, zettel are matched and therefore filtered. All [[supported|00001006020000]] metadata keys have a well-defined type. User-defined keys have the type ''e'' (string, possibly empty). For example, if you want to retrieve all zettel that contain the string ""API"" in its title, your request will be: @@ -40,11 +39,11 @@ By using the query parameter ""''_limit''"", which must have an integer value, you specifying an upper limit of list elements: ```sh # curl 'http://192.168.17.7:23121/z?title=API&_part=id&_sort=id&_limit=2' {"list":[{"id":"00001012000000","url":"/z/00001012000000"},{"id":"00001012050200","url":"/z/00001012050200"}]} ``` -The query parameter ""''_offset''"" allows to list not only the first elements, but start at a specific element: +The query parameter ""''_offset''"" allows to list not only the first elements, but to begin at a specific element: ```sh # curl 'http://192.168.17.7:23121/z?title=API&_part=id&_sort=id&_limit=2&_offset=1' {"list":[{"id":"00001012050200","url":"/z/00001012050200"},{"id":"00001012050400","url":"/z/00001012050400"}]} ``` Index: docs/manual/00001012052000.zettel ================================================================== --- docs/manual/00001012052000.zettel +++ docs/manual/00001012052000.zettel @@ -1,11 +1,10 @@ id: 00001012052000 title: API: Sort the list of zettel metadata role: manual tags: #api #manual #zettelstore syntax: zmk -modified: 20210112113839 If not specified, the list of zettel is sorted descending by the value of the zettel identifier. The highest zettel identifier, which is a number, comes first. You change that with the ""''_sort''"" query parameter. Alternatively, you can also use the ""''_order''"" query parameter. ADDED docs/manual/00001012052200.zettel Index: docs/manual/00001012052200.zettel ================================================================== --- /dev/null +++ docs/manual/00001012052200.zettel @@ -0,0 +1,18 @@ +id: 00001012052200 +title: API: List all tags +role: manual +tags: #api #manual #zettelstore +syntax: zmk + +To list all [[tags|00001006020000#tags]] used in the Zettelstore just send a HTTP GET request to the [[endpoint|00001012920000]] ''/t''. +If successful, the output is a JSON object: + +```sh +# curl http://127.0.0.1:23123/t +{"tags":{"#api":[:["00001012921000","00001012920800","00001012920522",...],"#authorization":["00001010040700","00001010040400",...],...,"#zettelstore":["00010000000000","00001014000000",...,"00001001000000"]}} +``` + +The JSON object only contains the key ''"tags"'' with the value of another object. +This second object contains all tags as keys and the list of identifier of those zettel with this tag as a value. + +Please note that this structure will likely change in the future to be more compliant with other API calls. ADDED docs/manual/00001012052400.zettel Index: docs/manual/00001012052400.zettel ================================================================== --- /dev/null +++ docs/manual/00001012052400.zettel @@ -0,0 +1,18 @@ +id: 00001012052400 +title: API: List all roles +role: manual +tags: #api #manual #zettelstore +syntax: zmk + +To list all [[roles|00001006020100]] used in the Zettelstore just send a HTTP GET request to the [[endpoint|00001012920000]] ''/r''. +If successful, the output is a JSON object: + +```sh +# curl http://127.0.0.1:23123/r +{"role-list":["configuration","manual","user","zettel"]} +``` + +The JSON object only contains the key ''"role-list"'' with the value of a sorted string list. +Each string names one role. + +Please note that this structure will likely change in the future to be more compliant with other API calls. Index: docs/manual/00001012053400.zettel ================================================================== --- docs/manual/00001012053400.zettel +++ docs/manual/00001012053400.zettel @@ -1,11 +1,12 @@ +id: 00001012053400 title: API: Retrieve metadata and content of an existing zettel +role: manual tags: #api #manual #zettelstore syntax: zmk -role: manual -The endpoint to work with metadata and content of a specific zettel is ''/z/{ID}'', where ''{ID}'' is a placeholder for the zettel identifier (14 digits). +The [[endpoint|00001012920000]] to work with metadata and content of a specific zettel is ''/z/{ID}'', where ''{ID}'' is a placeholder for the zettel identifier (14 digits). For example, to retrieve some data about this zettel you are currently viewing, just send a HTTP GET request to the endpoint ''/z/00001012053400''[^If [[authentication is enabled|00001010040100]], you must include the a valid [[access token|00001012050200]] in the ''Authorization'' header]. If successful, the output is a JSON object: ```sh # curl http://127.0.0.1:23123/z/00001012053400 Index: docs/manual/00001012053600.zettel ================================================================== --- docs/manual/00001012053600.zettel +++ docs/manual/00001012053600.zettel @@ -1,5 +1,6 @@ +id: 00001012053600 title: API: Retrieve references of an existing zettel tags: #api #manual #zettelstore syntax: zmk role: manual ADDED docs/manual/00001012053800.zettel Index: docs/manual/00001012053800.zettel ================================================================== --- /dev/null +++ docs/manual/00001012053800.zettel @@ -0,0 +1,80 @@ +id: 00001012053800 +title: API: Retrieve context of an existing zettel +role: manual +tags: #api #manual #zettelstore +syntax: zmk + +The context of an origin zettel consists of those zettel that are somehow connected to the origin zettel. +Direct connections of an origin zettel to other zettel are visible via [[metadata values|00001006020000]], such as ''backward'', ''forward'' or other values with type [[identifier|00001006032000]] or [[set of identifier|00001006032500]]. +Zettel are also connected by using same [[tags|00001006020000#tags]]. + +The context is defined by a //direction//, a //depth//, and a /limit//: +* Direction: connections are directed. + For example, the metadata value of ''backward'' lists all zettel that link to the current zettel, while ''formward'' list all zettel to which the current zettel links. + When you are only interested in one direction, set the parameter ''dir'' either to the value ""backward"" or ""forward"". + All other values, including a missing value, is interpreted as ""both"". +* Depth: a direct connection has depth 1, an indirect connection is the length of the shortest path between two zettel. + You should limit the depth by using the parameter ''depth''. + Its default value is ""5"". + A value of ""0"" does disable any depth check. +* Limit: to set an upper bound for the returned context, you should use the parameter ''limit''. + Its default value is ""200"". + A value of ""0"" disables does not limit the number of elements returned. + +Zettel with same tags as the origin zettel are considered depth 1. +Only for the origin zettel, tags are used to calculate a connection. +Currently, only some of the newest zettel with a given tag are considered a connection.[^The number of zettel is given by the value of parameter ''depth''.] +Otherwise the context would become too big and therefore unusable. + +To retrieve the context of an existing zettel, use the [[endpoint|00001012920000]] ''/y/{ID}''. + +```` +# curl 'http://127.0.0.1:23123/y/00001012053800?limit=3&dir=forward&depth=2' +{"id": "00001012053800","url": "/z/00001012053800","meta": {...},"list": [{"id": "00001012921000","url": "/z/00001012921000","meta": {...}},{"id": "00001012920800","url": "/z/00001012920800","meta": {...}},{"id": "00010000000000","url": "/z/00010000000000","meta": {...}}]} +```` +Formatted, this translates into:[^Metadata (key ''meta'') are hidden to make the overall structure easier to read.] +````json +{ + "id": "00001012053800", + "url": "/z/00001012053800", + "meta": {...}, + "list": [ + { + "id": "00001012921000", + "url": "/z/00001012921000", + "meta": {...} + }, + { + "id": "00001012920800", + "url": "/z/00001012920800", + "meta": {...} + }, + { + "id": "00010000000000", + "url": "/z/00010000000000", + "meta": {...} + } + ] +} +```` +=== Keys +The following top-level JSON keys are returned: +; ''id'' +: The zettel identifier for which the context was requested. +; ''url'' +: The API endpoint to fetch more information about the zettel. +; ''meta'': +: The metadata of the zettel, encoded as a JSON object. +; ''list'' +: A list of JSON objects with keys ''id'', ''url'' and ''meta'' that contains the zettel of the context. + +=== HTTP Status codes +; ''200'' +: Retrieval was successful, the body contains an appropriate JSON object. +; ''400'' +: Request was not valid. +; ''403'' +: You are not allowed to retrieve data of the given zettel. +; ''404'' +: Zettel not found. + You probably used a zettel identifier that is not used in the Zettelstore. ADDED docs/manual/00001012054000.zettel Index: docs/manual/00001012054000.zettel ================================================================== --- /dev/null +++ docs/manual/00001012054000.zettel @@ -0,0 +1,82 @@ +id: 00001012054000 +title: API: Retrieve zettel order within an existing zettel +role: manual +tags: #api #manual #zettelstore +syntax: zmk + +Some zettel act as a ""table of contents"" for other zettel. +The [[Home zettel|00010000000000]] of this manual is one example, the [[general API description|00001012000000]] is another. +Every zettel with a certain internal structure can act as the ""table of contents"" for others. + +What is a ""table of contents""? +Basically, it is just a list of references to other zettel. + +To retrieve the ""table of contents"", the software looks at first level [[list items|00001007030200]]. +If an item contains a valid reference to a zettel, this reference will be interpreted as an item in the table of contents. + +This applies only to first level list items (ordered or unordered list), but not to deeper levels. +Only the first reference to a valid zettel is collected for the table of contents. +Following references to zettel within such an list item are ignored. + +To retrieve the zettel order of an existing zettel, use the [[endpoint|00001012920000]] ''/o/{ID}''. + +```` +# curl http://127.0.0.1:23123/o/00010000000000 +{"id":"00010000000000","url":"/z/00010000000000","meta":{...},"list":[{"id":"00001001000000","url":"/z/00001001000000","meta":{...}},{"id":"00001002000000","url":"/z/00001002000000","meta":{...}},{"id":"00001003000000","url":"/z/00001003000000","meta":{...}},{"id":"00001004000000","url":"/z/00001004000000","meta":{...}},...,{"id":"00001014000000","url":"/z/00001014000000","meta":{...}}]} +```` +Formatted, this translates into:[^Metadata (key ''meta'') are hidden to make the overall structure easier to read.] +````json +{ + "id": "00010000000000", + "url": "/z/00010000000000", + "order": [ + { + "id": "00001001000000", + "url": "/z/00001001000000", + "meta": {...} + }, + { + "id": "00001002000000", + "url": "/z/00001002000000", + "meta": {...} + }, + { + "id": "00001003000000", + "url": "/z/00001003000000", + "meta": {...} + }, + { + "id": "00001004000000", + "url": "/z/00001004000000", + "meta": {...} + }, + ... + { + "id": "00001014000000", + "url": "/z/00001014000000", + "meta": {...} + } + ] +} +```` +=== Kind +The following top-level JSON keys are returned: +; ''id'' +: The zettel identifier for which the references were requested. +; ''url'' +: The API endpoint to fetch more information about the zettel. +; ''meta'': +: The metadata of the zettel, encoded as a JSON object. +; ''list'' +: A list of JSON objects with keys ''id'', ''url'', and ''meta'' that describe other zettel in the defined order. + +=== HTTP Status codes +; ''200'' +: Retrieval was successful, the body contains an appropriate JSON object. +; ''400'' +: Request was not valid. +; ''403'' +: You are not allowed to retrieve data of the given zettel. +; ''404'' +: Zettel not found. + You probably used a zettel identifier that is not used in the Zettelstore. Index: docs/manual/00001012920000.zettel ================================================================== --- docs/manual/00001012920000.zettel +++ docs/manual/00001012920000.zettel @@ -1,13 +1,14 @@ +id: 00001012920000 title: Endpoints used by the API +role: manual tags: #api #manual #reference #zettelstore syntax: zmk -role: manual All API endpoints conform to the pattern ''[PREFIX]/LETTER[/ZETTEL-ID]'', where: ; ''PREFIX'' -: is an optional URL prefix, configured via the ''url-prefix'' [[startup configuration|00001004010000]], +: is an optional URL prefix, configured via the ''url-prefix'' [[start-up configuration|00001004010000]], ; ''LETTER'' : is a single letter that specifies the ressource type, ; ''ZETTEL-ID'' : is an optional 14 digits string that uniquely [[identify a zettel|00001006050000]]. @@ -15,13 +16,17 @@ |= Letter:| Without zettel identifier | With [[zettel identifier|00001006050000]] | ''a'' | POST: [[Client authentication|00001012050200]] | | | PUT: [[renew access token|00001012050400]] | | ''l'' | | GET: [[list references|00001012053600]] +| ''o'' | | GET: [[list zettel order|00001012054000]] +| ''r'' | GET: [[list roles|00001012052400]] +| ''t'' | GET: [[list tags|00001012052200]] +| ''y'' | | GET: [[list zettel context|00001012053800]] | ''z'' | GET: [[list zettel|00001012051200]] | GET: [[retrieve zettel|00001012053400]] | | POST: add new zettel | PUT: change a zettel | | | DELETE: delete the zettel The full URL will contain either the ''http'' oder ''https'' scheme, a host name, and an optional port number. The API examples will assume the ''http'' schema, the local host ''127.0.0.1'', the default port ''23123'', and the default empty ''PREFIX''. -Therefore, all URLs will start with ''http://127.0.0.1:23123''. +Therefore, all URLs in the API documentation will begin with ''http://127.0.0.1:23123''. Index: docs/manual/00001012920500.zettel ================================================================== --- docs/manual/00001012920500.zettel +++ docs/manual/00001012920500.zettel @@ -1,5 +1,6 @@ +id: 00001012920500 title: Formats available by the API tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920501.zettel ================================================================== --- docs/manual/00001012920501.zettel +++ docs/manual/00001012920501.zettel @@ -1,5 +1,6 @@ +id: 00001012920501 title: JSON Format role: manual tags: #api #manual #reference #zettelstore syntax: zmk @@ -17,14 +18,14 @@ ''"id"'' and ''"url"'' are always sent to the client. It depends on the value of the required [[zettel part|00001012920800]], whether ''"meta"'' or ''"content"'' or both are sent. For an example, take a look at the JSON encoding of this page, which is available via the ""Info"" sub-page of this zettel: -* [[../z/00001012920501?_part=id]], -* [[../z/00001012920501?_part=zettel]], -* [[../z/00001012920501?_part=meta]], -* [[../z/00001012920501?_part=content]]. +* [[//z/00001012920501?_part=id]], +* [[//z/00001012920501?_part=zettel]], +* [[//z/00001012920501?_part=meta]], +* [[//z/00001012920501?_part=content]]. If transferred via HTTP, the content type will be ''application/json''. === Metadata This ia a JSON object, that maps [[metadata keys|00001006010000]] to their values. Index: docs/manual/00001012920503.zettel ================================================================== --- docs/manual/00001012920503.zettel +++ docs/manual/00001012920503.zettel @@ -1,5 +1,6 @@ +id: 00001012920503 title: DJSON Format role: manual tags: #api #manual #reference #zettelstore syntax: zmk Index: docs/manual/00001012920510.zettel ================================================================== --- docs/manual/00001012920510.zettel +++ docs/manual/00001012920510.zettel @@ -1,5 +1,6 @@ +id: 00001012920510 title: HTML Format tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920513.zettel ================================================================== --- docs/manual/00001012920513.zettel +++ docs/manual/00001012920513.zettel @@ -1,5 +1,6 @@ +id: 00001012920513 title: Native Format tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920516.zettel ================================================================== --- docs/manual/00001012920516.zettel +++ docs/manual/00001012920516.zettel @@ -1,5 +1,6 @@ +id: 00001012920516 title: Raw Format tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920519.zettel ================================================================== --- docs/manual/00001012920519.zettel +++ docs/manual/00001012920519.zettel @@ -1,5 +1,6 @@ +id: 00001012920519 title: Text Format tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920522.zettel ================================================================== --- docs/manual/00001012920522.zettel +++ docs/manual/00001012920522.zettel @@ -1,5 +1,6 @@ +id: 00001012920522 title: Zmk Format tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012920800.zettel ================================================================== --- docs/manual/00001012920800.zettel +++ docs/manual/00001012920800.zettel @@ -1,5 +1,6 @@ +id: 00001012920800 title: Values to specify zettel parts tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001012921000.zettel ================================================================== --- docs/manual/00001012921000.zettel +++ docs/manual/00001012921000.zettel @@ -1,5 +1,6 @@ +id: 00001012921000 title: API: JSON structure of an access token tags: #api #manual #reference #zettelstore syntax: zmk role: manual Index: docs/manual/00001014000000.zettel ================================================================== --- docs/manual/00001014000000.zettel +++ docs/manual/00001014000000.zettel @@ -1,5 +1,6 @@ +id: 00001014000000 title: Web user interface tags: #manual #webui #zettelstore syntax: zmk role: manual ADDED docs/manual/00010000000000.zettel Index: docs/manual/00010000000000.zettel ================================================================== --- /dev/null +++ docs/manual/00010000000000.zettel @@ -0,0 +1,21 @@ +id: 00001000000000 +title: Zettelstore Manual +role: manual +tags: #manual #zettelstore +syntax: zmk + +* [[Introduction|00001001000000]] +* [[Design goals|00001002000000]] +* [[Installation|00001003000000]] +* [[Configuration|00001004000000]] +* [[Structure of Zettelstore|00001005000000]] +* [[Layout of a zettel|00001006000000]] +* [[Zettelmarkup|00001007000000]] +* [[Other markup languages|00001008000000]] +* [[Security|00001010000000]] +* [[API|00001012000000]] +* [[Web user interface|00001014000000]] +* Troubleshooting +* Frequently asked questions + +Licensed under the EUPL-1.2-or-later. Index: domain/id/id.go ================================================================== --- domain/id/id.go +++ domain/id/id.go @@ -11,11 +11,10 @@ // Package id provides domain specific types, constants, and functions about // zettel identifier. package id import ( - "sort" "strconv" "time" ) // Zid is the internal identifier of a zettel. Typically, it is a @@ -22,31 +21,42 @@ // time stamp of the form "YYYYMMDDHHmmSS" converted to an unsigned integer. // A zettelstore implementation should try to set the last two digits to zero, // e.g. the seconds should be zero, type Zid uint64 -// Some important ZettelIDs +// Some important ZettelIDs. +// Note: if you change some values, ensure that you also change them in the +// constant place. They are mentioned there literally, because these +// constants are not available there. const ( - Invalid = Zid(0) // Invalid is a Zid that will never be valid - ConfigurationZid = Zid(100) - BaseTemplateZid = Zid(10100) - LoginTemplateZid = Zid(10200) - ListTemplateZid = Zid(10300) - DetailTemplateZid = Zid(10401) - InfoTemplateZid = Zid(10402) - FormTemplateZid = Zid(10403) - RenameTemplateZid = Zid(10404) - DeleteTemplateZid = Zid(10405) - RolesTemplateZid = Zid(10500) - TagsTemplateZid = Zid(10600) - BaseCSSZid = Zid(20001) + Invalid = Zid(0) // Invalid is a Zid that will never be valid + ConfigurationZid = Zid(100) + + // WebUI HTML templates are in the range 10000..19999 + BaseTemplateZid = Zid(10100) + LoginTemplateZid = Zid(10200) + ListTemplateZid = Zid(10300) + DetailTemplateZid = Zid(10401) + InfoTemplateZid = Zid(10402) + FormTemplateZid = Zid(10403) + RenameTemplateZid = Zid(10404) + DeleteTemplateZid = Zid(10405) + ContextTemplateZid = Zid(10406) + RolesTemplateZid = Zid(10500) + TagsTemplateZid = Zid(10600) + + // WebUI CSS pages are in the range 20000..29999 + BaseCSSZid = Zid(20001) + + // WebUI JS pages are in the range 30000..39999 // Range 90000...99999 is reserved for zettel templates - TemplateNewZettelZid = Zid(91001) - TemplateNewUserZid = Zid(96001) + TOCNewTemplateZid = Zid(90000) + TemplateNewZettelZid = Zid(90001) + TemplateNewUserZid = Zid(90002) - WelcomeZid = Zid(19700101000000) + DefaultHomeZid = Zid(10000000000) ) const maxZid = 99999999999999 // Parse interprets a string as a zettel identification and @@ -100,16 +110,5 @@ if err != nil { panic(err) } return res } - -// Sort a slice of Zids. -func Sort(zids []Zid) { - sort.Sort(zidSlice(zids)) -} - -type zidSlice []Zid - -func (zs zidSlice) Len() int { return len(zs) } -func (zs zidSlice) Less(i, j int) bool { return zs[i] < zs[j] } -func (zs zidSlice) Swap(i, j int) { zs[i], zs[j] = zs[j], zs[i] } Index: domain/id/id_test.go ================================================================== --- domain/id/id_test.go +++ domain/id/id_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -15,13 +15,10 @@ "testing" "zettelstore.de/z/domain/id" ) -func TestParseZettelID(t *testing.T) { -} - func TestIsValid(t *testing.T) { validIDs := []string{ "00000000000001", "00000000000020", "00000000000300", ADDED domain/id/set.go Index: domain/id/set.go ================================================================== --- /dev/null +++ domain/id/set.go @@ -0,0 +1,58 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package id provides domain specific types, constants, and functions about +// zettel identifier. +package id + +// Set is a set of zettel identifier +type Set map[Zid]bool + +// NewSet returns a new set of identifier with the given initial values. +func NewSet(zids ...Zid) Set { + l := len(zids) + if l < 8 { + l = 8 + } + result := make(Set, l) + for _, zid := range zids { + result[zid] = true + } + return result +} + +// NewSetCap returns a new set of identifier with the given capacity and initial values. +func NewSetCap(c int, zids ...Zid) Set { + l := len(zids) + if c < l { + c = l + } + if c < 8 { + c = 8 + } + result := make(Set, c) + for _, zid := range zids { + result[zid] = true + } + return result +} + +// Sort returns the set as a sorted slice of zettel identifier. +func (s Set) Sort() Slice { + if l := len(s); l > 0 { + result := make(Slice, 0, l) + for zid := range s { + result = append(result, zid) + } + result.Sort() + return result + } + return nil +} ADDED domain/id/slice.go Index: domain/id/slice.go ================================================================== --- /dev/null +++ domain/id/slice.go @@ -0,0 +1,52 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package id provides domain specific types, constants, and functions about +// zettel identifier. +package id + +import ( + "sort" + "strings" +) + +// Slice is a sequence of zettel identifier. A special case is a sorted slice. +type Slice []Zid + +func (zs Slice) Len() int { return len(zs) } +func (zs Slice) Less(i, j int) bool { return zs[i] < zs[j] } +func (zs Slice) Swap(i, j int) { zs[i], zs[j] = zs[j], zs[i] } + +// Sort a slice of Zids. +func (zs Slice) Sort() { sort.Sort(zs) } + +// Copy a zettel identifier slice +func (zs Slice) Copy() Slice { + if zs == nil { + return nil + } + result := make(Slice, len(zs)) + copy(result, zs) + return result +} + +func (zs Slice) String() string { + if len(zs) == 0 { + return "" + } + var sb strings.Builder + for i, zid := range zs { + if i > 0 { + sb.WriteByte(' ') + } + sb.WriteString(zid.String()) + } + return sb.String() +} ADDED domain/id/slice_test.go Index: domain/id/slice_test.go ================================================================== --- /dev/null +++ domain/id/slice_test.go @@ -0,0 +1,57 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package id provides domain specific types, constants, and functions about +// zettel identifier. +package id_test + +import ( + "testing" + + "zettelstore.de/z/domain/id" +) + +func TestSort(t *testing.T) { + zs := id.Slice{9, 4, 6, 1, 7} + zs.Sort() + if zs[0] != 1 || zs[1] != 4 || zs[2] != 6 || zs[3] != 7 || zs[4] != 9 { + t.Errorf("Slice.Sort did not work. Expected %v, got %v", id.Slice{1, 4, 6, 7, 9}, zs) + } +} + +func TestCopy(t *testing.T) { + var orig id.Slice + got := orig.Copy() + if got != nil { + t.Errorf("Nil copy resulted in %v", got) + } + orig = id.Slice{9, 4, 6, 1, 7} + got = orig.Copy() + if len(got) != len(orig) || got[0] != 9 || got[1] != 4 || got[2] != 6 || got[3] != 1 || got[4] != 7 { + t.Errorf("Slice.Copy did not work. Expected %v, got %v", orig, got) + } +} +func TestString(t *testing.T) { + testcases := []struct { + in id.Slice + exp string + }{ + {nil, ""}, + {id.Slice{}, ""}, + {id.Slice{1}, "00000000000001"}, + {id.Slice{1, 2}, "00000000000001 00000000000002"}, + } + for i, tc := range testcases { + got := tc.in.String() + if got != tc.exp { + t.Errorf("%d/%v: expected %q, but got %q", i, tc.in, tc.exp, got) + } + } +} Index: domain/meta/meta.go ================================================================== --- domain/meta/meta.go +++ domain/meta/meta.go @@ -73,10 +73,18 @@ if kd, ok := registeredKeys[name]; ok { return kd.IsComputed() } return false } + +// Inverse returns the name of the inverse key. +func Inverse(name string) string { + if kd, ok := registeredKeys[name]; ok { + return kd.Inverse + } + return "" +} // GetDescription returns the key description object of the given key name. func GetDescription(name string) DescriptionKey { if d, ok := registeredKeys[name]; ok { return *d @@ -120,21 +128,20 @@ KeyDuplicates = registerKey("duplicates", TypeBool, usageUser, "") KeyExpertMode = registerKey("expert-mode", TypeBool, usageUser, "") KeyFolge = registerKey("folge", TypeIDSet, usageProperty, "") KeyFooterHTML = registerKey("footer-html", TypeString, usageUser, "") KeyForward = registerKey("forward", TypeIDSet, usageProperty, "") + KeyHomeZettel = registerKey("home-zettel", TypeID, usageUser, "") KeyLang = registerKey("lang", TypeWord, usageUser, "") KeyLicense = registerKey("license", TypeEmpty, usageUser, "") KeyListPageSize = registerKey("list-page-size", TypeNumber, usageUser, "") - KeyNewRole = registerKey("new-role", TypeWord, usageUser, "") KeyMarkerExternal = registerKey("marker-external", TypeEmpty, usageUser, "") KeyModified = registerKey("modified", TypeTimestamp, usageComputed, "") KeyPrecursor = registerKey("precursor", TypeIDSet, usageUser, KeyFolge) KeyPublished = registerKey("published", TypeTimestamp, usageProperty, "") KeyReadOnly = registerKey("read-only", TypeWord, usageUser, "") KeySiteName = registerKey("site-name", TypeString, usageUser, "") - KeyStart = registerKey("start", TypeID, usageUser, "") KeyURL = registerKey("url", TypeURL, usageUser, "") KeyUserID = registerKey("user-id", TypeWord, usageUser, "") KeyUserRole = registerKey("user-role", TypeWord, usageUser, "") KeyVisibility = registerKey("visibility", TypeWord, usageUser, "") KeyYAMLHeader = registerKey("yaml-header", TypeBool, usageUser, "") @@ -143,11 +150,10 @@ // Important values for some keys. const ( ValueRoleConfiguration = "configuration" ValueRoleUser = "user" - ValueRoleNewTemplate = "new-template" ValueRoleZettel = "zettel" ValueSyntaxNone = "none" ValueSyntaxZmk = "zmk" ValueTrue = "true" ValueFalse = "false" @@ -235,11 +241,11 @@ return value, ok } // GetDefault retrieves the string value of the given key. If no value was // stored, the given default value is returned. -func (m *Meta) GetDefault(key string, def string) string { +func (m *Meta) GetDefault(key, def string) string { if value, ok := m.Get(key); ok { return value } return def } @@ -255,11 +261,11 @@ // predefined keys. The pairs are ordered by key. func (m *Meta) PairsRest(allowComputed bool) []Pair { return m.doPairs(false, allowComputed) } -func (m *Meta) doPairs(first bool, allowComputed bool) []Pair { +func (m *Meta) doPairs(first, allowComputed bool) []Pair { result := make([]Pair, 0, len(m.pairs)) if first { for _, key := range firstKeys { if value, ok := m.pairs[key]; ok { result = append(result, Pair{key, value}) Index: domain/meta/parse.go ================================================================== --- domain/meta/parse.go +++ domain/meta/parse.go @@ -179,11 +179,9 @@ }) case TypeTimestamp: if _, ok := TimeValue(v); ok { m.Set(key, v) } - case TypeEmpty: - fallthrough default: addData(m, key, v) } } Index: domain/meta/type.go ================================================================== --- domain/meta/type.go +++ domain/meta/type.go @@ -10,10 +10,11 @@ // Package meta provides the domain specific type 'meta'. package meta import ( + "strconv" "strings" "time" ) // DescriptionType is a description of a specific key type. @@ -76,10 +77,18 @@ values[i] = trimValue(val) } m.pairs[key] = strings.Join(values, " ") } } + +// CleanTag removes the number charachter ('#') from a tag value. +func CleanTag(tag string) string { + if len(tag) > 1 && tag[0] == '#' { + return tag[1:] + } + return tag +} // SetNow stores the current timestamp under the given key. func (m *Meta) SetNow(key string) { m.Set(key, time.Now().Format("20060102150405")) } @@ -131,14 +140,37 @@ if !ok { return nil, false } return ListFromValue(value), true } + +// GetTags returns the list of tags as a string list. Each tag does not begin +// with the '#' character, in contrast to `GetList`. +func (m *Meta) GetTags(key string) ([]string, bool) { + tags, ok := m.GetList(key) + if !ok { + return nil, false + } + for i, tag := range tags { + tags[i] = CleanTag(tag) + } + return tags, len(tags) > 0 +} // GetListOrNil retrieves the string list value of a given key. If there was // nothing stores, a nil list is returned. func (m *Meta) GetListOrNil(key string) []string { if value, ok := m.GetList(key); ok { return value } return nil } + +// GetNumber retrieves the numeric value of a given key. +func (m *Meta) GetNumber(key string) (int, bool) { + if value, ok := m.Get(key); ok { + if num, err := strconv.Atoi(value); err == nil { + return num, true + } + } + return 0, false +} Index: encoder/encoder.go ================================================================== --- encoder/encoder.go +++ encoder/encoder.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -33,15 +33,15 @@ WriteInlines(io.Writer, ast.InlineSlice) (int, error) } // Some errors to signal when encoder methods are not implemented. var ( - ErrNoWriteZettel = errors.New("Method WriteZettel is not implemented") - ErrNoWriteMeta = errors.New("Method WriteMeta is not implemented") - ErrNoWriteContent = errors.New("Method WriteContent is not implemented") - ErrNoWriteBlocks = errors.New("Method WriteBlocks is not implemented") - ErrNoWriteInlines = errors.New("Method WriteInlines is not implemented") + ErrNoWriteZettel = errors.New("method WriteZettel is not implemented") + ErrNoWriteMeta = errors.New("method WriteMeta is not implemented") + ErrNoWriteContent = errors.New("method WriteContent is not implemented") + ErrNoWriteBlocks = errors.New("method WriteBlocks is not implemented") + ErrNoWriteInlines = errors.New("method WriteInlines is not implemented") ) // Option allows to configure an encoder type Option interface { Name() string Index: encoder/htmlenc/block.go ================================================================== --- encoder/htmlenc/block.go +++ encoder/htmlenc/block.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -21,11 +21,11 @@ // VisitPara emits HTML code for a paragraph:

...

func (v *visitor) VisitPara(pn *ast.ParaNode) { v.b.WriteString("

") v.acceptInlineSlice(pn.Inlines) - v.b.WriteString("

\n") + v.writeEndPara() } // VisitVerbatim emits HTML code for verbatim lines. func (v *visitor) VisitVerbatim(vn *ast.VerbatimNode) { switch vn.Code { @@ -194,18 +194,18 @@ inPara = true } v.acceptInlineSlice(pn.Inlines) } else { if inPara { - v.b.WriteString("

\n") + v.writeEndPara() inPara = false } v.acceptItemSlice(item) } } if inPara { - v.b.WriteString("

\n") + v.writeEndPara() } v.b.WriteString("\n") } func getParaItem(its ast.ItemSlice) *ast.ParaNode { @@ -335,5 +335,9 @@ v.b.WriteString("\">\n") default: v.b.WriteStrings("

Unable to display BLOB with syntax '", bn.Syntax, "'.

\n") } } + +func (v *visitor) writeEndPara() { + v.b.WriteString("

\n") +} Index: encoder/htmlenc/htmlenc.go ================================================================== --- encoder/htmlenc/htmlenc.go +++ encoder/htmlenc/htmlenc.go @@ -54,12 +54,11 @@ he.newWindow = opt.Value case "xhtml": he.xhtml = opt.Value } case *encoder.StringsOption: - switch opt.Key { - case "no-meta": + if opt.Key == "no-meta" { he.ignoreMeta = make(map[string]bool, len(opt.Value)) for _, v := range opt.Value { he.ignoreMeta[v] = true } } Index: encoder/htmlenc/inline.go ================================================================== --- encoder/htmlenc/inline.go +++ encoder/htmlenc/inline.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -25,11 +25,11 @@ } // VisitTag writes tag content. func (v *visitor) VisitTag(tn *ast.TagNode) { // TODO: erst mal als span. Link wäre gut, muss man vermutlich via Callback lösen. - v.b.WriteString("") + v.b.WriteString("#") v.writeHTMLEscaped(tn.Tag) v.b.WriteString("") } // VisitSpace emits a white space. @@ -66,13 +66,13 @@ } v.lang.push(ln.Attrs) defer v.lang.pop() switch ln.Ref.State { - case ast.RefStateZettelSelf, ast.RefStateZettelFound, ast.RefStateLocal: + case ast.RefStateSelf, ast.RefStateFound, ast.RefStateHosted, ast.RefStateBased: v.writeAHref(ln.Ref, ln.Attrs, ln.Inlines) - case ast.RefStateZettelBroken: + case ast.RefStateBroken: attrs := ln.Attrs.Clone() attrs = attrs.Set("class", "zs-broken") attrs = attrs.Set("title", "Zettel not found") // l10n v.writeAHref(ln.Ref, attrs, ln.Inlines) case ast.RefStateExternal: Index: encoder/htmlenc/visitor.go ================================================================== --- encoder/htmlenc/visitor.go +++ encoder/htmlenc/visitor.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -47,35 +47,41 @@ meta.KeyLicense: "license", } func (v *visitor) acceptMeta(m *meta.Meta, withTitle bool) { for i, pair := range m.Pairs(true) { + if v.enc.ignoreMeta[pair.Key] { + continue + } if i == 0 { // "title" is number 0... - if withTitle && !v.enc.ignoreMeta[pair.Key] { + if withTitle { + // TODO: title value may contain zmk elements v.b.WriteStrings("") } continue } - if !v.enc.ignoreMeta[pair.Key] { - if pair.Key == meta.KeyTags { - v.b.WriteString("\n 0 { - v.b.WriteString(", ") - } - v.writeQuotedEscaped(strings.TrimPrefix(val, "#")) - } - v.b.WriteString("\">") - } else if key, ok := mapMetaKey[pair.Key]; ok { - v.writeMeta("", key, pair.Value) - } else { - v.writeMeta("zs-", pair.Key, pair.Value) - } - } - } + if pair.Key == meta.KeyTags { + v.writeTags(pair.Value) + } else if key, ok := mapMetaKey[pair.Key]; ok { + v.writeMeta("", key, pair.Value) + } else { + v.writeMeta("zs-", pair.Key, pair.Value) + } + } +} + +func (v *visitor) writeTags(tags string) { + v.b.WriteString("\n 0 { + v.b.WriteString(", ") + } + v.writeQuotedEscaped(strings.TrimPrefix(val, "#")) + } + v.b.WriteString("\">") } func (v *visitor) writeMeta(prefix, key, value string) { v.b.WriteStrings("\n 0 { - v.b.WriteByte(',') - } - v.b.WriteByte('"') - v.b.Write(Escape(val)) - v.b.WriteByte('"') - } - v.b.WriteByte(']') + v.writeSetValue(p.Value) } else { v.b.WriteByte('"') v.b.Write(Escape(p.Value)) v.b.WriteByte('"') } } } + +func (v *detailVisitor) writeSetValue(value string) { + v.b.WriteByte('[') + for i, val := range meta.ListFromValue(value) { + if i > 0 { + v.b.WriteByte(',') + } + v.b.WriteByte('"') + v.b.Write(Escape(val)) + v.b.WriteByte('"') + } + v.b.WriteByte(']') +} Index: encoder/jsonenc/jsonenc.go ================================================================== --- encoder/jsonenc/jsonenc.go +++ encoder/jsonenc/jsonenc.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -29,11 +29,11 @@ // jsonEncoder is just a stub. It is not implemented. The real implementation // is in file web/adapter/json.go type jsonEncoder struct{} -// SetOption sets an option for the encoder +// SetOption does nothing because this encoder does not recognize any option. func (je *jsonEncoder) SetOption(option encoder.Option) {} // WriteZettel writes the encoded zettel to the writer. func (je *jsonEncoder) WriteZettel( w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) { Index: encoder/nativeenc/nativeenc.go ================================================================== --- encoder/nativeenc/nativeenc.go +++ encoder/nativeenc/nativeenc.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -133,17 +133,17 @@ v.level-- v.b.WriteByte(']') } } -func (v *visitor) writeMetaString(m *meta.Meta, key string, native string) { +func (v *visitor) writeMetaString(m *meta.Meta, key, native string) { if val, ok := m.Get(key); ok && len(val) > 0 { v.b.WriteStrings("\n[", native, " \"", val, "\"]") } } -func (v *visitor) writeMetaList(m *meta.Meta, key string, native string) { +func (v *visitor) writeMetaList(m *meta.Meta, key, native string) { if vals, ok := m.GetList(key); ok && len(vals) > 0 { v.b.WriteStrings("\n[", native) for _, val := range vals { v.b.WriteByte(' ') v.b.WriteString(val) @@ -380,17 +380,18 @@ v.b.WriteString("Space") } } var mapRefState = map[ast.RefState]string{ - ast.RefStateInvalid: "INVALID", - ast.RefStateZettel: "ZETTEL", - ast.RefStateZettelSelf: "SELF", - ast.RefStateZettelFound: "ZETTEL", - ast.RefStateZettelBroken: "BROKEN", - ast.RefStateLocal: "LOCAL", - ast.RefStateExternal: "EXTERNAL", + ast.RefStateInvalid: "INVALID", + ast.RefStateZettel: "ZETTEL", + ast.RefStateSelf: "SELF", + ast.RefStateFound: "ZETTEL", + ast.RefStateBroken: "BROKEN", + ast.RefStateHosted: "LOCAL", + ast.RefStateBased: "BASED", + ast.RefStateExternal: "EXTERNAL", } // VisitLink writes native code for links. func (v *visitor) VisitLink(ln *ast.LinkNode) { if adapt := v.enc.adaptLink; adapt != nil { Index: encoder/rawenc/rawenc.go ================================================================== --- encoder/rawenc/rawenc.go +++ encoder/rawenc/rawenc.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -25,11 +25,11 @@ }) } type rawEncoder struct{} -// SetOption sets an option for the encoder +// SetOption does nothing because this encoder does not recognize any option. func (re *rawEncoder) SetOption(option encoder.Option) {} // WriteZettel writes the encoded zettel to the writer. func (re *rawEncoder) WriteZettel( w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) { Index: encoder/textenc/textenc.go ================================================================== --- encoder/textenc/textenc.go +++ encoder/textenc/textenc.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -25,11 +25,11 @@ }) } type textEncoder struct{} -// SetOption sets an option for this encoder +// SetOption does nothing because this encoder does not recognize any option. func (te *textEncoder) SetOption(option encoder.Option) {} // WriteZettel does nothing. func (te *textEncoder) WriteZettel( w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) { Index: encoder/zmkenc/zmkenc.go ================================================================== --- encoder/zmkenc/zmkenc.go +++ encoder/zmkenc/zmkenc.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -27,11 +27,11 @@ }) } type zmkEncoder struct{} -// SetOption sets an option for this encoder. +// SetOption does nothing because this encoder does not recognize any option. func (ze *zmkEncoder) SetOption(option encoder.Option) {} // WriteZettel writes the encoded zettel to the writer. func (ze *zmkEncoder) WriteZettel( w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) { Index: go.mod ================================================================== --- go.mod +++ go.mod @@ -3,9 +3,9 @@ go 1.15 require ( github.com/fsnotify/fsnotify v1.4.9 github.com/pascaldekloe/jwt v1.10.0 - github.com/yuin/goldmark v1.3.0 + github.com/yuin/goldmark v1.3.2 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/text v0.3.0 ) Index: go.sum ================================================================== --- go.sum +++ go.sum @@ -1,11 +1,11 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/pascaldekloe/jwt v1.10.0 h1:ktcIUV4TPvh404R5dIBEnPCsSwj0sqi3/0+XafE5gJs= github.com/pascaldekloe/jwt v1.10.0/go.mod h1:TKhllgThT7TOP5rGr2zMLKEDZRAgJfBbtKyVeRsNB9A= -github.com/yuin/goldmark v1.3.0 h1:DRvEHivhJ1fQhZbpmttnonfC674RycyZGE/5IJzDKgg= -github.com/yuin/goldmark v1.3.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.3.2 h1:YjHC5TgyMmHpicTgEqDN0Q96Xo8K6tLXPnmNOHXCgs0= +github.com/yuin/goldmark v1.3.2/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= Index: index/index.go ================================================================== --- index/index.go +++ index/index.go @@ -47,11 +47,11 @@ } // Port contains all the used functions to access zettel to be indexed. type Port interface { RegisterObserver(func(place.ChangeInfo)) - FetchZids(context.Context) (map[id.Zid]bool, error) + FetchZids(context.Context) (id.Set, error) GetMeta(context.Context, id.Zid) (*meta.Meta, error) GetZettel(context.Context, id.Zid) (domain.Zettel, error) } // Indexer contains all the functions of an index. @@ -89,19 +89,21 @@ // memory-based, file-based, based on SQLite, ... type Store interface { Enricher // UpdateReferences for a specific zettel. - UpdateReferences(context.Context, *ZettelIndex) + // Returns set of zettel identifier that must also be checked for changes. + UpdateReferences(context.Context, *ZettelIndex) id.Set // DeleteZettel removes index data for given zettel. - DeleteZettel(context.Context, id.Zid) + // Returns set of zettel identifier that must also be checked for changes. + DeleteZettel(context.Context, id.Zid) id.Set // ReadStats populates st with store statistics. ReadStats(st *StoreStats) - // Write the content to a Writer + // Write the content to a Writer. Write(io.Writer) } // StoreStats records statistics about the store. type StoreStats struct { Index: index/indexer/anteroom.go ================================================================== --- index/indexer/anteroom.go +++ index/indexer/anteroom.go @@ -14,14 +14,23 @@ import ( "sync" "zettelstore.de/z/domain/id" ) + +type arAction int + +const ( + arNothing arAction = iota + arReload + arUpdate + arDelete +) type anteroom struct { next *anteroom - waiting map[id.Zid]bool + waiting map[id.Zid]arAction curLoad int reload bool } type anterooms struct { @@ -33,124 +42,146 @@ func newAnterooms(maxLoad int) *anterooms { return &anterooms{maxLoad: maxLoad} } -func (ar *anterooms) Enqueue(zid id.Zid, val bool) { - if !zid.IsValid() { +func (ar *anterooms) Enqueue(zid id.Zid, action arAction) { + if !zid.IsValid() || action == arNothing || action == arReload { return } ar.mx.Lock() defer ar.mx.Unlock() if ar.first == nil { - ar.first = ar.makeAnteroom(zid, val) + ar.first = ar.makeAnteroom(zid, action) ar.last = ar.first return } for room := ar.first; room != nil; room = room.next { if room.reload { continue // Do not place zettel in reload room } - if v, ok := room.waiting[zid]; ok { - if val == v { - return - } - room.waiting[zid] = val + a, ok := room.waiting[zid] + if !ok { + continue + } + switch action { + case a: return + case arUpdate: + room.waiting[zid] = action + case arDelete: + room.waiting[zid] = action } + return } if room := ar.last; !room.reload && (ar.maxLoad == 0 || room.curLoad < ar.maxLoad) { - room.waiting[zid] = val + room.waiting[zid] = action room.curLoad++ return } - room := ar.makeAnteroom(zid, val) + room := ar.makeAnteroom(zid, action) ar.last.next = room ar.last = room } -func (ar *anterooms) makeAnteroom(zid id.Zid, val bool) *anteroom { - cap := ar.maxLoad - if cap == 0 { - cap = 100 +func (ar *anterooms) makeAnteroom(zid id.Zid, action arAction) *anteroom { + c := ar.maxLoad + if c == 0 { + c = 100 } - waiting := make(map[id.Zid]bool, cap) - waiting[zid] = val + waiting := make(map[id.Zid]arAction, c) + waiting[zid] = action return &anteroom{next: nil, waiting: waiting, curLoad: 1, reload: false} } func (ar *anterooms) Reset() { ar.mx.Lock() defer ar.mx.Unlock() - ar.first = ar.makeAnteroom(id.Invalid, true) + ar.first = ar.makeAnteroom(id.Invalid, arReload) ar.last = ar.first } -func (ar *anterooms) Reload(delZids []id.Zid, newZids map[id.Zid]bool) { +func (ar *anterooms) Reload(delZids id.Slice, newZids id.Set) { ar.mx.Lock() defer ar.mx.Unlock() - delWaiting := make(map[id.Zid]bool, len(delZids)) - for _, zid := range delZids { - if zid.IsValid() { - delWaiting[zid] = false - } - } - newWaiting := make(map[id.Zid]bool, len(newZids)) - for zid := range newZids { - if zid.IsValid() { - newWaiting[zid] = true - } - } - - // Delete previous reload rooms - room := ar.first - for ; room != nil && room.reload; room = room.next { - } - ar.first = room - if room == nil { - ar.last = nil - } + delWaiting := createWaitingSlice(delZids, arDelete) + newWaiting := createWaitingSet(newZids, arUpdate) + ar.deleteReloadedRooms() if ds := len(delWaiting); ds > 0 { if ns := len(newWaiting); ns > 0 { roomNew := &anteroom{next: ar.first, waiting: newWaiting, curLoad: ns, reload: true} ar.first = &anteroom{next: roomNew, waiting: delWaiting, curLoad: ds, reload: true} if roomNew.next == nil { ar.last = roomNew } - } else { - ar.first = &anteroom{next: ar.first, waiting: delWaiting, curLoad: ds} - if ar.first.next == nil { - ar.last = ar.first - } - } - } else { - if ns := len(newWaiting); ns > 0 { - ar.first = &anteroom{next: ar.first, waiting: newWaiting, curLoad: ns} - if ar.first.next == nil { - ar.last = ar.first - } - } else { - ar.first = nil - ar.last = nil - } - } -} - -func (ar *anterooms) Dequeue() (id.Zid, bool) { - ar.mx.Lock() - defer ar.mx.Unlock() - if ar.first == nil { - return id.Invalid, false - } - for zid, val := range ar.first.waiting { + return + } + + ar.first = &anteroom{next: ar.first, waiting: delWaiting, curLoad: ds} + if ar.first.next == nil { + ar.last = ar.first + } + return + } + + if ns := len(newWaiting); ns > 0 { + ar.first = &anteroom{next: ar.first, waiting: newWaiting, curLoad: ns} + if ar.first.next == nil { + ar.last = ar.first + } + return + } + + ar.first = nil + ar.last = nil +} + +func createWaitingSlice(zids id.Slice, action arAction) map[id.Zid]arAction { + waitingSet := make(map[id.Zid]arAction, len(zids)) + for _, zid := range zids { + if zid.IsValid() { + waitingSet[zid] = action + } + } + return waitingSet +} + +func createWaitingSet(zids id.Set, action arAction) map[id.Zid]arAction { + waitingSet := make(map[id.Zid]arAction, len(zids)) + for zid := range zids { + if zid.IsValid() { + waitingSet[zid] = action + } + } + return waitingSet +} + +func (ar *anterooms) deleteReloadedRooms() { + room := ar.first + for room != nil && room.reload { + room = room.next + } + ar.first = room + if room == nil { + ar.last = nil + } +} + +func (ar *anterooms) Dequeue() (arAction, id.Zid) { + ar.mx.Lock() + defer ar.mx.Unlock() + if ar.first == nil { + return arNothing, id.Invalid + } + for zid, action := range ar.first.waiting { delete(ar.first.waiting, zid) if len(ar.first.waiting) == 0 { ar.first = ar.first.next if ar.first == nil { ar.last = nil } } - return zid, val + return action, zid } - return id.Invalid, false + return arNothing, id.Invalid } Index: index/indexer/anteroom_test.go ================================================================== --- index/indexer/anteroom_test.go +++ index/indexer/anteroom_test.go @@ -17,33 +17,33 @@ "zettelstore.de/z/domain/id" ) func TestSimple(t *testing.T) { ar := newAnterooms(2) - ar.Enqueue(id.Zid(1), true) - zid, val := ar.Dequeue() - if zid != id.Zid(1) || val != true { - t.Errorf("Expected 1/true, but got %v/%v", zid, val) + ar.Enqueue(id.Zid(1), arUpdate) + action, zid := ar.Dequeue() + if zid != id.Zid(1) || action != arUpdate { + t.Errorf("Expected 1/arUpdate, but got %v/%v", zid, action) } - zid, val = ar.Dequeue() - if zid != id.Invalid && val != false { + action, zid = ar.Dequeue() + if zid != id.Invalid && action != arDelete { t.Errorf("Expected invalid Zid, but got %v", zid) } - ar.Enqueue(id.Zid(1), true) - ar.Enqueue(id.Zid(2), true) + ar.Enqueue(id.Zid(1), arUpdate) + ar.Enqueue(id.Zid(2), arUpdate) if ar.first != ar.last { t.Errorf("Expected one room, but got more") } - ar.Enqueue(id.Zid(3), true) + ar.Enqueue(id.Zid(3), arUpdate) if ar.first == ar.last { t.Errorf("Expected more than one room, but got only one") } count := 0 - for ; ; count++ { - zid, val := ar.Dequeue() - if zid == id.Invalid && val == false { + for ; count < 1000; count++ { + action, _ := ar.Dequeue() + if action == arNothing { break } } if count != 3 { t.Errorf("Expected 3 dequeues, but got %v", count) @@ -50,73 +50,73 @@ } } func TestReset(t *testing.T) { ar := newAnterooms(1) - ar.Enqueue(id.Zid(1), true) + ar.Enqueue(id.Zid(1), arUpdate) ar.Reset() - zid, val := ar.Dequeue() - if zid != id.Invalid && val != true { - t.Errorf("Expected invalid Zid, but got %v/%v", zid, val) - } - ar.Reload([]id.Zid{id.Zid(2)}, map[id.Zid]bool{id.Zid(3): true, id.Zid(4): false}) - ar.Enqueue(id.Zid(5), true) - ar.Enqueue(id.Zid(5), false) - ar.Enqueue(id.Zid(5), false) - ar.Enqueue(id.Zid(5), true) + action, zid := ar.Dequeue() + if action != arReload || zid != id.Invalid { + t.Errorf("Expected reload & invalid Zid, but got %v/%v", action, zid) + } + ar.Reload(id.Slice{2}, id.NewSet(3, 4)) + ar.Enqueue(id.Zid(5), arUpdate) + ar.Enqueue(id.Zid(5), arDelete) + ar.Enqueue(id.Zid(5), arDelete) + ar.Enqueue(id.Zid(5), arUpdate) if ar.first == ar.last || ar.first.next == ar.last || ar.first.next.next != ar.last { t.Errorf("Expected 3 rooms") } - zid, val = ar.Dequeue() - if zid != id.Zid(2) || val != false { - t.Errorf("Expected 2/false, but got %v/%v", zid, val) - } - zid1, val := ar.Dequeue() - if val != true { - t.Errorf("Expected true, but got %v", val) - } - zid2, val := ar.Dequeue() - if val != true { - t.Errorf("Expected true, but got %v", val) + action, zid = ar.Dequeue() + if zid != id.Zid(2) || action != arDelete { + t.Errorf("Expected 2/arDelete, but got %v/%v", zid, action) + } + action, zid1 := ar.Dequeue() + if action != arUpdate { + t.Errorf("Expected arUpdate, but got %v", action) + } + action, zid2 := ar.Dequeue() + if action != arUpdate { + t.Errorf("Expected arUpdate, but got %v", action) } if !(zid1 == id.Zid(3) && zid2 == id.Zid(4) || zid1 == id.Zid(4) && zid2 == id.Zid(3)) { t.Errorf("Zids must be 3 or 4, but got %v/%v", zid1, zid2) } - zid, val = ar.Dequeue() - if zid != id.Zid(5) || val != true { - t.Errorf("Expected 5/true, but got %v/%v", zid, val) - } - zid, val = ar.Dequeue() - if zid != id.Invalid && val != false { - t.Errorf("Expected invalid Zid, but got %v", zid) - } - - ar = newAnterooms(1) - ar.Reload(nil, map[id.Zid]bool{id.Zid(6): true}) - zid, val = ar.Dequeue() - if zid != id.Zid(6) || val != true { - t.Errorf("Expected 6/true, but got %v/%v", zid, val) - } - zid, val = ar.Dequeue() - if zid != id.Invalid && val != false { - t.Errorf("Expected invalid Zid, but got %v", zid) - } - - ar = newAnterooms(1) - ar.Reload([]id.Zid{id.Zid(7)}, nil) - zid, val = ar.Dequeue() - if zid != id.Zid(7) || val != false { - t.Errorf("Expected 7/false, but got %v/%v", zid, val) - } - zid, val = ar.Dequeue() - if zid != id.Invalid && val != false { - t.Errorf("Expected invalid Zid, but got %v", zid) - } - - ar = newAnterooms(1) - ar.Enqueue(id.Zid(8), true) - ar.Reload(nil, nil) - zid, val = ar.Dequeue() - if zid != id.Invalid && val != false { - t.Errorf("Expected invalid Zid, but got %v", zid) + action, zid = ar.Dequeue() + if zid != id.Zid(5) || action != arUpdate { + t.Errorf("Expected 5/arUpdate, but got %v/%v", zid, action) + } + action, zid = ar.Dequeue() + if action != arNothing || zid != id.Invalid { + t.Errorf("Expected nothing & invalid Zid, but got %v/%v", action, zid) + } + + ar = newAnterooms(1) + ar.Reload(nil, id.NewSet(id.Zid(6))) + action, zid = ar.Dequeue() + if zid != id.Zid(6) || action != arUpdate { + t.Errorf("Expected 6/arUpdate, but got %v/%v", zid, action) + } + action, zid = ar.Dequeue() + if action != arNothing || zid != id.Invalid { + t.Errorf("Expected nothing & invalid Zid, but got %v/%v", action, zid) + } + + ar = newAnterooms(1) + ar.Reload(id.Slice{7}, nil) + action, zid = ar.Dequeue() + if zid != id.Zid(7) || action != arDelete { + t.Errorf("Expected 7/arDelete, but got %v/%v", zid, action) + } + action, zid = ar.Dequeue() + if action != arNothing || zid != id.Invalid { + t.Errorf("Expected nothing & invalid Zid, but got %v/%v", action, zid) + } + + ar = newAnterooms(1) + ar.Enqueue(id.Zid(8), arUpdate) + ar.Reload(nil, nil) + action, zid = ar.Dequeue() + if action != arNothing || zid != id.Invalid { + t.Errorf("Expected nothing & invalid Zid, but got %v/%v", action, zid) } } Index: index/indexer/indexer.go ================================================================== --- index/indexer/indexer.go +++ index/indexer/indexer.go @@ -54,13 +54,13 @@ func (idx *indexer) observer(ci place.ChangeInfo) { switch ci.Reason { case place.OnReload: idx.ar.Reset() case place.OnUpdate: - idx.ar.Enqueue(ci.Zid, true) + idx.ar.Enqueue(ci.Zid, arUpdate) case place.OnDelete: - idx.ar.Enqueue(ci.Zid, false) + idx.ar.Enqueue(ci.Zid, arDelete) default: return } select { case idx.ready <- struct{}{}: @@ -109,15 +109,16 @@ idx.store.ReadStats(&st.Store) } type indexerPort interface { getMetaPort - FetchZids(ctx context.Context) (map[id.Zid]bool, error) + FetchZids(ctx context.Context) (id.Set, error) GetZettel(ctx context.Context, zid id.Zid) (domain.Zettel, error) } // indexer runs in the background and updates the index data structures. +// This is the main service of the indexer. func (idx *indexer) indexer(p indexerPort) { // Something may panic. Ensure a running indexer. defer func() { if err := recover(); err != nil { go idx.indexer(p) @@ -127,69 +128,77 @@ timerDuration := 15 * time.Second timer := time.NewTimer(timerDuration) ctx := index.NoEnrichContext(context.Background()) for { start := time.Now() - changed := false - for { - zid, val := idx.ar.Dequeue() - if zid.IsValid() { - changed = true - idx.mx.Lock() - idx.sinceReload++ - idx.mx.Unlock() - if !val { - idx.deleteZettel(zid) - continue - } - - zettel, err := p.GetZettel(ctx, zid) - if err != nil { - // TODO: on some errors put the zid into a "try later" set - continue - } - idx.updateZettel(ctx, zettel, p) - continue - } - - if val == false { - break - } + if idx.workService(ctx, p) { + idx.mx.Lock() + idx.durLastIndex = time.Since(start) + idx.mx.Unlock() + } + if !idx.sleepService(timer, timerDuration) { + return + } + } +} + +func (idx *indexer) workService(ctx context.Context, p indexerPort) bool { + changed := false + for { + switch action, zid := idx.ar.Dequeue(); action { + case arNothing: + return changed + case arReload: zids, err := p.FetchZids(ctx) if err == nil { idx.ar.Reload(nil, zids) idx.mx.Lock() idx.lastReload = time.Now() idx.sinceReload = 0 idx.mx.Unlock() } - } - if changed { - idx.mx.Lock() - idx.durLastIndex = time.Now().Sub(start) - idx.mx.Unlock() - } - - select { - case _, ok := <-idx.ready: - if !ok { - return - } - case _, ok := <-timer.C: - if !ok { - return - } - timer.Reset(timerDuration) - case _, ok := <-idx.done: - if !ok { - if !timer.Stop() { - <-timer.C - } - return - } - } - } + case arUpdate: + changed = true + idx.mx.Lock() + idx.sinceReload++ + idx.mx.Unlock() + zettel, err := p.GetZettel(ctx, zid) + if err != nil { + // TODO: on some errors put the zid into a "try later" set + continue + } + idx.updateZettel(ctx, zettel, p) + case arDelete: + changed = true + idx.mx.Lock() + idx.sinceReload++ + idx.mx.Unlock() + idx.deleteZettel(zid) + } + } +} + +func (idx *indexer) sleepService(timer *time.Timer, timerDuration time.Duration) bool { + select { + case _, ok := <-idx.ready: + if !ok { + return false + } + case _, ok := <-timer.C: + if !ok { + return false + } + timer.Reset(timerDuration) + case _, ok := <-idx.done: + if !ok { + if !timer.Stop() { + <-timer.C + } + return false + } + } + return true } type getMetaPort interface { GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) } @@ -213,15 +222,15 @@ } zn := parser.ParseZettel(zettel, "") refs := collect.References(zn) updateReferences(ctx, refs.Links, p, zi) updateReferences(ctx, refs.Images, p, zi) - idx.store.UpdateReferences(ctx, zi) + toCheck := idx.store.UpdateReferences(ctx, zi) + idx.checkZettel(toCheck) } -func updateValue( - ctx context.Context, inverse string, value string, p getMetaPort, zi *index.ZettelIndex) { +func updateValue(ctx context.Context, inverse string, value string, p getMetaPort, zi *index.ZettelIndex) { zid, err := id.Parse(value) if err != nil { return } if _, err := p.GetMeta(ctx, zid); err != nil { @@ -233,20 +242,18 @@ return } zi.AddMetaRef(inverse, zid) } -func updateReferences( - ctx context.Context, refs []*ast.Reference, p getMetaPort, zi *index.ZettelIndex) { +func updateReferences(ctx context.Context, refs []*ast.Reference, p getMetaPort, zi *index.ZettelIndex) { zrefs, _, _ := collect.DivideReferences(refs, false) for _, ref := range zrefs { - updateReference(ctx, ref.Value, p, zi) + updateReference(ctx, ref.URL.Path, p, zi) } } -func updateReference( - ctx context.Context, value string, p getMetaPort, zi *index.ZettelIndex) { +func updateReference(ctx context.Context, value string, p getMetaPort, zi *index.ZettelIndex) { zid, err := id.Parse(value) if err != nil { return } if _, err := p.GetMeta(ctx, zid); err != nil { @@ -255,7 +262,14 @@ } zi.AddBackRef(zid) } func (idx *indexer) deleteZettel(zid id.Zid) { - idx.store.DeleteZettel(context.Background(), zid) + toCheck := idx.store.DeleteZettel(context.Background(), zid) + idx.checkZettel(toCheck) +} + +func (idx *indexer) checkZettel(s id.Set) { + for zid := range s { + idx.ar.Enqueue(zid, arUpdate) + } } Index: index/memstore/memstore.go ================================================================== --- index/memstore/memstore.go +++ index/memstore/memstore.go @@ -21,40 +21,42 @@ "zettelstore.de/z/domain/meta" "zettelstore.de/z/index" ) type metaRefs struct { - forward []id.Zid - backward []id.Zid + forward id.Slice + backward id.Slice } type zettelIndex struct { - dead string - forward []id.Zid - backward []id.Zid + dead id.Slice + forward id.Slice + backward id.Slice meta map[string]metaRefs } func (zi *zettelIndex) isEmpty() bool { - if len(zi.forward) > 0 || len(zi.backward) > 0 || zi.dead != "" { + if len(zi.forward) > 0 || len(zi.backward) > 0 || len(zi.dead) > 0 { return false } return zi.meta == nil || len(zi.meta) == 0 } type memStore struct { - mx sync.RWMutex - idx map[id.Zid]*zettelIndex + mx sync.RWMutex + idx map[id.Zid]*zettelIndex + dead map[id.Zid]id.Slice // map dead refs where they occur // Stats updates uint64 } // New returns a new memory-based index store. func New() index.Store { return &memStore{ - idx: make(map[id.Zid]*zettelIndex), + idx: make(map[id.Zid]*zettelIndex), + dead: make(map[id.Zid]id.Slice), } } func (ms *memStore) Enrich(ctx context.Context, m *meta.Meta) { ms.mx.RLock() @@ -62,80 +64,126 @@ zi, ok := ms.idx[m.Zid] if !ok { return } var updated bool - if zi.dead != "" { - m.Set(meta.KeyDead, zi.dead) + if len(zi.dead) > 0 { + m.Set(meta.KeyDead, zi.dead.String()) updated = true } - back := zi.backward + back := removeOtherMetaRefs(m, zi.backward.Copy()) if len(zi.backward) > 0 { - m.Set(meta.KeyBackward, refsToString(zi.backward)) + m.Set(meta.KeyBackward, zi.backward.String()) updated = true } if len(zi.forward) > 0 { - m.Set(meta.KeyForward, refsToString(zi.forward)) + m.Set(meta.KeyForward, zi.forward.String()) back = remRefs(back, zi.forward) updated = true } if len(zi.meta) > 0 { for k, refs := range zi.meta { if len(refs.backward) > 0 { - m.Set(k, refsToString(refs.backward)) + m.Set(k, refs.backward.String()) back = remRefs(back, refs.backward) updated = true } } } if len(back) > 0 { - m.Set(meta.KeyBack, refsToString(back)) + m.Set(meta.KeyBack, back.String()) updated = true } if updated { ms.updates++ } } -func (ms *memStore) UpdateReferences(ctx context.Context, zidx *index.ZettelIndex) { +func removeOtherMetaRefs(m *meta.Meta, back id.Slice) id.Slice { + for _, p := range m.PairsRest(false) { + switch meta.Type(p.Key) { + case meta.TypeID: + if zid, err := id.Parse(p.Value); err == nil { + back = remRef(back, zid) + } + case meta.TypeIDSet: + for _, val := range meta.ListFromValue(p.Value) { + if zid, err := id.Parse(val); err == nil { + back = remRef(back, zid) + } + } + } + } + return back +} + +func (ms *memStore) UpdateReferences(ctx context.Context, zidx *index.ZettelIndex) id.Set { ms.mx.Lock() defer ms.mx.Unlock() zi, ziExist := ms.idx[zidx.Zid] if !ziExist || zi == nil { zi = &zettelIndex{} ziExist = false } - // Update dead references - if drefs := zidx.GetDeadRefs(); len(drefs) > 0 { - zi.dead = refsToString(drefs) - } else { - zi.dead = "" - } - - // Update forward and backward references + // Is this zettel an old dead reference mentioned in other zettel? + var toCheck id.Set + if refs, ok := ms.dead[zidx.Zid]; ok { + // These must be checked later again + toCheck = id.NewSet(refs...) + delete(ms.dead, zidx.Zid) + } + + ms.updateDeadReferences(zidx, zi) + ms.updateForwardBackwardReferences(zidx, zi) + ms.updateMetadataReferences(zidx, zi) + + // Check if zi must be inserted into ms.idx + if !ziExist && !zi.isEmpty() { + ms.idx[zidx.Zid] = zi + } + + return toCheck +} + +func (ms *memStore) updateDeadReferences(zidx *index.ZettelIndex, zi *zettelIndex) { + // Must only be called if ms.mx is write-locked! + drefs := zidx.GetDeadRefs() + newRefs, remRefs := refsDiff(drefs, zi.dead) + zi.dead = drefs + for _, ref := range remRefs { + ms.dead[ref] = remRef(ms.dead[ref], zidx.Zid) + } + for _, ref := range newRefs { + ms.dead[ref] = addRef(ms.dead[ref], zidx.Zid) + } +} + +func (ms *memStore) updateForwardBackwardReferences(zidx *index.ZettelIndex, zi *zettelIndex) { + // Must only be called if ms.mx is write-locked! brefs := zidx.GetBackRefs() newRefs, remRefs := refsDiff(brefs, zi.forward) zi.forward = brefs - for _, ref := range newRefs { - bzi := ms.getEntry(ref) - bzi.backward = addRef(bzi.backward, zidx.Zid) - } for _, ref := range remRefs { bzi := ms.getEntry(ref) bzi.backward = remRef(bzi.backward, zidx.Zid) } + for _, ref := range newRefs { + bzi := ms.getEntry(ref) + bzi.backward = addRef(bzi.backward, zidx.Zid) + } +} - // Update metadata references +func (ms *memStore) updateMetadataReferences(zidx *index.ZettelIndex, zi *zettelIndex) { + // Must only be called if ms.mx is write-locked! metarefs := zidx.GetMetaRefs() for key, mr := range zi.meta { if _, ok := metarefs[key]; ok { continue } ms.removeInverseMeta(zidx.Zid, key, mr.forward) } - if zi.meta == nil { zi.meta = make(map[string]metaRefs) } for key, mrefs := range metarefs { mr := zi.meta[key] @@ -152,69 +200,94 @@ bmr.backward = addRef(bmr.backward, zidx.Zid) bzi.meta[key] = bmr } ms.removeInverseMeta(zidx.Zid, key, remRefs) } - - // Check if zi must be inserted into ms.idx - if !ziExist && !zi.isEmpty() { - ms.idx[zidx.Zid] = zi - } } func (ms *memStore) getEntry(zid id.Zid) *zettelIndex { + // Must only be called if ms.mx is write-locked! if zi, ok := ms.idx[zid]; ok { return zi } zi := &zettelIndex{} ms.idx[zid] = zi return zi } -func (ms *memStore) DeleteZettel(ctx context.Context, zid id.Zid) { +func (ms *memStore) DeleteZettel(ctx context.Context, zid id.Zid) id.Set { ms.mx.Lock() defer ms.mx.Unlock() zi, ok := ms.idx[zid] if !ok { - return + return nil + } + + ms.deleteDeadSources(zid, zi) + toCheck := ms.deleteForwardBackward(zid, zi) + if len(zi.meta) > 0 { + for key, mrefs := range zi.meta { + ms.removeInverseMeta(zid, key, mrefs.forward) + } + } + delete(ms.idx, zid) + return toCheck +} + +func (ms *memStore) deleteDeadSources(zid id.Zid, zi *zettelIndex) { + // Must only be called if ms.mx is write-locked! + for _, ref := range zi.dead { + if drefs, ok := ms.dead[ref]; ok { + drefs = remRef(drefs, zid) + if len(drefs) > 0 { + ms.dead[ref] = drefs + } else { + delete(ms.dead, ref) + } + } } +} +func (ms *memStore) deleteForwardBackward(zid id.Zid, zi *zettelIndex) id.Set { + // Must only be called if ms.mx is write-locked! + var toCheck id.Set for _, ref := range zi.forward { if fzi, ok := ms.idx[ref]; ok { fzi.backward = remRef(fzi.backward, zid) } } for _, ref := range zi.backward { if bzi, ok := ms.idx[ref]; ok { bzi.forward = remRef(bzi.forward, zid) - } - } - if len(zi.meta) > 0 { - for key, mrefs := range zi.meta { - ms.removeInverseMeta(zid, key, mrefs.forward) + if toCheck == nil { + toCheck = id.NewSet() + } + toCheck[ref] = true } } - delete(ms.idx, zid) + return toCheck } -func (ms *memStore) removeInverseMeta(zid id.Zid, key string, forward []id.Zid) { +func (ms *memStore) removeInverseMeta(zid id.Zid, key string, forward id.Slice) { // Must only be called if ms.mx is write-locked! for _, ref := range forward { - if bzi, ok := ms.idx[ref]; ok { - if bzi.meta != nil { - if bmr, ok := bzi.meta[key]; ok { - bmr.backward = remRef(bmr.backward, zid) - if len(bmr.backward) > 0 || len(bmr.forward) > 0 { - bzi.meta[key] = bmr - } else { - delete(bzi.meta, key) - if len(bzi.meta) == 0 { - bzi.meta = nil - } - } - } + bzi, ok := ms.idx[ref] + if !ok || bzi.meta == nil { + continue + } + bmr, ok := bzi.meta[key] + if !ok { + continue + } + bmr.backward = remRef(bmr.backward, zid) + if len(bmr.backward) > 0 || len(bmr.forward) > 0 { + bzi.meta[key] = bmr + } else { + delete(bzi.meta, key) + if len(bzi.meta) == 0 { + bzi.meta = nil } } } } @@ -225,15 +298,17 @@ ms.mx.RUnlock() } func (ms *memStore) Write(w io.Writer) { ms.mx.RLock() - zids := make([]id.Zid, 0, len(ms.idx)) + defer ms.mx.RUnlock() + + zids := make(id.Slice, 0, len(ms.idx)) for id := range ms.idx { zids = append(zids, id) } - id.Sort(zids) + zids.Sort() for _, id := range zids { fmt.Fprintln(w, id) zi := ms.idx[id] fmt.Fprintln(w, "-", zi.dead) writeZidsLn(w, ">", zi.forward) @@ -248,16 +323,24 @@ writeZidsLn(w, "]", fb.forward) writeZidsLn(w, "[", fb.backward) } } } - ms.mx.RUnlock() + + zids = make(id.Slice, 0, len(ms.dead)) + for id := range ms.dead { + zids = append(zids, id) + } + zids.Sort() + for _, id := range zids { + fmt.Fprintln(w, "~", id, ms.dead[id]) + } } -func writeZidsLn(w io.Writer, prefix string, zids []id.Zid) { +func writeZidsLn(w io.Writer, prefix string, zids id.Slice) { io.WriteString(w, prefix) for _, zid := range zids { io.WriteString(w, " ") w.Write(zid.Bytes()) } fmt.Fprintln(w) } Index: index/memstore/refs.go ================================================================== --- index/memstore/refs.go +++ index/memstore/refs.go @@ -10,27 +10,14 @@ // Package memstore stored the index in main memory. package memstore import ( - "bytes" - "zettelstore.de/z/domain/id" ) -func refsToString(refs []id.Zid) string { - var buf bytes.Buffer - for i, dref := range refs { - if i > 0 { - buf.WriteByte(' ') - } - buf.Write(dref.Bytes()) - } - return buf.String() -} - -func refsDiff(refsN, refsO []id.Zid) (newRefs, remRefs []id.Zid) { +func refsDiff(refsN, refsO id.Slice) (newRefs, remRefs id.Slice) { npos, opos := 0, 0 for npos < len(refsN) && opos < len(refsO) { rn, ro := refsN[npos], refsO[opos] if rn == ro { npos++ @@ -52,30 +39,30 @@ remRefs = append(remRefs, refsO[opos:]...) } return newRefs, remRefs } -func addRef(refs []id.Zid, ref id.Zid) []id.Zid { +func addRef(refs id.Slice, ref id.Zid) id.Slice { if len(refs) == 0 { return append(refs, ref) } for i, r := range refs { if r == ref { return refs } if r > ref { - return append(refs[:i], append([]id.Zid{ref}, refs[i:]...)...) + return append(refs[:i], append(id.Slice{ref}, refs[i:]...)...) } } return append(refs, ref) } -func remRefs(refs []id.Zid, rem []id.Zid) []id.Zid { +func remRefs(refs id.Slice, rem id.Slice) id.Slice { if len(refs) == 0 || len(rem) == 0 { return refs } - result := make([]id.Zid, 0, len(refs)) + result := make(id.Slice, 0, len(refs)) rpos, dpos := 0, 0 for rpos < len(refs) && dpos < len(rem) { rr, dr := refs[rpos], rem[dpos] if rr < dr { result = append(result, rr) @@ -93,18 +80,16 @@ result = append(result, refs[rpos:]...) } return result } -func remRef(refs []id.Zid, ref id.Zid) []id.Zid { - if refs != nil { - for i, r := range refs { - if r == ref { - return append(refs[:i], refs[i+1:]...) - } - if r > ref { - return refs - } +func remRef(refs id.Slice, ref id.Zid) id.Slice { + for i, r := range refs { + if r == ref { + return append(refs[:i], refs[i+1:]...) + } + if r > ref { + return refs } } return refs } Index: index/memstore/refs_test.go ================================================================== --- index/memstore/refs_test.go +++ index/memstore/refs_test.go @@ -15,22 +15,11 @@ "testing" "zettelstore.de/z/domain/id" ) -func numsToRefs(nums []uint) []id.Zid { - if nums == nil { - return nil - } - refs := make([]id.Zid, 0, len(nums)) - for _, n := range nums { - refs = append(refs, id.Zid(n)) - } - return refs -} - -func assertRefs(t *testing.T, i int, got []id.Zid, exp []uint) { +func assertRefs(t *testing.T, i int, got, exp id.Slice) { t.Helper() if got == nil && exp != nil { t.Errorf("%d: got nil, but expected %v", i, exp) return } @@ -47,116 +36,99 @@ t.Errorf("%d: pos %d: expected %d, but got %d", i, p, n, got) } } } -func TestRefsToString(t *testing.T) { - testcases := []struct { - in []uint - exp string - }{ - {nil, ""}, - {[]uint{}, ""}, - {[]uint{1}, "00000000000001"}, - {[]uint{1, 2}, "00000000000001 00000000000002"}, - } - for i, tc := range testcases { - got := refsToString(numsToRefs(tc.in)) - if got != tc.exp { - t.Errorf("%d/%v: expected %q, but got %q", i, tc.in, tc.exp, got) - } - } -} - -func TestRefsDiff(t *testing.T) { - testcases := []struct { - in1, in2 []uint - exp1, exp2 []uint - }{ - {nil, nil, nil, nil}, - {[]uint{1}, nil, []uint{1}, nil}, - {nil, []uint{1}, nil, []uint{1}}, - {[]uint{1}, []uint{1}, nil, nil}, - {[]uint{1, 2}, []uint{1}, []uint{2}, nil}, - {[]uint{1, 2}, []uint{1, 3}, []uint{2}, []uint{3}}, - {[]uint{1, 4}, []uint{1, 3}, []uint{4}, []uint{3}}, - } - for i, tc := range testcases { - got1, got2 := refsDiff(numsToRefs(tc.in1), numsToRefs(tc.in2)) +func TestRefsDiff(t *testing.T) { + testcases := []struct { + in1, in2 id.Slice + exp1, exp2 id.Slice + }{ + {nil, nil, nil, nil}, + {id.Slice{1}, nil, id.Slice{1}, nil}, + {nil, id.Slice{1}, nil, id.Slice{1}}, + {id.Slice{1}, id.Slice{1}, nil, nil}, + {id.Slice{1, 2}, id.Slice{1}, id.Slice{2}, nil}, + {id.Slice{1, 2}, id.Slice{1, 3}, id.Slice{2}, id.Slice{3}}, + {id.Slice{1, 4}, id.Slice{1, 3}, id.Slice{4}, id.Slice{3}}, + } + for i, tc := range testcases { + got1, got2 := refsDiff(tc.in1, tc.in2) assertRefs(t, i, got1, tc.exp1) assertRefs(t, i, got2, tc.exp2) } } func TestAddRef(t *testing.T) { testcases := []struct { - ref []uint + ref id.Slice zid uint - exp []uint + exp id.Slice }{ - {nil, 5, []uint{5}}, - {[]uint{1}, 5, []uint{1, 5}}, - {[]uint{10}, 5, []uint{5, 10}}, - {[]uint{5}, 5, []uint{5}}, - {[]uint{1, 10}, 5, []uint{1, 5, 10}}, - {[]uint{1, 5, 10}, 5, []uint{1, 5, 10}}, + {nil, 5, id.Slice{5}}, + {id.Slice{1}, 5, id.Slice{1, 5}}, + {id.Slice{10}, 5, id.Slice{5, 10}}, + {id.Slice{5}, 5, id.Slice{5}}, + {id.Slice{1, 10}, 5, id.Slice{1, 5, 10}}, + {id.Slice{1, 5, 10}, 5, id.Slice{1, 5, 10}}, } for i, tc := range testcases { - got := addRef(numsToRefs(tc.ref), id.Zid(tc.zid)) + got := addRef(tc.ref, id.Zid(tc.zid)) assertRefs(t, i, got, tc.exp) } } func TestRemRefs(t *testing.T) { testcases := []struct { - in1, in2 []uint - exp []uint + in1, in2 id.Slice + exp id.Slice }{ {nil, nil, nil}, - {nil, []uint{}, nil}, - {[]uint{}, nil, []uint{}}, - {[]uint{}, []uint{}, []uint{}}, - {[]uint{1}, []uint{5}, []uint{1}}, - {[]uint{10}, []uint{5}, []uint{10}}, - {[]uint{1, 5}, []uint{5}, []uint{1}}, - {[]uint{5, 10}, []uint{5}, []uint{10}}, - {[]uint{1, 10}, []uint{5}, []uint{1, 10}}, - {[]uint{1}, []uint{2, 5}, []uint{1}}, - {[]uint{10}, []uint{2, 5}, []uint{10}}, - {[]uint{1, 5}, []uint{2, 5}, []uint{1}}, - {[]uint{5, 10}, []uint{2, 5}, []uint{10}}, - {[]uint{1, 2, 5}, []uint{2, 5}, []uint{1}}, - {[]uint{2, 5, 10}, []uint{2, 5}, []uint{10}}, - {[]uint{1, 10}, []uint{2, 5}, []uint{1, 10}}, - {[]uint{1}, []uint{5, 9}, []uint{1}}, - {[]uint{10}, []uint{5, 9}, []uint{10}}, - {[]uint{1, 5}, []uint{5, 9}, []uint{1}}, - {[]uint{5, 10}, []uint{5, 9}, []uint{10}}, - {[]uint{1, 5, 9}, []uint{5, 9}, []uint{1}}, - {[]uint{5, 9, 10}, []uint{5, 9}, []uint{10}}, - {[]uint{1, 10}, []uint{5, 9}, []uint{1, 10}}, + {nil, id.Slice{}, nil}, + {id.Slice{}, nil, id.Slice{}}, + {id.Slice{}, id.Slice{}, id.Slice{}}, + {id.Slice{1}, id.Slice{5}, id.Slice{1}}, + {id.Slice{10}, id.Slice{5}, id.Slice{10}}, + {id.Slice{1, 5}, id.Slice{5}, id.Slice{1}}, + {id.Slice{5, 10}, id.Slice{5}, id.Slice{10}}, + {id.Slice{1, 10}, id.Slice{5}, id.Slice{1, 10}}, + {id.Slice{1}, id.Slice{2, 5}, id.Slice{1}}, + {id.Slice{10}, id.Slice{2, 5}, id.Slice{10}}, + {id.Slice{1, 5}, id.Slice{2, 5}, id.Slice{1}}, + {id.Slice{5, 10}, id.Slice{2, 5}, id.Slice{10}}, + {id.Slice{1, 2, 5}, id.Slice{2, 5}, id.Slice{1}}, + {id.Slice{2, 5, 10}, id.Slice{2, 5}, id.Slice{10}}, + {id.Slice{1, 10}, id.Slice{2, 5}, id.Slice{1, 10}}, + {id.Slice{1}, id.Slice{5, 9}, id.Slice{1}}, + {id.Slice{10}, id.Slice{5, 9}, id.Slice{10}}, + {id.Slice{1, 5}, id.Slice{5, 9}, id.Slice{1}}, + {id.Slice{5, 10}, id.Slice{5, 9}, id.Slice{10}}, + {id.Slice{1, 5, 9}, id.Slice{5, 9}, id.Slice{1}}, + {id.Slice{5, 9, 10}, id.Slice{5, 9}, id.Slice{10}}, + {id.Slice{1, 10}, id.Slice{5, 9}, id.Slice{1, 10}}, } for i, tc := range testcases { - got := remRefs(numsToRefs(tc.in1), numsToRefs(tc.in2)) + got := remRefs(tc.in1, tc.in2) assertRefs(t, i, got, tc.exp) } } func TestRemRef(t *testing.T) { testcases := []struct { - ref []uint + ref id.Slice zid uint - exp []uint + exp id.Slice }{ {nil, 5, nil}, - {[]uint{}, 5, []uint{}}, - {[]uint{1}, 5, []uint{1}}, - {[]uint{10}, 5, []uint{10}}, - {[]uint{1, 5}, 5, []uint{1}}, - {[]uint{5, 10}, 5, []uint{10}}, - {[]uint{1, 5, 10}, 5, []uint{1, 10}}, + {id.Slice{}, 5, id.Slice{}}, + {id.Slice{5}, 5, id.Slice{}}, + {id.Slice{1}, 5, id.Slice{1}}, + {id.Slice{10}, 5, id.Slice{10}}, + {id.Slice{1, 5}, 5, id.Slice{1}}, + {id.Slice{5, 10}, 5, id.Slice{10}}, + {id.Slice{1, 5, 10}, 5, id.Slice{1, 10}}, } for i, tc := range testcases { - got := remRef(numsToRefs(tc.ref), id.Zid(tc.zid)) + got := remRef(tc.ref, id.Zid(tc.zid)) assertRefs(t, i, got, tc.exp) } } Index: index/zettel.go ================================================================== --- index/zettel.go +++ index/zettel.go @@ -15,23 +15,23 @@ "zettelstore.de/z/domain/id" ) // ZettelIndex contains all index data of a zettel. type ZettelIndex struct { - Zid id.Zid // zid of the indexed zettel - backrefs map[id.Zid]bool // set of back references - metarefs map[string]map[id.Zid]bool // references to inverse keys - deadrefs map[id.Zid]bool // set of dead references + Zid id.Zid // zid of the indexed zettel + backrefs id.Set // set of back references + metarefs map[string]id.Set // references to inverse keys + deadrefs id.Set // set of dead references } // NewZettelIndex creates a new zettel index. func NewZettelIndex(zid id.Zid) *ZettelIndex { return &ZettelIndex{ Zid: zid, - backrefs: make(map[id.Zid]bool), - metarefs: make(map[string]map[id.Zid]bool), - deadrefs: make(map[id.Zid]bool), + backrefs: id.NewSet(), + metarefs: make(map[string]id.Set), + deadrefs: id.NewSet(), } } // AddBackRef adds a reference to a zettel where the current zettel links to // without any more information. @@ -44,46 +44,34 @@ func (zi *ZettelIndex) AddMetaRef(key string, zid id.Zid) { if zids, ok := zi.metarefs[key]; ok { zids[zid] = true return } - zi.metarefs[key] = map[id.Zid]bool{zid: true} + zi.metarefs[key] = id.NewSet(zid) } // AddDeadRef adds a dead reference to a zettel. func (zi *ZettelIndex) AddDeadRef(zid id.Zid) { zi.deadrefs[zid] = true } // GetDeadRefs returns all dead references as a sorted list. -func (zi *ZettelIndex) GetDeadRefs() []id.Zid { - return sortedZids(zi.deadrefs) +func (zi *ZettelIndex) GetDeadRefs() id.Slice { + return zi.deadrefs.Sort() } // GetBackRefs returns all back references as a sorted list. -func (zi *ZettelIndex) GetBackRefs() []id.Zid { - return sortedZids(zi.backrefs) +func (zi *ZettelIndex) GetBackRefs() id.Slice { + return zi.backrefs.Sort() } // GetMetaRefs returns all meta references as a map of strings to a sorted list of references -func (zi *ZettelIndex) GetMetaRefs() map[string][]id.Zid { +func (zi *ZettelIndex) GetMetaRefs() map[string]id.Slice { if len(zi.metarefs) == 0 { return nil } - result := make(map[string][]id.Zid, len(zi.metarefs)) - for key, refs := range zi.metarefs { - result[key] = sortedZids(refs) - } - return result -} - -func sortedZids(refmap map[id.Zid]bool) []id.Zid { - if l := len(refmap); l > 0 { - result := make([]id.Zid, 0, l) - for zid := range refmap { - result = append(result, zid) - } - id.Sort(result) - return result - } - return nil + result := make(map[string]id.Slice, len(zi.metarefs)) + for key, refs := range zi.metarefs { + result[key] = refs.Sort() + } + return result } Index: input/input.go ================================================================== --- input/input.go +++ input/input.go @@ -87,11 +87,10 @@ inp.Ch = '\n' inp.Next() case '\n': inp.Next() } - return } // SetPos allows to reset the read position. func (inp *Input) SetPos(pos int) { inp.readPos = pos Index: parser/cleanup.go ================================================================== --- parser/cleanup.go +++ parser/cleanup.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -114,11 +114,11 @@ } if !cv.doMark { cv.hasMark = true return } - if len(mn.Text) == 0 { + if mn.Text == "" { mn.Text = cv.addIdentifier("*", mn) return } mn.Text = cv.addIdentifier(mn.Text, mn) } Index: parser/markdown/markdown.go ================================================================== --- parser/markdown/markdown.go +++ parser/markdown/markdown.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -282,11 +282,11 @@ return result } // splitText transform the text into a sequence of TextNode and SpaceNode func splitText(text string) ast.InlineSlice { - if len(text) == 0 { + if text == "" { return ast.InlineSlice{} } result := make(ast.InlineSlice, 0, 1) state := 0 // 0=unknown,1=non-spaces,2=spaces @@ -317,41 +317,37 @@ return result } // cleanText removes backslashes from TextNodes and expands entities func cleanText(text string, cleanBS bool) string { - if len(text) == 0 { + if text == "" { return "" } lastPos := 0 var sb strings.Builder for pos, ch := range text { if pos < lastPos { continue } - switch ch { - case '\\': - if cleanBS && pos < len(text)-1 { - switch b := text[pos+1]; b { - case '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', - ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', - '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~': - sb.WriteString(text[lastPos:pos]) - sb.WriteByte(b) - lastPos = pos + 2 - default: - } - } - case '&': + if ch == '&' { inp := input.NewInput(text[pos:]) - s, ok := inp.ScanEntity() - if ok { + if s, ok := inp.ScanEntity(); ok { sb.WriteString(text[lastPos:pos]) sb.WriteString(s) lastPos = pos + inp.Pos } - default: + continue + } + if cleanBS && ch == '\\' && pos < len(text)-1 { + switch b := text[pos+1]; b { + case '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', + ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', + '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~': + sb.WriteString(text[lastPos:pos]) + sb.WriteByte(b) + lastPos = pos + 2 + } } } if lastPos == 0 { return text } @@ -370,18 +366,17 @@ }, } } func cleanCodeSpan(text string) string { - if len(text) == 0 { + if text == "" { return "" } lastPos := 0 var sb strings.Builder for pos, ch := range text { - switch ch { - case '\n': + if ch == '\n' { sb.WriteString(text[lastPos:pos]) if pos < len(text)-1 { sb.WriteByte(' ') } lastPos = pos + 1 Index: parser/parser.go ================================================================== --- parser/parser.go +++ parser/parser.go @@ -81,11 +81,11 @@ // ParseZettel parses the zettel based on the syntax. func ParseZettel(zettel domain.Zettel, syntax string) *ast.ZettelNode { m := zettel.Meta inhMeta := runtime.AddDefaultValues(zettel.Meta) - if len(syntax) == 0 { + if syntax == "" { syntax, _ = inhMeta.Get(meta.KeySyntax) } title, _ := inhMeta.Get(meta.KeyTitle) parseMeta := inhMeta if syntax == meta.ValueSyntaxNone { Index: parser/plain/plain.go ================================================================== --- parser/plain/plain.go +++ parser/plain/plain.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -57,12 +57,11 @@ func readLines(inp *input.Input) (lines []string) { for { inp.EatEOL() posL := inp.Pos - switch inp.Ch { - case input.EOS: + if inp.Ch == input.EOS { return lines } inp.SkipToEOL() lines = append(lines, inp.Src[posL:inp.Pos]) } Index: parser/zettelmark/block.go ================================================================== --- parser/zettelmark/block.go +++ parser/zettelmark/block.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -369,13 +369,11 @@ } if prevItems := cp.lists[parentPos].Items; len(prevItems) > 0 { lastItem := len(prevItems) - 1 prevItems[lastItem] = append(prevItems[lastItem], cp.lists[childPos]) } else { - cp.lists[parentPos].Items = []ast.ItemSlice{ - ast.ItemSlice{cp.lists[childPos]}, - } + cp.lists[parentPos].Items = []ast.ItemSlice{{cp.lists[childPos]}} } } return nil, true } @@ -454,64 +452,70 @@ break } cnt++ } if cp.lists != nil { - // Identation for a list? - if len(cp.lists) < cnt { - cnt = len(cp.lists) - } - cp.lists = cp.lists[:cnt] - if cnt == 0 { - return nil, false - } - ln := cp.lists[cnt-1] - pn := cp.parseLinePara() - lbn := ln.Items[len(ln.Items)-1] - if lpn, ok := lbn[len(lbn)-1].(*ast.ParaNode); ok { - lpn.Inlines = append(lpn.Inlines, pn.Inlines...) - } else { - ln.Items[len(ln.Items)-1] = append(ln.Items[len(ln.Items)-1], pn) - } - return nil, true + return nil, cp.parseIndentForList(cnt) } if cp.descrl != nil { - // Indentation for definition list - defPos := len(cp.descrl.Descriptions) - 1 - if cnt < 1 || defPos < 0 { - return nil, false - } - if len(cp.descrl.Descriptions[defPos].Descriptions) == 0 { - // Continuation of a definition term - for { - in := cp.parseInline() - if in == nil { - return nil, true - } - cp.descrl.Descriptions[defPos].Term = append(cp.descrl.Descriptions[defPos].Term, in) - if _, ok := in.(*ast.BreakNode); ok { - return nil, true - } - } - } else { - // Continuation of a definition description - pn := cp.parseLinePara() - if pn == nil { - return nil, false - } - descrPos := len(cp.descrl.Descriptions[defPos].Descriptions) - 1 - lbn := cp.descrl.Descriptions[defPos].Descriptions[descrPos] - if lpn, ok := lbn[len(lbn)-1].(*ast.ParaNode); ok { - lpn.Inlines = append(lpn.Inlines, pn.Inlines...) - } else { - descrPos := len(cp.descrl.Descriptions[defPos].Descriptions) - 1 - cp.descrl.Descriptions[defPos].Descriptions[descrPos] = append(cp.descrl.Descriptions[defPos].Descriptions[descrPos], pn) - } - return nil, true - } - } - return nil, false + return nil, cp.parseIndentForDescription(cnt) + } + return nil, false +} + +func (cp *zmkP) parseIndentForList(cnt int) bool { + if len(cp.lists) < cnt { + cnt = len(cp.lists) + } + cp.lists = cp.lists[:cnt] + if cnt == 0 { + return false + } + ln := cp.lists[cnt-1] + pn := cp.parseLinePara() + lbn := ln.Items[len(ln.Items)-1] + if lpn, ok := lbn[len(lbn)-1].(*ast.ParaNode); ok { + lpn.Inlines = append(lpn.Inlines, pn.Inlines...) + } else { + ln.Items[len(ln.Items)-1] = append(ln.Items[len(ln.Items)-1], pn) + } + return true +} + +func (cp *zmkP) parseIndentForDescription(cnt int) bool { + defPos := len(cp.descrl.Descriptions) - 1 + if cnt < 1 || defPos < 0 { + return false + } + if len(cp.descrl.Descriptions[defPos].Descriptions) == 0 { + // Continuation of a definition term + for { + in := cp.parseInline() + if in == nil { + return true + } + cp.descrl.Descriptions[defPos].Term = append(cp.descrl.Descriptions[defPos].Term, in) + if _, ok := in.(*ast.BreakNode); ok { + return true + } + } + } + + // Continuation of a definition description + pn := cp.parseLinePara() + if pn == nil { + return false + } + descrPos := len(cp.descrl.Descriptions[defPos].Descriptions) - 1 + lbn := cp.descrl.Descriptions[defPos].Descriptions[descrPos] + if lpn, ok := lbn[len(lbn)-1].(*ast.ParaNode); ok { + lpn.Inlines = append(lpn.Inlines, pn.Inlines...) + } else { + descrPos := len(cp.descrl.Descriptions[defPos].Descriptions) - 1 + cp.descrl.Descriptions[defPos].Descriptions[descrPos] = append(cp.descrl.Descriptions[defPos].Descriptions[descrPos], pn) + } + return true } // parseLinePara parses one line of inline material. func (cp *zmkP) parseLinePara() *ast.ParaNode { pn := &ast.ParaNode{} Index: parser/zettelmark/inline.go ================================================================== --- parser/zettelmark/inline.go +++ parser/zettelmark/inline.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -61,12 +61,11 @@ case '!': in, success = cp.parseMark() } case '{': inp.Next() - switch inp.Ch { - case '{': + if inp.Ch == '{' { in, success = cp.parseImage() } case '#': return cp.parseTag() case '%': Index: parser/zettelmark/node.go ================================================================== --- parser/zettelmark/node.go +++ parser/zettelmark/node.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -16,26 +16,20 @@ ) // Internal nodes for parsing zettelmark. These will be removed in // post-processing. -// nullItemNode specifies a removable placeholder for an item block. +// nullItemNode specifies a removable placeholder for an item node. type nullItemNode struct { ast.ItemNode } -func (nn *nullItemNode) blockNode() {} -func (nn *nullItemNode) itemNode() {} - // Accept a visitor and visit the node. func (nn *nullItemNode) Accept(v ast.Visitor) {} // nullDescriptionNode specifies a removable placeholder. type nullDescriptionNode struct { ast.DescriptionNode } -func (nn *nullDescriptionNode) blockNode() {} -func (nn *nullDescriptionNode) descriptionNode() {} - // Accept a visitor and visit the node. func (nn *nullDescriptionNode) Accept(v ast.Visitor) {} Index: parser/zettelmark/post-processor.go ================================================================== --- parser/zettelmark/post-processor.go +++ parser/zettelmark/post-processor.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -39,11 +39,11 @@ if pn != nil { pn.Inlines = pp.processInlineSlice(pn.Inlines) } } -// VisitVerbatim post-processes a verbatim block. +// VisitVerbatim does nothing, no post-processing needed. func (pp *postProcessor) VisitVerbatim(vn *ast.VerbatimNode) {} // VisitRegion post-processes a region. func (pp *postProcessor) VisitRegion(rn *ast.RegionNode) { oldVerse := pp.inVerse @@ -58,11 +58,11 @@ // VisitHeading post-processes a heading. func (pp *postProcessor) VisitHeading(hn *ast.HeadingNode) { hn.Inlines = pp.processInlineSlice(hn.Inlines) } -// VisitHRule post-processes a horizontal rule. +// VisitHRule does nothing, no post-processing needed. func (pp *postProcessor) VisitHRule(hn *ast.HRuleNode) {} // VisitList post-processes a list. func (pp *postProcessor) VisitNestedList(ln *ast.NestedListNode) { for i, item := range ln.Items { @@ -91,13 +91,11 @@ tn.Header = tn.Rows[0] tn.Rows = tn.Rows[1:] for pos, cell := range tn.Header { if inlines := cell.Inlines; len(inlines) > 0 { if textNode, ok := inlines[0].(*ast.TextNode); ok { - if strings.HasPrefix(textNode.Text, "=") { - textNode.Text = textNode.Text[1:] - } + textNode.Text = strings.TrimPrefix(textNode.Text, "=") } if textNode, ok := inlines[len(inlines)-1].(*ast.TextNode); ok { if tnl := len(textNode.Text); tnl > 0 { if align := getAlignment(textNode.Text[tnl-1]); align != ast.AlignDefault { tn.Align[pos] = align @@ -439,15 +437,14 @@ return toPos } func (pp *postProcessor) processInlineSliceInplace(ins ast.InlineSlice) { for _, in := range ins { - switch n := in.(type) { - case *ast.TextNode: + if n, ok := in.(*ast.TextNode); ok { if n.Text == "..." { n.Text = "\u2026" } else if len(n.Text) == 4 && strings.IndexByte(",;:!?", n.Text[3]) >= 0 && n.Text[:3] == "..." { n.Text = "\u2026" + n.Text[3:] } } } } Index: parser/zettelmark/zettelmark.go ================================================================== --- parser/zettelmark/zettelmark.go +++ parser/zettelmark/zettelmark.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -137,11 +137,11 @@ } inp.Next() } } -func updateAttrs(attrs map[string]string, key string, val string) { +func updateAttrs(attrs map[string]string, key, val string) { if prevVal := attrs[key]; len(prevVal) > 0 { attrs[key] = prevVal + " " + val } else { attrs[key] = val } Index: parser/zettelmark/zettelmark_test.go ================================================================== --- parser/zettelmark/zettelmark_test.go +++ parser/zettelmark/zettelmark_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -876,11 +876,11 @@ tv.b.WriteByte(' ') tv.b.WriteString(k) v := a.Attrs[k] if len(v) > 0 { tv.b.WriteByte('=') - if strings.IndexRune(v, ' ') >= 0 { + if strings.ContainsRune(v, ' ') { tv.b.WriteByte('"') tv.b.WriteString(v) tv.b.WriteByte('"') } else { tv.b.WriteString(v) Index: place/constplace/constdata.go ================================================================== --- place/constplace/constdata.go +++ place/constplace/constdata.go @@ -10,53 +10,68 @@ // Package constplace stores zettel inside the executable. package constplace import ( - "zettelstore.de/z/domain" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" ) const ( syntaxTemplate = "mustache" ) var constZettelMap = map[id.Zid]constZettel{ - id.ConfigurationZid: constZettel{ + id.ConfigurationZid: { constHeader{ meta.KeyTitle: "Zettelstore Runtime Configuration", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityOwner, meta.KeySyntax: meta.ValueSyntaxNone, }, "", }, - id.BaseTemplateZid: constZettel{ + id.BaseTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Base HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - domain.NewContent( - ` + ` {{{MetaHeader}}} -{{{Header}}} {{Title}}
@@ -102,23 +99,20 @@
{{{FooterHTML}}}
{{/FooterHTML}} -`, - ), - }, +`}, - id.LoginTemplateZid: constZettel{ + id.LoginTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Login Form HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - domain.NewContent( - `
+ `

{{Title}}

{{#Retry}}
Wrong user name / password. Try again.
@@ -132,24 +126,25 @@ -
`, - )}, +
`}, - id.ListTemplateZid: constZettel{ + id.ListTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore List Meta HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - domain.NewContent( - `

{{Title}}

+ ``}, - id.DetailTemplateZid: constZettel{ + id.DetailTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Detail HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - domain.NewContent( - `
+ `

{{{HTMLTitle}}}

{{#CanWrite}}Edit ·{{/CanWrite}} {{Zid}} · @@ -178,11 +173,10 @@ Info · ({{RoleText}}) {{#HasTags}}· {{#Tags}} {{Text}}{{/Tags}}{{/HasTags}} {{#CanCopy}}· Copy{{/CanCopy}} {{#CanFolge}}· Folge{{/CanFolge}} -{{#CanNew}}· New{{/CanNew}} {{#FolgeRefs}}
Folge: {{{FolgeRefs}}}{{/FolgeRefs}} {{#PrecursorRefs}}
Precursor: {{{PrecursorRefs}}}{{/PrecursorRefs}} {{#HasExtURL}}
URL: {{ExtURL}}{{/HasExtURL}}
@@ -195,32 +189,31 @@
  • {{Text}}
  • {{/BackLinks}} {{/HasBackLinks}} -
    `)}, +
    `}, - id.InfoTemplateZid: constZettel{ + id.InfoTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Info HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - domain.NewContent( - `
    + `

    Information for Zettel {{Zid}}

    WebContext {{#CanWrite}} · Edit{{/CanWrite}} {{#CanFolge}} · Folge{{/CanFolge}} {{#CanCopy}} · Copy{{/CanCopy}} -{{#CanNew}} · New{{/CanNew}} {{#CanRename}}· Rename{{/CanRename}} {{#CanDelete}}· Delete{{/CanDelete}}
    -

    Interpreted Meta Data

    +

    Interpreted Metadata

    {{#MetaData}}{{/MetaData}}
    {{Key}}{{{Value}}}
    {{#HasLinks}}

    References

    {{#HasLocLinks}}

    Local

    @@ -246,14 +239,37 @@ {{#Elements}}{{#HasURL}}{{Text}}{{/HasURL}}{{^HasURL}}{{Text}}{{/HasURL}} {{/Elements}} {{/Matrix}} -
    `), - }, +
    `}, + + id.ContextTemplateZid: { + constHeader{ + meta.KeyTitle: "Zettelstore Context HTML Template", + meta.KeyRole: meta.ValueRoleConfiguration, + meta.KeyVisibility: meta.ValueVisibilityExpert, + meta.KeySyntax: syntaxTemplate, + }, + ``}, - id.FormTemplateZid: constZettel{ + id.FormTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Form HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, @@ -288,21 +304,19 @@
    {{#IsTextContent}} - + {{/IsTextContent}}
    `, }, - id.RenameTemplateZid: constZettel{ + id.RenameTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Rename Form HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, @@ -326,11 +340,11 @@ {{/MetaPairs}} `, }, - id.DeleteTemplateZid: constZettel{ + id.DeleteTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore Delete HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, @@ -350,39 +364,45 @@ {{end}}`, }, - id.RolesTemplateZid: constZettel{ + id.RolesTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore List Roles HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - `

    Currently used roles

    + ``}, - id.TagsTemplateZid: constZettel{ + id.TagsTemplateZid: { constHeader{ meta.KeyTitle: "Zettelstore List Tags HTML Template", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityExpert, meta.KeySyntax: syntaxTemplate, }, - `

    Currently used tags

    + ``}, - id.BaseCSSZid: constZettel{ + id.BaseCSSZid: { constHeader{ meta.KeyTitle: "Zettelstore Base CSS", meta.KeyRole: meta.ValueRoleConfiguration, meta.KeyVisibility: meta.ValueVisibilityPublic, meta.KeySyntax: "css", @@ -635,15 +655,15 @@ padding: .1rem .2rem; font-size: 75%; } .zs-meta { font-size:.75rem; - color:#888; + color:#444; margin-bottom:1rem; } .zs-meta a { - color:#888; + color:#444; } h1+.zs-meta { margin-top:-1rem; } details > summary { @@ -664,32 +684,90 @@ animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; } -} -`, - }, +}`}, + + id.TOCNewTemplateZid: { + constHeader{ + meta.KeyTitle: "New Menu", + meta.KeyRole: meta.ValueRoleConfiguration, + meta.KeySyntax: meta.ValueSyntaxZmk, + }, + `This zettel lists all zettel that should act as a template for new zettel. +These zettel will be included in the ""New"" menu of the WebUI. +* [[New Zettel|00000000090001]] +* [[New User|00000000090002]]`}, - id.TemplateNewZettelZid: constZettel{ + id.TemplateNewZettelZid: { constHeader{ - meta.KeyTitle: "New Zettel", - meta.KeyRole: meta.ValueRoleNewTemplate, - meta.KeyNewRole: meta.ValueRoleZettel, - meta.KeySyntax: meta.ValueSyntaxZmk, - }, - "", - }, - - id.TemplateNewUserZid: constZettel{ + meta.KeyTitle: "New Zettel", + meta.KeyRole: meta.ValueRoleZettel, + meta.KeySyntax: meta.ValueSyntaxZmk, + }, + ""}, + + id.TemplateNewUserZid: { constHeader{ meta.KeyTitle: "New User", - meta.KeyRole: meta.ValueRoleNewTemplate, - meta.KeyNewRole: meta.ValueRoleUser, + meta.KeyRole: meta.ValueRoleUser, meta.KeyCredential: "", meta.KeyUserID: "", meta.KeyUserRole: meta.ValueUserRoleReader, meta.KeySyntax: meta.ValueSyntaxNone, }, - "", - }, + ""}, + + id.DefaultHomeZid: { + constHeader{ + meta.KeyTitle: "Home", + meta.KeyRole: meta.ValueRoleZettel, + meta.KeySyntax: meta.ValueSyntaxZmk, + }, + `=== Thank you for using Zettelstore! + +You will find the lastest information about Zettelstore at [[https://zettelstore.de]]. +Check that website regulary for [[upgrades|https://zettelstore.de/home/doc/trunk/www/download.wiki]] to the latest version. +You should consult the [[change log|https://zettelstore.de/home/doc/trunk/www/changes.wiki]] before upgrading. +Sometimes, you have to edit some of your Zettelstore-related zettel before upgrading. +Since Zettelstore is currently in a development state, every upgrade might fix some of your problems. +To check for versions, there is a zettel with the [[current version|00000000000001]] of your Zettelstore. + +If you have problems concerning Zettelstore, +do not hesitate to get in [[contact with the main developer|mailto:ds@zettelstore.de]]. + +=== Reporting errors +If you have encountered an error, please include the content of the following zettel in your mail: +* [[Zettelstore Version|00000000000001]] +* [[Zettelstore Operating System|00000000000003]] +* [[Zettelstore Startup Configuration|00000000000096]] +* [[Zettelstore Startup Values|00000000000098]] +* [[Zettelstore Runtime Configuration|00000000000100]] + +Additionally, you have to describe, what you have done before that error occurs +and what you have expected instead. +Please do not forget to include the error message, if there is one. + +Some of above Zettelstore zettel can only be retrieved if you enabled ""expert mode"". +Otherwise, only some zettel are linked. +To enable expert mode, edit the zettel [[Zettelstore Runtime Configuration|00000000000100]]: +please set the metadata value of the key ''expert-mode'' to true. +To do you, enter the string ''expert-mode:true'' inside the edit view of the metadata. + +=== Information about this zettel +This zettel is your home zettel. +It is part of the Zettelstore software itself. +Every time you click on the [[Home|//]] link in the menu bar, you will be redirected to this zettel. + +You can change the content of this zettel by clicking on ""Edit"" above. +This allows you to customize your home zettel. + +Alternatively, you can designate another zettel as your home zettel. +Edit the [[Zettelstore Runtime Configuration|00000000000100]] by adding the metadata key ''home-zettel''. +Its value is the identifier of the zettel that should act as the new home zettel. +You will find the identifier of each zettel between their ""Edit"" and the ""Info"" link above. +The identifier of this zettel is ''00010000000000''. +If you provide a wrong identifier, this zettel will be shown as the home zettel. +Take a look inside the manual for further details. +`}, } Index: place/constplace/constplace.go ================================================================== --- place/constplace/constplace.go +++ place/constplace/constplace.go @@ -75,12 +75,12 @@ return makeMeta(zid, z.header), nil } return nil, place.ErrNotFound } -func (cp *constPlace) FetchZids(ctx context.Context) (map[id.Zid]bool, error) { - result := make(map[id.Zid]bool, len(cp.zettel)) +func (cp *constPlace) FetchZids(ctx context.Context) (id.Set, error) { + result := id.NewSetCap(len(cp.zettel)) for zid := range cp.zettel { result[zid] = true } return result, nil } @@ -124,11 +124,9 @@ return place.ErrReadOnly } return place.ErrNotFound } -func (cp *constPlace) Reload(ctx context.Context) error { return nil } - func (cp *constPlace) ReadStats(st *place.Stats) { st.ReadOnly = true st.Zettel = len(cp.zettel) } Index: place/dirplace/directory/service.go ================================================================== --- place/dirplace/directory/service.go +++ place/dirplace/directory/service.go @@ -50,11 +50,11 @@ if ev.ext == "meta" { de.MetaSpec = MetaSpecFile de.MetaPath = ev.path return } - if len(de.ContentExt) != 0 && de.ContentExt != ev.ext { + if de.ContentExt != "" && de.ContentExt != ev.ext { de.Duplicates = true return } if de.MetaSpec != MetaSpecFile { if ev.ext == "zettel" { @@ -113,31 +113,39 @@ } srv.notifyChange(place.OnReload, id.Invalid) case fileStatusError: log.Println("DIRPLACE", "ERROR", ev.err) case fileStatusUpdate: - if newMap != nil { - dirMapUpdate(newMap, ev) - } else { - dirMapUpdate(curMap, ev) - srv.notifyChange(place.OnUpdate, ev.zid) - } + srv.processFileUpdateEvent(ev, curMap, newMap) case fileStatusDelete: - if newMap != nil { - deleteFromMap(newMap, ev) - } else { - deleteFromMap(curMap, ev) - srv.notifyChange(place.OnDelete, ev.zid) - } + srv.processFileDeleteEvent(ev, curMap, newMap) } case cmd, ok := <-srv.cmds: if ok { cmd.run(curMap) } } } } + +func (srv *Service) processFileUpdateEvent(ev *fileEvent, curMap, newMap dirMap) { + if newMap != nil { + dirMapUpdate(newMap, ev) + } else { + dirMapUpdate(curMap, ev) + srv.notifyChange(place.OnUpdate, ev.zid) + } +} + +func (srv *Service) processFileDeleteEvent(ev *fileEvent, curMap, newMap dirMap) { + if newMap != nil { + deleteFromMap(newMap, ev) + } else { + deleteFromMap(curMap, ev) + srv.notifyChange(place.OnDelete, ev.zid) + } +} type dirCmd interface { run(m dirMap) } Index: place/dirplace/directory/watch.go ================================================================== --- place/dirplace/directory/watch.go +++ place/dirplace/directory/watch.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -21,11 +21,11 @@ "github.com/fsnotify/fsnotify" "zettelstore.de/z/domain/id" ) -var validFileName = regexp.MustCompile("^(\\d{14}).*(\\.(.+))$") +var validFileName = regexp.MustCompile(`^(\d{14}).*(\.(.+))$`) func matchValidFileName(name string) []string { return validFileName.FindStringSubmatch(name) } @@ -190,25 +190,16 @@ return ok } } } - pause := func() bool { - for { - select { - case _, ok := <-tick: - return ok - } - } - } - for { if !reloadFiles() { return } if watcher == nil { - if !pause() { + if _, ok := <-tick; !ok { return } } else { if !handleEvents() { return @@ -230,30 +221,34 @@ case fileStatusNone: return events case fileStatusReloadStart: events = events[0:0] case fileStatusUpdate, fileStatusDelete: - if len(events) == 0 { - return append(events, ev) - } - for i := len(events) - 1; i >= 0; i-- { - oev := events[i] - switch oev.status { - case fileStatusReloadStart, fileStatusReloadEnd: - return append(events, ev) - case fileStatusUpdate, fileStatusDelete: - if ev.path == oev.path { - if ev.status == oev.status { - return events - } - oev.status = fileStatusNone - return append(events, ev) - } + if len(events) > 0 && mergeEvents(events, ev) { + return events + } + } + return append(events, ev) +} + +func mergeEvents(events []*fileEvent, ev *fileEvent) bool { + for i := len(events) - 1; i >= 0; i-- { + oev := events[i] + switch oev.status { + case fileStatusReloadStart, fileStatusReloadEnd: + return false + case fileStatusUpdate, fileStatusDelete: + if ev.path == oev.path { + if ev.status == oev.status { + return true + } + oev.status = fileStatusNone + return false } } } - return append(events, ev) + return false } func collectEvents(out chan<- *fileEvent, in <-chan *fileEvent) { defer close(out) Index: place/dirplace/dirplace.go ================================================================== --- place/dirplace/dirplace.go +++ place/dirplace/dirplace.go @@ -179,13 +179,13 @@ } dp.cleanupMeta(ctx, m) return m, nil } -func (dp *dirPlace) FetchZids(ctx context.Context) (map[id.Zid]bool, error) { +func (dp *dirPlace) FetchZids(ctx context.Context) (id.Set, error) { entries := dp.dirSrv.GetEntries() - result := make(map[id.Zid]bool, len(entries)) + result := id.NewSetCap(len(entries)) for _, entry := range entries { result[entry.Zid] = true } return result, nil } @@ -196,11 +196,12 @@ hasMatch := place.CreateFilterFunc(f) entries := dp.dirSrv.GetEntries() res = make([]*meta.Meta, 0, len(entries)) for _, entry := range entries { // TODO: execute requests in parallel - m, err := getMeta(dp, &entry, entry.Zid) + m, err1 := getMeta(dp, &entry, entry.Zid) + err = err1 if err != nil { continue } dp.cleanupMeta(ctx, m) dp.cdata.Filter.Enrich(ctx, m) @@ -311,14 +312,11 @@ if err := setZettel(dp, &newEntry, newZettel); err != nil { // "Rollback" rename. No error checking... dp.dirSrv.RenameEntry(&newEntry, &curEntry) return err } - if err := deleteZettel(dp, &curEntry, curZid); err != nil { - return err - } - return nil + return deleteZettel(dp, &curEntry, curZid) } func (dp *dirPlace) CanDeleteZettel(ctx context.Context, zid id.Zid) bool { if dp.readonly { return false @@ -336,20 +334,10 @@ if !entry.IsValid() { return nil } dp.dirSrv.DeleteEntry(zid) err := deleteZettel(dp, &entry, zid) - return err -} - -func (dp *dirPlace) Reload(ctx context.Context) error { - // Brute force: stop everything, then start everything. - // Could be done better in the future... - err := dp.Stop(ctx) - if err == nil { - err = dp.Start(ctx) - } return err } func (dp *dirPlace) ReadStats(st *place.Stats) { st.ReadOnly = dp.readonly Index: place/dirplace/service.go ================================================================== --- place/dirplace/service.go +++ place/dirplace/service.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -35,12 +35,12 @@ // COMMAND: getMeta ---------------------------------------- // // Retrieves the meta data from a zettel. func getMeta(dp *dirPlace, entry *directory.Entry, zid id.Zid) (*meta.Meta, error) { - rc := make(chan resGetMetaContent) - dp.getFileChan(zid) <- &fileGetMetaContent{entry, rc} + rc := make(chan resGetMeta) + dp.getFileChan(zid) <- &fileGetMeta{entry, rc} res := <-rc close(rc) return res.meta, res.err } @@ -98,11 +98,13 @@ var err error switch cmd.entry.MetaSpec { case directory.MetaSpecFile: m, err = parseMetaFile(cmd.entry.Zid, cmd.entry.MetaPath) - content, err = readFileContent(cmd.entry.ContentPath) + if err == nil { + content, err = readFileContent(cmd.entry.ContentPath) + } case directory.MetaSpecHeader: m, content, err = parseMetaContentFile(cmd.entry.Zid, cmd.entry.ContentPath) default: m = cmd.entry.CalcDefaultMeta() content, err = readFileContent(cmd.entry.ContentPath) @@ -131,56 +133,59 @@ rc chan<- resSetZettel } type resSetZettel = error func (cmd *fileSetZettel) run() { - var f *os.File var err error - switch cmd.entry.MetaSpec { case directory.MetaSpecFile: - f, err = openFileWrite(cmd.entry.MetaPath) - if err == nil { - err = writeFileZid(f, cmd.zettel.Meta.Zid) - if err == nil { - _, err = cmd.zettel.Meta.Write(f, true) - if err1 := f.Close(); err == nil { - err = err1 - } - - if err == nil { - err = writeFileContent(cmd.entry.ContentPath, cmd.zettel.Content.AsString()) - } - } - } - + err = cmd.runMetaSpecFile() case directory.MetaSpecHeader: - f, err = openFileWrite(cmd.entry.ContentPath) - if err == nil { - err = writeFileZid(f, cmd.zettel.Meta.Zid) - if err == nil { - _, err = cmd.zettel.Meta.WriteAsHeader(f, true) - if err == nil { - _, err = f.WriteString(cmd.zettel.Content.AsString()) - if err1 := f.Close(); err == nil { - err = err1 - } - } - } - } - + err = cmd.runMetaSpecHeader() case directory.MetaSpecNone: // TODO: if meta has some additional infos: write meta to new .meta; // update entry in dir - err = writeFileContent(cmd.entry.ContentPath, cmd.zettel.Content.AsString()) - case directory.MetaSpecUnknown: panic("TODO: ???") } cmd.rc <- err } + +func (cmd *fileSetZettel) runMetaSpecFile() error { + f, err := openFileWrite(cmd.entry.MetaPath) + if err == nil { + err = writeFileZid(f, cmd.zettel.Meta.Zid) + if err == nil { + _, err = cmd.zettel.Meta.Write(f, true) + if err1 := f.Close(); err == nil { + err = err1 + } + if err == nil { + err = writeFileContent(cmd.entry.ContentPath, cmd.zettel.Content.AsString()) + } + } + } + return err +} + +func (cmd *fileSetZettel) runMetaSpecHeader() error { + f, err := openFileWrite(cmd.entry.ContentPath) + if err == nil { + err = writeFileZid(f, cmd.zettel.Meta.Zid) + if err == nil { + _, err = cmd.zettel.Meta.WriteAsHeader(f, true) + if err == nil { + _, err = f.WriteString(cmd.zettel.Content.AsString()) + if err1 := f.Close(); err == nil { + err = err1 + } + } + } + } + return err +} // COMMAND: deleteZettel ---------------------------------------- // // Deletes an existing zettel. @@ -250,12 +255,11 @@ func cleanupMeta(m *meta.Meta, entry *directory.Entry) { if title, ok := m.Get(meta.KeyTitle); !ok || title == "" { m.Set(meta.KeyTitle, entry.Zid.String()) } - switch entry.MetaSpec { - case directory.MetaSpecFile: + if entry.MetaSpec == directory.MetaSpecFile { if syntax, ok := m.Get(meta.KeySyntax); !ok || syntax == "" { dm := entry.CalcDefaultMeta() syntax, ok = dm.Get(meta.KeySyntax) if !ok { panic("Default meta must contain syntax") @@ -268,11 +272,11 @@ m.Set(meta.KeyDuplicates, meta.ValueTrue) } } func openFileWrite(path string) (*os.File, error) { - return os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + return os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) } func writeFileZid(f *os.File, zid id.Zid) error { _, err := f.WriteString("id: ") if err == nil { Index: place/filter.go ================================================================== --- place/filter.go +++ place/filter.go @@ -31,12 +31,11 @@ func selectAll(m *meta.Meta) bool { return true } type matchFunc func(value string) bool -func matchAlways(value string) bool { return true } -func matchNever(value string) bool { return false } +func matchNever(value string) bool { return false } type matchSpec struct { key string match matchFunc } @@ -44,26 +43,11 @@ // CreateFilterFunc calculates a filter func based on the given filter. func CreateFilterFunc(filter *Filter) FilterFunc { if filter == nil { return selectAll } - - specs := make([]matchSpec, 0, len(filter.Expr)) - var searchAll FilterFunc - for key, values := range filter.Expr { - if len(key) == 0 { - // Special handling if searching all keys... - searchAll = createSearchAllFunc(values, filter.Negate) - continue - } - if meta.KeyIsValid(key) { - match := createMatchFunc(key, values) - if match != nil { - specs = append(specs, matchSpec{key, match}) - } - } - } + specs, searchAll := createFilterSpecs(filter) if len(specs) == 0 { if searchAll == nil { if sel := filter.Select; sel != nil { return sel } @@ -86,10 +70,29 @@ } return addSelectFunc(filter, func(meta *meta.Meta) bool { return searchAll(meta) || searchMeta(meta) }) } + +func createFilterSpecs(filter *Filter) ([]matchSpec, FilterFunc) { + specs := make([]matchSpec, 0, len(filter.Expr)) + var searchAll FilterFunc + for key, values := range filter.Expr { + if key == "" { + // Special handling if searching all keys... + searchAll = createSearchAllFunc(values, filter.Negate) + continue + } + if meta.KeyIsValid(key) { + match := createMatchFunc(key, values) + if match != nil { + specs = append(specs, matchSpec{key, match}) + } + } + } + return specs, searchAll +} func addSelectFunc(filter *Filter, f FilterFunc) FilterFunc { if filter == nil { return f } @@ -102,86 +105,117 @@ } func createMatchFunc(key string, values []string) matchFunc { switch meta.Type(key) { case meta.TypeBool: - preValues := make([]bool, 0, len(values)) - for _, v := range values { - preValues = append(preValues, meta.BoolValue(v)) - } - return func(value string) bool { - bValue := meta.BoolValue(value) - for _, v := range preValues { - if bValue != v { - return false - } - } - return true - } + return createMatchBoolFunc(values) case meta.TypeCredential: return matchNever case meta.TypeID, meta.TypeTimestamp: // ID and timestamp use the same layout - return func(value string) bool { - for _, v := range values { - if !strings.HasPrefix(value, v) { - return false - } - } - return true - } - case meta.TypeIDSet: - idValues := preprocessSet(sliceToLower(values)) - return func(value string) bool { - ids := meta.ListFromValue(value) - for _, neededIDs := range idValues { - for _, neededID := range neededIDs { - if !matchAllID(ids, neededID) { - return false - } - } - } - return true - } - case meta.TypeTagSet: - tagValues := preprocessSet(values) - return func(value string) bool { - tags := meta.ListFromValue(value) - for _, neededTags := range tagValues { - for _, neededTag := range neededTags { - if !matchAllTag(tags, neededTag) { - return false - } - } - } - return true - } - case meta.TypeWord: - values = sliceToLower(values) - return func(value string) bool { - value = strings.ToLower(value) - for _, v := range values { - if value != v { - return false - } - } - return true - } - case meta.TypeWordSet: - wordValues := preprocessSet(sliceToLower(values)) - return func(value string) bool { - words := meta.ListFromValue(value) - for _, neededWords := range wordValues { - for _, neededWord := range neededWords { - if !matchAllWord(words, neededWord) { - return false - } - } - } - return true - } - } - + return createMatchIDFunc(values) + case meta.TypeIDSet: + return createMatchIDSetFunc(values) + case meta.TypeTagSet: + return createMatchTagSetFunc(values) + case meta.TypeWord: + return createMatchWordFunc(values) + case meta.TypeWordSet: + return createMatchWordSetFunc(values) + } + return createMatchStringFunc(values) +} + +func createMatchBoolFunc(values []string) matchFunc { + preValues := make([]bool, 0, len(values)) + for _, v := range values { + preValues = append(preValues, meta.BoolValue(v)) + } + return func(value string) bool { + bValue := meta.BoolValue(value) + for _, v := range preValues { + if bValue != v { + return false + } + } + return true + } +} + +func createMatchIDFunc(values []string) matchFunc { + return func(value string) bool { + for _, v := range values { + if !strings.HasPrefix(value, v) { + return false + } + } + return true + } +} + +func createMatchIDSetFunc(values []string) matchFunc { + idValues := preprocessSet(sliceToLower(values)) + return func(value string) bool { + ids := meta.ListFromValue(value) + for _, neededIDs := range idValues { + for _, neededID := range neededIDs { + if !matchAllID(ids, neededID) { + return false + } + } + } + return true + } +} + +func createMatchTagSetFunc(values []string) matchFunc { + tagValues := preprocessSet(values) + return func(value string) bool { + tags := meta.ListFromValue(value) + // Remove leading '#' from each tag + for i, tag := range tags { + tags[i] = meta.CleanTag(tag) + } + for _, neededTags := range tagValues { + for _, neededTag := range neededTags { + if !matchAllTag(tags, neededTag) { + return false + } + } + } + return true + } +} + +func createMatchWordFunc(values []string) matchFunc { + values = sliceToLower(values) + return func(value string) bool { + value = strings.ToLower(value) + for _, v := range values { + if value != v { + return false + } + } + return true + } +} + +func createMatchWordSetFunc(values []string) matchFunc { + wordValues := preprocessSet(sliceToLower(values)) + return func(value string) bool { + words := meta.ListFromValue(value) + for _, neededWords := range wordValues { + for _, neededWord := range neededWords { + if !matchAllWord(words, neededWord) { + return false + } + } + } + return true + } +} + +func createMatchStringFunc(values []string) matchFunc { values = sliceToLower(values) return func(value string) bool { value = strings.ToLower(value) for _, v := range values { if !strings.Contains(value, v) { @@ -236,19 +270,10 @@ result = append(result, strings.ToLower(s)) } return result } -func isEmptySlice(sl []string) bool { - for _, s := range sl { - if len(s) > 0 { - return false - } - } - return true -} - func preprocessSet(set []string) [][]string { result := make([][]string, 0, len(set)) for _, elem := range set { splitElems := strings.Split(elem, ",") valueElems := make([]string, 0, len(splitElems)) Index: place/manager/manager.go ================================================================== --- place/manager/manager.go +++ place/manager/manager.go @@ -89,11 +89,10 @@ // Manager is a coordinating place. type Manager struct { mx sync.RWMutex started bool - placeURIs []url.URL subplaces []place.Place filter index.MetaFilter observers []func(place.ChangeInfo) mxObserver sync.RWMutex done chan struct{} @@ -191,21 +190,25 @@ if mgr.started { mgr.mx.Unlock() return place.ErrStarted } for i := len(mgr.subplaces) - 1; i >= 0; i-- { - if ssi, ok := mgr.subplaces[i].(place.StartStopper); ok { - if err := ssi.Start(ctx); err != nil { - for j := i + 1; j < len(mgr.subplaces); j++ { - if ssj, ok := mgr.subplaces[j].(place.StartStopper); ok { - ssj.Stop(ctx) - } - } - mgr.mx.Unlock() - return err + ssi, ok := mgr.subplaces[i].(place.StartStopper) + if !ok { + continue + } + err := ssi.Start(ctx) + if err == nil { + continue + } + for j := i + 1; j < len(mgr.subplaces); j++ { + if ssj, ok := mgr.subplaces[j].(place.StartStopper); ok { + ssj.Stop(ctx) } } + mgr.mx.Unlock() + return err } mgr.done = make(chan struct{}) go notifier(mgr.notifyObserver, mgr.infos, mgr.done) mgr.started = true mgr.mx.Unlock() @@ -282,11 +285,11 @@ } return nil, place.ErrNotFound } // FetchZids returns the set of all zettel identifer managed by the place. -func (mgr *Manager) FetchZids(ctx context.Context) (result map[id.Zid]bool, err error) { +func (mgr *Manager) FetchZids(ctx context.Context) (result id.Set, err error) { mgr.mx.RLock() defer mgr.mx.RUnlock() if !mgr.started { return nil, place.ErrStopped } @@ -417,28 +420,10 @@ } } return place.ErrNotFound } -// Reload clears all caches, reloads all internal data to reflect changes -// that were possibly undetected. -func (mgr *Manager) Reload(ctx context.Context) error { - mgr.mx.RLock() - defer mgr.mx.RUnlock() - if !mgr.started { - return place.ErrStopped - } - var err error - for _, p := range mgr.subplaces { - if err1 := p.Reload(ctx); err1 != nil && err == nil { - err = err1 - } - } - mgr.infos <- place.ChangeInfo{Reason: place.OnReload, Zid: id.Invalid} - return err -} - // ReadStats populates st with place statistics func (mgr *Manager) ReadStats(st *place.Stats) { subStats := make([]place.Stats, len(mgr.subplaces)) for i, p := range mgr.subplaces { p.ReadStats(&subStats[i]) Index: place/memplace/memplace.go ================================================================== --- place/memplace/memplace.go +++ place/memplace/memplace.go @@ -109,13 +109,13 @@ return nil, place.ErrNotFound } return zettel.Meta.Clone(), nil } -func (mp *memPlace) FetchZids(ctx context.Context) (map[id.Zid]bool, error) { +func (mp *memPlace) FetchZids(ctx context.Context) (id.Set, error) { mp.mx.RLock() - result := make(map[id.Zid]bool, len(mp.zettel)) + result := id.NewSetCap(len(mp.zettel)) for zid := range mp.zettel { result[zid] = true } mp.mx.RUnlock() return result, nil @@ -197,13 +197,11 @@ mp.mx.Unlock() mp.notifyChanged(place.OnDelete, zid) return nil } -func (mp *memPlace) Reload(ctx context.Context) error { return nil } - func (mp *memPlace) ReadStats(st *place.Stats) { st.ReadOnly = false mp.mx.RLock() st.Zettel = len(mp.zettel) mp.mx.RUnlock() } Index: place/place.go ================================================================== --- place/place.go +++ place/place.go @@ -39,11 +39,11 @@ // GetMeta retrieves just the meta data of a specific zettel. GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) // FetchZids returns the set of all zettel identifer managed by the place. - FetchZids(ctx context.Context) (map[id.Zid]bool, error) + FetchZids(ctx context.Context) (id.Set, error) // SelectMeta returns all zettel meta data that match the selection criteria. // TODO: more docs SelectMeta(ctx context.Context, f *Filter, s *Sorter) ([]*meta.Meta, error) @@ -63,14 +63,10 @@ CanDeleteZettel(ctx context.Context, zid id.Zid) bool // DeleteZettel removes the zettel from the place. DeleteZettel(ctx context.Context, zid id.Zid) error - // Reload clears all caches, reloads all internal data to reflect changes - // that were possibly undetected. - Reload(ctx context.Context) error - // ReadStats populates st with place statistics ReadStats(st *Stats) } // Stats records statistics about the place. @@ -140,26 +136,26 @@ func (err *ErrNotAllowed) Error() string { if err.User == nil { if err.Zid.IsValid() { return fmt.Sprintf( - "Operation %q on zettel %v not allowed for not authorized user", + "operation %q on zettel %v not allowed for not authorized user", err.Op, err.Zid.String()) } - return fmt.Sprintf("Operation %q not allowed for not authorized user", err.Op) + return fmt.Sprintf("operation %q not allowed for not authorized user", err.Op) } if err.Zid.IsValid() { return fmt.Sprintf( - "Operation %q on zettel %v not allowed for user %v/%v", + "operation %q on zettel %v not allowed for user %v/%v", err.Op, err.Zid.String(), err.User.GetDefault(meta.KeyUserID, "?"), err.User.Zid.String()) } return fmt.Sprintf( - "Operation %q not allowed for user %v/%v", + "operation %q not allowed for user %v/%v", err.Op, err.User.GetDefault(meta.KeyUserID, "?"), err.User.Zid.String()) } @@ -168,25 +164,25 @@ _, ok := err.(*ErrNotAllowed) return ok } // ErrStarted is returned when trying to start an already started place. -var ErrStarted = errors.New("Place is already started") +var ErrStarted = errors.New("place is already started") // ErrStopped is returned if calling methods on a place that was not started. -var ErrStopped = errors.New("Place is stopped") +var ErrStopped = errors.New("place is stopped") // ErrReadOnly is returned if there is an attepmt to write to a read-only place. -var ErrReadOnly = errors.New("Read-only place") +var ErrReadOnly = errors.New("read-only place") // ErrNotFound is returned if a zettel was not found in the place. -var ErrNotFound = errors.New("Zettel not found") +var ErrNotFound = errors.New("zettel not found") // ErrInvalidID is returned if the zettel id is not appropriate for the place operation. type ErrInvalidID struct{ Zid id.Zid } -func (err *ErrInvalidID) Error() string { return "Invalid Zettel id: " + err.Zid.String() } +func (err *ErrInvalidID) Error() string { return "invalid Zettel id: " + err.Zid.String() } // Filter specifies a mechanism for selecting zettel. type Filter struct { Expr FilterExpr Negate bool Index: place/progplace/progplace.go ================================================================== --- place/progplace/progplace.go +++ place/progplace/progplace.go @@ -120,12 +120,12 @@ } } return nil, place.ErrNotFound } -func (pp *progPlace) FetchZids(ctx context.Context) (map[id.Zid]bool, error) { - result := make(map[id.Zid]bool, len(pp.zettel)) +func (pp *progPlace) FetchZids(ctx context.Context) (id.Set, error) { + result := id.NewSetCap(len(pp.zettel)) for zid, gen := range pp.zettel { if genMeta := gen.meta; genMeta != nil { if genMeta(zid) != nil { result[zid] = true } @@ -178,12 +178,10 @@ return place.ErrReadOnly } return place.ErrNotFound } -func (pp *progPlace) Reload(ctx context.Context) error { return nil } - func (pp *progPlace) ReadStats(st *place.Stats) { st.ReadOnly = true st.Zettel = len(pp.zettel) } Index: place/sorter.go ================================================================== --- place/sorter.go +++ place/sorter.go @@ -44,17 +44,17 @@ }) return metaList } if s.Order == "" { - sort.Slice(metaList, getSortFunc(meta.KeyID, true, metaList)) + sort.Slice(metaList, createSortFunc(meta.KeyID, true, metaList)) } else if s.Order == RandomOrder { rand.Shuffle(len(metaList), func(i, j int) { metaList[i], metaList[j] = metaList[j], metaList[i] }) } else { - sort.Slice(metaList, getSortFunc(s.Order, s.Descending, metaList)) + sort.Slice(metaList, createSortFunc(s.Order, s.Descending, metaList)) } if s.Offset > 0 { if s.Offset > len(metaList) { return nil @@ -67,49 +67,62 @@ return metaList } type sortFunc func(i, j int) bool -func getSortFunc(key string, descending bool, ml []*meta.Meta) sortFunc { +func createSortFunc(key string, descending bool, ml []*meta.Meta) sortFunc { keyType := meta.Type(key) if key == meta.KeyID || keyType == meta.TypeCredential { if descending { return func(i, j int) bool { return ml[i].Zid > ml[j].Zid } } return func(i, j int) bool { return ml[i].Zid < ml[j].Zid } - } else if keyType == meta.TypeBool { - if descending { - return func(i, j int) bool { - left := ml[i].GetBool(key) - if left == ml[j].GetBool(key) { - return i > j - } - return left - } - } - return func(i, j int) bool { - right := ml[j].GetBool(key) - if ml[i].GetBool(key) == right { - return i < j - } - return right - } - } else if keyType == meta.TypeNumber { - if descending { - return func(i, j int) bool { - iVal, iOk := getNum(ml[i], key) - jVal, jOk := getNum(ml[j], key) - return (iOk && (!jOk || iVal > jVal)) || !jOk - } - } - return func(i, j int) bool { - iVal, iOk := getNum(ml[i], key) - jVal, jOk := getNum(ml[j], key) - return (iOk && (!jOk || iVal < jVal)) || !jOk - } - } - + } + if keyType == meta.TypeBool { + return createSortBoolFunc(ml, key, descending) + } + if keyType == meta.TypeNumber { + return createSortNumberFunc(ml, key, descending) + } + return createSortStringFunc(ml, key, descending) +} + +func createSortBoolFunc(ml []*meta.Meta, key string, descending bool) sortFunc { + if descending { + return func(i, j int) bool { + left := ml[i].GetBool(key) + if left == ml[j].GetBool(key) { + return i > j + } + return left + } + } + return func(i, j int) bool { + right := ml[j].GetBool(key) + if ml[i].GetBool(key) == right { + return i < j + } + return right + } +} + +func createSortNumberFunc(ml []*meta.Meta, key string, descending bool) sortFunc { + if descending { + return func(i, j int) bool { + iVal, iOk := getNum(ml[i], key) + jVal, jOk := getNum(ml[j], key) + return (iOk && (!jOk || iVal > jVal)) || !jOk + } + } + return func(i, j int) bool { + iVal, iOk := getNum(ml[i], key) + jVal, jOk := getNum(ml[j], key) + return (iOk && (!jOk || iVal < jVal)) || !jOk + } +} + +func createSortStringFunc(ml []*meta.Meta, key string, descending bool) sortFunc { if descending { return func(i, j int) bool { iVal, iOk := ml[i].Get(key) jVal, jOk := ml[j].Get(key) return (iOk && (!jOk || iVal > jVal)) || !jOk ADDED strfun/strfun.go Index: strfun/strfun.go ================================================================== --- /dev/null +++ strfun/strfun.go @@ -0,0 +1,23 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package strfun provides some string functions. +package strfun + +import ( + "strings" + "unicode" +) + +// TrimSpaceRight returns a slice of the string s, with all trailing white space removed, +// as defined by Unicode. +func TrimSpaceRight(s string) string { + return strings.TrimRightFunc(s, unicode.IsSpace) +} ADDED strfun/strfun_test.go Index: strfun/strfun_test.go ================================================================== --- /dev/null +++ strfun/strfun_test.go @@ -0,0 +1,50 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package strfun provides some string functions. +package strfun_test + +import ( + "testing" + + "zettelstore.de/z/strfun" +) + +func TestTrimSpaceRight(t *testing.T) { + const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000" + testcases := []struct { + in string + exp string + }{ + {"", ""}, + {"abc", "abc"}, + {" ", ""}, + {space, ""}, + {space + "abc" + space, space + "abc"}, + {" \t\r\n \t\t\r\r\n\n ", ""}, + {" \t\r\n x\t\t\r\r\n\n ", " \t\r\n x"}, + {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", " \u2000\t\r\n x\t\t\r\r\ny"}, + {"1 \t\r\n2", "1 \t\r\n2"}, + {" x\x80", " x\x80"}, + {" x\xc0", " x\xc0"}, + {"x \xc0\xc0 ", "x \xc0\xc0"}, + {"x \xc0", "x \xc0"}, + {"x \xc0 ", "x \xc0"}, + {"x \xc0\xc0 ", "x \xc0\xc0"}, + {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"}, + {"x ☺ ", "x ☺"}, + } + for i, tc := range testcases { + got := strfun.TrimSpaceRight(tc.in) + if got != tc.exp { + t.Errorf("%d/%q: expected %q, got %q", i, tc.in, tc.exp, got) + } + } +} Index: template/mustache.go ================================================================== --- template/mustache.go +++ template/mustache.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -226,10 +226,14 @@ type tagReadingResult struct { tag string standalone bool } + +var skipWhitespaceTagTypes = map[byte]bool{ + '#': true, '^': true, '/': true, '<': true, '>': true, '=': true, '!': true, +} func (tmpl *Template) readTag(mayStandalone bool) (*tagReadingResult, error) { var text string var err error if tmpl.p < len(tmpl.data) && tmpl.data[tmpl.p] == '{' { @@ -245,11 +249,11 @@ text = text[:len(text)-len(tmpl.ctag)] //trim the close tag off the text tag := strings.TrimSpace(text) - if len(tag) == 0 { + if tag == "" { return nil, parseError{tmpl.curline, "empty tag"} } eow := tmpl.p for i := tmpl.p; i < len(tmpl.data); i++ { @@ -259,15 +263,13 @@ } } // Skip all whitespaces apeared after these types of tags until end of line if // the line only contains a tag and whitespaces. - const skipWhitespaceTagTypes = "#^/<>=!" - standalone := true if mayStandalone { - if !strings.Contains(skipWhitespaceTagTypes, tag[0:1]) { + if _, ok := skipWhitespaceTagTypes[tag[0]]; !ok { standalone = false } else { if eow == len(tmpl.data) { standalone = true tmpl.p = eow @@ -325,11 +327,10 @@ tag := tagResult.tag switch tag[0] { case '!': //ignore comment - break case '#', '^': name := strings.TrimSpace(tag[1:]) sn := §ionNode{name, tag[0] == '^', tmpl.curline, []node{}} err := tmpl.parseSection(sn) if err != nil { @@ -401,11 +402,10 @@ tag := tagResult.tag switch tag[0] { case '!': //ignore comment - break case '#', '^': name := strings.TrimSpace(tag[1:]) sn := §ionNode{name, tag[0] == '^', tmpl.curline, []node{}} err := tmpl.parseSection(sn) if err != nil { @@ -497,11 +497,11 @@ continue Outer } } } if errMissing { - return reflect.Value{}, fmt.Errorf("Missing variable %q", name) + return reflect.Value{}, fmt.Errorf("missing variable %q", name) } return reflect.Value{}, nil } func isEmpty(v reflect.Value) bool { @@ -515,11 +515,11 @@ } switch val := valueInd; val.Kind() { case reflect.Array, reflect.Slice: return val.Len() == 0 case reflect.String: - return len(strings.TrimSpace(val.String())) == 0 + return strings.TrimSpace(val.String()) == "" default: return valueInd.IsZero() } } Index: template/mustache_test.go ================================================================== --- template/mustache_test.go +++ template/mustache_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -273,11 +273,11 @@ // Now set "error on missing varaible" and confirm we get errors. for _, test := range missing { output, err := renderString(test.tmpl, true, test.context) if err == nil { t.Errorf("%q expected missing variable error but got %q", test.tmpl, output) - } else if !strings.Contains(err.Error(), "Missing variable") { + } else if !strings.Contains(err.Error(), "missing variable") { t.Errorf("%q expected missing variable error but got %q", test.tmpl, err.Error()) } } } @@ -465,13 +465,10 @@ } case template.Section, template.InvertedSection: compareTags(t, tag.Tags(), expected[i].Tags) case template.Partial: compareTags(t, tag.Tags(), expected[i].Tags) - case template.Invalid: - t.Errorf("invalid tag type: %s", tagString(tag.Type())) - return default: t.Errorf("invalid tag type: %s", tagString(tag.Type())) return } } Index: template/spec_test.go ================================================================== --- template/spec_test.go +++ template/spec_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -30,11 +30,11 @@ "zettelstore.de/z/template" ) var enabledTests = map[string]map[string]bool{ - "comments.json": map[string]bool{ + "comments.json": { "Inline": true, "Multiline": true, "Standalone": true, "Indented Standalone": true, "Standalone Line Endings": true, @@ -43,11 +43,11 @@ "Multiline Standalone": true, "Indented Multiline Standalone": true, "Indented Inline": true, "Surrounding Whitespace": true, }, - "delimiters.json": map[string]bool{ + "delimiters.json": { "Pair Behavior": true, "Special Characters": true, "Sections": true, "Inverted Sections": true, "Partial Inheritence": true, @@ -59,11 +59,11 @@ "Surrounding Whitespace": true, "Standalone Line Endings": true, "Standalone Without Previous Line": true, "Standalone Without Newline": true, }, - "interpolation.json": map[string]bool{ + "interpolation.json": { "No Interpolation": true, "Basic Interpolation": true, "HTML Escaping": true, "Triple Mustache": true, "Ampersand": true, @@ -91,11 +91,11 @@ "Ampersand - Standalone": true, "Interpolation With Padding": true, "Triple Mustache With Padding": true, "Ampersand With Padding": true, }, - "inverted.json": map[string]bool{ + "inverted.json": { "Falsey": true, "Truthy": true, "Context": true, "List": true, "Empty List": true, @@ -114,11 +114,11 @@ "Surrounding Whitespace": true, "Standalone Line Endings": true, "Standalone Without Previous Line": true, "Standalone Without Newline": true, }, - "partials.json": map[string]bool{ + "partials.json": { "Basic Behavior": true, "Failed Lookup": true, "Context": true, "Recursion": true, "Surrounding Whitespace": true, @@ -127,11 +127,11 @@ "Standalone Without Previous Line": true, "Standalone Without Newline": true, "Standalone Indentation": true, "Padding Whitespace": true, }, - "sections.json": map[string]bool{ + "sections.json": { "Truthy": true, "Falsey": true, "Context": true, "Deeply Nested Contexts": true, "List": true, Index: testdata/content/link/20200215204700.zettel ================================================================== --- testdata/content/link/20200215204700.zettel +++ testdata/content/link/20200215204700.zettel @@ -4,5 +4,8 @@ [[https://zettelstore.de]] [[Config|00000000000100]] [[00000000000100]] [[Frag|#frag]] [[#frag]] +[[H|/hosted]] +[[B|//based]] +[[R|../rel]] Index: tests/markdown_test.go ================================================================== --- tests/markdown_test.go +++ tests/markdown_test.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -17,10 +17,11 @@ "io/ioutil" "regexp" "strings" "testing" + "zettelstore.de/z/ast" "zettelstore.de/z/encoder" _ "zettelstore.de/z/encoder/htmlenc" _ "zettelstore.de/z/encoder/jsonenc" _ "zettelstore.de/z/encoder/nativeenc" _ "zettelstore.de/z/encoder/textenc" @@ -63,10 +64,24 @@ "[foo\n\n[ref]: /uri\n", // 534 "\n", // 591 } var reHeadingID = regexp.MustCompile(` id="[^"]*"`) + +func TestEncoderAvailability(t *testing.T) { + encoderMissing := false + for _, format := range formats { + enc := encoder.Create(format) + if enc == nil { + t.Errorf("No encoder for %q found", format) + encoderMissing = true + } + } + if encoderMissing { + panic("At least one encoder is missing. See test log") + } +} func TestMarkdownSpec(t *testing.T) { content, err := ioutil.ReadFile("../testdata/markdown/spec.json") if err != nil { panic(err) @@ -73,76 +88,86 @@ } var testcases []markdownTestCase if err = json.Unmarshal(content, &testcases); err != nil { panic(err) } - for _, format := range formats { - enc := encoder.Create(format) - if enc == nil { - panic(fmt.Sprintf("No encoder for %q found", format)) - } - } excMap := make(map[string]bool, len(exceptions)) for _, exc := range exceptions { excMap[exc] = true } - htmlEncoder := encoder.Create("html", &encoder.BoolOption{Key: "xhtml", Value: true}) - zmkEncoder := encoder.Create("zmk") - var sb strings.Builder - for _, tc := range testcases { - testID := tc.Example*100 + 1 - ast := parser.ParseBlocks(input.NewInput(tc.Markdown), nil, "markdown") - - for _, format := range formats { - t.Run(fmt.Sprintf("Encode %v %v", format, testID), func(st *testing.T) { - encoder.Create(format).WriteBlocks(&sb, ast) - sb.Reset() - }) - } - if _, found := excMap[tc.Markdown]; !found { - t.Run(fmt.Sprintf("Encode md html %v", testID), func(st *testing.T) { - htmlEncoder.WriteBlocks(&sb, ast) - gotHTML := sb.String() - sb.Reset() - - mdHTML := tc.HTML - mdHTML = strings.ReplaceAll(mdHTML, "\"MAILTO:", "\"mailto:") - gotHTML = strings.ReplaceAll(gotHTML, " class=\"zs-external\"", "") - gotHTML = strings.ReplaceAll(gotHTML, "%2A", "*") // url.QueryEscape - if strings.Count(gotHTML, " 0 { - gotHTML = reHeadingID.ReplaceAllString(gotHTML, "") - } - if gotHTML != mdHTML { - mdHTML := strings.ReplaceAll(mdHTML, "
  • \n", "
  • ") - if gotHTML != mdHTML { - st.Errorf("\nCMD: %q\nExp: %q\nGot: %q", tc.Markdown, mdHTML, gotHTML) - } - } - }) - } - t.Run(fmt.Sprintf("Encode zmk %14d", testID), func(st *testing.T) { - zmkEncoder.WriteBlocks(&sb, ast) - gotFirst := sb.String() - sb.Reset() - - testID = tc.Example*100 + 2 - secondAst := parser.ParseBlocks(input.NewInput(gotFirst), nil, "zmk") - zmkEncoder.WriteBlocks(&sb, secondAst) - gotSecond := sb.String() - sb.Reset() - - if gotFirst != gotSecond { - //st.Errorf("\nCMD: %q\n1st: %q\n2nd: %q", tc.Markdown, gotFirst, gotSecond) - } - - testID = tc.Example*100 + 3 - thirdAst := parser.ParseBlocks(input.NewInput(gotFirst), nil, "zmk") - zmkEncoder.WriteBlocks(&sb, thirdAst) - gotThird := sb.String() - sb.Reset() - - if gotSecond != gotThird { - st.Errorf("\n1st: %q\n2nd: %q", gotSecond, gotThird) - } - }) - } + for _, tc := range testcases { + ast := parser.ParseBlocks(input.NewInput(tc.Markdown), nil, "markdown") + testAllEncodings(t, tc, ast) + if _, found := excMap[tc.Markdown]; !found { + testHTMLEncoding(t, tc, ast) + } + testZmkEncoding(t, tc, ast) + } +} + +func testAllEncodings(t *testing.T, tc markdownTestCase, ast ast.BlockSlice) { + var sb strings.Builder + testID := tc.Example*100 + 1 + for _, format := range formats { + t.Run(fmt.Sprintf("Encode %v %v", format, testID), func(st *testing.T) { + encoder.Create(format).WriteBlocks(&sb, ast) + sb.Reset() + }) + } +} + +func testHTMLEncoding(t *testing.T, tc markdownTestCase, ast ast.BlockSlice) { + htmlEncoder := encoder.Create("html", &encoder.BoolOption{Key: "xhtml", Value: true}) + var sb strings.Builder + testID := tc.Example*100 + 1 + t.Run(fmt.Sprintf("Encode md html %v", testID), func(st *testing.T) { + htmlEncoder.WriteBlocks(&sb, ast) + gotHTML := sb.String() + sb.Reset() + + mdHTML := tc.HTML + mdHTML = strings.ReplaceAll(mdHTML, "\"MAILTO:", "\"mailto:") + gotHTML = strings.ReplaceAll(gotHTML, " class=\"zs-external\"", "") + gotHTML = strings.ReplaceAll(gotHTML, "%2A", "*") // url.QueryEscape + if strings.Count(gotHTML, " 0 { + gotHTML = reHeadingID.ReplaceAllString(gotHTML, "") + } + if gotHTML != mdHTML { + mdHTML = strings.ReplaceAll(mdHTML, "
  • \n", "
  • ") + if gotHTML != mdHTML { + st.Errorf("\nCMD: %q\nExp: %q\nGot: %q", tc.Markdown, mdHTML, gotHTML) + } + } + }) +} + +func testZmkEncoding(t *testing.T, tc markdownTestCase, ast ast.BlockSlice) { + zmkEncoder := encoder.Create("zmk") + var sb strings.Builder + testID := tc.Example*100 + 1 + t.Run(fmt.Sprintf("Encode zmk %14d", testID), func(st *testing.T) { + zmkEncoder.WriteBlocks(&sb, ast) + gotFirst := sb.String() + sb.Reset() + + testID = tc.Example*100 + 2 + secondAst := parser.ParseBlocks(input.NewInput(gotFirst), nil, "zmk") + zmkEncoder.WriteBlocks(&sb, secondAst) + gotSecond := sb.String() + sb.Reset() + + // if gotFirst != gotSecond { + // st.Errorf("\nCMD: %q\n1st: %q\n2nd: %q", tc.Markdown, gotFirst, gotSecond) + // } + + testID = tc.Example*100 + 3 + thirdAst := parser.ParseBlocks(input.NewInput(gotFirst), nil, "zmk") + zmkEncoder.WriteBlocks(&sb, thirdAst) + gotThird := sb.String() + sb.Reset() + + if gotSecond != gotThird { + st.Errorf("\n1st: %q\n2nd: %q", gotSecond, gotThird) + } + }) + } Index: tests/regression_test.go ================================================================== --- tests/regression_test.go +++ tests/regression_test.go @@ -85,10 +85,11 @@ src, err := ioutil.ReadAll(f) return string(src), err } func checkFileContent(t *testing.T, filename string, gotContent string) { + t.Helper() wantContent, err := resultFile(filename) if err != nil { t.Error(err) return } @@ -125,46 +126,54 @@ if gotFirst != gotSecond { t.Errorf("\n1st: %q\n2nd: %q", gotFirst, gotSecond) } } + +func getPlaceName(p place.Place, root string) string { + return p.Location()[len("dir://")+len(root):] +} + +func checkContentPlace(t *testing.T, p place.Place, wd, placeName string) { + ss := p.(place.StartStopper) + if err := ss.Start(context.Background()); err != nil { + panic(err) + } + metaList, err := p.SelectMeta(context.Background(), nil, nil) + if err != nil { + panic(err) + } + for _, meta := range metaList { + zettel, err := p.GetZettel(context.Background(), meta.Zid) + if err != nil { + panic(err) + } + z := parser.ParseZettel(zettel, "") + for _, format := range formats { + t.Run(fmt.Sprintf("%s::%d(%s)", p.Location(), meta.Zid, format), func(st *testing.T) { + resultName := filepath.Join(wd, "result", "content", placeName, z.Zid.String()+"."+format) + checkBlocksFile(st, resultName, z, format) + }) + } + t.Run(fmt.Sprintf("%s::%d", p.Location(), meta.Zid), func(st *testing.T) { + checkZmkEncoder(st, z) + }) + } + if err := ss.Stop(context.Background()); err != nil { + panic(err) + } + +} func TestContentRegression(t *testing.T) { wd, err := os.Getwd() if err != nil { panic(err) } root, places := getFilePlaces(wd, "content") for _, p := range places { - ss := p.(place.StartStopper) - if err := ss.Start(context.Background()); err != nil { - panic(err) - } - placeName := p.Location()[len("dir://")+len(root):] - metaList, err := p.SelectMeta(context.Background(), nil, nil) - if err != nil { - panic(err) - } - for _, meta := range metaList { - zettel, err := p.GetZettel(context.Background(), meta.Zid) - if err != nil { - panic(err) - } - z := parser.ParseZettel(zettel, "") - for _, format := range formats { - t.Run(fmt.Sprintf("%s::%d(%s)", p.Location(), meta.Zid, format), func(st *testing.T) { - resultName := filepath.Join(wd, "result", "content", placeName, z.Zid.String()+"."+format) - checkBlocksFile(st, resultName, z, format) - }) - } - t.Run(fmt.Sprintf("%s::%d", p.Location(), meta.Zid), func(st *testing.T) { - checkZmkEncoder(st, z) - }) - } - if err := ss.Stop(context.Background()); err != nil { - panic(err) - } + checkContentPlace(t, p, wd, getPlaceName(p, root)) } } func checkMetaFile(t *testing.T, resultName string, zn *ast.ZettelNode, format string) { t.Helper() @@ -175,40 +184,43 @@ checkFileContent(t, resultName, sb.String()) return } panic(fmt.Sprintf("Unknown writer format %q", format)) } + +func checkMetaPlace(t *testing.T, p place.Place, wd, placeName string) { + ss := p.(place.StartStopper) + if err := ss.Start(context.Background()); err != nil { + panic(err) + } + metaList, err := p.SelectMeta(context.Background(), nil, nil) + if err != nil { + panic(err) + } + for _, meta := range metaList { + zettel, err := p.GetZettel(context.Background(), meta.Zid) + if err != nil { + panic(err) + } + z := parser.ParseZettel(zettel, "") + for _, format := range formats { + t.Run(fmt.Sprintf("%s::%d(%s)", p.Location(), meta.Zid, format), func(st *testing.T) { + resultName := filepath.Join(wd, "result", "meta", placeName, z.Zid.String()+"."+format) + checkMetaFile(st, resultName, z, format) + }) + } + } + if err := ss.Stop(context.Background()); err != nil { + panic(err) + } +} func TestMetaRegression(t *testing.T) { wd, err := os.Getwd() if err != nil { panic(err) } root, places := getFilePlaces(wd, "meta") for _, p := range places { - ss := p.(place.StartStopper) - if err := ss.Start(context.Background()); err != nil { - panic(err) - } - placeName := p.Location()[len("dir://")+len(root):] - metaList, err := p.SelectMeta(context.Background(), nil, nil) - if err != nil { - panic(err) - } - for _, meta := range metaList { - zettel, err := p.GetZettel(context.Background(), meta.Zid) - if err != nil { - panic(err) - } - z := parser.ParseZettel(zettel, "") - for _, format := range formats { - t.Run(fmt.Sprintf("%s::%d(%s)", p.Location(), meta.Zid, format), func(st *testing.T) { - resultName := filepath.Join(wd, "result", "meta", placeName, z.Zid.String()+"."+format) - checkMetaFile(st, resultName, z, format) - }) - } - } - if err := ss.Stop(context.Background()); err != nil { - panic(err) - } + checkMetaPlace(t, p, wd, getPlaceName(p, root)) } } Index: tests/result/content/link/20200215204700.djson ================================================================== --- tests/result/content/link/20200215204700.djson +++ tests/result/content/link/20200215204700.djson @@ -1,1 +1,1 @@ -[{"t":"Para","i":[{"t":"Link","q":"external","s":"https://zettelstore.de/z","i":[{"t":"Text","s":"Home"}]},{"t":"Soft"},{"t":"Link","q":"external","s":"https://zettelstore.de","i":[{"t":"Text","s":"https://zettelstore.de"}]},{"t":"Soft"},{"t":"Link","q":"zettel","s":"00000000000100","i":[{"t":"Text","s":"Config"}]},{"t":"Soft"},{"t":"Link","q":"zettel","s":"00000000000100","i":[{"t":"Text","s":"00000000000100"}]},{"t":"Soft"},{"t":"Link","q":"self","s":"#frag","i":[{"t":"Text","s":"Frag"}]},{"t":"Soft"},{"t":"Link","q":"self","s":"#frag","i":[{"t":"Text","s":"#frag"}]}]}] +[{"t":"Para","i":[{"t":"Link","q":"external","s":"https://zettelstore.de/z","i":[{"t":"Text","s":"Home"}]},{"t":"Soft"},{"t":"Link","q":"external","s":"https://zettelstore.de","i":[{"t":"Text","s":"https://zettelstore.de"}]},{"t":"Soft"},{"t":"Link","q":"zettel","s":"00000000000100","i":[{"t":"Text","s":"Config"}]},{"t":"Soft"},{"t":"Link","q":"zettel","s":"00000000000100","i":[{"t":"Text","s":"00000000000100"}]},{"t":"Soft"},{"t":"Link","q":"self","s":"#frag","i":[{"t":"Text","s":"Frag"}]},{"t":"Soft"},{"t":"Link","q":"self","s":"#frag","i":[{"t":"Text","s":"#frag"}]},{"t":"Soft"},{"t":"Link","q":"local","s":"/hosted","i":[{"t":"Text","s":"H"}]},{"t":"Soft"},{"t":"Link","q":"based","s":"/based","i":[{"t":"Text","s":"B"}]},{"t":"Soft"},{"t":"Link","q":"local","s":"../rel","i":[{"t":"Text","s":"R"}]}]}] Index: tests/result/content/link/20200215204700.html ================================================================== --- tests/result/content/link/20200215204700.html +++ tests/result/content/link/20200215204700.html @@ -1,6 +1,9 @@

    Home https://zettelstore.de Config 00000000000100 Frag -#frag

    +#frag +H +B +R

    Index: tests/result/content/link/20200215204700.native ================================================================== --- tests/result/content/link/20200215204700.native +++ tests/result/content/link/20200215204700.native @@ -1,1 +1,1 @@ -[Para Link EXTERNAL "https://zettelstore.de/z" [Text "Home"],Space,Link EXTERNAL "https://zettelstore.de" [],Space,Link ZETTEL "00000000000100" [Text "Config"],Space,Link ZETTEL "00000000000100" [],Space,Link SELF "#frag" [Text "Frag"],Space,Link SELF "#frag" []] +[Para Link EXTERNAL "https://zettelstore.de/z" [Text "Home"],Space,Link EXTERNAL "https://zettelstore.de" [],Space,Link ZETTEL "00000000000100" [Text "Config"],Space,Link ZETTEL "00000000000100" [],Space,Link SELF "#frag" [Text "Frag"],Space,Link SELF "#frag" [],Space,Link LOCAL "/hosted" [Text "H"],Space,Link BASED "/based" [Text "B"],Space,Link LOCAL "../rel" [Text "R"]] Index: tests/result/content/link/20200215204700.text ================================================================== --- tests/result/content/link/20200215204700.text +++ tests/result/content/link/20200215204700.text @@ -1,1 +1,1 @@ -Home Config Frag +Home Config Frag H B R ADDED tools/build.go Index: tools/build.go ================================================================== --- /dev/null +++ tools/build.go @@ -0,0 +1,369 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package main provides a command to build and run the software. +package main + +import ( + "archive/zip" + "bytes" + "errors" + "flag" + "fmt" + "io" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "time" +) + +func executeCommand(env []string, name string, arg ...string) (string, error) { + if verbose { + if len(env) > 0 { + for i, e := range env { + fmt.Fprintf(os.Stderr, "ENV%d %v\n", i+1, e) + } + } + fmt.Fprintln(os.Stderr, "EXEC", name, arg) + } + if len(env) > 0 { + env = append(env, os.Environ()...) + } + var out bytes.Buffer + cmd := exec.Command(name, arg...) + cmd.Env = env + cmd.Stdin = nil + cmd.Stdout = &out + cmd.Stderr = os.Stderr + err := cmd.Run() + return out.String(), err +} + +func readVersionFile() (string, error) { + content, err := ioutil.ReadFile("VERSION") + if err != nil { + return "", err + } + return strings.TrimFunc(string(content), func(r rune) bool { + return r <= ' ' + }), nil +} + +var fossilHash = regexp.MustCompile(`\[[0-9a-fA-F]+\]`) +var dirtyPrefixes = []string{"DELETED", "ADDED", "UPDATED", "CONFLICT", "EDITED", "RENAMED"} + +const dirtySuffix = "-dirty" + +func readFossilVersion() (string, error) { + s, err := executeCommand(nil, "fossil", "timeline", "--limit", "1") + if err != nil { + return "", err + } + hash := fossilHash.FindString(s) + if len(hash) < 3 { + return "", errors.New("no fossil hash found") + } + hash = hash[1 : len(hash)-1] + + s, err = executeCommand(nil, "fossil", "status") + if err != nil { + return "", err + } + for _, line := range splitLines(s) { + for _, prefix := range dirtyPrefixes { + if strings.HasPrefix(line, prefix) { + return hash + dirtySuffix, nil + } + } + } + return hash, nil +} + +func splitLines(s string) []string { + return strings.FieldsFunc(s, func(r rune) bool { + return r == '\n' || r == '\r' + }) +} + +func getVersionData() (string, string) { + base, err := readVersionFile() + if err != nil { + base = "dev" + } + fossil, err := readFossilVersion() + if err != nil { + return base, "" + } + return base, fossil +} + +func calcVersion(base, vcs string) string { return base + "+" + vcs } + +func getVersion() string { + base, vcs := getVersionData() + return calcVersion(base, vcs) +} + +func findExec(cmd string) string { + if path, err := executeCommand(nil, "which", "shadow"); err == nil && path != "" { + return path + } + return "" +} + +func cmdCheck() error { + if err := checkGoTest(); err != nil { + return err + } + if err := checkGoVet(); err != nil { + return err + } + if err := checkGoLint(); err != nil { + return err + } + if err := checkGoVetShadow(); err != nil { + return err + } + return checkFossilExtra() +} + +func checkGoTest() error { + out, err := executeCommand(nil, "go", "test", "./...") + if err != nil { + for _, line := range splitLines(out) { + if strings.HasPrefix(line, "ok") || strings.HasPrefix(line, "?") { + continue + } + fmt.Fprintln(os.Stderr, line) + } + } + return err +} + +func checkGoVet() error { + out, err := executeCommand(nil, "go", "vet", "./...") + if err != nil { + fmt.Fprintln(os.Stderr, "Some checks failed") + if len(out) > 0 { + fmt.Fprintln(os.Stderr, out) + } + } + return err +} + +func checkGoLint() error { + out, err := executeCommand(nil, "golint", "./...") + if err != nil { + fmt.Fprintln(os.Stderr, "Some lints failed") + if len(out) > 0 { + fmt.Fprintln(os.Stderr, out) + } + } + return err +} + +func checkGoVetShadow() error { + path := findExec("shadow") + if path == "" { + return nil + } + out, err := executeCommand(nil, "go", "vet", "-vettool", strings.TrimSpace(path), "./...") + if err != nil { + fmt.Fprintln(os.Stderr, "Some shadowed variables found") + if len(out) > 0 { + fmt.Fprintln(os.Stderr, out) + } + } + return err +} + +func checkFossilExtra() error { + out, err := executeCommand(nil, "fossil", "extra") + if err != nil { + fmt.Fprintln(os.Stderr, "Unable to execute 'fossil extra'") + return err + } + if len(out) > 0 { + fmt.Fprint(os.Stderr, "Warning: unversioned file(s):") + for i, extra := range splitLines(out) { + if i > 0 { + fmt.Fprint(os.Stderr, ",") + } + fmt.Fprintf(os.Stderr, " %q", extra) + } + fmt.Fprintln(os.Stderr) + } + return nil +} + +func cmdBuild() error { + return doBuild(nil, getVersion(), "bin/zettelstore") +} + +func doBuild(env []string, version, target string) error { + out, err := executeCommand( + env, + "go", "build", + "-tags", "osusergo,netgo", + "-trimpath", + "-ldflags", fmt.Sprintf("-X main.version=%v -w", version), + "-o", target, + "zettelstore.de/z/cmd/zettelstore", + ) + if err != nil { + return err + } + if len(out) > 0 { + fmt.Println(out) + } + return nil +} + +func cmdRelease() error { + base, fossil := getVersionData() + if strings.HasSuffix(base, "dev") { + base = base[:len(base)-3] + "preview-" + time.Now().Format("20060102") + } + if strings.HasSuffix(fossil, dirtySuffix) { + fmt.Fprintf(os.Stderr, "Warning: releasing a dirty version %v\n", fossil) + base = base + dirtySuffix + } + if err := cmdCheck(); err != nil { + return err + } + releases := []struct { + arch string + os string + env []string + name string + }{ + {"amd64", "linux", nil, "zettelstore"}, + {"arm", "linux", []string{"GOARM=6"}, "zettelstore"}, + {"amd64", "darwin", nil, "iZettelstore"}, + {"arm64", "darwin", nil, "iZettelstore"}, + {"amd64", "windows", nil, "zettelstore.exe"}, + } + for _, rel := range releases { + env := append(rel.env, "GOARCH="+rel.arch, "GOOS="+rel.os) + zsName := filepath.Join("releases", rel.name) + if err := doBuild(env, calcVersion(base, fossil), zsName); err != nil { + return err + } + zipName := fmt.Sprintf("zettelstore-%v-%v-%v.zip", base, rel.os, rel.arch) + if err := createZip(zsName, zipName, rel.name); err != nil { + return err + } + if err := os.Remove(zsName); err != nil { + return err + } + } + return nil +} + +func createZip(zsName, zipName, fileName string) error { + zsFile, err := os.Open(zsName) + if err != nil { + return err + } + defer zsFile.Close() + zipFile, err := os.OpenFile(filepath.Join("releases", zipName), os.O_RDWR|os.O_CREATE, 0600) + if err != nil { + return err + } + defer zipFile.Close() + + stat, err := zsFile.Stat() + if err != nil { + return err + } + fh, err := zip.FileInfoHeader(stat) + if err != nil { + return err + } + fh.Name = fileName + fh.Method = zip.Deflate + zw := zip.NewWriter(zipFile) + defer zw.Close() + w, err := zw.CreateHeader(fh) + if err != nil { + return err + } + _, err = io.Copy(w, zsFile) + return err +} + +func cmdClean() error { + for _, dir := range []string{"bin", "releases"} { + err := os.RemoveAll(dir) + if err != nil { + return err + } + } + return nil +} + +func cmdHelp() { + fmt.Println(`Usage: go run tools/build.go [-v] COMMAND + +Options: + -v Verbose output. + +Commands: + build Build the software for local computer. + check Check current working state: execute tests, static analysis tools, + extra files, ... + Is automatically done when releasing the software. + clean Remove all build and release directories. + help Outputs this text. + release Create the software for various platforms and put them in + appropriate named ZIP files. + version Print the current version of the software. + +All commands can be abbreviated as long as they remain unique.`) +} + +var ( + verbose bool +) + +func main() { + flag.BoolVar(&verbose, "v", false, "Verbose output") + flag.Parse() + var err error + args := flag.Args() + if len(args) < 1 { + cmdHelp() + } else { + switch args[0] { + case "b", "bu", "bui", "buil", "build": + err = cmdBuild() + case "r", "re", "rel", "rele", "relea", "releas", "release": + err = cmdRelease() + case "cl", "cle", "clea", "clean": + err = cmdClean() + case "v", "ve", "ver", "vers", "versi", "versio", "version": + fmt.Print(getVersion()) + case "ch", "che", "chec", "check": + err = cmdCheck() + case "h", "he", "hel", "help": + cmdHelp() + default: + fmt.Fprintf(os.Stderr, "Unknown command %q\n", args[0]) + cmdHelp() + os.Exit(1) + } + } + if err != nil { + fmt.Fprintln(os.Stderr, err) + } +} DELETED tools/version.go Index: tools/version.go ================================================================== --- tools/version.go +++ /dev/null @@ -1,74 +0,0 @@ -package main - -import ( - "bytes" - "errors" - "fmt" - "io/ioutil" - "os" - "os/exec" - "regexp" - "strings" -) - -func readVersionFile() (string, error) { - content, err := ioutil.ReadFile("VERSION") - if err != nil { - return "", err - } - return strings.TrimFunc(string(content), func(r rune) bool { - return r <= ' ' - }), nil -} - -var fossilHash = regexp.MustCompile("\\[[0-9a-fA-F]+\\]") -var dirtyPrefixes = []string{"DELETED", "ADDED", "UPDATED", "CONFLICT", "EDITED", "RENAMED"} - -func readFossilVersion() (string, error) { - var out bytes.Buffer - cmd := exec.Command("fossil", "timeline", "--limit", "1") - cmd.Stdin = nil - cmd.Stdout = &out - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return "", err - } - hash := fossilHash.FindString(out.String()) - if len(hash) < 3 { - return "", errors.New("No fossil hash found") - } - hash = hash[1 : len(hash)-1] - - out.Reset() - cmd = exec.Command("fossil", "status") - cmd.Stdin = nil - cmd.Stdout = &out - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return "", err - } - lines := strings.FieldsFunc(out.String(), func(r rune) bool { - return r == '\n' || r == '\r' - }) - for _, line := range lines { - for _, prefix := range dirtyPrefixes { - if strings.HasPrefix(line, prefix) { - return hash + "-dirty", nil - } - } - } - return hash, nil -} - -func main() { - base, err := readVersionFile() - if err != nil { - fmt.Fprintf(os.Stderr, "No VERSION found: %v\n", err) - base = "dev" - } - fossil, err := readFossilVersion() - if err != nil { - fmt.Print(base) - } - fmt.Printf("%v+%v", base, fossil) -} Index: usecase/authenticate.go ================================================================== --- usecase/authenticate.go +++ usecase/authenticate.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -42,11 +42,11 @@ ucGetUser: NewGetUser(port), } } // Run executes the use case. -func (uc Authenticate) Run(ctx context.Context, ident string, credential string, d time.Duration, k token.Kind) ([]byte, error) { +func (uc Authenticate) Run(ctx context.Context, ident, credential string, d time.Duration, k token.Kind) ([]byte, error) { identMeta, err := uc.ucGetUser.Run(ctx, ident) defer addDelay(time.Now(), 500*time.Millisecond, 100*time.Millisecond) if identMeta == nil || err != nil { compensateCompare() ADDED usecase/context.go Index: usecase/context.go ================================================================== --- /dev/null +++ usecase/context.go @@ -0,0 +1,189 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package usecase provides (business) use cases for the Zettelstore. +package usecase + +import ( + "context" + + "zettelstore.de/z/domain/id" + "zettelstore.de/z/domain/meta" + "zettelstore.de/z/place" +) + +// ZettelContextPort is the interface used by this use case. +type ZettelContextPort interface { + // GetMeta retrieves just the meta data of a specific zettel. + GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) + + SelectMeta(ctx context.Context, f *place.Filter, s *place.Sorter) ([]*meta.Meta, error) +} + +// ZettelContext is the data for this use case. +type ZettelContext struct { + port ZettelContextPort +} + +// NewZettelContext creates a new use case. +func NewZettelContext(port ZettelContextPort) ZettelContext { + return ZettelContext{port: port} +} + +// ZettelContextDirection determines the way, the context is calculated. +type ZettelContextDirection int + +// Constant values for ZettelContextDirection +const ( + _ ZettelContextDirection = iota + ZettelContextForward // Traverse all forwarding links + ZettelContextBackward // Traverse all backwaring links + ZettelContextBoth // Traverse both directions +) + +// ParseZCDirection returns a direction value for a given string. +func ParseZCDirection(s string) ZettelContextDirection { + switch s { + case "backward": + return ZettelContextBackward + case "forward": + return ZettelContextForward + } + return ZettelContextBoth +} + +// Run executes the use case. +func (uc ZettelContext) Run(ctx context.Context, zid id.Zid, dir ZettelContextDirection, depth, limit int) (result []*meta.Meta, err error) { + start, err := uc.port.GetMeta(ctx, zid) + if err != nil { + return nil, err + } + tasks := ztlCtx{depth: depth} + uc.addInitialTasks(ctx, &tasks, start) + visited := id.NewSet() + isBackward := dir == ZettelContextBoth || dir == ZettelContextBackward + isForward := dir == ZettelContextBoth || dir == ZettelContextForward + for !tasks.empty() { + m, curDepth := tasks.pop() + if _, ok := visited[m.Zid]; ok { + continue + } + visited[m.Zid] = true + result = append(result, m) + if limit > 0 && len(result) > limit { // start is the first element of result + break + } + curDepth++ + for _, p := range m.PairsRest(true) { + if p.Key == meta.KeyBackward { + if isBackward { + uc.addIDSet(ctx, &tasks, curDepth, p.Value) + } + continue + } + if p.Key == meta.KeyForward { + if isForward { + uc.addIDSet(ctx, &tasks, curDepth, p.Value) + } + continue + } + if p.Key != meta.KeyBack { + hasInverse := meta.Inverse(p.Key) != "" + if (!hasInverse || !isBackward) && (hasInverse || !isForward) { + continue + } + if t := meta.Type(p.Key); t == meta.TypeID { + uc.addID(ctx, &tasks, curDepth, p.Value) + } else if t == meta.TypeIDSet { + uc.addIDSet(ctx, &tasks, curDepth, p.Value) + } + } + } + } + return result, nil +} + +func (uc ZettelContext) addInitialTasks(ctx context.Context, tasks *ztlCtx, start *meta.Meta) { + tasks.add(start, 0) + tags, ok := start.GetTags(meta.KeyTags) + if !ok { + return + } + filter := place.Filter{Expr: map[string][]string{}} + limit := tasks.depth + if limit == 0 || limit > 10 { + limit = 10 + } + sorter := place.Sorter{Limit: limit} + for _, tag := range tags { + filter.Expr[meta.KeyTags] = []string{tag} + if ml, err := uc.port.SelectMeta(ctx, &filter, &sorter); err == nil { + for _, m := range ml { + tasks.add(m, 1) + } + } + } +} + +func (uc ZettelContext) addID(ctx context.Context, tasks *ztlCtx, depth int, value string) { + if zid, err := id.Parse(value); err == nil { + if m, err := uc.port.GetMeta(ctx, zid); err == nil { + tasks.add(m, depth) + } + } +} + +func (uc ZettelContext) addIDSet(ctx context.Context, tasks *ztlCtx, depth int, value string) { + for _, val := range meta.ListFromValue(value) { + uc.addID(ctx, tasks, depth, val) + } +} + +type ztlCtxTask struct { + next *ztlCtxTask + meta *meta.Meta + depth int +} + +type ztlCtx struct { + first *ztlCtxTask + last *ztlCtxTask + depth int +} + +func (zc *ztlCtx) add(m *meta.Meta, depth int) { + if zc.depth > 0 && depth > zc.depth { + return + } + task := &ztlCtxTask{next: nil, meta: m, depth: depth} + if zc.first == nil { + zc.first = task + zc.last = task + } else { + zc.last.next = task + zc.last = task + } +} + +func (zc *ztlCtx) empty() bool { + return zc.first == nil +} + +func (zc *ztlCtx) pop() (*meta.Meta, int) { + task := zc.first + if task == nil { + return nil, -1 + } + zc.first = task.next + if zc.first == nil { + zc.last = nil + } + return task.meta, task.depth +} Index: usecase/copy_zettel.go ================================================================== --- usecase/copy_zettel.go +++ usecase/copy_zettel.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -12,10 +12,11 @@ package usecase import ( "zettelstore.de/z/domain" "zettelstore.de/z/domain/meta" + "zettelstore.de/z/strfun" ) // CopyZettel is the data for this use case. type CopyZettel struct{} @@ -33,7 +34,8 @@ } else { title = "Copy" } m.Set(meta.KeyTitle, title) } - return domain.Zettel{Meta: m, Content: origZettel.Content} + content := strfun.TrimSpaceRight(origZettel.Content.AsString()) + return domain.Zettel{Meta: m, Content: domain.Content(content)} } Index: usecase/create_zettel.go ================================================================== --- usecase/create_zettel.go +++ usecase/create_zettel.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -16,10 +16,11 @@ "zettelstore.de/z/config/runtime" "zettelstore.de/z/domain" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" + "zettelstore.de/z/strfun" ) // CreateZettelPort is the interface used by this use case. type CreateZettelPort interface { // CreateZettel creates a new zettel. @@ -52,7 +53,8 @@ if syntax, ok := m.Get(meta.KeySyntax); !ok || syntax == "" { m.Set(meta.KeySyntax, runtime.GetDefaultSyntax()) } m.YamlSep = runtime.GetYAMLHeader() + zettel.Content = domain.Content(strfun.TrimSpaceRight(zettel.Content.AsString())) return uc.port.CreateZettel(ctx, zettel) } Index: usecase/get_user.go ================================================================== --- usecase/get_user.go +++ usecase/get_user.go @@ -59,12 +59,12 @@ return identMeta, nil } // Owner was not found or has another ident. Try via list search. filter := place.Filter{ Expr: map[string][]string{ - meta.KeyRole: []string{meta.ValueRoleUser}, - meta.KeyUserID: []string{ident}, + meta.KeyRole: {meta.ValueRoleUser}, + meta.KeyUserID: {ident}, }, } metaList, err := uc.port.SelectMeta(ctx, &filter, nil) if err != nil { return nil, err Index: usecase/list_tags.go ================================================================== --- usecase/list_tags.go +++ usecase/list_tags.go @@ -47,10 +47,11 @@ } result := make(TagData) for _, m := range metas { if tl, ok := m.GetList(meta.KeyTags); ok && len(tl) > 0 { for _, t := range tl { + t = meta.CleanTag(t) result[t] = append(result[t], m) } } } if minCount > 1 { Index: usecase/new_zettel.go ================================================================== --- usecase/new_zettel.go +++ usecase/new_zettel.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -11,11 +11,11 @@ // Package usecase provides (business) use cases for the zettelstore. package usecase import ( "zettelstore.de/z/domain" - "zettelstore.de/z/domain/meta" + "zettelstore.de/z/strfun" ) // NewZettel is the data for this use case. type NewZettel struct{} @@ -25,16 +25,15 @@ } // Run executes the use case. func (uc NewZettel) Run(origZettel domain.Zettel) domain.Zettel { m := origZettel.Meta.Clone() - if role, ok := m.Get(meta.KeyRole); ok && role == meta.ValueRoleNewTemplate { - const prefix = "new-" - for _, pair := range m.PairsRest(false) { - if key := pair.Key; len(key) > len(prefix) && key[0:len(prefix)] == prefix { - m.Set(key[len(prefix):], pair.Value) - m.Delete(key) - } + const prefix = "new-" + for _, pair := range m.PairsRest(false) { + if key := pair.Key; len(key) > len(prefix) && key[0:len(prefix)] == prefix { + m.Set(key[len(prefix):], pair.Value) + m.Delete(key) } } - return domain.Zettel{Meta: m, Content: origZettel.Content} + content := strfun.TrimSpaceRight(origZettel.Content.AsString()) + return domain.Zettel{Meta: m, Content: domain.Content(content)} } ADDED usecase/order.go Index: usecase/order.go ================================================================== --- /dev/null +++ usecase/order.go @@ -0,0 +1,55 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package usecase provides (business) use cases for the Zettelstore. +package usecase + +import ( + "context" + + "zettelstore.de/z/collect" + "zettelstore.de/z/domain/id" + "zettelstore.de/z/domain/meta" +) + +// ZettelOrderPort is the interface used by this use case. +type ZettelOrderPort interface { + // GetMeta retrieves just the meta data of a specific zettel. + GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) +} + +// ZettelOrder is the data for this use case. +type ZettelOrder struct { + port ZettelOrderPort + parseZettel ParseZettel +} + +// NewZettelOrder creates a new use case. +func NewZettelOrder(port ZettelOrderPort, parseZettel ParseZettel) ZettelOrder { + return ZettelOrder{port: port, parseZettel: parseZettel} +} + +// Run executes the use case. +func (uc ZettelOrder) Run( + ctx context.Context, zid id.Zid, syntax string, +) (start *meta.Meta, result []*meta.Meta, err error) { + zn, err := uc.parseZettel.Run(ctx, zid, syntax) + if err != nil { + return nil, nil, err + } + for _, ref := range collect.Order(zn) { + if zid, err := id.Parse(ref.URL.Path); err == nil { + if m, err := uc.port.GetMeta(ctx, zid); err == nil { + result = append(result, m) + } + } + } + return zn.Zettel.Meta, result, nil +} DELETED usecase/reload.go Index: usecase/reload.go ================================================================== --- usecase/reload.go +++ /dev/null @@ -1,38 +0,0 @@ -//----------------------------------------------------------------------------- -// Copyright (c) 2020 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. -//----------------------------------------------------------------------------- - -// Package usecase provides (business) use cases for the zettelstore. -package usecase - -import ( - "context" -) - -// ReloadPort is the interface used by this use case. -type ReloadPort interface { - // Reload clears all caches, reloads all internal data to reflect changes - // that were possibly undetected. - Reload(ctx context.Context) error -} - -// Reload is the data for this use case. -type Reload struct { - port ReloadPort -} - -// NewReload creates a new use case. -func NewReload(port ReloadPort) Reload { - return Reload{port: port} -} - -// Run executes the use case. -func (uc Reload) Run(ctx context.Context) error { - return uc.port.Reload(ctx) -} Index: usecase/rename_zettel.go ================================================================== --- usecase/rename_zettel.go +++ usecase/rename_zettel.go @@ -48,10 +48,14 @@ // Run executes the use case. func (uc RenameZettel) Run(ctx context.Context, curZid, newZid id.Zid) error { noEnrichCtx := index.NoEnrichContext(ctx) if _, err := uc.port.GetMeta(noEnrichCtx, curZid); err != nil { return err + } + if newZid == curZid { + // Nothing to do + return nil } if _, err := uc.port.GetMeta(noEnrichCtx, newZid); err == nil { return &ErrZidInUse{Zid: newZid} } return uc.port.RenameZettel(ctx, curZid, newZid) Index: usecase/update_zettel.go ================================================================== --- usecase/update_zettel.go +++ usecase/update_zettel.go @@ -16,10 +16,11 @@ "zettelstore.de/z/domain" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" "zettelstore.de/z/index" + "zettelstore.de/z/strfun" ) // UpdateZettelPort is the interface used by this use case. type UpdateZettelPort interface { // GetZettel retrieves a specific zettel. @@ -53,9 +54,9 @@ m.YamlSep = oldZettel.Meta.YamlSep if m.Zid == id.ConfigurationZid { m.Set(meta.KeySyntax, meta.ValueSyntaxNone) } if !hasContent { - zettel.Content = oldZettel.Content + zettel.Content = domain.Content(strfun.TrimSpaceRight(oldZettel.Content.AsString())) } return uc.port.UpdateZettel(ctx, zettel) } Index: web/adapter/api/get_links.go ================================================================== --- web/adapter/api/get_links.go +++ web/adapter/api/get_links.go @@ -97,14 +97,14 @@ } if kind&kindCite != 0 { outData.Cites = stringCites(summary.Cites) } - w.Header().Set("Content-Type", format2ContentType("json")) + w.Header().Set(adapter.ContentType, format2ContentType("json")) enc := json.NewEncoder(w) enc.SetEscapeHTML(false) - err = enc.Encode(&outData) + enc.Encode(&outData) } } func idURLRefs(refs []*ast.Reference) []jsonIDURL { result := make([]jsonIDURL, 0, len(refs)) @@ -201,14 +201,11 @@ func validKindMatter(kind kindType, matter matterType) bool { if kind == 0 { return false } if kind&kindLink != 0 { - if matter == 0 { - return false - } - return true + return matter != 0 } if kind&kindImage != 0 { if matter == 0 || matter == matterIncoming { return false } ADDED web/adapter/api/get_order.go Index: web/adapter/api/get_order.go ================================================================== --- /dev/null +++ web/adapter/api/get_order.go @@ -0,0 +1,39 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2021 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. +//----------------------------------------------------------------------------- + +// Package api provides api handlers for web requests. +package api + +import ( + "net/http" + + "zettelstore.de/z/domain/id" + "zettelstore.de/z/usecase" + "zettelstore.de/z/web/adapter" +) + +// MakeGetOrderHandler creates a new API handler to return zettel references +// of a given zettel. +func MakeGetOrderHandler(zettelOrder usecase.ZettelOrder) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + zid, err := id.Parse(r.URL.Path[1:]) + if err != nil { + http.NotFound(w, r) + return + } + q := r.URL.Query() + start, metas, err := zettelOrder.Run(r.Context(), zid, q.Get("syntax")) + if err != nil { + adapter.ReportUsecaseError(w, err) + return + } + writeMetaList(w, start, metas) + } +} Index: web/adapter/api/get_role_list.go ================================================================== --- web/adapter/api/get_role_list.go +++ web/adapter/api/get_role_list.go @@ -31,11 +31,11 @@ } format := adapter.GetFormat(r, r.URL.Query(), encoder.GetDefaultFormat()) switch format { case "json": - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) renderListRoleJSON(w, roleList) default: adapter.BadRequest(w, fmt.Sprintf("Role list not available in format %q", format)) } Index: web/adapter/api/get_tags_list.go ================================================================== --- web/adapter/api/get_tags_list.go +++ web/adapter/api/get_tags_list.go @@ -34,11 +34,11 @@ } format := adapter.GetFormat(r, r.URL.Query(), encoder.GetDefaultFormat()) switch format { case "json": - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) renderListTagsJSON(w, tagData) default: adapter.BadRequest(w, fmt.Sprintf("Tags list not available in format %q", format)) } } Index: web/adapter/api/get_zettel.go ================================================================== --- web/adapter/api/get_zettel.go +++ web/adapter/api/get_zettel.go @@ -17,10 +17,11 @@ "zettelstore.de/z/config/runtime" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" "zettelstore.de/z/encoder" + "zettelstore.de/z/index" "zettelstore.de/z/usecase" "zettelstore.de/z/web/adapter" ) // MakeGetZettelHandler creates a new HTTP handler to return a rendered zettel. @@ -33,25 +34,28 @@ return } ctx := r.Context() q := r.URL.Query() + format := adapter.GetFormat(r, q, encoder.GetDefaultFormat()) + if format == "raw" { + ctx = index.NoEnrichContext(ctx) + } zn, err := parseZettel.Run(ctx, zid, q.Get("syntax")) if err != nil { adapter.ReportUsecaseError(w, err) return } - format := adapter.GetFormat(r, q, encoder.GetDefaultFormat()) part := getPart(q, partZettel) switch format { case "json", "djson": if part == partUnknown { adapter.BadRequest(w, "Unknown _part parameter") return } - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) if format != "djson" { err = writeJSONZettel(w, zn, part) } else { err = writeDJSONZettel(ctx, w, zn, part, partZettel, getMeta) } @@ -69,11 +73,11 @@ switch part { case partZettel: inhMeta := false if format != "raw" { - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) inhMeta = true } enc := encoder.Create(format, &langOption, &linkAdapter, &imageAdapter, @@ -88,24 +92,24 @@ err = adapter.ErrNoSuchFormat } else { _, err = enc.WriteZettel(w, zn, inhMeta) } case partMeta: - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) if format == "raw" { // Don't write inherited meta data, just the raw err = writeMeta(w, zn.Zettel.Meta, format) } else { err = writeMeta(w, zn.InhMeta, format) } case partContent: if format == "raw" { if ct, ok := syntax2contentType(runtime.GetSyntax(zn.Zettel.Meta)); ok { - w.Header().Add("Content-Type", ct) + w.Header().Add(adapter.ContentType, ct) } } else { - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) } err = writeContent(w, zn, format, &langOption, &encoder.StringOption{ Key: meta.KeyMarkerExternal, ADDED web/adapter/api/get_zettel_context.go Index: web/adapter/api/get_zettel_context.go ================================================================== --- /dev/null +++ web/adapter/api/get_zettel_context.go @@ -0,0 +1,48 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2020-2021 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. +//----------------------------------------------------------------------------- + +// Package api provides api handlers for web requests. +package api + +import ( + "net/http" + + "zettelstore.de/z/domain/id" + "zettelstore.de/z/usecase" + "zettelstore.de/z/web/adapter" +) + +// MakeZettelContextHandler creates a new HTTP handler for the use case "zettel context". +func MakeZettelContextHandler(getContext usecase.ZettelContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + zid, err := id.Parse(r.URL.Path[1:]) + if err != nil { + http.NotFound(w, r) + return + } + q := r.URL.Query() + dir := usecase.ParseZCDirection(q.Get("dir")) + depth, ok := adapter.GetInteger(q, "depth") + if !ok || depth < 0 { + depth = 5 + } + limit, ok := adapter.GetInteger(q, "limit") + if !ok || limit < 0 { + limit = 200 + } + ctx := r.Context() + metaList, err := getContext.Run(ctx, zid, dir, depth, limit) + if err != nil { + adapter.ReportUsecaseError(w, err) + return + } + writeMetaList(w, metaList[0], metaList[1:]) + } +} Index: web/adapter/api/get_zettel_list.go ================================================================== --- web/adapter/api/get_zettel_list.go +++ web/adapter/api/get_zettel_list.go @@ -44,11 +44,11 @@ if err != nil { adapter.ReportUsecaseError(w, err) return } - w.Header().Set("Content-Type", format2ContentType(format)) + w.Header().Set(adapter.ContentType, format2ContentType(format)) switch format { case "html": renderListMetaHTML(w, metaList) case "json", "djson": renderListMetaXJSON(ctx, w, metaList, format, part, partMeta, getMeta, parseZettel) Index: web/adapter/api/json.go ================================================================== --- web/adapter/api/json.go +++ web/adapter/api/json.go @@ -42,10 +42,16 @@ type jsonMeta struct { ID string `json:"id"` URL string `json:"url"` Meta map[string]string `json:"meta"` } +type jsonMetaList struct { + ID string `json:"id"` + URL string `json:"url"` + Meta map[string]string `json:"meta"` + List []jsonMeta `json:"list"` +} type jsonContent struct { ID string `json:"id"` URL string `json:"url"` Encoding string `json:"encoding"` Content interface{} `json:"content"` @@ -85,13 +91,11 @@ case partID: outData = idData default: panic(part) } - enc := json.NewEncoder(w) - enc.SetEscapeHTML(false) - return enc.Encode(outData) + return encodeJSONData(w, outData, false) } func encodedContent(content domain.Content) (string, interface{}) { if content.IsBinary() { return "base64", content.AsBytes() @@ -286,5 +290,27 @@ } _, err := enc.WriteMeta(w, m) return err } + +func encodeJSONData(w http.ResponseWriter, data interface{}, addHeader bool) error { + w.Header().Set(adapter.ContentType, format2ContentType("json")) + enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) + return enc.Encode(data) +} + +func writeMetaList(w http.ResponseWriter, m *meta.Meta, metaList []*meta.Meta) error { + outData := jsonMetaList{ + ID: m.Zid.String(), + URL: adapter.NewURLBuilder('z').SetZid(m.Zid).String(), + Meta: m.Map(), + List: make([]jsonMeta, len(metaList)), + } + for i, m := range metaList { + outData.List[i].ID = m.Zid.String() + outData.List[i].URL = adapter.NewURLBuilder('z').SetZid(m.Zid).String() + outData.List[i].Meta = m.Map() + } + return encodeJSONData(w, outData, true) +} Index: web/adapter/api/login.go ================================================================== --- web/adapter/api/login.go +++ web/adapter/api/login.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -25,11 +25,11 @@ // MakePostLoginHandlerAPI creates a new HTTP handler to authenticate the given user via API. func MakePostLoginHandlerAPI(auth usecase.Authenticate) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !startup.WithAuth() { - w.Header().Set("Content-Type", format2ContentType("json")) + w.Header().Set(adapter.ContentType, format2ContentType("json")) writeJSONToken(w, "freeaccess", 24*366*10*time.Hour) return } _, apiDur := startup.TokenLifetime() authenticateViaJSON(auth, w, r, apiDur) @@ -51,11 +51,11 @@ w.Header().Set("WWW-Authenticate", `Bearer realm="Default"`) http.Error(w, "Authentication failed", http.StatusUnauthorized) return } - w.Header().Set("Content-Type", format2ContentType("json")) + w.Header().Set(adapter.ContentType, format2ContentType("json")) writeJSONToken(w, string(token), authDuration) } func authenticateForJSON( auth usecase.Authenticate, @@ -97,11 +97,11 @@ } totalLifetime := auth.Expires.Sub(auth.Issued) currentLifetime := auth.Now.Sub(auth.Issued) // If we are in the first quarter of the tokens lifetime, return the token if currentLifetime*4 < totalLifetime { - w.Header().Set("Content-Type", format2ContentType("json")) + w.Header().Set(adapter.ContentType, format2ContentType("json")) writeJSONToken(w, string(auth.Token), totalLifetime-currentLifetime) return } // Toke is a little bit aged. Create a new one @@ -109,9 +109,9 @@ token, err := token.GetToken(auth.User, apiDur, token.KindJSON) if err != nil { adapter.ReportUsecaseError(w, err) return } - w.Header().Set("Content-Type", format2ContentType("json")) + w.Header().Set(adapter.ContentType, format2ContentType("json")) writeJSONToken(w, string(token), apiDur) } } DELETED web/adapter/api/reload.go Index: web/adapter/api/reload.go ================================================================== --- web/adapter/api/reload.go +++ /dev/null @@ -1,22 +0,0 @@ -//----------------------------------------------------------------------------- -// Copyright (c) 2020 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. -//----------------------------------------------------------------------------- - -// Package api provides api handlers for web requests. -package api - -import ( - "net/http" -) - -// ReloadHandlerAPI creates a new HTTP handler for the use case "reload". -func ReloadHandlerAPI(w http.ResponseWriter, r *http.Request, format string) { - w.Header().Set("Content-Type", format2ContentType(format)) - w.WriteHeader(http.StatusNoContent) -} Index: web/adapter/api/request.go ================================================================== --- web/adapter/api/request.go +++ web/adapter/api/request.go @@ -30,11 +30,11 @@ "zettel": partZettel, } func getPart(q url.Values, defPart partType) partType { p := q.Get("_part") - if len(p) == 0 { + if p == "" { return defPart } if part, ok := partMap[p]; ok { return part } Index: web/adapter/encoding.go ================================================================== --- web/adapter/encoding.go +++ web/adapter/encoding.go @@ -15,10 +15,11 @@ "context" "errors" "strings" "zettelstore.de/z/ast" + "zettelstore.de/z/config/startup" "zettelstore.de/z/domain/id" "zettelstore.de/z/encoder" "zettelstore.de/z/index" "zettelstore.de/z/place" "zettelstore.de/z/usecase" @@ -49,11 +50,21 @@ getMeta usecase.GetMeta, part, format string, ) func(*ast.LinkNode) ast.InlineNode { return func(origLink *ast.LinkNode) ast.InlineNode { origRef := origLink.Ref - if origRef == nil || origRef.State != ast.RefStateZettel { + if origRef == nil { + return origLink + } + if origRef.State == ast.RefStateBased { + newLink := *origLink + newRef := ast.ParseReference(startup.URLPrefix() + origRef.Value[1:]) + newRef.State = ast.RefStateHosted + newLink.Ref = newRef + return &newLink + } + if origRef.State != ast.RefStateZettel { return origLink } zid, err := id.Parse(origRef.URL.Path) if err != nil { panic(err) @@ -70,11 +81,11 @@ } if fragment := origRef.URL.EscapedFragment(); len(fragment) > 0 { u.SetFragment(fragment) } newRef := ast.ParseReference(u.String()) - newRef.State = ast.RefStateZettelFound + newRef.State = ast.RefStateFound newLink.Ref = newRef return &newLink } if place.IsErrNotAllowed(err) { return &ast.FormatNode{ @@ -82,11 +93,11 @@ Attrs: origLink.Attrs, Inlines: origLink.Inlines, } } newRef := ast.ParseReference(origRef.Value) - newRef.State = ast.RefStateZettelBroken + newRef.State = ast.RefStateBroken newLink.Ref = newRef return &newLink } } @@ -102,9 +113,9 @@ panic(err) } newImage.Ref = ast.ParseReference( NewURLBuilder('z').SetZid(zid).AppendQuery("_part", "content").AppendQuery( "_format", "raw").String()) - newImage.Ref.State = ast.RefStateZettelFound + newImage.Ref.State = ast.RefStateFound return &newImage } } DELETED web/adapter/reload.go Index: web/adapter/reload.go ================================================================== --- web/adapter/reload.go +++ /dev/null @@ -1,38 +0,0 @@ -//----------------------------------------------------------------------------- -// Copyright (c) 2020 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. -//----------------------------------------------------------------------------- - -// Package adapter provides handlers for web requests. -package adapter - -import ( - "net/http" - - "zettelstore.de/z/encoder" - "zettelstore.de/z/usecase" -) - -// MakeReloadHandler creates a new HTTP handler for the use case "reload". -func MakeReloadHandler( - reload usecase.Reload, - apiHandler func(http.ResponseWriter, *http.Request, string), - htmlHandler http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - err := reload.Run(r.Context()) - if err != nil { - ReportUsecaseError(w, err) - return - } - - if format := GetFormat(r, r.URL.Query(), encoder.GetDefaultFormat()); format != "html" { - apiHandler(w, r, format) - } - htmlHandler(w, r) - } -} Index: web/adapter/request.go ================================================================== --- web/adapter/request.go +++ web/adapter/request.go @@ -18,10 +18,24 @@ "strings" "zettelstore.de/z/domain/meta" "zettelstore.de/z/place" ) + +// GetInteger returns the integer value of the named query key. +func GetInteger(q url.Values, key string) (int, bool) { + s := q.Get(key) + if s != "" { + if val, err := strconv.Atoi(s); err == nil { + return val, true + } + } + return 0, false +} + +// ContentType defines the HTTP header value "Content-Type". +const ContentType = "Content-Type" // GetFormat returns the data format selected by the caller. func GetFormat(r *http.Request, q url.Values, defFormat string) string { format := q.Get("_format") if len(format) > 0 { @@ -28,11 +42,11 @@ return format } if format, ok := getOneFormat(r, "Accept"); ok { return format } - if format, ok := getOneFormat(r, "Content-Type"); ok { + if format, ok := getOneFormat(r, ContentType); ok { return format } return defFormat } @@ -93,13 +107,13 @@ } case negateQKey: filter = place.EnsureFilter(filter) filter.Negate = true case sQKey: - if values := cleanQueryValues(values); len(values) > 0 { + if vals := cleanQueryValues(values); len(vals) > 0 { filter = place.EnsureFilter(filter) - filter.Expr[""] = values + filter.Expr[""] = vals } default: if !forSearch && meta.KeyIsValid(key) { filter = place.EnsureFilter(filter) filter.Expr[key] = cleanQueryValues(values) Index: web/adapter/urlbuilder.go ================================================================== --- web/adapter/urlbuilder.go +++ web/adapter/urlbuilder.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -34,26 +34,22 @@ return &URLBuilder{key: key} } // Clone an URLBuilder func (ub *URLBuilder) Clone() *URLBuilder { - copy := new(URLBuilder) - copy.key = ub.key + cpy := new(URLBuilder) + cpy.key = ub.key if len(ub.path) > 0 { - copy.path = make([]string, 0, len(ub.path)) - } - for _, p := range ub.path { - copy.path = append(copy.path, p) + cpy.path = make([]string, 0, len(ub.path)) + cpy.path = append(cpy.path, ub.path...) } if len(ub.query) > 0 { - copy.query = make([]urlQuery, 0, len(ub.query)) + cpy.query = make([]urlQuery, 0, len(ub.query)) + cpy.query = append(cpy.query, ub.query...) } - for _, q := range ub.query { - copy.query = append(copy.query, q) - } - copy.fragment = ub.fragment - return copy + cpy.fragment = ub.fragment + return cpy } // SetZid sets the zettel identifier. func (ub *URLBuilder) SetZid(zid id.Zid) *URLBuilder { if len(ub.path) > 0 { @@ -68,11 +64,11 @@ ub.path = append(ub.path, p) return ub } // AppendQuery adds a new query parameter -func (ub *URLBuilder) AppendQuery(key string, value string) *URLBuilder { +func (ub *URLBuilder) AppendQuery(key, value string) *URLBuilder { ub.query = append(ub.query, urlQuery{key, value}) return ub } // ClearQuery removes all query parameters. Index: web/adapter/webui/create_zettel.go ================================================================== --- web/adapter/webui/create_zettel.go +++ web/adapter/webui/create_zettel.go @@ -112,12 +112,11 @@ func renderZettelForm( w http.ResponseWriter, r *http.Request, te *TemplateEngine, zettel domain.Zettel, - title string, - heading string, + title, heading string, ) { ctx := r.Context() user := session.GetUser(ctx) m := zettel.Meta var base baseData Index: web/adapter/webui/get_info.go ================================================================== --- web/adapter/webui/get_info.go +++ web/adapter/webui/get_info.go @@ -18,11 +18,10 @@ "zettelstore.de/z/ast" "zettelstore.de/z/collect" "zettelstore.de/z/config/runtime" "zettelstore.de/z/domain/id" - "zettelstore.de/z/domain/meta" "zettelstore.de/z/encoder" "zettelstore.de/z/usecase" "zettelstore.de/z/web/adapter" "zettelstore.de/z/web/session" ) @@ -30,17 +29,10 @@ type metaDataInfo struct { Key string Value string } -type zettelReference struct { - Zid id.Zid - Title string - HasURL bool - URL string -} - type matrixElement struct { Text string HasURL bool URL string } @@ -118,18 +110,17 @@ te.makeBaseData(ctx, langOption.Value, textTitle, user, &base) canCopy := base.CanCreate && !zn.Zettel.Content.IsBinary() te.renderTemplate(ctx, w, id.InfoTemplateZid, &base, struct { Zid string WebURL string + ContextURL string CanWrite bool EditURL string CanFolge bool FolgeURL string CanCopy bool CopyURL string - CanNew bool - NewURL string CanRename bool RenameURL string CanDelete bool DeleteURL string MetaData []metaDataInfo @@ -139,23 +130,21 @@ HasExtLinks bool ExtLinks []string ExtNewWindow string Matrix []matrixLine }{ - Zid: zid.String(), - WebURL: adapter.NewURLBuilder('h').SetZid(zid).String(), - CanWrite: te.canWrite(ctx, user, zn.Zettel), - EditURL: adapter.NewURLBuilder('e').SetZid(zid).String(), - CanFolge: base.CanCreate && !zn.Zettel.Content.IsBinary(), - FolgeURL: adapter.NewURLBuilder('f').SetZid(zid).String(), - CanCopy: canCopy, - CopyURL: adapter.NewURLBuilder('c').SetZid(zid).String(), - CanNew: canCopy && zn.Zettel.Meta.GetDefault(meta.KeyRole, "") == - meta.ValueRoleNewTemplate, - NewURL: adapter.NewURLBuilder('n').SetZid(zid).String(), + Zid: zid.String(), + WebURL: adapter.NewURLBuilder('h').SetZid(zid).String(), + ContextURL: adapter.NewURLBuilder('j').SetZid(zid).String(), + CanWrite: te.canWrite(ctx, user, zn.Zettel), + EditURL: adapter.NewURLBuilder('e').SetZid(zid).String(), + CanFolge: base.CanCreate && !zn.Zettel.Content.IsBinary(), + FolgeURL: adapter.NewURLBuilder('f').SetZid(zid).String(), + CanCopy: canCopy, + CopyURL: adapter.NewURLBuilder('c').SetZid(zid).String(), CanRename: te.canRename(ctx, user, zn.Zettel.Meta), - RenameURL: adapter.NewURLBuilder('r').SetZid(zid).String(), + RenameURL: adapter.NewURLBuilder('b').SetZid(zid).String(), CanDelete: te.canDelete(ctx, user, zn.Zettel.Meta), DeleteURL: adapter.NewURLBuilder('d').SetZid(zid).String(), MetaData: metaData, HasLinks: len(extLinks)+len(locLinks) > 0, HasLocLinks: len(locLinks) > 0, @@ -166,16 +155,16 @@ Matrix: matrix, }) } } -func splitLocExtLinks(links []*ast.Reference) (locLinks []string, extLinks []string) { +func splitLocExtLinks(links []*ast.Reference) (locLinks, extLinks []string) { if len(links) == 0 { return nil, nil } for _, ref := range links { - if ref.State == ast.RefStateZettelSelf { + if ref.State == ast.RefStateSelf { continue } if ref.IsZettel() { continue } else if ref.IsExternal() { Index: web/adapter/webui/get_zettel.go ================================================================== --- web/adapter/webui/get_zettel.go +++ web/adapter/webui/get_zettel.go @@ -107,12 +107,10 @@ RoleURL string HasTags bool Tags []simpleLink CanCopy bool CopyURL string - CanNew bool - NewURL string CanFolge bool FolgeURL string FolgeRefs string PrecursorRefs string HasExtURL bool @@ -131,12 +129,10 @@ RoleURL: adapter.NewURLBuilder('h').AppendQuery("role", roleText).String(), HasTags: len(tags) > 0, Tags: tags, CanCopy: canCopy, CopyURL: adapter.NewURLBuilder('c').SetZid(zid).String(), - CanNew: canCopy && roleText == meta.ValueRoleNewTemplate, - NewURL: adapter.NewURLBuilder('n').SetZid(zid).String(), CanFolge: base.CanCreate && !zn.Zettel.Content.IsBinary(), FolgeURL: adapter.NewURLBuilder('f').SetZid(zid).String(), FolgeRefs: formatMetaKey(zn.InhMeta, meta.KeyFolge, getTitle), PrecursorRefs: formatMetaKey(zn.InhMeta, meta.KeyPrecursor, getTitle), ExtURL: extURL, @@ -179,18 +175,14 @@ } func buildTagInfos(m *meta.Meta) []simpleLink { var tagInfos []simpleLink if tags, ok := m.GetList(meta.KeyTags); ok { - tagInfos = make([]simpleLink, 0, len(tags)) ub := adapter.NewURLBuilder('h') - for _, t := range tags { - // Cast to template.HTML is ok, because "t" is a tag name - // and contains only legal characters by construction. - tagInfos = append( - tagInfos, - simpleLink{Text: t, URL: ub.AppendQuery("tags", t).String()}) + tagInfos = make([]simpleLink, len(tags)) + for i, tag := range tags { + tagInfos[i] = simpleLink{Text: tag, URL: ub.AppendQuery("tags", meta.CleanTag(tag)).String()} ub.ClearQuery() } } return tagInfos } Index: web/adapter/webui/home.go ================================================================== --- web/adapter/webui/home.go +++ web/adapter/webui/home.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -14,33 +14,48 @@ import ( "context" "net/http" "zettelstore.de/z/config/runtime" + "zettelstore.de/z/config/startup" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" + "zettelstore.de/z/place" + "zettelstore.de/z/web/adapter" + "zettelstore.de/z/web/session" ) type getRootStore interface { // GetMeta retrieves just the meta data of a specific zettel. GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error) } // MakeGetRootHandler creates a new HTTP handler to show the root URL. -func MakeGetRootHandler( - s getRootStore, startNotFound, startFound http.HandlerFunc) http.HandlerFunc { +func MakeGetRootHandler(s getRootStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } - startID := runtime.GetStart() - if startID.IsValid() { - if _, err := s.GetMeta(r.Context(), startID); err == nil { - r.URL.Path = "/" + startID.String() - startFound(w, r) + ok := false + ctx := r.Context() + homeZid := runtime.GetHomeZettel() + if homeZid != id.DefaultHomeZid && homeZid.IsValid() { + if _, err := s.GetMeta(ctx, homeZid); err != nil { + homeZid = id.DefaultHomeZid + } else { + ok = true + } + } + if !ok { + if _, err := s.GetMeta(ctx, homeZid); err != nil { + if place.IsErrNotAllowed(err) && startup.WithAuth() && session.GetUser(ctx) == nil { + http.Redirect(w, r, adapter.NewURLBuilder('a').String(), http.StatusFound) + return + } + http.Redirect(w, r, adapter.NewURLBuilder('h').String(), http.StatusFound) return } } - startNotFound(w, r) + http.Redirect(w, r, adapter.NewURLBuilder('h').SetZid(homeZid).String(), http.StatusFound) } } Index: web/adapter/webui/htmlmeta.go ================================================================== --- web/adapter/webui/htmlmeta.go +++ web/adapter/webui/htmlmeta.go @@ -65,21 +65,23 @@ if l, ok := m.GetList(key); ok { writeWordSet(w, key, l) } case meta.TypeZettelmarkup: writeZettelmarkup(w, m.GetDefault(key, "???z"), option) + case meta.TypeUnknown: + writeUnknown(w, m.GetDefault(key, "???u")) default: strfun.HTMLEscape(w, m.GetDefault(key, "???w"), false) fmt.Fprintf(w, " (Unhandled type: %v, key: %v)", kt, key) } } func writeHTMLBool(w io.Writer, key string, val bool) { if val { - writeLink(w, key, "True") + writeLink(w, key, "true", "True") } else { - writeLink(w, key, "False") + writeLink(w, key, "false", "False") } } func writeCredential(w io.Writer, val string) { strfun.HTMLEscape(w, val, false) @@ -130,17 +132,21 @@ } func writeString(w io.Writer, val string) { strfun.HTMLEscape(w, val, false) } + +func writeUnknown(w io.Writer, val string) { + strfun.HTMLEscape(w, val, false) +} func writeTagSet(w io.Writer, key string, tags []string) { for i, tag := range tags { if i > 0 { w.Write(space) } - writeLink(w, key, tag) + writeLink(w, key, meta.CleanTag(tag), tag) } } func writeTimestamp(w io.Writer, ts time.Time) { io.WriteString(w, ts.Format("2006-01-02 15:04:05")) @@ -156,11 +162,11 @@ strfun.HTMLEscape(w, val, false) io.WriteString(w, "") } func writeWord(w io.Writer, key, word string) { - writeLink(w, key, word) + writeLink(w, key, word, word) } func writeWordSet(w io.Writer, key string, words []string) { for i, word := range words { if i > 0 { @@ -177,15 +183,15 @@ return } io.WriteString(w, title) } -func writeLink(w io.Writer, key, value string) { +func writeLink(w io.Writer, key, value, text string) { fmt.Fprintf( w, "", adapter.NewURLBuilder('h'), url.QueryEscape(key), url.QueryEscape(value)) - strfun.HTMLEscape(w, value, false) + strfun.HTMLEscape(w, text, false) io.WriteString(w, "") } type getTitleFunc func(id.Zid, string) (string, int) Index: web/adapter/webui/lists.go ================================================================== --- web/adapter/webui/lists.go +++ web/adapter/webui/lists.go @@ -15,10 +15,11 @@ "context" "net/http" "net/url" "sort" "strconv" + "strings" "zettelstore.de/z/config/runtime" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" "zettelstore.de/z/encoder" @@ -30,47 +31,36 @@ "zettelstore.de/z/web/session" ) // MakeListHTMLMetaHandler creates a HTTP handler for rendering the list of zettel as HTML. func MakeListHTMLMetaHandler( - te *TemplateEngine, listMeta usecase.ListMeta) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - renderWebUIZettelList(w, r, te, listMeta) - } -} - -// MakeWebUIListsHandler creates a new HTTP handler for the use case "list some zettel". -func MakeWebUIListsHandler( te *TemplateEngine, listMeta usecase.ListMeta, listRole usecase.ListRole, listTags usecase.ListTags, ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - zid, err := id.Parse(r.URL.Path[1:]) - if err != nil { - http.NotFound(w, r) - return - } - switch zid { - case 1: - renderWebUIZettelList(w, r, te, listMeta) - case 2: + query := r.URL.Query() + switch query.Get("_l") { + case "r": renderWebUIRolesList(w, r, te, listRole) - case 3: + case "t": renderWebUITagsList(w, r, te, listTags) + default: + renderWebUIZettelList(w, r, te, listMeta) } } } func renderWebUIZettelList( w http.ResponseWriter, r *http.Request, te *TemplateEngine, listMeta usecase.ListMeta) { query := r.URL.Query() filter, sorter := adapter.GetFilterSorter(query, false) ctx := r.Context() + title := listTitleFilterSorter("Filter", filter, sorter) renderWebUIMetaList( - ctx, w, te, sorter, + ctx, w, te, title, sorter, func(sorter *place.Sorter) ([]*meta.Meta, error) { if filter == nil && (sorter == nil || sorter.Order == "") { ctx = index.NoEnrichContext(ctx) } return listMeta.Run(ctx, filter, sorter) @@ -203,26 +193,92 @@ http.Redirect(w, r, adapter.NewURLBuilder('h').String(), http.StatusFound) return } ctx := r.Context() + title := listTitleFilterSorter("Search", filter, sorter) renderWebUIMetaList( - ctx, w, te, sorter, + ctx, w, te, title, sorter, func(sorter *place.Sorter) ([]*meta.Meta, error) { if filter == nil && (sorter == nil || sorter.Order == "") { ctx = index.NoEnrichContext(ctx) } return search.Run(ctx, filter, sorter) }, func(offset int) string { - return newPageURL('s', query, offset, "offset", "limit") + return newPageURL('f', query, offset, "offset", "limit") }) } } + +// MakeZettelContextHandler creates a new HTTP handler for the use case "zettel context". +func MakeZettelContextHandler(te *TemplateEngine, getContext usecase.ZettelContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + zid, err := id.Parse(r.URL.Path[1:]) + if err != nil { + http.NotFound(w, r) + return + } + q := r.URL.Query() + dir := usecase.ParseZCDirection(q.Get("dir")) + depth, ok := adapter.GetInteger(q, "depth") + if !ok || depth < 0 { + depth = 5 + } + limit, ok := adapter.GetInteger(q, "limit") + if !ok || limit < 0 { + limit = 200 + } + ctx := r.Context() + metaList, err := getContext.Run(ctx, zid, dir, depth, limit) + if err != nil { + adapter.ReportUsecaseError(w, err) + return + } + metaLinks, err := buildHTMLMetaList(metaList) + if err != nil { + adapter.InternalServerError(w, "Build HTML meta list", err) + return + } + + depths := []string{"2", "3", "4", "5", "6", "7", "8", "9", "10"} + depthLinks := make([]simpleLink, len(depths)) + depthURL := adapter.NewURLBuilder('j').SetZid(zid) + for i, depth := range depths { + depthURL.ClearQuery() + switch dir { + case usecase.ZettelContextBackward: + depthURL.AppendQuery("dir", "backward") + case usecase.ZettelContextForward: + depthURL.AppendQuery("dir", "forward") + } + depthURL.AppendQuery("depth", depth) + depthLinks[i].Text = depth + depthLinks[i].URL = depthURL.String() + } + var base baseData + user := session.GetUser(ctx) + te.makeBaseData(ctx, runtime.GetDefaultLang(), runtime.GetSiteName(), user, &base) + te.renderTemplate(ctx, w, id.ContextTemplateZid, &base, struct { + Title string + InfoURL string + Depths []simpleLink + Start simpleLink + Metas []simpleLink + }{ + Title: "Zettel Context", + InfoURL: adapter.NewURLBuilder('i').SetZid(zid).String(), + Depths: depthLinks, + Start: metaLinks[0], + Metas: metaLinks[1:], + }) + } +} func renderWebUIMetaList( ctx context.Context, w http.ResponseWriter, te *TemplateEngine, + title string, sorter *place.Sorter, ucMetaList func(sorter *place.Sorter) ([]*meta.Meta, error), pageURL func(int) string) { var metaList []*meta.Meta @@ -265,26 +321,124 @@ } var base baseData te.makeBaseData(ctx, runtime.GetDefaultLang(), runtime.GetSiteName(), user, &base) te.renderTemplate(ctx, w, id.ListTemplateZid, &base, struct { Title string - Metas []metaInfo + Metas []simpleLink HasPrevNext bool HasPrev bool PrevURL string HasNext bool NextURL string }{ - Title: base.Title, + Title: title, Metas: metas, HasPrevNext: len(prevURL) > 0 || len(nextURL) > 0, HasPrev: len(prevURL) > 0, PrevURL: prevURL, HasNext: len(nextURL) > 0, NextURL: nextURL, }) } + +func listTitleFilterSorter(prefix string, filter *place.Filter, sorter *place.Sorter) string { + if filter == nil && sorter == nil { + return runtime.GetSiteName() + } + var sb strings.Builder + sb.WriteString(prefix) + sb.WriteString(": ") + if filter != nil { + listTitleFilter(&sb, filter) + if sorter != nil { + sb.WriteString(" | ") + listTitleSorter(&sb, sorter) + } + } else if sorter != nil { + listTitleSorter(&sb, sorter) + } + return sb.String() +} + +func listTitleFilter(sb *strings.Builder, filter *place.Filter) { + if filter.Negate { + sb.WriteString("NOT (") + } + names := make([]string, 0, len(filter.Expr)) + for name := range filter.Expr { + names = append(names, name) + } + sort.Strings(names) + for i, name := range names { + if i > 0 { + sb.WriteString(" AND ") + } + if name == "" { + sb.WriteString("ANY") + } else { + sb.WriteString(name) + } + sb.WriteString(" MATCH ") + writeFilterExprValues(sb, filter.Expr[name]) + } + if filter.Negate { + sb.WriteByte(')') + } +} + +func writeFilterExprValues(sb *strings.Builder, values []string) { + if len(values) == 0 { + sb.WriteString("ANY") + return + } + + for j, val := range values { + if j > 0 { + sb.WriteString(" AND ") + } + if val == "" { + sb.WriteString("ANY") + } else { + sb.WriteString(val) + } + } +} + +func listTitleSorter(sb *strings.Builder, sorter *place.Sorter) { + var space bool + if ord := sorter.Order; len(ord) > 0 { + switch ord { + case meta.KeyID: + // Ignore + case place.RandomOrder: + sb.WriteString("RANDOM") + space = true + default: + sb.WriteString("SORT ") + sb.WriteString(ord) + if sorter.Descending { + sb.WriteString(" DESC") + } + space = true + } + } + if off := sorter.Offset; off > 0 { + if space { + sb.WriteByte(' ') + } + sb.WriteString("OFFSET ") + sb.WriteString(strconv.Itoa(off)) + space = true + } + if lim := sorter.Limit; lim > 0 { + if space { + sb.WriteByte(' ') + } + sb.WriteString("LIMIT ") + sb.WriteString(strconv.Itoa(lim)) + } +} func newPageURL(key byte, query url.Values, offset int, offsetKey, limitKey string) string { urlBuilder := adapter.NewURLBuilder(key) for key, values := range query { if key != offsetKey && key != limitKey { @@ -297,20 +451,15 @@ urlBuilder.AppendQuery(offsetKey, strconv.Itoa(offset)) } return urlBuilder.String() } -type metaInfo struct { - Title string - URL string -} - // buildHTMLMetaList builds a zettel list based on a meta list for HTML rendering. -func buildHTMLMetaList(metaList []*meta.Meta) ([]metaInfo, error) { +func buildHTMLMetaList(metaList []*meta.Meta) ([]simpleLink, error) { defaultLang := runtime.GetDefaultLang() langOption := encoder.StringOption{Key: "lang", Value: ""} - metas := make([]metaInfo, 0, len(metaList)) + metas := make([]simpleLink, 0, len(metaList)) for _, m := range metaList { if lang, ok := m.Get(meta.KeyLang); ok { langOption.Value = lang } else { langOption.Value = defaultLang @@ -319,12 +468,12 @@ htmlTitle, err := adapter.FormatInlines( parser.ParseTitle(title), "html", &langOption) if err != nil { return nil, err } - metas = append(metas, metaInfo{ - Title: htmlTitle, - URL: adapter.NewURLBuilder('h').SetZid(m.Zid).String(), + metas = append(metas, simpleLink{ + Text: htmlTitle, + URL: adapter.NewURLBuilder('h').SetZid(m.Zid).String(), }) } return metas, nil } DELETED web/adapter/webui/reload.go Index: web/adapter/webui/reload.go ================================================================== --- web/adapter/webui/reload.go +++ /dev/null @@ -1,23 +0,0 @@ -//----------------------------------------------------------------------------- -// Copyright (c) 2020 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. -//----------------------------------------------------------------------------- - -// Package webui provides wet-UI handlers for web requests. -package webui - -import ( - "net/http" - - "zettelstore.de/z/web/adapter" -) - -// ReloadHandlerHTML creates a new HTTP handler for the use case "reload". -func ReloadHandlerHTML(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, adapter.NewURLBuilder('/').String(), http.StatusFound) -} Index: web/adapter/webui/rename_zettel.go ================================================================== --- web/adapter/webui/rename_zettel.go +++ web/adapter/webui/rename_zettel.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -66,16 +66,16 @@ curZid, err := id.Parse(r.URL.Path[1:]) if err != nil { http.NotFound(w, r) return } - if err := r.ParseForm(); err != nil { + if err = r.ParseForm(); err != nil { adapter.BadRequest(w, "Unable to read rename zettel form") return } - if formCurZid, err := id.Parse( - r.PostFormValue("curzid")); err != nil || formCurZid != curZid { + if formCurZid, err1 := id.Parse( + r.PostFormValue("curzid")); err1 != nil || formCurZid != curZid { adapter.BadRequest(w, "Invalid value for current zettel id in form") return } newZid, err := id.Parse(strings.TrimSpace(r.PostFormValue("newzid"))) if err != nil { @@ -85,9 +85,8 @@ if err := renameZettel.Run(r.Context(), curZid, newZid); err != nil { adapter.ReportUsecaseError(w, err) return } - http.Redirect( - w, r, adapter.NewURLBuilder('h').SetZid(newZid).String(), http.StatusFound) + http.Redirect(w, r, adapter.NewURLBuilder('h').SetZid(newZid).String(), http.StatusFound) } } Index: web/adapter/webui/template.go ================================================================== --- web/adapter/webui/template.go +++ web/adapter/webui/template.go @@ -17,10 +17,11 @@ "net/http" "sync" "zettelstore.de/z/auth/policy" "zettelstore.de/z/auth/token" + "zettelstore.de/z/collect" "zettelstore.de/z/config/runtime" "zettelstore.de/z/config/startup" "zettelstore.de/z/domain" "zettelstore.de/z/domain/id" "zettelstore.de/z/domain/meta" @@ -56,11 +57,10 @@ listZettelURL string listRolesURL string listTagsURL string withAuth bool loginURL string - reloadURL string searchURL string } // NewTemplateEngine creates a new TemplateEngine. func NewTemplateEngine(mgr place.Manager, pol policy.Policy) *TemplateEngine { @@ -71,16 +71,15 @@ stylesheetURL: adapter.NewURLBuilder('z').SetZid( id.BaseCSSZid).AppendQuery("_format", "raw").AppendQuery( "_part", "content").String(), homeURL: adapter.NewURLBuilder('/').String(), listZettelURL: adapter.NewURLBuilder('h').String(), - listRolesURL: adapter.NewURLBuilder('k').SetZid(2).String(), - listTagsURL: adapter.NewURLBuilder('k').SetZid(3).String(), + listRolesURL: adapter.NewURLBuilder('h').AppendQuery("_l", "r").String(), + listTagsURL: adapter.NewURLBuilder('h').AppendQuery("_l", "t").String(), withAuth: startup.WithAuth(), loginURL: adapter.NewURLBuilder('a').String(), - reloadURL: adapter.NewURLBuilder('c').AppendQuery("_format", "html").String(), - searchURL: adapter.NewURLBuilder('s').String(), + searchURL: adapter.NewURLBuilder('f').String(), } te.observe(place.ChangeInfo{Reason: place.OnReload, Zid: id.Invalid}) mgr.RegisterObserver(te.observe) return te } @@ -117,17 +116,15 @@ ctx context.Context, user *meta.Meta, zettel domain.Zettel) bool { return te.policy.CanWrite(user, zettel.Meta, zettel.Meta) && te.place.CanUpdateZettel(ctx, zettel) } -func (te *TemplateEngine) canRename( - ctx context.Context, user *meta.Meta, m *meta.Meta) bool { +func (te *TemplateEngine) canRename(ctx context.Context, user, m *meta.Meta) bool { return te.policy.CanRename(user, m) && te.place.AllowRenameZettel(ctx, m.Zid) } -func (te *TemplateEngine) canDelete( - ctx context.Context, user *meta.Meta, m *meta.Meta) bool { +func (te *TemplateEngine) canDelete(ctx context.Context, user, m *meta.Meta) bool { return te.policy.CanDelete(user, m) && te.place.CanDeleteZettel(ctx, m.Zid) } func (te *TemplateEngine) getTemplate( ctx context.Context, templateID id.Zid) (*template.Template, error) { @@ -138,10 +135,11 @@ if err != nil { return nil, err } t, err := template.ParseString(realTemplateZettel.Content.AsString(), nil) if err == nil { + // t.SetErrorOnMissing() te.cacheSetTemplate(templateID, t) } return t, err } @@ -154,31 +152,30 @@ Lang string MetaHeader string StylesheetURL string Title string HomeURL string - ListZettelURL string - ListRolesURL string - ListTagsURL string - CanCreate bool - NewZettelURL string - NewZettelLinks []simpleLink + WithUser bool WithAuth bool UserIsValid bool UserZettelURL string UserIdent string UserLogoutURL string LoginURL string - CanReload bool - ReloadURL string + ListZettelURL string + ListRolesURL string + ListTagsURL string + CanCreate bool + NewZettelURL string + NewZettelLinks []simpleLink SearchURL string Content string FooterHTML string } func (te *TemplateEngine) makeBaseData( - ctx context.Context, lang string, title string, user *meta.Meta, data *baseData) { + ctx context.Context, lang, title string, user *meta.Meta, data *baseData) { var ( newZettelLinks []simpleLink userZettelURL string userIdent string userLogoutURL string @@ -196,23 +193,22 @@ data.Lang = lang data.StylesheetURL = te.stylesheetURL data.Title = title data.HomeURL = te.homeURL - data.ListZettelURL = te.listZettelURL - data.ListRolesURL = te.listRolesURL - data.ListTagsURL = te.listTagsURL - data.CanCreate = canCreate - data.NewZettelLinks = newZettelLinks data.WithAuth = te.withAuth + data.WithUser = data.WithAuth data.UserIsValid = userIsValid data.UserZettelURL = userZettelURL data.UserIdent = userIdent data.UserLogoutURL = userLogoutURL data.LoginURL = te.loginURL - data.CanReload = te.policy.CanReload(user) - data.ReloadURL = te.reloadURL + data.ListZettelURL = te.listZettelURL + data.ListRolesURL = te.listRolesURL + data.ListTagsURL = te.listTagsURL + data.CanCreate = canCreate + data.NewZettelLinks = newZettelLinks data.SearchURL = te.searchURL data.FooterHTML = runtime.GetFooterHTML() } // htmlAttrNewWindow eturns HTML attribute string for opening a link in a new window. @@ -222,48 +218,46 @@ return " target=\"_blank\" ref=\"noopener noreferrer\"" } return "" } -var templatePlaceFilter = &place.Filter{ - Expr: place.FilterExpr{ - meta.KeyRole: []string{meta.ValueRoleNewTemplate}, - }, -} - -var templatePlaceSorter = &place.Sorter{ - Order: "id", - Descending: false, - Offset: -1, - Limit: 31, // Just to be one the safe side... -} - func (te *TemplateEngine) fetchNewTemplates(ctx context.Context, user *meta.Meta) []simpleLink { ctx = index.NoEnrichContext(ctx) - templateList, err := te.place.SelectMeta(ctx, templatePlaceFilter, templatePlaceSorter) + menu, err := te.place.GetZettel(ctx, id.TOCNewTemplateZid) if err != nil { return nil } - result := make([]simpleLink, 0, len(templateList)) - for _, m := range templateList { - if te.policy.CanRead(user, m) { - title := runtime.GetTitle(m) - langOption := encoder.StringOption{Key: "lang", Value: runtime.GetLang(m)} - astTitle := parser.ParseInlines( - input.NewInput(runtime.GetTitle(m)), meta.ValueSyntaxZmk) - menuTitle, err := adapter.FormatInlines(astTitle, "html", &langOption) - if err != nil { - menuTitle, err = adapter.FormatInlines(astTitle, "text", &langOption) - if err != nil { - menuTitle = title - } - } - result = append(result, simpleLink{ - Text: menuTitle, - URL: adapter.NewURLBuilder('n').SetZid(m.Zid).String(), - }) - } + zn := parser.ParseZettel(menu, "") + refs := collect.Order(zn) + result := make([]simpleLink, 0, len(refs)) + for _, ref := range refs { + zid, err := id.Parse(ref.URL.Path) + if err != nil { + continue + } + m, err := te.place.GetMeta(ctx, zid) + if err != nil { + continue + } + if !te.policy.CanRead(user, m) { + continue + } + title := runtime.GetTitle(m) + langOption := encoder.StringOption{Key: "lang", Value: runtime.GetLang(m)} + astTitle := parser.ParseInlines( + input.NewInput(runtime.GetTitle(m)), meta.ValueSyntaxZmk) + menuTitle, err := adapter.FormatInlines(astTitle, "html", &langOption) + if err != nil { + menuTitle, err = adapter.FormatInlines(astTitle, "text", &langOption) + if err != nil { + menuTitle = title + } + } + result = append(result, simpleLink{ + Text: menuTitle, + URL: adapter.NewURLBuilder('g').SetZid(m.Zid).String(), + }) } return result } func (te *TemplateEngine) renderTemplate( @@ -283,19 +277,20 @@ adapter.InternalServerError(w, "Unable to get template", err) return } if user := session.GetUser(ctx); user != nil { htmlLifetime, _ := startup.TokenLifetime() - t, err := token.GetToken(user, htmlLifetime, token.KindHTML) - if err == nil { - session.SetToken(w, t, htmlLifetime) + if tok, err1 := token.GetToken(user, htmlLifetime, token.KindHTML); err1 == nil { + session.SetToken(w, tok, htmlLifetime) } } var content bytes.Buffer err = t.Render(&content, data) - base.Content = content.String() - w.Header().Set("Content-Type", "text/html; charset=utf-8") - err = bt.Render(w, base) + if err == nil { + base.Content = content.String() + w.Header().Set(adapter.ContentType, "text/html; charset=utf-8") + err = bt.Render(w, base) + } if err != nil { adapter.InternalServerError(w, "Unable to render template", err) } } Index: web/router/router.go ================================================================== --- web/router/router.go +++ web/router/router.go @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------- -// Copyright (c) 2020 Detlef Stern +// Copyright (c) 2020-2021 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 @@ -92,11 +92,11 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { match := rt.reURL.FindStringSubmatch(r.URL.Path) if len(match) == 3 { key := match[1][0] index := indexZettel - if len(match[2]) == 0 { + if match[2] == "" { index = indexList } if mh, ok := rt.tables[index][key]; ok { if handler, ok := mh[r.Method]; ok { r.URL.Path = "/" + match[2] ADDED www/build.md Index: www/build.md ================================================================== --- /dev/null +++ www/build.md @@ -0,0 +1,59 @@ +# How to build the Zettelstore +## Prerequisites +You must install the following software: + +* A current, supported [release of Go](https://golang.org/doc/devel/release.html), +* [golint](https://github.com/golang/lint|golint), +* [Fossil](https://fossil-scm.org/). + +## Clone the repository +Most of this is covered by the excellent Fossil documentation. + +1. Create a directory to store your Fossil repositories. + Let's assume, you have created $HOME/fossil. +1. Clone the repository: `fossil clone https://zettelstore.de/ $HOME/fossil/zettelstore.fossil`. +1. Create a working directory. + Let's assume, you have created $HOME/zettelstore. +1. Change into this directory: `cd $HOME/zettelstore`. +1. Open development: `fossil open $HOME/fossil/zettelstore.fossil`. + +(If you are not able to use Fossil, you could try the Git mirror +.) + +## The build tool +In directory tools there is a Go file called build.go. +It automates most aspects, (hopefully) platform-independent. + +The script is called as: + +``` +go run tools/build.go [-v] COMMAND +``` + +The flag `-v` enables the verbose mode. +It outputs all commands called by the tool. + +`COMMAND` is one of: + +* `build`: builds the software with correct version information and places it + into a freshly created directory bin. +* `check`: checks the current state of the working directory to be ready for + release (or commit). +* `release`: executes `check` command and if this was successful, builds the + software for various platforms, and creates ZIP files for each executable. + Everything is placed in the directory releases. +* `clean`: removes the directories bin and releases. +* `version`: prints the current version information. + +Therefore, the easiest way to build your own version of the Zettelstore +software is to execute the command + +``` +go run tools/build.go build +``` + +In case of errors, please send the output of the verbose execution: + +``` +go run tools/build.go -v build +``` Index: www/changes.wiki ================================================================== --- www/changes.wiki +++ www/changes.wiki @@ -1,7 +1,54 @@ Change Log + +

    Changes for Version 0.0.11 (pending)

    + + +

    Changes for Version 0.0.10 (2021-02-26)

    + * Menu item “Home” now redirects to a home zettel. + Its default identifier is 000100000000. + The identifier can be changed with configuration key home-zettel, which supersedes key start. + The default home zettel contains some welcoming information for the new user. + (major: webui) + * Show context of a zettel by following all backward and/or forward reference + up to a defined depth and list the resulting zettel. Additionally, some zettel + with similar tags as the initial zettel are also taken into account. + (major: api, webui) + * A zettel that references other zettel within first-level list items, can act + as a “table of contents” zettel. + The API endpoint /o/{ID} allows to retrieve the referenced zettel in + the same order as they occur in the zettel. + (major: api) + * The zettel “New Menu” with identifier 00000000090000 contains + a list of all zettel that should act as a template for new zettel. + They are listed in the WebUIs ”New“ menu. + This is an application of the previous item. + It supersedes the usage of a role new-template introduced in [#0_0_6|version 0.0.6]. + Please update your zettel if you make use of the now deprecated feature. + (major: webui) + * A reference that starts with two slash characters (“//”) + it will be interpreted relative to the value of url-prefix. + For example, if url-prefix has the value /manual/, + the reference [[Zettel list|//h]] will render as + <a href="/manual/h">Zettel list</a>. (minor: syntax) + * Searching/filtering ignores the leading '#' character of tags. + (minor: api, webui) + * When result of filtering or searching is presented, the query is written as the page heading. + (minor: webui) + * A reference to a zettel that contains a URL fragment, will now be processed by the indexer. + (bug: server) + * Runtime configuration key marker-external now defaults to + “&#10138;” (“➚”). It is more beautiful + than the previous “&#8599;&#xfe0e;” + (“↗︎”), which also needed the additional + “&#xfe0e;” to disable the conversion to an emoji on iPadOS. + (minor: webui) + * A pre-build binary for macOS ARM64 (also known as Apple silicon) is available. + (minor: infrastructure) + * Many smaller bug fixes and inprovements, to the software and to the documentation. +

    Changes for Version 0.0.9 (2021-01-29)

    This is the first version that is managed by [https://fossil-scm.org|Fossil] instead of GitHub. To access older versions, use the Git repository under [https://github.com/zettelstore/zettelstore-github|zettelstore-github]. @@ -11,19 +58,19 @@ Metadata key published is the first example of such a property. * (major) A background activity (called indexer) continuously monitors zettel changes to establish the reverse direction of found internal links. This affects the new metadata keys precursor and folge. A user specifies the precursor of a zettel and the indexer computes the - property metadata for Folgezettel. + property metadata for [https://forum.zettelkasten.de/discussion/996/definition-folgezettel|Folgezettel]. Metadata keys with type “Identifier” or “IdentifierSet” that have no inverse key (like precursor and folge with add to the key forward that also collects all internal links within the content. The computed inverse is backward, which provides all backlinks. The key back is computed as the value of backward, but without forward links. Therefore, back is something like the list of “smart backlinks”. * (minor) If Zettelstore is being stopped, an appropriate message is written in the console log. - * (minor) New computed zettel with enviromental data, the list of supported meta data keys, + * (minor) New computed zettel with environmental data, the list of supported meta data keys, and statistics about all configured zettel places. Some other computed zettel got a new identifier (to make room for other variant). * (minor) Remove zettel 00000000000004, which contained the Go version that produced the Zettelstore executable. It was too specific to the current implementation. This information is now included in zettel 00000000000006 (Zettelstore Environment Values). @@ -41,19 +88,19 @@ This affects both the API and the WebUI. * (minor) Add a sort option “_random” to produce a zettel list in random order. _order / order are now an aliases for the query parameters _sort / sort.

    WebUI

    - * (major) HTML template zettel for WebUI now use Mustache - syntax instead of previously used Go template syntax. + * (major) HTML template zettel for WebUI now use [https://mustache.github.io/|Mustache] + syntax instead of previously used [https://golang.org/pkg/html/template/|Go template] syntax. This allows these zettel to be used, even when there is another Zettelstore implementation, in another programming language. Mustache is available for approx. 48 programming languages, instead of only one for Go templates. If you modified your templates, you must adapt them to the new syntax. Otherwise the WebUI will not work. * (major) Show zettel identifier of folgezettel and precursor zettel in the header of a rendered zettel. If a zettel has backlinks, they are shown at the botton of the page - (ldquo;Links to this zettel”). + (“Links to this zettel”). * (minor) All property metadata, even computed metadata is shown in the info page of a zettel. * (minor) Rendering of metadata keys title and default-title in info page changed to a full HTML output for these Zettelmarkup encoded values. * (minor) Always show the zettel identifier on the zettel detail view. Previously, the identifier was not shown if the zettel was not editable. * (minor) Do not show computed metadata in edit forms anymore. @@ -107,11 +154,11 @@ There was some feedback that the additional tags were not helpful. * (minor) Move zettel field "role" above "tags" and move "syntax" more to "content". * (minor) Rename zettel operation “clone” to “copy”. * (major) All predefined HTML templates have now a visibility value “expert”. If you want to see them as an non-expert owner, you must temporary enable expert-mode and change the visibility metadata value. - * (minor) Initial support for Folgezettel. + * (minor) Initial support for [https://zettelkasten.de/posts/tags/folgezettel/|Folgezettel]. If you click on “Folge” (detail view or info view), a new zettel is created with a reference (precursor) to the original zettel. Title, role, tags, and syntax are copied from the original zettel. * (major) Most predefined zettel have a title prefix of “Zettelstore”. * (minor) If started in simple mode, e.g. via double click or without any command, some information for the new user is presented. In the terminal, there is a hint about opening the web browser and use a specific URL. @@ -119,11 +166,11 @@ (This change also applies to the server itself, but it is more suited to the WebUI user.)

    Changes for Version 0.0.7 (2020-11-24)

    * With this version, Zettelstore and this manual got a new license, the - European Union Public Licence (EUPL), version 1.2 or later. + [https://joinup.ec.europa.eu/collection/eupl|European Union Public Licence] (EUPL), version 1.2 or later. Nothing else changed. If you want to stay with the old licenses (AGPLv3+, CC BY-SA 4.0), you are free to fork from the previous version. @@ -174,10 +221,11 @@ You can use this mechanism to specify a role for the new zettel, or a different title. The title of the template zettel is used in the drop-down list. The initial template zettel “New Zettel” has now a different zettel identifier (now: 00000000091001, was: 00000000040001). Please update it, if you changed that zettel. +
    Note: this feature was superseded in [#0_0_10|version 0.0.10] by the “New Menu” zettel. * (minor) When a page should be opened in a new windows (e.g. for external references), the web browser is instructed to decouple the new page from the previous one for privacy and security reasons. In detail, the web browser is instructed to omit referrer information and to omit a JS object linking to the page that contained the external link. * (minor) If the value of the Zettelstore Runtime Configuration key list-page-size is greater than zero, the number of WebUI list elements will be restricted and it is possible to change to the next/previous page to list more elements. Index: www/download.wiki ================================================================== --- www/download.wiki +++ www/download.wiki @@ -4,20 +4,21 @@ * Zettelstore is free/libre open source software, licensed under EUPL-1.2-or-later. * The software is provided as-is. * There is no guarantee that it will not damage your system. * However, it is in use by the main developer since March 2020 without any damage. * It may be useful for you. It is useful for me. - * Take a look at the manual to know how to start and use it. + * Take a look at the [https://zettelstore.de/manual/|manual] to know how to start and use it.

    ZIP-ped Executables

    -Build: v0.0.9 (2021-01-29). +Build: v0.0.10 (2021-02-26). - * [/uv/zettelstore.zip|Linux] (amd64) - * [/uv/zettelstore-arm6.zip|Linux] (arm6, e.g. Raspberry Pi) - * [/uv/zettelstore.exe.zip|Windows] (amd64) - * [/uv/iZettelstore.zip|macOs] (amd64) + * [/uv/zettelstore-0.0.10-linux-amd64.zip|Linux] (amd64) + * [/uv/zettelstore-0.0.10-linux-arm.zip|Linux] (arm6, e.g. Raspberry Pi) + * [/uv/zettelstore-0.0.10-windows-amd64.zip|Windows] (amd64) + * [/uv/zettelstore-0.0.10-darwin-amd64.zip|macOS] (amd64) + * [/uv/zettelstore-0.0.10-darwin-arm64.zip|macOS] (arm64, aka Apple silicon) Unzip the appropriate file, install and execute Zettelstore according to the manual.

    Zettel for the manual

    -As a starter, you can download the zettel for the manual [/uv/manual.zip|here]. -Just unzip the file and put it into your zettel folder. +As a starter, you can download the zettel for the manual [/uv/manual-0.0.10.zip|here]. +Just unzip the contained files and put them into your zettel folder. Index: www/impri.wiki ================================================================== --- www/impri.wiki +++ www/impri.wiki @@ -1,23 +1,18 @@ Imprint & Privacy

    Imprint

    -

    Detlef Stern
    Max-Planck-Str. 39
    74081 Heilbronn
    -Phone: +49 (173) 4905619
    +Phone: +49 (15678) 386566
    Mail: ds (at) zettelstore.de -

    Privacy

    -

    If you do not log into this site, or login as the user "anonymous", the only personal data this web service will process is your IP adress. It will be used to send the data of the website you requested to you and to mitigate possible attacks against this website. -

    -

    -This website is hosted by 1&1 IONOS SE. + +This website is hosted by [https://ionos.de|1&1 IONOS SE]. According to -their information, +[https://www.ionos.de/hilfe/datenschutz/datenverarbeitung-von-webseitenbesuchern-ihres-11-ionos-produktes/andere-11-ionos-produkte/|their information], no processing of personal data is done by them. -

    Index: www/index.wiki ================================================================== --- www/index.wiki +++ www/index.wiki @@ -1,26 +1,38 @@ Home Zettelstore is a software that collects and relates your notes (“zettel”) to represent and enhance your knowledge. It helps with many tasks of personal knowledge management by explicitly supporting the -[href="https://en.wikipedia.org/wiki/Zettelkasten|Zettelkasten method]. The +[https://en.wikipedia.org/wiki/Zettelkasten|Zettelkasten method]. The method is based on creating many individual notes, each with one idea or information, that are related to each other. Since knowledge is typically build up gradually, one major focus is a long-term store of these notes, hence the name “Zettelstore”. -To get an initial impression, take a look at the manual. +To get an initial impression, take a look at the [https://zettelstore.de/manual/|manual]. It is a live example of the zettelstore software, running in read-only mode. The software, including the manual, is licensed under the [/file?name=LICENSE.txt&ci=trunk|European Union Public License 1.2 (or later)]. [https://twitter.com/zettelstore|Stay tuned]…
    -

    Latest Release: 0.0.9 (2021-01-29)

    +

    Latest Release: 0.0.10 (2021-02-26)

    * [./download.wiki|Download] - * [./changes.wiki#0_0_9|Change Summary] - * [./plan.wiki|Limitations and Planned Improvements] - * [/dir?ci=trunk|Source Code] (mirrored on GitHub) + * [./changes.wiki#0_0_10|Change Summary] + * [/timeline?p=version-0.0.10&bt=version-0.0.9&y=ci|Check-ins for version 0.0.10], + [/vdiff?to=version-0.0.10&from=version-0.0.9|content diff] + * [/timeline?df=version-0.0.10&y=ci|Check-ins derived from the 0.0.10 release], + [/vdiff?from=version-0.0.10&to=trunk|content diff] + * [./plan.wiki|Limitations and planned Improvements] + * [/timeline?t=release|Timeline of all past releases] + +
    +

    Build instructions

    +Just install [https://fossil-scm.org|Fossil], +[https://golang.org/dl/|Go] and some Go-based tools. Please read the +[./build.md|instructions] for details. + + * [/dir?ci=trunk|Source Code] * [/download|Download the source code] as a Tarball or a ZIP file (you must [/login|login] as user "anonymous"). Index: www/plan.wiki ================================================================== --- www/plan.wiki +++ www/plan.wiki @@ -1,6 +1,6 @@ -Limitations and Planned Improvements +Limitations and planned Improvements Here is a list of some shortcomings of Zettelstore. They are planned to be solved.

    Serious limitations

    @@ -23,8 +23,8 @@ * …

    Planned improvements

    * Support for mathematical content is missing, e.g. $$F(x) &= \\int^a_b \\frac{1}{3}x^3$$. - * Render zettel in pandoc's JSON version of + * Render zettel in [https://pandoc.org|pandoc's] JSON version of their native AST to make pandoc an external renderer for Zettelstore. * …