Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From version-0.0.11
To version-0.0.10
2021-04-05
| | |
15:59 |
|
...
(check-in: 737632737f user: stern tags: trunk)
|
12:18 |
|
...
(check-in: 6db9ad537f user: stern tags: trunk, release, version-0.0.11)
|
2021-04-03
| | |
17:31 |
|
...
(check-in: ca6e7ae6d7 user: stern tags: trunk)
|
2021-02-26
| | |
11:53 |
|
...
(check-in: d69e61d8eb user: stern tags: trunk)
|
11:14 |
|
...
(check-in: 5d9e4fc19e user: stern tags: trunk, release, version-0.0.10)
|
10:36 |
|
...
(check-in: 09837eae5e user: stern tags: trunk)
|
| | |
Deleted .github/dependabot.yml.
1
2
3
4
5
6
7
8
9
10
11
12
|
|
-
-
-
-
-
-
-
-
-
-
-
-
|
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
rebase-strategy: "disabled"
|
Changes to README.md.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
|
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
|
# Zettelstore
# zettelstore
**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
[Zettelkasten method](https://en.wikipedia.org/wiki/Zettelkasten). 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”.
A storage and service for zettel notes.
To get an initial impression, take a look at the
[manual](https://zettelstore.de/manual/). It is a live example of the
zettelstore software, running in read-only mode.
The software, including the manual, is licensed
under the [European Union Public License 1.2 (or
later)](https://zettelstore.de/home/file?name=LICENSE.txt&ci=trunk).
[Stay tuned](https://twitter.com/zettelstore)…
|
Changes to VERSION.
Changes to ast/ast.go.
︙ | | |
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
-
+
-
-
-
-
-
+
+
+
+
|
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
)
// ZettelNode is the root node of the abstract syntax tree.
// It is *not* part of the visitor pattern.
type ZettelNode struct {
// Zettel domain.Zettel
Zettel domain.Zettel
Meta *meta.Meta // Original metadata
Content domain.Content // Original content
Zid id.Zid // Zettel identification.
InhMeta *meta.Meta // Metadata of the zettel, with inherited values.
Ast BlockSlice // Zettel abstract syntax tree is a sequence of block nodes.
Zid id.Zid // Zettel identification.
InhMeta *meta.Meta // Meta data of the zettel, with inherited values.
Title InlineSlice // Zettel title is a sequence of inline nodes.
Ast BlockSlice // Zettel abstract syntax tree is a sequence of block nodes.
}
// Node is the interface, all nodes must implement.
type Node interface {
Accept(v Visitor)
}
|
︙ | | |
Changes to ast/block.go.
1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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.
//-----------------------------------------------------------------------------
|
︙ | | |
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
-
+
|
func (tn *TableNode) blockNode() {}
// Accept a visitor and visit the node.
func (tn *TableNode) Accept(v Visitor) { v.VisitTable(tn) }
//--------------------------------------------------------------------------
// BLOBNode contains just binary data that must be interpreted according to
// BLOBNode contains just binary data that must be interpreted accordung to
// a syntax.
type BLOBNode struct {
Title string
Syntax string
Blob []byte
}
func (bn *BLOBNode) blockNode() {}
// Accept a visitor and visit the node.
func (bn *BLOBNode) Accept(v Visitor) { v.VisitBLOB(bn) }
|
Changes to ast/inline.go.
1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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.
//-----------------------------------------------------------------------------
|
︙ | | |
Changes to ast/traverser.go.
1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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.
//-----------------------------------------------------------------------------
|
︙ | | |
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
-
+
-
+
-
+
-
+
-
+
-
+
|
// VisitVerbatim has nothing to traverse.
func (t TopDownTraverser) VisitVerbatim(vn *VerbatimNode) { t.v.VisitVerbatim(vn) }
// VisitRegion traverses the content and the additional text.
func (t TopDownTraverser) VisitRegion(rn *RegionNode) {
t.v.VisitRegion(rn)
t.VisitBlockSlice(rn.Blocks)
t.VisitInlineSlice(rn.Inlines)
t.visitInlineSlice(rn.Inlines)
}
// VisitHeading traverses the heading.
func (t TopDownTraverser) VisitHeading(hn *HeadingNode) {
t.v.VisitHeading(hn)
t.VisitInlineSlice(hn.Inlines)
t.visitInlineSlice(hn.Inlines)
}
// VisitHRule traverses nothing.
func (t TopDownTraverser) VisitHRule(hn *HRuleNode) { t.v.VisitHRule(hn) }
// VisitNestedList traverses all nested list elements.
func (t TopDownTraverser) VisitNestedList(ln *NestedListNode) {
t.v.VisitNestedList(ln)
for _, item := range ln.Items {
t.visitItemSlice(item)
}
}
// VisitDescriptionList traverses all description terms and their associated
// descriptions.
func (t TopDownTraverser) VisitDescriptionList(dn *DescriptionListNode) {
t.v.VisitDescriptionList(dn)
for _, defs := range dn.Descriptions {
t.VisitInlineSlice(defs.Term)
t.visitInlineSlice(defs.Term)
for _, descr := range defs.Descriptions {
t.visitDescriptionSlice(descr)
}
}
}
// VisitPara traverses the inlines of a paragraph.
func (t TopDownTraverser) VisitPara(pn *ParaNode) {
t.v.VisitPara(pn)
t.VisitInlineSlice(pn.Inlines)
t.visitInlineSlice(pn.Inlines)
}
// VisitTable traverses all cells of the header and then row-wise all cells of
// the table body.
func (t TopDownTraverser) VisitTable(tn *TableNode) {
t.v.VisitTable(tn)
for _, col := range tn.Header {
t.VisitInlineSlice(col.Inlines)
t.visitInlineSlice(col.Inlines)
}
for _, row := range tn.Rows {
for _, col := range row {
t.VisitInlineSlice(col.Inlines)
t.visitInlineSlice(col.Inlines)
}
}
}
// VisitBLOB traverses nothing.
func (t TopDownTraverser) VisitBLOB(bn *BLOBNode) { t.v.VisitBLOB(bn) }
|
︙ | | |
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
-
+
-
+
-
+
-
+
-
+
|
// VisitBreak traverses nothing.
func (t TopDownTraverser) VisitBreak(bn *BreakNode) { t.v.VisitBreak(bn) }
// VisitLink traverses the link text.
func (t TopDownTraverser) VisitLink(ln *LinkNode) {
t.v.VisitLink(ln)
t.VisitInlineSlice(ln.Inlines)
t.visitInlineSlice(ln.Inlines)
}
// VisitImage traverses the image text.
func (t TopDownTraverser) VisitImage(in *ImageNode) {
t.v.VisitImage(in)
t.VisitInlineSlice(in.Inlines)
t.visitInlineSlice(in.Inlines)
}
// VisitCite traverses the cite text.
func (t TopDownTraverser) VisitCite(cn *CiteNode) {
t.v.VisitCite(cn)
t.VisitInlineSlice(cn.Inlines)
t.visitInlineSlice(cn.Inlines)
}
// VisitFootnote traverses the footnote text.
func (t TopDownTraverser) VisitFootnote(fn *FootnoteNode) {
t.v.VisitFootnote(fn)
t.VisitInlineSlice(fn.Inlines)
t.visitInlineSlice(fn.Inlines)
}
// VisitMark traverses nothing.
func (t TopDownTraverser) VisitMark(mn *MarkNode) { t.v.VisitMark(mn) }
// VisitFormat traverses the formatted text.
func (t TopDownTraverser) VisitFormat(fn *FormatNode) {
t.v.VisitFormat(fn)
t.VisitInlineSlice(fn.Inlines)
t.visitInlineSlice(fn.Inlines)
}
// VisitLiteral traverses nothing.
func (t TopDownTraverser) VisitLiteral(ln *LiteralNode) { t.v.VisitLiteral(ln) }
// VisitBlockSlice traverses a block slice.
func (t TopDownTraverser) VisitBlockSlice(bns BlockSlice) {
|
︙ | | |
149
150
151
152
153
154
155
156
157
158
159
160
161
|
149
150
151
152
153
154
155
156
157
158
159
160
|
-
-
+
|
func (t TopDownTraverser) visitDescriptionSlice(dns DescriptionSlice) {
for _, dn := range dns {
dn.Accept(t)
}
}
// VisitInlineSlice traverses a block slice.
func (t TopDownTraverser) VisitInlineSlice(ins InlineSlice) {
func (t TopDownTraverser) visitInlineSlice(ins InlineSlice) {
for _, in := range ins {
in.Accept(t)
}
}
|
Changes to auth/policy/place.go.
︙ | | |
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
-
|
import (
"context"
"zettelstore.de/z/domain"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/place"
"zettelstore.de/z/search"
"zettelstore.de/z/web/session"
)
// PlaceWithPolicy wraps the given place inside a policy place.
func PlaceWithPolicy(
place place.Place,
simpleMode bool,
|
︙ | | |
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
-
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
|
return nil, place.NewErrNotAllowed("GetMeta", user, zid)
}
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, s *search.Search) ([]*meta.Meta, error) {
func (pp *polPlace) SelectMeta(
ctx context.Context, f *place.Filter, s *place.Sorter) ([]*meta.Meta, error) {
user := session.GetUser(ctx)
f = place.EnsureFilter(f)
canRead := pp.policy.CanRead
if sel := f.Select; sel != nil {
f.Select = func(m *meta.Meta) bool {
return canRead(user, m) && sel(m)
}
} else {
f.Select = func(m *meta.Meta) bool {
s = s.AddPreMatch(func(m *meta.Meta) bool { return canRead(user, m) })
return pp.place.SelectMeta(ctx, s)
return canRead(user, m)
}
}
result, err := pp.place.SelectMeta(ctx, f, s)
return result, err
}
func (pp *polPlace) CanUpdateZettel(ctx context.Context, zettel domain.Zettel) bool {
return pp.place.CanUpdateZettel(ctx, zettel)
}
func (pp *polPlace) UpdateZettel(ctx context.Context, zettel domain.Zettel) error {
|
︙ | | |
Changes to cmd/cmd_file.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-
+
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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 cmd
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"zettelstore.de/z/config/runtime"
"zettelstore.de/z/domain"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
|
︙ | | |
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
-
+
+
+
+
-
+
-
+
-
+
|
z := parser.ParseZettel(
domain.Zettel{
Meta: meta,
Content: domain.NewContent(inp.Src[inp.Pos:]),
},
runtime.GetSyntax(meta),
)
enc := encoder.Create(format, &encoder.Environment{Lang: runtime.GetLang(meta)})
enc := encoder.Create(
format,
&encoder.StringOption{Key: "lang", Value: runtime.GetLang(meta)},
)
if enc == nil {
fmt.Fprintf(os.Stderr, "Unknown format %q\n", format)
return 2, nil
}
_, err = enc.WriteZettel(os.Stdout, z, format != "raw")
if err != nil {
return 2, err
}
fmt.Println()
return 0, nil
}
func getInput(args []string) (*meta.Meta, *input.Input, error) {
if len(args) < 1 {
src, err := io.ReadAll(os.Stdin)
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, nil, err
}
inp := input.NewInput(string(src))
m := meta.NewFromInput(id.New(true), inp)
return m, inp, nil
}
src, err := os.ReadFile(args[0])
src, err := ioutil.ReadFile(args[0])
if err != nil {
return nil, nil, err
}
inp := input.NewInput(string(src))
m := meta.NewFromInput(id.New(true), inp)
if len(args) > 1 {
src, err := os.ReadFile(args[1])
src, err := ioutil.ReadFile(args[1])
if err != nil {
return nil, nil, err
}
inp = input.NewInput(string(src))
}
return m, inp, nil
}
|
Changes to cmd/cmd_password.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
-
+
|
package cmd
import (
"flag"
"fmt"
"os"
"golang.org/x/term"
"golang.org/x/crypto/ssh/terminal"
"zettelstore.de/z/auth/cred"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
)
// ---------- Subcommand: password -------------------------------------------
|
︙ | | |
64
65
66
67
68
69
70
71
72
73
74
|
64
65
66
67
68
69
70
71
72
73
74
|
-
+
|
meta.KeyUserID, ident,
)
return 0, nil
}
func getPassword(prompt string) (string, error) {
fmt.Fprintf(os.Stderr, "%s: ", prompt)
password, err := term.ReadPassword(int(os.Stdin.Fd()))
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprintln(os.Stderr)
return string(password), err
}
|
Changes to cmd/cmd_run.go.
︙ | | |
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
ucParseZettel := usecase.NewParseZettel(ucGetZettel)
ucListMeta := usecase.NewListMeta(pp)
ucListRoles := usecase.NewListRole(pp)
ucListTags := usecase.NewListTags(pp)
ucZettelContext := usecase.NewZettelContext(pp)
router := router.NewRouter()
router.Handle("/", webui.MakeGetRootHandler(te, pp))
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(te))
router.AddZettelRoute('a', http.MethodGet, webui.MakeGetLogoutHandler())
if !readonlyMode {
router.AddZettelRoute('b', http.MethodGet, webui.MakeGetRenameZettelHandler(
te, ucGetMeta))
router.AddZettelRoute('b', http.MethodPost, webui.MakePostRenameZettelHandler(
te, usecase.NewRenameZettel(pp)))
usecase.NewRenameZettel(pp)))
router.AddZettelRoute('c', http.MethodGet, webui.MakeGetCopyZettelHandler(
te, ucGetZettel, usecase.NewCopyZettel()))
router.AddZettelRoute('c', http.MethodPost, webui.MakePostCreateZettelHandler(
te, usecase.NewCreateZettel(pp)))
usecase.NewCreateZettel(pp)))
router.AddZettelRoute('d', http.MethodGet, webui.MakeGetDeleteZettelHandler(
te, ucGetZettel))
router.AddZettelRoute('d', http.MethodPost, webui.MakePostDeleteZettelHandler(
te, usecase.NewDeleteZettel(pp)))
usecase.NewDeleteZettel(pp)))
router.AddZettelRoute('e', http.MethodGet, webui.MakeEditGetZettelHandler(
te, ucGetZettel))
router.AddZettelRoute('e', http.MethodPost, webui.MakeEditSetZettelHandler(
te, usecase.NewUpdateZettel(pp)))
usecase.NewUpdateZettel(pp)))
router.AddZettelRoute('f', http.MethodGet, webui.MakeGetFolgeZettelHandler(
te, ucGetZettel, usecase.NewFolgeZettel()))
router.AddZettelRoute('f', http.MethodPost, webui.MakePostCreateZettelHandler(
te, usecase.NewCreateZettel(pp)))
usecase.NewCreateZettel(pp)))
router.AddZettelRoute('g', http.MethodGet, webui.MakeGetNewZettelHandler(
te, ucGetZettel, usecase.NewNewZettel()))
router.AddZettelRoute('g', http.MethodPost, webui.MakePostCreateZettelHandler(
te, usecase.NewCreateZettel(pp)))
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))
|
︙ | | |
Changes to cmd/main.go.
︙ | | |
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
+
|
package cmd
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"zettelstore.de/z/config/runtime"
"zettelstore.de/z/config/startup"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
|
︙ | | |
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
-
+
|
func getConfig(fs *flag.FlagSet) (cfg *meta.Meta) {
var configFile string
if configFlag := fs.Lookup("c"); configFlag != nil {
configFile = configFlag.Value.String()
} else {
configFile = defConfigfile
}
content, err := os.ReadFile(configFile)
content, err := ioutil.ReadFile(configFile)
if err != nil {
cfg = meta.New(id.Invalid)
} else {
cfg = meta.NewFromInput(id.Invalid, input.NewInput(string(content)))
}
fs.Visit(func(flg *flag.Flag) {
switch flg.Name {
|
︙ | | |
Changes to cmd/register.go.
︙ | | |
22
23
24
25
26
27
28
29
30
31
|
22
23
24
25
26
27
28
29
30
|
-
|
_ "zettelstore.de/z/parser/blob" // Allow to use BLOB parser.
_ "zettelstore.de/z/parser/markdown" // Allow to use markdown parser.
_ "zettelstore.de/z/parser/none" // Allow to use none parser.
_ "zettelstore.de/z/parser/plain" // Allow to use plain parser.
_ "zettelstore.de/z/parser/zettelmark" // Allow to use zettelmark parser.
_ "zettelstore.de/z/place/constplace" // Allow to use global internal place.
_ "zettelstore.de/z/place/dirplace" // Allow to use directory place.
_ "zettelstore.de/z/place/fileplace" // Allow to use file place.
_ "zettelstore.de/z/place/memplace" // Allow to use memory place.
)
|
Changes to cmd/zettelstore/main.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-
+
+
-
+
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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 main is the starting point for the zettelstore command.
package main
import (
import "zettelstore.de/z/cmd"
"zettelstore.de/z/cmd"
)
// Version variable. Will be filled by build process.
var version string = ""
func main() {
cmd.Main("Zettelstore", version)
}
|
Changes to config/runtime/runtime.go.
︙ | | |
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
-
-
-
-
+
+
+
-
|
panic("configStock not set")
}
return configStock.GetMeta(id.ConfigurationZid)
}
// GetDefaultTitle returns the current value of the "default-title" key.
func GetDefaultTitle() string {
if configStock != nil {
if config := getConfigurationMeta(); config != nil {
if title, ok := config.Get(meta.KeyDefaultTitle); ok {
return title
if config := getConfigurationMeta(); config != nil {
if title, ok := config.Get(meta.KeyDefaultTitle); ok {
return title
}
}
}
return "Untitled"
}
// GetDefaultSyntax returns the current value of the "default-syntax" key.
func GetDefaultSyntax() string {
|
︙ | | |
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
+
|
func GetDefaultCopyright() string {
if configStock != nil {
if config := getConfigurationMeta(); config != nil {
if copyright, ok := config.Get(meta.KeyDefaultCopyright); ok {
return copyright
}
}
// TODO: get owner
}
return ""
}
// GetDefaultLicense returns the current value of the "default-license" key.
func GetDefaultLicense() string {
if configStock != nil {
|
︙ | | |
Changes to docs/manual/00000000000100.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
1
2
3
4
5
6
7
8
9
10
11
|
-
-
|
id: 00000000000100
title: Zettelstore Runtime Configuration
role: configuration
syntax: none
default-copyright: (c) 2020-2021 by Detlef Stern <ds@zettelstore.de>
default-license: EUPL-1.2-or-later
default-visibility: public
footer-html: <hr><p><a href="/home/doc/trunk/www/impri.wiki">Imprint / Privacy</a></p>
home-zettel: 00001000000000
no-index: true
site-name: Zettelstore Manual
visibility: owner
|
Deleted docs/manual/00001000000000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
|
Changes to docs/manual/00001004011200.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
-
+
-
+
-
-
-
-
-
+
+
|
id: 00001004011200
title: Zettelstore places
role: manual
tags: #configuration #manual #zettelstore
syntax: zmk
role: manual
A Zettelstore must store its zettel somehow and somewhere.
In most cases you want to store your zettel as files in a directory.
Under certain circumstances you may want to store your zettel in other places.
An example are the [[predefined zettel|00001005090000]] that come with a Zettelstore.
They are stored within the software itself.
In another situation you may want to store your zettel volatile, e.g. if you want to provide a sandbox for experimenting.
In another situation you may want to store your zettel volatilely, e.g. if you want to provide a sandbox for experimenting.
To cope with these (and more) situations, you configure Zettelstore to use one or more places.
This is done via the ''place-X-uri'' keys of the [[start-up configuration|00001004010000#place-X-uri]] (X is a number).
Places are specified using special [[URIs|https://en.wikipedia.org/wiki/Uniform_Resource_Identifier]], somehow similar to web addresses.
The following place URIs are supported:
; ''dir:\//DIR''
: Specifies a directory where zettel files are stored.
''DIR'' is the file path.
Although it is possible to use relative file paths, such as ''./zettel'' (→ URI is ''dir:\//.zettel''), it is preferable to use absolute file paths, e.g. ''/home/user/zettel''.
The directory must exist before starting the Zettelstore[^There is one exception: when Zettelstore is [[started without any parameter|00001004050000]], e.g. via double-clicking its icon, an directory called ''./zettel'' will be created.].
It is possible to [[configure|00001004011400]] a directory place.
; ''file:FILE.zip'' oder ''file:/\//path/to/file.zip''
: Specifies a ZIP file which contains files that store zettel.
You can create such a ZIP file, if you zip a directory full of zettel files.
This place is always read-only.
; ''mem:''
: Stores all its zettel in volatile memory.
If you stop the Zettelstore, all changes are lost.
; ''const:''
: Is a place of predefined, essential zettel.
All places that you configure via the ''store-X-uri'' keys form a chain of places.
If a zettel should be retrieved, a search starts in the place specified with the ''place-1-uri'' key, then ''place-2-uri'' and so on.
If a zettel is created or changed, it is always stored in the place specified with the ''place-1-uri'' key.
This allows to overwrite zettel from other places, e.g. the predefined zettel.
If you did not configure the place of the predefined zettel (''const:'') they will automatically be appended as a last place.
Otherwise Zettelstore will not work in certain situations.
If you use the ''mem:'' place, where zettel are stored in volatile memory, it makes only sense if you configure it as ''place-1-uri''.
Such a place will be empty when Zettelstore starts and only the place 1 will receive updates.
You must make sure that your computer has enough RAM to store all zettel.
|
Changes to docs/manual/00001006020000.zettel.
︙ | | |
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
-
-
+
+
|
If not given, the value ''default-license'' from the [[configuration zettel|00001004020000#default-license]] will be used.
; [!modified]''modified''
: Date and time when a zettel was modified through Zettelstore.
If you edit a zettel with an editor software outside Zettelstore, you should set it manually to an appropriate value.
This is a computed value.
There is no need to set it via Zettelstore.
; [!no-index]''no-index''
: If set to true, the zettel will not be indexed and therefore not be found in full-text searches.
; [!new-role]''new-role''
: Used in a template zettel to specify the ''role'' of the new zettel.
; [!precursor]''precursor''
: References zettel for which this zettel is a ""Folgezettel"" / follow-up zettel.
Basically the inverse of key [[''folge''|#folge]].
; [!published]''published''
: This property contains the timestamp of the mast modification / creation of the zettel.
If [[''modified''|#modified]]is set, it contains the same value.
Otherwise, if the zettel identifier contains a valid timestamp, the identifier is used.
|
︙ | | |
Changes to docs/manual/00001006034000.zettel.
︙ | | |
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
9
10
11
12
13
14
15
16
17
18
19
|
-
-
+
-
-
-
-
-
-
|
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
It depends of the first character of a search string how it is matched against a tag set value:
A value matches a tag set value, if the first value is equal to at least one tag in the tag set.
* If the first character of the search string is a number sign character,
it must exactly match one of the values of a tag.
* In other cases, the search string must be the prefix of at least one tag.
Conpectually, all number sign characters are removed at the beginning of the search string
and of all tags.
=== Sorting
Sorting is done by comparing the [[String|00001006033500]] values.
|
Changes to docs/manual/00001006050000.zettel.
︙ | | |
17
18
19
20
21
22
23
24
25
26
27
|
17
18
19
20
21
22
23
24
25
26
27
|
-
+
|
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"", except the home zettel ''00010000000000''.
Zettel identifier of this manual have be chosen to begin with ""000010"".
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.
|
Changes to docs/manual/00001010070200.zettel.
︙ | | |
33
34
35
36
37
38
39
40
41
|
33
34
35
36
37
38
39
40
41
|
-
+
|
The first zettel is the zettel that contains CSS for displaying the web interface.
This is to ensure that the web interface looks nice even for not authenticated users.
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 [[start-up configuration|00001004010000]] is not shown, because the associated computed zettel with identifier ''00000000000098'' 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.
|
Changes to docs/manual/00001012051800.zettel.
︙ | | |
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
-
-
-
-
-
-
-
-
-
-
-
-
-
|
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:
```sh
# curl 'http://127.0.0.1:23123/z?title=API'
{"list":[{"id":"00001012921000","url":"/z/00001012921000","meta":{"title":"API: JSON structure of an access token","tags":"#api #manual #reference #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012920500","url":"/z/00001012920500","meta":{"title":"Formats available by the API","tags":"#api #manual #reference #zettelstore","syntax":"zmk","role":"manual"}},{"id":"00001012920000","url":"/z/00001012920000","meta":{"title":"Endpoints used by the API","tags":"#api #manual #reference #zettelstore","syntax":"zmk","role":"manual"}}, ...
```
However, if you want all zettel that does //not// match a given value, you must prefix the value with the exclamation mark character (""!"", ''U+0021'').
For example, if you want to retrieve all zettel that do not contain the string ""API"" in their title, your request will be:
```sh
# curl 'http://127.0.0.1:23123/z?title=!API'
{"list":[{"id":"00010000000000","url":"/z/00010000000000","meta":{"back":"00001003000000 00001005090000","backward":"00001003000000 00001005090000","copyright":"(c) 2020-2021 by Detlef Stern <ds@zettelstore.de>","forward":"00000000000001 00000000000003 00000000000096 00000000000098 00000000000100","lang":"en","license":"EUPL-1.2-or-later","role":"zettel","syntax":"zmk","title":"Home"}},{"id":"00001014000000","url":"/z/00001014000000","meta":{"back":"00001000000000 00001004020000 00001012920510","backward":"00001000000000 00001004020000 00001012000000 00001012920510","copyright":"(c) 2020-2021 by Detlef Stern <ds@zettelstore.de>","forward":"00001012000000","lang":"en","license":"EUPL-1.2-or-later","published":"00001014000000","role":"manual","syntax":"zmk","tags":"#manual #webui #zettelstore","title":"Web user interface"}},
...
```
The empty query parameter values matches all zettel that contain the given metadata key.
Similar, if you specify just the exclamation mark character as a query parameter value, only those zettel match that does //not// contain the given metadata key.
For example ``curl 'http://localhost:23123/z?back=!&backward='`` returns all zettel that are reachable via other zettel, but also references these zettel.
=== Output only specific parts of a zettel
If you are just interested in the zettel identifier, you should add the ""''_part''"" query parameter:
```sh
# curl 'http://127.0.0.1:23123/z?title=API&_part=id'
{"list":[{"id":"00001012921000","url":"/z/00001012921000"},{"id":"00001012920500","url":"/z/00001012920500"},{"id":"00001012920000","url":"/z/00001012920000"},{"id":"00001012051800","url":"/z/00001012051800"},{"id":"00001012051600","url":"/z/00001012051600"},{"id":"00001012051400","url":"/z/00001012051400"},{"id":"00001012051200","url":"/z/00001012051200"},{"id":"00001012050600","url":"/z/00001012050600"},{"id":"00001012050400","url":"/z/00001012050400"},{"id":"00001012050200","url":"/z/00001012050200"},{"id":"00001012000000","url":"/z/00001012000000"}]}
```
If you want only those zettel that additionally must contain the string ""JSON"", you have to add an additional query parameter:
```sh
|
︙ | | |
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
-
-
+
+
|
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"}]}
```
=== General filter
The query parameter ""''_s''"" allows to provide a string for a full-text search of all zettel.
The search string will be normalized according to Unicode NKFD, ignoring everything except letters and numbers.
The query parameter ""''_s''"" allows to provide a string, which will be searched for in all metadata.
While searching, the [[type|00001006030000]] of each metadata key will be respected.
You are allowed to specify this query parameter more than once.
All results will be intersected, i.e. a zettel will be included into the list if both of the provided values match.
This parameter loosely resembles the search box of the web user interface.
|
Changes to docs/manual/00001012054000.zettel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-
+
-
-
+
+
-
-
+
+
|
id: 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 [[initial zettel|00001000000000]] of this manual is one example, the [[general API description|00001012000000]] is another.
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/00001000000000
{"id":"00001000000000","url":"/z/00001000000000","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":{...}}]}
# 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": "00001000000000",
"url": "/z/00001000000000",
"id": "00010000000000",
"url": "/z/00010000000000",
"order": [
{
"id": "00001001000000",
"url": "/z/00001001000000",
"meta": {...}
},
{
|
︙ | | |
Added docs/manual/00010000000000.zettel.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
|
| | | | | | | | | | | | | | | | | | | |
Deleted docs/readmezip.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
Zettelstore
===========
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 Zettelkasten method (see:
https://en.wikipedia.org/wiki/Zettelkasten). 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 impression, take a look at the manual at
https://zettelstore.de/manual/. It is a live example of the zettelstore
software, running in read-only mode. You can download it separately and it is
possible to make it directly available for your local Zettelstore.
The software, including the manual, is licensed under the European Union Public
License 1.2 (or later). See the separate file LICENSE.txt.
To get in contact with the developer, send an email to ds@zettelstore.de or
follow Zettelstore on Twitter: https://twitter.com/zettelstore.
|
Changes to domain/content.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-
+
+
-
+
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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 domain provides domain specific types, constants, and functions.
package domain
import (
import "unicode/utf8"
"unicode/utf8"
)
// Content is just the uninterpreted content of a zettel.
type Content string
// NewContent creates a new content from a string.
func NewContent(s string) Content { return Content(s) }
|
︙ | | |
Changes to domain/id/id.go.
︙ | | |
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
-
+
-
|
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)
ZettelTemplateZid = Zid(10401)
DetailTemplateZid = Zid(10401)
InfoTemplateZid = Zid(10402)
FormTemplateZid = Zid(10403)
RenameTemplateZid = Zid(10404)
DeleteTemplateZid = Zid(10405)
ContextTemplateZid = Zid(10406)
RolesTemplateZid = Zid(10500)
TagsTemplateZid = Zid(10600)
ErrorTemplateZid = Zid(10700)
// 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
|
︙ | | |
Changes to domain/id/set.go.
︙ | | |
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
result := make(Set, c)
for _, zid := range zids {
result[zid] = true
}
return result
}
// Sorted returns the set as a sorted slice of zettel identifier.
func (s Set) Sorted() Slice {
// 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
}
// Intersect removes all zettel identifier that are not in the other set.
// Both sets can be modified by this method. One of them is the set returned.
// It contains the intersection of both.
func (s Set) Intersect(other Set) Set {
if len(s) > len(other) {
s, other = other, s
}
for zid, inSet := range s {
if !inSet {
delete(s, zid)
continue
}
otherInSet, otherOk := other[zid]
if !otherInSet || !otherOk {
delete(s, zid)
}
}
return s
}
|
Deleted domain/id/set_test.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// 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 TestSetSorted(t *testing.T) {
testcases := []struct {
set id.Set
exp id.Slice
}{
{nil, nil},
{id.NewSet(), nil},
{id.NewSet(9, 4, 6, 1, 7), id.Slice{1, 4, 6, 7, 9}},
}
for i, tc := range testcases {
got := tc.set.Sorted()
if !got.Equal(tc.exp) {
t.Errorf("%d: %v.Sorted() should be %v, but got %v", i, tc.set, tc.exp, got)
}
}
}
func TestSetIntersection(t *testing.T) {
testcases := []struct {
s1, s2 id.Set
exp id.Slice
}{
{nil, nil, nil},
{id.NewSet(), nil, nil},
{id.NewSet(), id.NewSet(), nil},
{id.NewSet(1), nil, nil},
{id.NewSet(1), id.NewSet(), nil},
{id.NewSet(1), id.NewSet(2), nil},
{id.NewSet(1), id.NewSet(1), id.Slice{1}},
}
for i, tc := range testcases {
sl1 := tc.s1.Sorted()
sl2 := tc.s2.Sorted()
got := tc.s1.Intersect(tc.s2).Sorted()
if !got.Equal(tc.exp) {
t.Errorf("%d: %v.Intersect(%v) should be %v, but got %v", i, sl1, sl2, tc.exp, got)
}
got = id.NewSet(sl2...).Intersect(id.NewSet(sl1...)).Sorted()
if !got.Equal(tc.exp) {
t.Errorf("%d: %v.Intersect(%v) should be %v, but got %v", i, sl2, sl1, tc.exp, got)
}
}
}
|
Changes to domain/id/slice.go.
︙ | | |
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
return nil
}
result := make(Slice, len(zs))
copy(result, zs)
return result
}
// Equal reports whether zs and other are the same length and contain the samle zettel
// identifier. A nil argument is equivalent to an empty slice.
func (zs Slice) Equal(other Slice) bool {
if len(zs) != len(other) {
return false
}
if len(zs) == 0 {
return true
}
for i, e := range zs {
if e != other[i] {
return false
}
}
return true
}
func (zs Slice) String() string {
if len(zs) == 0 {
return ""
}
var sb strings.Builder
for i, zid := range zs {
if i > 0 {
|
︙ | | |
Changes to domain/id/slice_test.go.
︙ | | |
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
-
+
-
+
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
import (
"testing"
"zettelstore.de/z/domain/id"
)
func TestSliceSort(t *testing.T) {
func TestSort(t *testing.T) {
zs := id.Slice{9, 4, 6, 1, 7}
zs.Sort()
exp := id.Slice{1, 4, 6, 7, 9}
if zs[0] != 1 || zs[1] != 4 || zs[2] != 6 || zs[3] != 7 || zs[4] != 9 {
if !zs.Equal(exp) {
t.Errorf("Slice.Sort did not work. Expected %v, got %v", exp, zs)
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 !orig.Equal(got) {
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 TestSliceEqual(t *testing.T) {
testcases := []struct {
s1, s2 id.Slice
exp bool
}{
{nil, nil, true},
{nil, id.Slice{}, true},
{nil, id.Slice{1}, false},
{id.Slice{1}, id.Slice{1}, true},
{id.Slice{1}, id.Slice{2}, false},
{id.Slice{1, 2}, id.Slice{2, 1}, false},
{id.Slice{1, 2}, id.Slice{1, 2}, true},
}
for i, tc := range testcases {
got := tc.s1.Equal(tc.s2)
if got != tc.exp {
t.Errorf("%d/%v.Equal(%v)==%v, but got %v", i, tc.s1, tc.s2, tc.exp, got)
}
got = tc.s2.Equal(tc.s1)
if got != tc.exp {
t.Errorf("%d/%v.Equal(%v)==%v, but got %v", i, tc.s2, tc.s1, tc.exp, got)
}
}
}
func TestSliceString(t *testing.T) {
func TestString(t *testing.T) {
testcases := []struct {
in id.Slice
exp string
}{
{nil, ""},
{id.Slice{}, ""},
{id.Slice{1}, "00000000000001"},
|
︙ | | |
Changes to domain/meta/meta.go.
︙ | | |
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
-
|
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, "")
KeyMarkerExternal = registerKey("marker-external", TypeEmpty, usageUser, "")
KeyModified = registerKey("modified", TypeTimestamp, usageComputed, "")
KeyNoIndex = registerKey("no-index", TypeBool, usageUser, "")
KeyPrecursor = registerKey("precursor", TypeIDSet, usageUser, KeyFolge)
KeyPublished = registerKey("published", TypeTimestamp, usageProperty, "")
KeyReadOnly = registerKey("read-only", TypeWord, usageUser, "")
KeySiteName = registerKey("site-name", TypeString, usageUser, "")
KeyURL = registerKey("url", TypeURL, usageUser, "")
KeyUserID = registerKey("user-id", TypeWord, usageUser, "")
KeyUserRole = registerKey("user-role", TypeWord, usageUser, "")
|
︙ | | |
Changes to domain/meta/meta_test.go.
︙ | | |
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
"strings"
"testing"
"zettelstore.de/z/domain/id"
)
const testID = id.Zid(98765432101234)
func newMeta(title string, tags []string, syntax string) *Meta {
m := New(testID)
if title != "" {
m.Set(KeyTitle, title)
}
if tags != nil {
m.Set(KeyTags, strings.Join(tags, " "))
}
if syntax != "" {
m.Set(KeySyntax, syntax)
}
return m
}
func TestKeyIsValid(t *testing.T) {
validKeys := []string{"0", "a", "0-", "title", "title-----", strings.Repeat("r", 255)}
for _, key := range validKeys {
if !KeyIsValid(key) {
t.Errorf("Key %q wrongly identified as invalid key", key)
}
|
︙ | | |
Deleted encoder/encfun/encfun.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 2021 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 encfun provides some helper function to work with encodings.
package encfun
import (
"strings"
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/parser"
)
// MetaAsInlineSlice returns the value of the given metadata key as an inlince slice.
func MetaAsInlineSlice(m *meta.Meta, key string) ast.InlineSlice {
return parser.ParseMetadata(m.GetDefault(key, ""))
}
// MetaAsText returns the value of given metadata as text.
func MetaAsText(m *meta.Meta, key string) string {
textEncoder := encoder.Create("text", nil)
var sb strings.Builder
_, err := textEncoder.WriteInlines(&sb, MetaAsInlineSlice(m, key))
if err == nil {
return sb.String()
}
return ""
}
|
Changes to encoder/encoder.go.
︙ | | |
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
-
+
|
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
)
// Encoder is an interface that allows to encode different parts of a zettel.
type Encoder interface {
SetOption(Option)
WriteZettel(io.Writer, *ast.ZettelNode, bool) (int, error)
WriteMeta(io.Writer, *meta.Meta) (int, error)
WriteContent(io.Writer, *ast.ZettelNode) (int, error)
WriteBlocks(io.Writer, ast.BlockSlice) (int, error)
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")
)
// Option allows to configure an encoder
type Option interface {
Name() string
}
// Create builds a new encoder with the given options.
func Create(format string, env *Environment) Encoder {
func Create(format string, options ...Option) Encoder {
if info, ok := registry[format]; ok {
enc := info.Create()
for _, opt := range options {
enc.SetOption(opt)
}
return info.Create(env)
return enc
}
return nil
}
// Info stores some data about an encoder.
type Info struct {
Create func(*Environment) Encoder
Create func() Encoder
Default bool
}
var registry = map[string]Info{}
var defFormat string
// Register the encoder for later retrieval.
|
︙ | | |
Deleted encoder/env.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// 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 encoder provides a generic interface to encode the abstract syntax
// tree into some text form.
package encoder
import "zettelstore.de/z/ast"
// Environment specifies all data and functions that affects encoding.
type Environment struct {
// Important for many encoder.
LinkAdapter func(*ast.LinkNode) ast.InlineNode
ImageAdapter func(*ast.ImageNode) ast.InlineNode
CiteAdapter func(*ast.CiteNode) ast.InlineNode
// Important for HTML encoder
Lang string // default language
Interactive bool // Encoded data will be placed in interactive content
Xhtml bool // use XHTML syntax instead of HTML syntax
MarkerExternal string // Marker after link to (external) material.
NewWindow bool // open link in new window
IgnoreMeta map[string]bool
footnotes []*ast.FootnoteNode // Stores footnotes detected while encoding
}
// AdaptLink helps to call the link adapter.
func (env *Environment) AdaptLink(ln *ast.LinkNode) (*ast.LinkNode, ast.InlineNode) {
if env == nil || env.LinkAdapter == nil {
return ln, nil
}
n := env.LinkAdapter(ln)
if n == nil {
return ln, nil
}
if ln2, ok := n.(*ast.LinkNode); ok {
return ln2, nil
}
return nil, n
}
// AdaptImage helps to call the link adapter.
func (env *Environment) AdaptImage(in *ast.ImageNode) (*ast.ImageNode, ast.InlineNode) {
if env == nil || env.ImageAdapter == nil {
return in, nil
}
n := env.ImageAdapter(in)
if n == nil {
return in, nil
}
if in2, ok := n.(*ast.ImageNode); ok {
return in2, nil
}
return nil, n
}
// AdaptCite helps to call the link adapter.
func (env *Environment) AdaptCite(cn *ast.CiteNode) (*ast.CiteNode, ast.InlineNode) {
if env == nil || env.CiteAdapter == nil {
return cn, nil
}
n := env.CiteAdapter(cn)
if n == nil {
return cn, nil
}
if cn2, ok := n.(*ast.CiteNode); ok {
return cn2, nil
}
return nil, n
}
// IsInteractive returns true, if Interactive is enabled and currently embedded
// interactive encoding will take place.
func (env *Environment) IsInteractive(inInteractive bool) bool {
return inInteractive && env != nil && env.Interactive
}
// IsXHTML return true, if XHTML is enabled.
func (env *Environment) IsXHTML() bool {
return env != nil && env.Xhtml
}
// HasNewWindow retruns true, if a new browser windows should be opened.
func (env *Environment) HasNewWindow() bool {
return env != nil && env.NewWindow
}
// AddFootnote adds a footnote node to the environment and returns the number of that footnote.
func (env *Environment) AddFootnote(fn *ast.FootnoteNode) int {
if env == nil {
return 0
}
env.footnotes = append(env.footnotes, fn)
return len(env.footnotes)
}
// GetCleanFootnotes returns the list of remembered footnote and forgets about them.
func (env *Environment) GetCleanFootnotes() []*ast.FootnoteNode {
if env == nil {
return nil
}
result := env.footnotes
env.footnotes = nil
return result
}
|
Changes to encoder/htmlenc/block.go.
︙ | | |
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
-
+
|
v.b.WriteStrings("</h", strLvl, ">\n")
}
// VisitHRule writes HTML code for a horizontal rule: <hr>.
func (v *visitor) VisitHRule(hn *ast.HRuleNode) {
v.b.WriteString("<hr")
v.visitAttributes(hn.Attrs)
if v.env.IsXHTML() {
if v.xhtml {
v.b.WriteString(" />\n")
} else {
v.b.WriteString(">\n")
}
}
var listCode = map[ast.NestedListCode]string{
|
︙ | | |
Changes to encoder/htmlenc/htmlenc.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
-
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
-
-
-
-
+
+
+
-
-
-
+
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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 htmlenc encodes the abstract syntax tree into HTML5.
package htmlenc
import (
"fmt"
"io"
"strings"
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/encoder/encfun"
"zettelstore.de/z/parser"
)
func init() {
encoder.Register("html", encoder.Info{
Create: func(env *encoder.Environment) encoder.Encoder { return &htmlEncoder{env: env} },
Create: func() encoder.Encoder { return &htmlEncoder{} },
})
}
type htmlEncoder struct {
lang string // default language
xhtml bool // use XHTML syntax instead of HTML syntax
markerExternal string // Marker after link to (external) material.
newWindow bool // open link in new window
adaptLink func(*ast.LinkNode) ast.InlineNode
adaptImage func(*ast.ImageNode) ast.InlineNode
adaptCite func(*ast.CiteNode) ast.InlineNode
ignoreMeta map[string]bool
footnotes []*ast.FootnoteNode
}
func (he *htmlEncoder) SetOption(option encoder.Option) {
switch opt := option.(type) {
case *encoder.StringOption:
switch opt.Key {
case "lang":
he.lang = opt.Value
case meta.KeyMarkerExternal:
he.markerExternal = opt.Value
}
env *encoder.Environment
}
case *encoder.BoolOption:
switch opt.Key {
case "newwindow":
he.newWindow = opt.Value
case "xhtml":
he.xhtml = opt.Value
}
case *encoder.StringsOption:
if opt.Key == "no-meta" {
he.ignoreMeta = make(map[string]bool, len(opt.Value))
for _, v := range opt.Value {
he.ignoreMeta[v] = true
}
}
case *encoder.AdaptLinkOption:
he.adaptLink = opt.Adapter
case *encoder.AdaptImageOption:
he.adaptImage = opt.Adapter
case *encoder.AdaptCiteOption:
he.adaptCite = opt.Adapter
default:
var name string
if option != nil {
name = option.Name()
}
fmt.Println("HESO", option, name)
}
}
// WriteZettel encodes a full zettel as HTML5.
func (he *htmlEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
func (he *htmlEncoder) WriteZettel(
w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
v := newVisitor(he, w)
if !he.env.IsXHTML() {
if !he.xhtml {
v.b.WriteString("<!DOCTYPE html>\n")
}
if env := he.env; env != nil && env.Lang == "" {
v.b.WriteStrings("<html>\n<head>")
} else {
v.b.WriteStrings("<html lang=\"", env.Lang, "\">")
v.b.WriteStrings("<html lang=\"", he.lang, "\">\n<head>\n<meta charset=\"utf-8\">\n")
textEnc := encoder.Create("text")
var sb strings.Builder
}
v.b.WriteString("\n<head>\n<meta charset=\"utf-8\">\n")
v.b.WriteStrings("<title>", encfun.MetaAsText(zn.InhMeta, meta.KeyTitle), "</title>")
textEnc.WriteInlines(&sb, zn.Title)
v.b.WriteStrings("<title>", sb.String(), "</title>")
if inhMeta {
v.acceptMeta(zn.InhMeta)
v.acceptMeta(zn.InhMeta, false)
} else {
v.acceptMeta(zn.Meta)
v.acceptMeta(zn.Zettel.Meta, false)
}
v.b.WriteString("\n</head>\n<body>\n")
v.acceptBlockSlice(zn.Ast)
v.writeEndnotes()
v.b.WriteString("</body>\n</html>")
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data as HTML5.
func (he *htmlEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
v := newVisitor(he, w)
// Write title
if title, ok := m.Get(meta.KeyTitle); ok {
textEnc := encoder.Create("text", nil)
var sb strings.Builder
textEnc.WriteInlines(&sb, parser.ParseMetadata(title))
v.b.WriteStrings("<meta name=\"zs-", meta.KeyTitle, "\" content=\"")
v.writeQuotedEscaped(sb.String())
v.b.WriteString("\">")
}
// Write other metadata
v.acceptMeta(m)
v.acceptMeta(m, true)
length, err := v.b.Flush()
return length, err
}
func (he *htmlEncoder) WriteContent(w io.Writer, zn *ast.ZettelNode) (int, error) {
return he.WriteBlocks(w, zn.Ast)
}
// WriteBlocks encodes a block slice.
func (he *htmlEncoder) WriteBlocks(w io.Writer, bs ast.BlockSlice) (int, error) {
v := newVisitor(he, w)
v.acceptBlockSlice(bs)
v.writeEndnotes()
length, err := v.b.Flush()
return length, err
}
// WriteInlines writes an inline slice to the writer
func (he *htmlEncoder) WriteInlines(w io.Writer, is ast.InlineSlice) (int, error) {
v := newVisitor(he, w)
if env := he.env; env != nil {
v.inInteractive = env.Interactive
}
v.acceptInlineSlice(is)
length, err := v.b.Flush()
return length, err
}
|
Changes to encoder/htmlenc/inline.go.
︙ | | |
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
-
+
-
+
+
-
-
-
-
+
+
+
+
+
+
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
+
+
+
+
|
v.b.WriteString("<span class=\"zettel-tag\">#")
v.writeHTMLEscaped(tn.Tag)
v.b.WriteString("</span>")
}
// VisitSpace emits a white space.
func (v *visitor) VisitSpace(sn *ast.SpaceNode) {
if v.inVerse || v.env.IsXHTML() {
if v.inVerse || v.xhtml {
v.b.WriteString(sn.Lexeme)
} else {
v.b.WriteByte(' ')
}
}
// VisitBreak writes HTML code for line breaks.
func (v *visitor) VisitBreak(bn *ast.BreakNode) {
if bn.Hard {
if v.env.IsXHTML() {
if v.xhtml {
v.b.WriteString("<br />\n")
} else {
v.b.WriteString("<br>\n")
}
} else {
v.b.WriteByte('\n')
}
}
// VisitLink writes HTML code for links.
func (v *visitor) VisitLink(ln *ast.LinkNode) {
if adapt := v.enc.adaptLink; adapt != nil {
ln, n := v.env.AdaptLink(ln)
if n != nil {
n.Accept(v)
return
n := adapt(ln)
var ok bool
if ln, ok = n.(*ast.LinkNode); !ok {
n.Accept(v)
return
}
}
v.lang.push(ln.Attrs)
defer v.lang.pop()
switch ln.Ref.State {
case ast.RefStateSelf, ast.RefStateFound, ast.RefStateHosted, ast.RefStateBased:
v.writeAHref(ln.Ref, ln.Attrs, ln.Inlines)
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:
attrs := ln.Attrs.Clone()
attrs = attrs.Set("class", "zs-external")
if v.env.HasNewWindow() {
if v.enc.newWindow {
attrs = attrs.Set("target", "_blank").Set("rel", "noopener noreferrer")
}
v.writeAHref(ln.Ref, attrs, ln.Inlines)
if v.env != nil {
v.b.WriteString(v.env.MarkerExternal)
v.b.WriteString(v.enc.markerExternal)
}
default:
if v.env.IsInteractive(v.inInteractive) {
v.writeSpan(ln.Inlines, ln.Attrs)
return
}
v.b.WriteString("<a href=\"")
v.writeQuotedEscaped(ln.Ref.Value)
v.b.WriteByte('"')
v.visitAttributes(ln.Attrs)
v.b.WriteByte('>')
v.inInteractive = true
v.acceptInlineSlice(ln.Inlines)
v.inInteractive = false
v.b.WriteString("</a>")
}
}
func (v *visitor) writeAHref(ref *ast.Reference, attrs *ast.Attributes, ins ast.InlineSlice) {
if v.env.IsInteractive(v.inInteractive) {
v.writeSpan(ins, attrs)
return
}
v.b.WriteString("<a href=\"")
v.writeReference(ref)
v.b.WriteByte('"')
v.visitAttributes(attrs)
v.b.WriteByte('>')
v.inInteractive = true
v.acceptInlineSlice(ins)
v.inInteractive = false
v.b.WriteString("</a>")
}
// VisitImage writes HTML code for images.
func (v *visitor) VisitImage(in *ast.ImageNode) {
if adapt := v.enc.adaptImage; adapt != nil {
in, n := v.env.AdaptImage(in)
if n != nil {
n.Accept(v)
return
}
n := adapt(in)
var ok bool
if in, ok = n.(*ast.ImageNode); !ok {
n.Accept(v)
return
}
}
v.lang.push(in.Attrs)
defer v.lang.pop()
if in.Ref == nil {
v.b.WriteString("<img src=\"data:image/")
switch in.Syntax {
case "svg":
|
︙ | | |
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
-
+
+
-
-
-
-
-
+
+
+
+
+
-
-
+
+
+
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
-
-
-
|
v.b.WriteString("<img src=\"")
v.writeReference(in.Ref)
}
v.b.WriteString("\" alt=\"")
v.acceptInlineSlice(in.Inlines)
v.b.WriteByte('"')
v.visitAttributes(in.Attrs)
if v.env.IsXHTML() {
if v.xhtml {
v.b.WriteString(" />")
} else {
v.b.WriteByte('>')
}
}
// VisitCite writes code for citations.
func (v *visitor) VisitCite(cn *ast.CiteNode) {
if adapt := v.enc.adaptCite; adapt != nil {
cn, n := v.env.AdaptCite(cn)
if n != nil {
n.Accept(v)
return
}
n := adapt(cn)
if n != cn {
n.Accept(v)
return
}
if cn == nil {
return
}
v.lang.push(cn.Attrs)
defer v.lang.pop()
if cn != nil {
v.b.WriteString(cn.Key)
if len(cn.Inlines) > 0 {
v.b.WriteString(", ")
v.acceptInlineSlice(cn.Inlines)
v.b.WriteString(cn.Key)
if len(cn.Inlines) > 0 {
v.b.WriteString(", ")
v.acceptInlineSlice(cn.Inlines)
}
}
}
// VisitFootnote write HTML code for a footnote.
func (v *visitor) VisitFootnote(fn *ast.FootnoteNode) {
v.lang.push(fn.Attrs)
defer v.lang.pop()
if v.env.IsInteractive(v.inInteractive) {
return
}
n := strconv.Itoa(v.env.AddFootnote(fn))
v.enc.footnotes = append(v.enc.footnotes, fn)
n := strconv.Itoa(len(v.enc.footnotes))
v.b.WriteStrings("<sup id=\"fnref:", n, "\"><a href=\"#fn:", n, "\" class=\"zs-footnote-ref\" role=\"doc-noteref\">", n, "</a></sup>")
// TODO: what to do with Attrs?
}
// VisitMark writes HTML code to mark a position.
func (v *visitor) VisitMark(mn *ast.MarkNode) {
if v.env.IsInteractive(v.inInteractive) {
return
}
if len(mn.Text) > 0 {
v.b.WriteStrings("<a id=\"", mn.Text, "\"></a>")
}
}
// VisitFormat write HTML code for formatting text.
func (v *visitor) VisitFormat(fn *ast.FormatNode) {
|
︙ | | |
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
+
-
+
-
-
-
-
-
-
-
-
-
-
|
case ast.FormatSub:
code = "sub"
case ast.FormatQuotation:
code = "q"
case ast.FormatSmall:
code = "small"
case ast.FormatSpan:
code = "span"
v.writeSpan(fn.Inlines, processSpanAttributes(attrs))
attrs = processSpanAttributes(attrs)
return
case ast.FormatMonospace:
code = "span"
attrs = attrs.Set("style", "font-family:monospace")
case ast.FormatQuote:
v.visitQuotes(fn)
return
default:
panic(fmt.Sprintf("Unknown format code %v", fn.Code))
}
v.b.WriteStrings("<", code)
v.visitAttributes(attrs)
v.b.WriteByte('>')
v.acceptInlineSlice(fn.Inlines)
v.b.WriteStrings("</", code, ">")
}
func (v *visitor) writeSpan(ins ast.InlineSlice, attrs *ast.Attributes) {
v.b.WriteString("<span")
v.visitAttributes(attrs)
v.b.WriteByte('>')
v.acceptInlineSlice(ins)
v.b.WriteString("</span>")
}
var langQuotes = map[string][2]string{
"en": {"“", "”"},
"de": {"„", "“"},
"fr": {"« ", " »"},
}
|
︙ | | |
Changes to encoder/htmlenc/visitor.go.
︙ | | |
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
+
+
+
+
+
+
|
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/strfun"
)
// visitor writes the abstract syntax tree to an io.Writer.
type visitor struct {
env *encoder.Environment
b encoder.BufWriter
visibleSpace bool // Show space character in raw text
inVerse bool // In verse block
inInteractive bool // Rendered interactive HTML code
lang langStack
enc *htmlEncoder
b encoder.BufWriter
visibleSpace bool // Show space character in raw text
inVerse bool // In verse block
xhtml bool // copied from enc.xhtml
lang langStack
}
func newVisitor(he *htmlEncoder, w io.Writer) *visitor {
var lang string
if he.env != nil {
lang = he.env.Lang
}
return &visitor{
env: he.env,
b: encoder.NewBufWriter(w),
lang: newLangStack(lang),
enc: he,
b: encoder.NewBufWriter(w),
xhtml: he.xhtml,
lang: newLangStack(he.lang),
}
}
var mapMetaKey = map[string]string{
meta.KeyCopyright: "copyright",
meta.KeyLicense: "license",
}
func (v *visitor) acceptMeta(m *meta.Meta) {
for _, pair := range m.Pairs(true) {
if env := v.env; env != nil && env.IgnoreMeta[pair.Key] {
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 pair.Key == meta.KeyTitle {
if withTitle {
// TODO: title value may contain zmk elements
v.b.WriteStrings("<meta name=\"zs-", pair.Key, "\" content=\"")
v.writeQuotedEscaped(pair.Value)
v.b.WriteString("\">")
}
continue
}
if pair.Key == meta.KeyTags {
v.writeTags(pair.Value)
} else if key, ok := mapMetaKey[pair.Key]; ok {
v.writeMeta("", key, pair.Value)
} else {
|
︙ | | |
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
-
-
+
-
+
-
+
|
func (v *visitor) acceptInlineSlice(ins ast.InlineSlice) {
for _, in := range ins {
in.Accept(v)
}
}
func (v *visitor) writeEndnotes() {
footnotes := v.env.GetCleanFootnotes()
if len(footnotes) > 0 {
if len(v.enc.footnotes) > 0 {
v.b.WriteString("<ol class=\"zs-endnotes\">\n")
for i := 0; i < len(footnotes); i++ {
for i := 0; i < len(v.enc.footnotes); i++ {
// Do not use a range loop above, because a footnote may contain
// a footnote. Therefore v.enc.footnote may grow during the loop.
fn := footnotes[i]
fn := v.enc.footnotes[i]
n := strconv.Itoa(i + 1)
v.b.WriteStrings("<li id=\"fn:", n, "\" role=\"doc-endnote\">")
v.acceptInlineSlice(fn.Inlines)
v.b.WriteStrings(
" <a href=\"#fnref:",
n,
"\" class=\"zs-footnote-backref\" role=\"doc-backlink\">↩︎</a></li>\n")
|
︙ | | |
Changes to encoder/jsonenc/djsonenc.go.
︙ | | |
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
-
+
+
-
+
-
+
-
+
+
+
+
+
-
-
-
+
+
+
+
|
"io"
"sort"
"strconv"
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/encoder/encfun"
)
func init() {
encoder.Register("djson", encoder.Info{
Create: func(env *encoder.Environment) encoder.Encoder { return &jsonDetailEncoder{env: env} },
Create: func() encoder.Encoder { return &jsonDetailEncoder{} },
})
}
type jsonDetailEncoder struct {
adaptLink func(*ast.LinkNode) ast.InlineNode
adaptImage func(*ast.ImageNode) ast.InlineNode
title ast.InlineSlice
}
// SetOption sets an option for the encoder
func (je *jsonDetailEncoder) SetOption(option encoder.Option) {
switch opt := option.(type) {
env *encoder.Environment
case *encoder.TitleOption:
je.title = opt.Inline
case *encoder.AdaptLinkOption:
je.adaptLink = opt.Adapter
case *encoder.AdaptImageOption:
je.adaptImage = opt.Adapter
}
}
// WriteZettel writes the encoded zettel to the writer.
func (je *jsonDetailEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
func (je *jsonDetailEncoder) WriteZettel(
w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
v := newDetailVisitor(w, je)
v.b.WriteString("{\"meta\":{\"title\":")
v.acceptInlineSlice(encfun.MetaAsInlineSlice(zn.InhMeta, meta.KeyTitle))
v.acceptInlineSlice(zn.Title)
if inhMeta {
v.writeMeta(zn.InhMeta)
v.writeMeta(zn.InhMeta, false)
} else {
v.writeMeta(zn.Meta)
v.writeMeta(zn.Zettel.Meta, false)
}
v.b.WriteByte('}')
v.b.WriteString(",\"content\":")
v.acceptBlockSlice(zn.Ast)
v.b.WriteByte('}')
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data as JSON.
func (je *jsonDetailEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
v := newDetailVisitor(w, je)
v.b.WriteByte('{')
if je.title == nil {
v.writeMeta(m, true)
} else {
v.b.WriteString("{\"title\":")
v.acceptInlineSlice(encfun.MetaAsInlineSlice(m, meta.KeyTitle))
v.writeMeta(m)
v.b.WriteString("\"title\":")
v.acceptInlineSlice(je.title)
v.writeMeta(m, false)
}
v.b.WriteByte('}')
length, err := v.b.Flush()
return length, err
}
func (je *jsonDetailEncoder) WriteContent(w io.Writer, zn *ast.ZettelNode) (int, error) {
return je.WriteBlocks(w, zn.Ast)
|
︙ | | |
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
-
+
-
+
|
length, err := v.b.Flush()
return length, err
}
// detailVisitor writes the abstract syntax tree to an io.Writer.
type detailVisitor struct {
b encoder.BufWriter
env *encoder.Environment
enc *jsonDetailEncoder
}
func newDetailVisitor(w io.Writer, je *jsonDetailEncoder) *detailVisitor {
return &detailVisitor{b: encoder.NewBufWriter(w), env: je.env}
return &detailVisitor{b: encoder.NewBufWriter(w), enc: je}
}
// VisitPara emits JSON code for a paragraph.
func (v *detailVisitor) VisitPara(pn *ast.ParaNode) {
v.writeNodeStart("Para")
v.writeContentStart('i')
v.acceptInlineSlice(pn.Inlines)
|
︙ | | |
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
|
ast.RefStateHosted: "local",
ast.RefStateBased: "based",
ast.RefStateExternal: "external",
}
// VisitLink writes JSON code for links.
func (v *detailVisitor) VisitLink(ln *ast.LinkNode) {
if adapt := v.enc.adaptLink; adapt != nil {
ln, n := v.env.AdaptLink(ln)
if n != nil {
n.Accept(v)
return
n := adapt(ln)
var ok bool
if ln, ok = n.(*ast.LinkNode); !ok {
n.Accept(v)
return
}
}
v.writeNodeStart("Link")
v.visitAttributes(ln.Attrs)
v.writeContentStart('q')
writeEscaped(&v.b, mapRefState[ln.Ref.State])
v.writeContentStart('s')
writeEscaped(&v.b, ln.Ref.String())
v.writeContentStart('i')
v.acceptInlineSlice(ln.Inlines)
v.b.WriteByte('}')
}
// VisitImage writes JSON code for images.
func (v *detailVisitor) VisitImage(in *ast.ImageNode) {
if adapt := v.enc.adaptImage; adapt != nil {
in, n := v.env.AdaptImage(in)
if n != nil {
n.Accept(v)
return
n := adapt(in)
var ok bool
if in, ok = n.(*ast.ImageNode); !ok {
n.Accept(v)
return
}
}
v.writeNodeStart("Image")
v.visitAttributes(in.Attrs)
if in.Ref == nil {
v.writeContentStart('j')
v.b.WriteString("\"s\":")
writeEscaped(&v.b, in.Syntax)
|
︙ | | |
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
-
+
+
-
+
+
+
+
+
-
+
+
|
if b, ok := contentCode[code]; ok {
v.b.Write(b)
return
}
panic("Unknown content code " + strconv.Itoa(int(code)))
}
func (v *detailVisitor) writeMeta(m *meta.Meta) {
func (v *detailVisitor) writeMeta(m *meta.Meta, withTitle bool) {
first := withTitle
for _, p := range m.Pairs(true) {
if p.Key == meta.KeyTitle {
if p.Key == "title" && !withTitle {
continue
}
if first {
v.b.WriteByte('"')
first = false
} else {
v.b.WriteString(",\"")
v.b.WriteString(",\"")
}
v.b.Write(Escape(p.Key))
v.b.WriteString("\":")
if m.Type(p.Key).IsSet {
v.writeSetValue(p.Value)
} else {
v.b.WriteByte('"')
v.b.Write(Escape(p.Value))
|
︙ | | |
Changes to encoder/jsonenc/jsonenc.go.
︙ | | |
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
-
+
+
+
+
|
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
)
func init() {
encoder.Register("json", encoder.Info{
Create: func(*encoder.Environment) encoder.Encoder { return &jsonEncoder{} },
Create: func() encoder.Encoder { return &jsonEncoder{} },
Default: true,
})
}
// jsonEncoder is just a stub. It is not implemented. The real implementation
// is in file web/adapter/json.go
type jsonEncoder struct{}
// 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) {
return 0, encoder.ErrNoWriteZettel
}
|
︙ | | |
Changes to encoder/nativeenc/nativeenc.go.
︙ | | |
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
-
-
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
-
+
-
+
|
"io"
"sort"
"strconv"
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/encoder/encfun"
"zettelstore.de/z/parser"
)
func init() {
encoder.Register("native", encoder.Info{
Create: func(env *encoder.Environment) encoder.Encoder { return &nativeEncoder{env: env} },
Create: func() encoder.Encoder { return &nativeEncoder{} },
})
}
type nativeEncoder struct {
adaptLink func(*ast.LinkNode) ast.InlineNode
adaptImage func(*ast.ImageNode) ast.InlineNode
}
// SetOption sets one option for this encoder.
func (ne *nativeEncoder) SetOption(option encoder.Option) {
switch opt := option.(type) {
env *encoder.Environment
case *encoder.AdaptLinkOption:
ne.adaptLink = opt.Adapter
case *encoder.AdaptImageOption:
ne.adaptImage = opt.Adapter
}
}
// WriteZettel encodes the zettel to the writer.
func (ne *nativeEncoder) WriteZettel(
w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
v := newVisitor(w, ne)
v.b.WriteString("[Title ")
v.acceptInlineSlice(encfun.MetaAsInlineSlice(zn.InhMeta, meta.KeyTitle))
v.acceptInlineSlice(zn.Title)
v.b.WriteByte(']')
if inhMeta {
v.acceptMeta(zn.InhMeta, false)
} else {
v.acceptMeta(zn.Meta, false)
v.acceptMeta(zn.Zettel.Meta, false)
}
v.b.WriteByte('\n')
v.acceptBlockSlice(zn.Ast)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data in native format.
// WriteMeta encodes meta data as HTML5.
func (ne *nativeEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
v := newVisitor(w, ne)
v.acceptMeta(m, true)
length, err := v.b.Flush()
return length, err
}
|
︙ | | |
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
-
+
-
+
-
-
-
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
return length, err
}
// visitor writes the abstract syntax tree to an io.Writer.
type visitor struct {
b encoder.BufWriter
level int
env *encoder.Environment
enc *nativeEncoder
}
func newVisitor(w io.Writer, enc *nativeEncoder) *visitor {
return &visitor{b: encoder.NewBufWriter(w), env: enc.env}
return &visitor{b: encoder.NewBufWriter(w), enc: enc}
}
var (
rawBackslash = []byte{'\\', '\\'}
rawDoubleQuote = []byte{'\\', '"'}
rawNewline = []byte{'\\', 'n'}
)
func (v *visitor) acceptMeta(m *meta.Meta, withTitle bool) {
if withTitle {
v.b.WriteString("[Title ")
v.acceptInlineSlice(parser.ParseMetadata(m.GetDefault(meta.KeyTitle, "")))
v.b.WriteByte(']')
v.b.WriteString("[Title \"")
v.writeEscaped(m.GetDefault(meta.KeyTitle, ""))
v.b.WriteString("\"]")
}
v.writeMetaString(m, meta.KeyRole, "Role")
v.writeMetaList(m, meta.KeyTags, "Tags")
v.writeMetaString(m, meta.KeySyntax, "Syntax")
pairs := m.PairsRest(true)
if pairs := m.PairsRest(true); len(pairs) > 0 {
if len(pairs) == 0 {
return
}
v.b.WriteString("\n[Header")
v.level++
for i, p := range pairs {
if i > 0 {
v.b.WriteByte(',')
}
v.writeNewLine()
v.b.WriteByte('[')
v.b.WriteStrings(p.Key, " \"")
v.writeEscaped(p.Value)
v.b.WriteString("\"]")
}
v.level--
v.b.WriteByte(']')
v.b.WriteString("\n[Header")
first := true
v.level++
for _, p := range pairs {
if !first {
v.b.WriteByte(',')
}
v.writeNewLine()
v.b.WriteByte('[')
v.b.WriteStrings(p.Key, " \"")
v.writeEscaped(p.Value)
v.b.WriteString("\"]")
first = false
}
v.level--
v.b.WriteByte(']')
}
}
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, "\"]")
}
}
|
︙ | | |
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
|
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 {
ln, n := v.env.AdaptLink(ln)
if n != nil {
n.Accept(v)
return
n := adapt(ln)
var ok bool
if ln, ok = n.(*ast.LinkNode); !ok {
n.Accept(v)
return
}
}
v.b.WriteString("Link")
v.visitAttributes(ln.Attrs)
v.b.WriteByte(' ')
v.b.WriteString(mapRefState[ln.Ref.State])
v.b.WriteString(" \"")
v.writeEscaped(ln.Ref.String())
v.b.WriteString("\" [")
if !ln.OnlyRef {
v.acceptInlineSlice(ln.Inlines)
}
v.b.WriteByte(']')
}
// VisitImage writes native code for images.
func (v *visitor) VisitImage(in *ast.ImageNode) {
if adapt := v.enc.adaptImage; adapt != nil {
in, n := v.env.AdaptImage(in)
if n != nil {
n.Accept(v)
return
n := adapt(in)
var ok bool
if in, ok = n.(*ast.ImageNode); !ok {
n.Accept(v)
return
}
}
v.b.WriteString("Image")
v.visitAttributes(in.Attrs)
if in.Ref == nil {
v.b.WriteStrings(" {\"", in.Syntax, "\" \"")
switch in.Syntax {
case "svg":
|
︙ | | |
Added encoder/options.go.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
//-----------------------------------------------------------------------------
// 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 encoder provides a generic interface to encode the abstract syntax
// tree into some text form.
package encoder
import (
"zettelstore.de/z/ast"
)
// StringOption is an option with a string value
type StringOption struct {
Key string
Value string
}
// Name returns the visible name of this option.
func (so *StringOption) Name() string { return so.Key }
// BoolOption is an option with a boolean value.
type BoolOption struct {
Key string
Value bool
}
// Name returns the visible name of this option.
func (bo *BoolOption) Name() string { return bo.Key }
// TitleOption is an option to give the title as a AST inline slice
type TitleOption struct {
Inline ast.InlineSlice
}
// Name returns the visible name of this option.
func (mo *TitleOption) Name() string { return "title" }
// StringsOption is an option that have a sequence of strings as the value.
type StringsOption struct {
Key string
Value []string
}
// Name returns the visible name of this option.
func (so *StringsOption) Name() string { return so.Key }
// AdaptLinkOption specifies a link adapter.
type AdaptLinkOption struct {
Adapter func(*ast.LinkNode) ast.InlineNode
}
// Name returns the visible name of this option.
func (al *AdaptLinkOption) Name() string { return "AdaptLinkOption" }
// AdaptImageOption specifies an image adapter.
type AdaptImageOption struct {
Adapter func(*ast.ImageNode) ast.InlineNode
}
// Name returns the visible name of this option.
func (al *AdaptImageOption) Name() string { return "AdaptImageOption" }
// AdaptCiteOption specifies a citation adapter.
type AdaptCiteOption struct {
Adapter func(*ast.CiteNode) ast.InlineNode
}
// Name returns the visible name of this option.
func (al *AdaptCiteOption) Name() string { return "AdaptCiteOption" }
|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Changes to encoder/rawenc/rawenc.go.
︙ | | |
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
-
+
+
+
+
-
+
-
+
-
+
|
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
)
func init() {
encoder.Register("raw", encoder.Info{
Create: func(*encoder.Environment) encoder.Encoder { return &rawEncoder{} },
Create: func() encoder.Encoder { return &rawEncoder{} },
})
}
type rawEncoder struct{}
// 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) {
b := encoder.NewBufWriter(w)
if inhMeta {
zn.InhMeta.Write(&b, true)
} else {
zn.Meta.Write(&b, true)
zn.Zettel.Meta.Write(&b, true)
}
b.WriteByte('\n')
b.WriteString(zn.Content.AsString())
b.WriteString(zn.Zettel.Content.AsString())
length, err := b.Flush()
return length, err
}
// WriteMeta encodes meta data as HTML5.
func (re *rawEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
b := encoder.NewBufWriter(w)
m.Write(&b, true)
length, err := b.Flush()
return length, err
}
func (re *rawEncoder) WriteContent(w io.Writer, zn *ast.ZettelNode) (int, error) {
b := encoder.NewBufWriter(w)
b.WriteString(zn.Content.AsString())
b.WriteString(zn.Zettel.Content.AsString())
length, err := b.Flush()
return length, err
}
// WriteBlocks writes a block slice to the writer
func (re *rawEncoder) WriteBlocks(w io.Writer, bs ast.BlockSlice) (int, error) {
return 0, encoder.ErrNoWriteBlocks
}
// WriteInlines writes an inline slice to the writer
func (re *rawEncoder) WriteInlines(w io.Writer, is ast.InlineSlice) (int, error) {
return 0, encoder.ErrNoWriteInlines
}
|
Changes to encoder/textenc/textenc.go.
︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
-
-
+
+
+
+
-
-
+
+
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
|
import (
"io"
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
"zettelstore.de/z/parser"
)
func init() {
encoder.Register("text", encoder.Info{
Create: func(*encoder.Environment) encoder.Encoder { return &textEncoder{} },
Create: func() encoder.Encoder { return &textEncoder{} },
})
}
type textEncoder struct{}
// SetOption does nothing because this encoder does not recognize any option.
func (te *textEncoder) SetOption(option encoder.Option) {}
// WriteZettel writes metadata and content.
func (te *textEncoder) WriteZettel(w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
// WriteZettel does nothing.
func (te *textEncoder) WriteZettel(
w io.Writer, zn *ast.ZettelNode, inhMeta bool) (int, error) {
v := newVisitor(w)
if inhMeta {
te.WriteMeta(&v.b, zn.InhMeta)
} else {
te.WriteMeta(&v.b, zn.Meta)
te.WriteMeta(&v.b, zn.Zettel.Meta)
}
v.acceptBlockSlice(zn.Ast)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes metadata as text.
// WriteMeta encodes meta data as text.
func (te *textEncoder) WriteMeta(w io.Writer, m *meta.Meta) (int, error) {
b := encoder.NewBufWriter(w)
for _, pair := range m.Pairs(true) {
switch meta.Type(pair.Key) {
case meta.TypeBool:
if meta.BoolValue(pair.Value) {
b.WriteString("true")
} else {
b.WriteString("false")
}
case meta.TypeTagSet:
for i, tag := range meta.ListFromValue(pair.Value) {
if i > 0 {
b.WriteByte(' ')
}
b.WriteString(meta.CleanTag(tag))
}
case meta.TypeZettelmarkup:
te.WriteInlines(w, parser.ParseMetadata(pair.Value))
default:
b.WriteString(pair.Value)
b.WriteString(pair.Value)
}
b.WriteByte('\n')
}
length, err := b.Flush()
return length, err
}
func (te *textEncoder) WriteContent(w io.Writer, zn *ast.ZettelNode) (int, error) {
|
︙ | | |
Changes to encoder/zmkenc/zmkenc.go.
︙ | | |
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
-
+
+
+
+
-
+
|
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/encoder"
)
func init() {
encoder.Register("zmk", encoder.Info{
Create: func(*encoder.Environment) encoder.Encoder { return &zmkEncoder{} },
Create: func() encoder.Encoder { return &zmkEncoder{} },
})
}
type zmkEncoder struct{}
// 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) {
v := newVisitor(w, ze)
if inhMeta {
zn.InhMeta.WriteAsHeader(&v.b, true)
} else {
zn.Meta.WriteAsHeader(&v.b, true)
zn.Zettel.Meta.WriteAsHeader(&v.b, true)
}
v.acceptBlockSlice(zn.Ast)
length, err := v.b.Flush()
return length, err
}
// WriteMeta encodes meta data as zmk.
|
︙ | | |
Changes to go.mod.
1
2
3
4
5
6
7
8
9
10
11
12
|
1
2
3
4
5
6
7
8
9
10
11
|
-
+
-
-
+
+
-
-
+
|
module zettelstore.de/z
go 1.16
go 1.15
require (
github.com/fsnotify/fsnotify v1.4.9
github.com/pascaldekloe/jwt v1.10.0
github.com/yuin/goldmark v1.3.3
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
github.com/yuin/goldmark v1.3.2
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221
golang.org/x/text v0.3.6
golang.org/x/text v0.3.0
)
|
Changes to go.sum.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
|
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.3 h1:37BdQwPx8VOSic8eDSWee6QL9mRpZRm9VJp/QugNrW0=
github.com/yuin/goldmark v1.3.3/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-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
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=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 h1:L2auWcuQIvxz9xSEqzESnV/QN/gNRXNApHi3fYwl2w0=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
Changes to index/index.go.
︙ | | |
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
|
"context"
"io"
"time"
"zettelstore.de/z/domain"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/place/change"
"zettelstore.de/z/place"
)
// Enricher is used to update metadata by adding new properties.
type Enricher interface {
// Enrich computes additional properties and updates the given metadata.
// It is typically called by zettel reading methods.
Enrich(ctx context.Context, m *meta.Meta)
}
// Selector is used to select zettel identifier based on selection criteria.
type Selector interface {
// Select all zettel that contains the given exact word.
// The word must be normalized through Unicode NKFD.
Select(word string) id.Set
// Select all zettel that have a word with the given prefix.
// The prefix must be normalized through Unicode NKFD.
SelectPrefix(prefix string) id.Set
// Select all zettel that contains the given string.
// The string must be normalized through Unicode NKFD.
SelectContains(s string) id.Set
}
// NoEnrichContext will signal an enricher that nothing has to be done.
// This is useful for an Indexer, but also for some place.Place calls, when
// just the plain metadata is needed.
func NoEnrichContext(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxNoEnrichKey, &ctxNoEnrichKey)
}
type ctxNoEnrichType struct{}
var ctxNoEnrichKey ctxNoEnrichType
// DoNotEnrich determines if the context is marked to not enrich metadata.
func DoNotEnrich(ctx context.Context) bool {
_, ok := ctx.Value(ctxNoEnrichKey).(*ctxNoEnrichType)
return ok
}
// Port contains all the used functions to access zettel to be indexed.
type Port interface {
RegisterObserver(change.Func)
RegisterObserver(func(place.ChangeInfo))
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.
type Indexer interface {
Enricher
Selector
// Start the index. It will read all zettel and store index data for later retrieval.
Start(Port)
// Stop the index. No zettel are read any more, but the current index data
// can stil be retrieved.
Stop()
|
︙ | | |
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
-
|
Store StoreStats
}
// Store all relevant zettel data. There may be multiple implementations, i.e.
// memory-based, file-based, based on SQLite, ...
type Store interface {
Enricher
Selector
// UpdateReferences for a specific zettel.
// 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.
// Returns set of zettel identifier that must also be checked for changes.
|
︙ | | |
125
126
127
128
129
130
131
132
133
134
135
|
108
109
110
111
112
113
114
115
|
-
+
-
-
-
|
// StoreStats records statistics about the store.
type StoreStats struct {
// Zettel is the number of zettel managed by the indexer.
Zettel int
// Updates count the number of metadata updates.
Updates uint64
}
// Words count the different words stored in the store.
Words uint64
}
|
Deleted index/indexer/collect.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// Copyright (c) 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 indexer allows to search for metadata and content.
package indexer
import (
"zettelstore.de/z/ast"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/index"
"zettelstore.de/z/strfun"
)
func collectZettelIndexData(zn *ast.ZettelNode, refs id.Set, words index.WordSet) {
ixv := ixVisitor{refs: refs, words: words}
ast.NewTopDownTraverser(&ixv).VisitBlockSlice(zn.Ast)
}
func collectInlineIndexData(ins ast.InlineSlice, refs id.Set, words index.WordSet) {
ixv := ixVisitor{refs: refs, words: words}
ast.NewTopDownTraverser(&ixv).VisitInlineSlice(ins)
}
type ixVisitor struct {
refs id.Set
words index.WordSet
}
// VisitVerbatim collects the verbatim text in the word set.
func (lv *ixVisitor) VisitVerbatim(vn *ast.VerbatimNode) {
for _, line := range vn.Lines {
lv.addText(line)
}
}
// VisitRegion does nothing.
func (lv *ixVisitor) VisitRegion(rn *ast.RegionNode) {}
// VisitHeading does nothing.
func (lv *ixVisitor) VisitHeading(hn *ast.HeadingNode) {}
// VisitHRule does nothing.
func (lv *ixVisitor) VisitHRule(hn *ast.HRuleNode) {}
// VisitList does nothing.
func (lv *ixVisitor) VisitNestedList(ln *ast.NestedListNode) {}
// VisitDescriptionList does nothing.
func (lv *ixVisitor) VisitDescriptionList(dn *ast.DescriptionListNode) {}
// VisitPara does nothing.
func (lv *ixVisitor) VisitPara(pn *ast.ParaNode) {}
// VisitTable does nothing.
func (lv *ixVisitor) VisitTable(tn *ast.TableNode) {}
// VisitBLOB does nothing.
func (lv *ixVisitor) VisitBLOB(bn *ast.BLOBNode) {}
// VisitText collects the text in the word set.
func (lv *ixVisitor) VisitText(tn *ast.TextNode) {
lv.addText(tn.Text)
}
// VisitTag collects the tag name in the word set.
func (lv *ixVisitor) VisitTag(tn *ast.TagNode) {
lv.addText(tn.Tag)
}
// VisitSpace does nothing.
func (lv *ixVisitor) VisitSpace(sn *ast.SpaceNode) {}
// VisitBreak does nothing.
func (lv *ixVisitor) VisitBreak(bn *ast.BreakNode) {}
// VisitLink collects the given link as a reference.
func (lv *ixVisitor) VisitLink(ln *ast.LinkNode) {
ref := ln.Ref
if ref == nil || !ref.IsZettel() {
return
}
if zid, err := id.Parse(ref.URL.Path); err == nil {
lv.refs[zid] = true
}
}
// VisitImage collects the image links as a reference.
func (lv *ixVisitor) VisitImage(in *ast.ImageNode) {
ref := in.Ref
if ref == nil || !ref.IsZettel() {
return
}
if zid, err := id.Parse(ref.URL.Path); err == nil {
lv.refs[zid] = true
}
}
// VisitCite does nothing.
func (lv *ixVisitor) VisitCite(cn *ast.CiteNode) {}
// VisitFootnote does nothing.
func (lv *ixVisitor) VisitFootnote(fn *ast.FootnoteNode) {}
// VisitMark does nothing.
func (lv *ixVisitor) VisitMark(mn *ast.MarkNode) {}
// VisitFormat does nothing.
func (lv *ixVisitor) VisitFormat(fn *ast.FormatNode) {}
// VisitLiteral collects the literal words in the word set.
func (lv *ixVisitor) VisitLiteral(ln *ast.LiteralNode) {
lv.addText(ln.Text)
}
func (lv *ixVisitor) addText(s string) {
for _, word := range strfun.NormalizeWords(s) {
lv.words[word] = lv.words[word] + 1
}
}
|
Changes to index/indexer/indexer.go.
︙ | | |
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
-
-
+
+
-
+
-
|
//-----------------------------------------------------------------------------
// Package indexer allows to search for metadata and content.
package indexer
import (
"context"
"log"
"runtime/debug"
"sync"
"time"
"zettelstore.de/z/ast"
"zettelstore.de/z/collect"
"zettelstore.de/z/domain"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/index"
"zettelstore.de/z/index/memstore"
"zettelstore.de/z/parser"
"zettelstore.de/z/place/change"
"zettelstore.de/z/place"
"zettelstore.de/z/strfun"
)
type indexer struct {
store index.Store
ar *anterooms
ready chan struct{} // Signal a non-empty anteroom to background task
done chan struct{} // Stop background task
|
︙ | | |
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
-
+
-
+
-
+
-
+
|
return &indexer{
store: memstore.New(),
ar: newAnterooms(10),
ready: make(chan struct{}, 1),
}
}
func (idx *indexer) observer(ci change.Info) {
func (idx *indexer) observer(ci place.ChangeInfo) {
switch ci.Reason {
case change.OnReload:
case place.OnReload:
idx.ar.Reset()
case change.OnUpdate:
case place.OnUpdate:
idx.ar.Enqueue(ci.Zid, arUpdate)
case change.OnDelete:
case place.OnDelete:
idx.ar.Enqueue(ci.Zid, arDelete)
default:
return
}
select {
case idx.ready <- struct{}{}:
default:
|
︙ | | |
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
|
// Enrich is called indirectly via indexer or enrichment is not requested
// because of other reasons -> ignore this call, do not update meta data
return
}
idx.store.Enrich(ctx, m)
}
// Select all zettel that contains the given exact word.
// The word must be normalized through Unicode NKFD.
func (idx *indexer) Select(word string) id.Set {
return idx.store.Select(word)
}
// Select all zettel that have a word with the given prefix.
// The prefix must be normalized through Unicode NKFD.
func (idx *indexer) SelectPrefix(prefix string) id.Set {
return idx.store.SelectPrefix(prefix)
}
// Select all zettel that contains the given string.
// The string must be normalized through Unicode NKFD.
func (idx *indexer) SelectContains(s string) id.Set {
return idx.store.SelectContains(s)
}
// ReadStats populates st with indexer statistics.
func (idx *indexer) ReadStats(st *index.IndexerStats) {
idx.mx.RLock()
st.LastReload = idx.lastReload
st.IndexesSinceReload = idx.sinceReload
st.DurLastIndex = idx.durLastIndex
idx.mx.RUnlock()
idx.store.ReadStats(&st.Store)
}
type indexerPort interface {
getMetaPort
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 r := recover(); r != nil {
if err := recover(); err != nil {
log.Println("recovered from:", r)
debug.PrintStack()
go idx.indexer(p)
}
}()
timerDuration := 15 * time.Second
timer := time.NewTimer(timerDuration)
ctx := index.NoEnrichContext(context.Background())
|
︙ | | |
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
type getMetaPort interface {
GetMeta(ctx context.Context, zid id.Zid) (*meta.Meta, error)
}
func (idx *indexer) updateZettel(ctx context.Context, zettel domain.Zettel, p getMetaPort) {
m := zettel.Meta
if m.GetBool(meta.KeyNoIndex) {
// Zettel maybe in index
toCheck := idx.store.DeleteZettel(ctx, m.Zid)
idx.checkZettel(toCheck)
return
}
refs := id.NewSet()
words := make(index.WordSet)
collectZettelIndexData(parser.ParseZettel(zettel, ""), refs, words)
zi := index.NewZettelIndex(m.Zid)
for _, pair := range m.Pairs(false) {
for _, pair := range m.PairsRest(false) {
descr := meta.GetDescription(pair.Key)
if descr.IsComputed() {
continue
}
switch descr.Type {
case meta.TypeID:
updateValue(ctx, descr.Inverse, pair.Value, p, zi)
case meta.TypeIDSet:
for _, val := range meta.ListFromValue(pair.Value) {
updateValue(ctx, descr.Inverse, val, p, zi)
}
case meta.TypeZettelmarkup:
collectInlineIndexData(parser.ParseMetadata(pair.Value), refs, words)
default:
for _, word := range strfun.NormalizeWords(pair.Value) {
words[word] = words[word] + 1
}
}
}
}
}
for ref := range refs {
if _, err := p.GetMeta(ctx, ref); err == nil {
zi.AddBackRef(ref)
zn := parser.ParseZettel(zettel, "")
refs := collect.References(zn)
updateReferences(ctx, refs.Links, p, zi)
updateReferences(ctx, refs.Images, p, zi)
} else {
zi.AddDeadRef(ref)
}
}
zi.SetWords(words)
toCheck := idx.store.UpdateReferences(ctx, zi)
idx.checkZettel(toCheck)
}
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 {
zi.AddDeadRef(zid)
return
}
if inverse == "" {
zi.AddBackRef(zid)
return
}
zi.AddMetaRef(inverse, zid)
}
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.URL.Path, p, zi)
}
}
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 {
zi.AddDeadRef(zid)
return
}
zi.AddBackRef(zid)
}
func (idx *indexer) deleteZettel(zid id.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)
}
}
|
Changes to index/memstore/memstore.go.
︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
-
-
-
+
-
-
-
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
|
// Package memstore stored the index in main memory.
package memstore
import (
"context"
"fmt"
"io"
"strings"
"sync"
"zettelstore.de/z/domain/id"
"zettelstore.de/z/domain/meta"
"zettelstore.de/z/index"
)
type metaRefs struct {
forward id.Slice
backward id.Slice
}
type zettelIndex struct {
dead id.Slice
forward id.Slice
backward id.Slice
meta map[string]metaRefs
words []string
}
func (zi *zettelIndex) isEmpty() bool {
if len(zi.forward) > 0 || len(zi.backward) > 0 || len(zi.dead) > 0 || len(zi.words) > 0 {
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
dead map[id.Zid]id.Slice // map dead refs where they occur
mx sync.RWMutex
idx map[id.Zid]*zettelIndex
dead map[id.Zid]id.Slice // map dead refs where they occur
words map[string]id.Slice
// Stats
updates uint64
}
// New returns a new memory-based index store.
func New() index.Store {
return &memStore{
idx: make(map[id.Zid]*zettelIndex),
dead: make(map[id.Zid]id.Slice),
idx: make(map[id.Zid]*zettelIndex),
dead: make(map[id.Zid]id.Slice),
words: make(map[string]id.Slice),
}
}
func (ms *memStore) Enrich(ctx context.Context, m *meta.Meta) {
if ms.doEnrich(ctx, m) {
ms.mx.Lock()
ms.updates++
ms.mx.Unlock()
}
}
func (ms *memStore) doEnrich(ctx context.Context, m *meta.Meta) bool {
ms.mx.RLock()
defer ms.mx.RUnlock()
zi, ok := ms.idx[m.Zid]
if !ok {
return false
return
}
var updated bool
if len(zi.dead) > 0 {
m.Set(meta.KeyDead, zi.dead.String())
updated = true
}
back := removeOtherMetaRefs(m, zi.backward.Copy())
|
︙ | | |
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
}
}
}
if len(back) > 0 {
m.Set(meta.KeyBack, back.String())
updated = true
}
return updated
if updated {
}
ms.updates++
// Select all zettel that contains the given exact word.
// The word must be normalized through Unicode NKFD.
func (ms *memStore) Select(word string) id.Set {
ms.mx.RLock()
defer ms.mx.RUnlock()
if refs, ok := ms.words[word]; ok {
return id.NewSet(refs...)
}
return nil
}
// Select all zettel that have a word with the given prefix.
// The prefix must be normalized through Unicode NKFD.
func (ms *memStore) SelectPrefix(prefix string) id.Set {
ms.mx.RLock()
defer ms.mx.RUnlock()
return ms.selectWithPred(prefix, strings.HasPrefix)
}
// Select all zettel that contains the given string.
// The string must be normalized through Unicode NKFD.
func (ms *memStore) SelectContains(s string) id.Set {
ms.mx.RLock()
defer ms.mx.RUnlock()
return ms.selectWithPred(s, strings.Contains)
}
func (ms *memStore) selectWithPred(s string, pred func(string, string) bool) id.Set {
result := id.NewSet()
for word, refs := range ms.words {
if !pred(word, s) {
continue
}
for _, ref := range refs {
result[ref] = true
}
}
return result
}
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 {
|
︙ | | |
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
-
|
toCheck = id.NewSet(refs...)
delete(ms.dead, zidx.Zid)
}
ms.updateDeadReferences(zidx, zi)
ms.updateForwardBackwardReferences(zidx, zi)
ms.updateMetadataReferences(zidx, zi)
ms.updateWords(zidx, zi)
// Check if zi must be inserted into ms.idx
if !ziExist && !zi.isEmpty() {
ms.idx[zidx.Zid] = zi
}
return toCheck
|
︙ | | |
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
bmr.backward = addRef(bmr.backward, zidx.Zid)
bzi.meta[key] = bmr
}
ms.removeInverseMeta(zidx.Zid, key, remRefs)
}
}
func (ms *memStore) updateWords(zidx *index.ZettelIndex, zi *zettelIndex) {
// Must only be called if ms.mx is write-locked!
words := zidx.GetWords()
newWords, removeWords := words.Diff(zi.words)
for _, word := range newWords {
if refs, ok := ms.words[word]; ok {
ms.words[word] = addRef(refs, zidx.Zid)
continue
}
ms.words[word] = id.Slice{zidx.Zid}
}
for _, word := range removeWords {
refs, ok := ms.words[word]
if !ok {
continue
}
refs2 := remRef(refs, zidx.Zid)
if len(refs2) == 0 {
delete(ms.words, word)
continue
}
ms.words[word] = refs2
}
zi.words = words.Words()
}
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
|
︙ | | |
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
-
|
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)
}
}
ms.deleteWords(zid, zi.words)
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 {
|
︙ | | |
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
if len(bzi.meta) == 0 {
bzi.meta = nil
}
}
}
}
func (ms *memStore) deleteWords(zid id.Zid, words []string) {
// Must only be called if ms.mx is write-locked!
for _, word := range words {
refs, ok := ms.words[word]
if !ok {
continue
}
refs2 := remRef(refs, zid)
if len(refs2) == 0 {
delete(ms.words, word)
continue
}
ms.words[word] = refs2
}
}
func (ms *memStore) ReadStats(st *index.StoreStats) {
ms.mx.RLock()
st.Zettel = len(ms.idx)
st.Updates = ms.updates
st.Words = uint64(len(ms.words))
ms.mx.RUnlock()
}
func (ms *memStore) Write(w io.Writer) {
ms.mx.RLock()
defer ms.mx.RUnlock()
|
︙ | | |
Deleted index/wordset.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// 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 index allows to search for metadata and content.
package index
// WordSet contains the set of all words, with the count of their occurrences.
type WordSet map[string]int
// Words gives the slice of all words in the set.
func (ws WordSet) Words() []string {
if len(ws) == 0 {
return nil
}
words := make([]string, 0, len(ws))
for w := range ws {
words = append(words, w)
}
return words
}
// Diff calculates the word slice to be added and to be removed from oldWords
// to get the given word set.
func (ws WordSet) Diff(oldWords []string) (newWords, removeWords []string) {
if len(ws) == 0 {
return nil, oldWords
}
if len(oldWords) == 0 {
return ws.Words(), nil
}
oldSet := make(WordSet, len(oldWords))
for _, ow := range oldWords {
if _, ok := ws[ow]; ok {
oldSet[ow] = 1
continue
}
removeWords = append(removeWords, ow)
}
for w := range ws {
if _, ok := oldSet[w]; ok {
continue
}
newWords = append(newWords, w)
}
return newWords, removeWords
}
|
Deleted index/wordset_test.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// 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 index allows to search for metadata and content.
package index_test
import (
"sort"
"testing"
"zettelstore.de/z/index"
)
func equalWordList(exp, got []string) bool {
if len(exp) != len(got) {
return false
}
if len(got) == 0 {
return len(exp) == 0
}
sort.Strings(got)
for i, w := range exp {
if w != got[i] {
return false
}
}
return true
}
func TestWordsWords(t *testing.T) {
testcases := []struct {
words index.WordSet
exp []string
}{
{nil, nil},
{index.WordSet{}, nil},
{index.WordSet{"a": 1, "b": 2}, []string{"a", "b"}},
}
for i, tc := range testcases {
got := tc.words.Words()
if !equalWordList(tc.exp, got) {
t.Errorf("%d: %v.Words() == %v, but got %v", i, tc.words, tc.exp, got)
}
}
}
func TestWordsDiff(t *testing.T) {
testcases := []struct {
cur index.WordSet
old []string
expN, expR []string
}{
{nil, nil, nil, nil},
{index.WordSet{}, []string{}, nil, nil},
{index.WordSet{"a": 1}, []string{}, []string{"a"}, nil},
{index.WordSet{"a": 1}, []string{"b"}, []string{"a"}, []string{"b"}},
{index.WordSet{}, []string{"b"}, nil, []string{"b"}},
{index.WordSet{"a": 1}, []string{"a"}, nil, nil},
}
for i, tc := range testcases {
gotN, gotR := tc.cur.Diff(tc.old)
if !equalWordList(tc.expN, gotN) {
t.Errorf("%d: %v.Diff(%v)->new %v, but got %v", i, tc.cur, tc.old, tc.expN, gotN)
}
if !equalWordList(tc.expR, gotR) {
t.Errorf("%d: %v.Diff(%v)->rem %v, but got %v", i, tc.cur, tc.old, tc.expR, gotR)
}
}
}
|
Changes to index/zettel.go.
︙ | | |
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
-
|
// ZettelIndex contains all index data of a zettel.
type ZettelIndex struct {
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
words WordSet
}
// NewZettelIndex creates a new zettel index.
func NewZettelIndex(zid id.Zid) *ZettelIndex {
return &ZettelIndex{
Zid: zid,
backrefs: id.NewSet(),
|
︙ | | |
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
-
-
-
-
+
-
+
-
+
-
-
-
|
}
// AddDeadRef adds a dead reference to a zettel.
func (zi *ZettelIndex) AddDeadRef(zid id.Zid) {
zi.deadrefs[zid] = true
}
// SetWords sets the words to the given value.
func (zi *ZettelIndex) SetWords(words WordSet) { zi.words = words }
// GetDeadRefs returns all dead references as a sorted list.
func (zi *ZettelIndex) GetDeadRefs() id.Slice {
return zi.deadrefs.Sorted()
return zi.deadrefs.Sort()
}
// GetBackRefs returns all back references as a sorted list.
func (zi *ZettelIndex) GetBackRefs() id.Slice {
return zi.backrefs.Sorted()
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.Slice {
if len(zi.metarefs) == 0 {
return nil
}
result := make(map[string]id.Slice, len(zi.metarefs))
for key, refs := range zi.metarefs {
result[key] = refs.Sorted()
result[key] = refs.Sort()
}
return result
}
// GetWords returns a reference to the WordSet. It must not be modified.
func (zi *ZettelIndex) GetWords() WordSet { return zi.words }
|
Changes to input/input.go.
1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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.
//-----------------------------------------------------------------------------
|
︙ | | |
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
-
-
-
-
-
-
-
-
-
|
return ' '
}
return r
}
return EOS
}
// IsEOLEOS returns true if char is either EOS or EOL.
func IsEOLEOS(ch rune) bool {
switch ch {
case EOS, '\n', '\r':
return true
}
return false
}
// EatEOL transforms both "\r" and "\r\n" into "\n".
func (inp *Input) EatEOL() {
switch inp.Ch {
case '\r':
if inp.Peek() == '\n' {
inp.Next()
}
|
︙ | | |
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
+
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
func (inp *Input) ScanEntity() (res string, success bool) {
if inp.Ch != '&' {
return "", false
}
pos := inp.Pos
inp.Next()
if inp.Ch == '#' {
code := 0
inp.Next()
if inp.Ch == 'x' || inp.Ch == 'X' {
return inp.scanEntityBase16()
}
return inp.scanEntityBase10()
}
return inp.scanEntityNamed(pos)
}
// Base 16 code
func (inp *Input) scanEntityBase16() (string, bool) {
inp.Next()
if inp.Ch == ';' {
return "", false
}
inp.Next()
if inp.Ch == ';' {
return "", false
}
code := 0
for {
switch ch := inp.Ch; ch {
case ';':
inp.Next()
return string(rune(code)), true
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
code = 16*code + int(ch-'0')
case 'a', 'b', 'c', 'd', 'e', 'f':
code = 16*code + int(ch-'a'+10)
case 'A', 'B', 'C', 'D', 'E', 'F':
code = 16*code + int(ch-'A'+10)
default:
return "", false
}
if code > unicode.MaxRune {
return "", false
}
inp.Next()
}
}
for {
switch ch := inp.Ch; ch {
case ';':
inp.Next()
return string(rune(code)), true
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
code = 16*code + int(ch-'0')
case 'a', 'b', 'c', 'd', 'e', 'f':
code = 16*code + int(ch-'a'+10)
case 'A', 'B', 'C', 'D', 'E', 'F':
code = 16*code + int(ch-'A'+10)
default:
return "", false
}
if code > unicode.MaxRune {
return "", false
}
inp.Next()
}
}
func (inp *Input) scanEntityBase10() (string, bool) {
// Base 10 code
if inp.Ch == ';' {
return "", false
}
// Base 10 code
if inp.Ch == ';' {
return "", false
}
code := 0
for {
switch ch := inp.Ch; ch {
case ';':
inp.Next()
return string(rune(code)), true
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
code = 10*code + int(ch-'0')
default:
return "", false
}
if code > unicode.MaxRune {
return "", false
}
inp.Next()
}
}
func (inp *Input) scanEntityNamed(pos int) (string, bool) {
for {
switch ch := inp.Ch; ch {
case ';':
inp.Next()
return string(rune(code)), true
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
code = 10*code + int(ch-'0')
default:
return "", false
}
if code > unicode.MaxRune {
return "", false
}
inp.Next()
}
}
for {
switch inp.Ch {
case EOS, '\n', '\r':
return "", false
case ';':
inp.Next()
es := inp.Src[pos:inp.Pos]
|
︙ | | |
Changes to input/input_test.go.
1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
//-----------------------------------------------------------------------------
// Copyright (c) 2020-2021 Detlef Stern
// 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.
//-----------------------------------------------------------------------------
|
︙ | | |
Deleted parser/cleaner/cleaner.go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
//-----------------------------------------------------------------------------
// 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 cleaner provides funxtions to clean up the parsed AST.
package cleaner
import (
"strconv"
"strings"
"zettelstore.de/z/ast"
"zettelstore.de/z/encoder"
"zettelstore.de/z/strfun"
)
// CleanupBlockSlice cleans the given block slice.
func CleanupBlockSlice(bs ast.BlockSlice) {
cv := &cleanupVisitor{
textEnc: encoder.Create("text", nil),
doMark: false,
}
t := ast.NewTopDownTraverser(cv)
t.VisitBlockSlice(bs)
if cv.hasMark {
cv.doMark = true
t.VisitBlockSlice(bs)
}
}
type cleanupVisitor struct {
textEnc encoder.Encoder
ids map[string]ast.Node
hasMark bool
doMark bool
}
// VisitVerbatim does nothing.
func (cv *cleanupVisitor) VisitVerbatim(vn *ast.VerbatimNode) {}
// VisitRegion does nothing.
func (cv *cleanupVisitor) VisitRegion(rn *ast.RegionNode) {}
// VisitHeading calculates the heading slug.
func (cv *cleanupVisitor) VisitHeading(hn *ast.HeadingNode) {
if cv.doMark || hn == nil || hn.Inlines == nil {
return
}
var sb strings.Builder
_, err := cv.textEnc.WriteInlines(&sb, hn.Inlines)
if err != nil {
return
}
s := strfun.Slugify(sb.String())
if len(s) > 0 {
hn.Slug = cv.addIdentifier(s, hn)
}
}
// VisitHRule does nothing.
func (cv *cleanupVisitor) VisitHRule(hn *ast.HRuleNode) {}
// VisitList does nothing.
func (cv *cleanupVisitor) VisitNestedList(ln *ast.NestedListNode) {}
// VisitDescriptionList does nothing.
func (cv *cleanupVisitor) VisitDescriptionList(dn *ast.DescriptionListNode) {}
// VisitPara does nothing.
func (cv *cleanupVisitor) VisitPara(pn *ast.ParaNode) {}
// VisitTable does nothing.
func (cv *cleanupVisitor) VisitTable(tn *ast.TableNode) {}
// VisitBLOB does nothing.
func (cv *cleanupVisitor) VisitBLOB(bn *ast.BLOBNode) {}
// VisitText does nothing.
func (cv *cleanupVisitor) VisitText(tn *ast.TextNode) {}
// VisitTag does nothing.
func (cv *cleanupVisitor) VisitTag(tn *ast.TagNode) {}
// VisitSpace does nothing.
func (cv *cleanupVisitor) VisitSpace(sn *ast.SpaceNode) {}
// VisitBreak does nothing.
func (cv *cleanupVisitor) VisitBreak(bn *ast.BreakNode) {}
// VisitLink collects the given link as a reference.
func (cv *cleanupVisitor) VisitLink(ln *ast.LinkNode) {}
// VisitImage collects the image links as a reference.
func (cv *cleanupVisitor) VisitImage(in *ast.ImageNode) {}
// VisitCite does nothing.
func (cv *cleanupVisitor) VisitCite(cn *ast.CiteNode) {}
// VisitFootnote does nothing.
func (cv *cleanupVisitor) VisitFootnote(fn *ast.FootnoteNode) {}
// VisitMark checks for duplicate marks and changes them.
func (cv *cleanupVisitor) VisitMark(mn *ast.MarkNode) {
if mn == nil {
return
}
if !cv.doMark {
cv.hasMark = true
return
}
if mn.Text == "" {
mn.Text = cv.addIdentifier("*", mn)
return
}
mn.Text = cv.addIdentifier(mn.Text, mn)
}
// VisitFormat does nothing.
func (cv *cleanupVisitor) VisitFormat(fn *ast.FormatNode) {}
// VisitLiteral does nothing.
func (cv *cleanupVisitor) VisitLiteral(ln *ast.LiteralNode) {}
func (cv *cleanupVisitor) addIdentifier(id string, node ast.Node) string {
if cv.ids == nil {
cv.ids = map[string]ast.Node{id: node}
return id
}
if n, ok := cv.ids[id]; ok && n != node {
prefix := id + "-"
for count := 1; ; count++ {
newID := prefix + strconv.Itoa(count)
if n, ok := cv.ids[newID]; !ok || n == node {
cv.ids[newID] = node
return newID
}
}
}
cv.ids[id] = node
return id
}
|
Added parser/cleanup.go.