Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From v0.10.0 To v0.9.0
2023-03-27
| ||
14:37 | Version 0.11.0 ... (check-in: 331cad470c user: stern tags: trunk, release, v0.11.0) | |
14:34 | Version 0.11.0 ... (check-in: 712c21afc8 user: stern tags: trunk, release, v0.10.0) | |
2023-03-20
| ||
13:26 | SHTML: remove unneeded TransformInline method ... (check-in: f18740e1e8 user: stern tags: trunk) | |
2022-12-15
| ||
18:39 | Add constants to merge endpoint into /z ... (check-in: e93b6cc96d user: stern tags: trunk) | |
2022-12-12
| ||
12:50 | Version 0.9.0 ... (check-in: 668f89b43e user: stern tags: trunk, release, v0.9.0) | |
11:22 | Remove Deepsource integration, because it still fails on Go 1.18 elements ... (check-in: 201e5b2e91 user: stern tags: trunk) | |
Changes to LICENSE.txt.
|
| | | 1 2 3 4 5 6 7 8 | Copyright (c) 2021-2022 Detlef Stern Licensed under the EUPL Zettelstore client is licensed under the European Union Public License, version 1.2 or later (EUPL v. 1.2). The license is available in the official languages of the EU. The English version is included here. Please see https://joinup.ec.europa.eu/community/eupl/og_page/eupl for official |
︙ | ︙ |
Changes to api/api.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2021-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ | |||
92 93 94 95 96 97 98 | ID ZettelID `json:"id"` Meta ZettelMeta `json:"meta"` Encoding string `json:"encoding"` Content string `json:"content"` Rights ZettelRights `json:"rights"` } | < < < < < < | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | ID ZettelID `json:"id"` Meta ZettelMeta `json:"meta"` Encoding string `json:"encoding"` Content string `json:"content"` Rights ZettelRights `json:"rights"` } // ZettelListJSON contains data for a zettel list. type ZettelListJSON struct { Query string `json:"query"` Human string `json:"human"` List []ZidMetaJSON `json:"list"` } |
︙ | ︙ |
Changes to api/const.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2021-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ | |||
96 97 98 99 100 101 102 | KeyForward = "forward" KeyLang = "lang" KeyLicense = "license" KeyModified = "modified" KeyPrecursor = "precursor" KeyPredecessor = "predecessor" KeyPublished = "published" | < | 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | KeyForward = "forward" KeyLang = "lang" KeyLicense = "license" KeyModified = "modified" KeyPrecursor = "precursor" KeyPredecessor = "predecessor" KeyPublished = "published" KeyReadOnly = "read-only" KeySuccessors = "successors" KeySummary = "summary" KeyURL = "url" KeyUselessFiles = "useless-files" KeyUserID = "user-id" KeyUserRole = "user-role" |
︙ | ︙ | |||
148 149 150 151 152 153 154 | HeaderContentType = "Content-Type" HeaderDestination = "Destination" HeaderLocation = "Location" ) // Values for HTTP query parameter. const ( | | | | | < | | | | < < > < < < < > < < < | 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 | HeaderContentType = "Content-Type" HeaderDestination = "Destination" HeaderLocation = "Location" ) // Values for HTTP query parameter. const ( QueryKeyCommand = "cmd" QueryKeyCost = "cost" QueryKeyDir = "dir" QueryKeyEncoding = "enc" QueryKeyLimit = "limit" QueryKeyPart = "part" QueryKeyPhrase = "phrase" QueryKeyQuery = "q" ) // Supported dir values. const ( DirBackward = "backward" DirForward = "forward" ) // Supported encoding values. const ( EncodingHTML = "html" EncodingMD = "md" EncodingSexpr = "sexpr" EncodingText = "text" EncodingZJSON = "zjson" EncodingZMK = "zmk" ) var mapEncodingEnum = map[string]EncodingEnum{ EncodingHTML: EncoderHTML, EncodingMD: EncoderMD, EncodingSexpr: EncoderSexpr, EncodingText: EncoderText, EncodingZJSON: EncoderZJSON, EncodingZMK: EncoderZmk, } var mapEnumEncoding = map[EncodingEnum]string{} func init() { for k, v := range mapEncodingEnum { mapEnumEncoding[v] = k } |
︙ | ︙ | |||
215 216 217 218 219 220 221 | // Values for EncoderEnum const ( EncoderUnknown EncodingEnum = iota EncoderHTML EncoderMD EncoderSexpr | | | < < < | 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | // Values for EncoderEnum const ( EncoderUnknown EncodingEnum = iota EncoderHTML EncoderMD EncoderSexpr EncoderText EncoderZJSON EncoderZmk ) // String representation of an encoder key. func (e EncodingEnum) String() string { if f, ok := mapEnumEncoding[e]; ok { return f } |
︙ | ︙ |
Changes to api/urlbuilder.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2020-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ | |||
109 110 111 112 113 114 115 | ub.rawLocal = "" ub.fragment = s return ub } // String produces a string value. func (ub *URLBuilder) String() string { | < < < < < < < < < | 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | ub.rawLocal = "" ub.fragment = s return ub } // String produces a string value. func (ub *URLBuilder) String() string { var sb strings.Builder sb.WriteString(ub.prefix) if ub.key != '/' { sb.WriteByte(ub.key) } if ub.rawLocal != "" { |
︙ | ︙ | |||
142 143 144 145 146 147 148 | sb.WriteByte('#') sb.WriteString(ub.fragment) } for i, q := range ub.query { if i == 0 { sb.WriteByte('?') } else { | | | 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | sb.WriteByte('#') sb.WriteString(ub.fragment) } for i, q := range ub.query { if i == 0 { sb.WriteByte('?') } else { sb.WriteByte('&') } sb.WriteString(q.key) if val := q.val; val != "" { sb.WriteByte('=') sb.WriteString(url.QueryEscape(val)) } } return sb.String() } |
Changes to attrs/attrs.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2020-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ |
Changes to attrs/attrs_test.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2020-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ |
Changes to client/client.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2021-2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ | |||
21 22 23 24 25 26 27 | "net/http" "net/url" "strconv" "strings" "time" "codeberg.org/t73fde/sxpf" | < > > | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | "net/http" "net/url" "strconv" "strings" "time" "codeberg.org/t73fde/sxpf" "zettelstore.de/c/api" "zettelstore.de/c/sexpr" "zettelstore.de/c/zjson" ) // Client contains all data to execute requests. type Client struct { base string username string password string |
︙ | ︙ | |||
231 232 233 234 235 236 237 | // CreateZettelJSON creates a new zettel and returns its URL. func (c *Client) CreateZettelJSON(ctx context.Context, data *api.ZettelDataJSON) (api.ZettelID, error) { var buf bytes.Buffer if err := encodeZettelData(&buf, data); err != nil { return api.InvalidZID, err } | | | 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | // CreateZettelJSON creates a new zettel and returns its URL. func (c *Client) CreateZettelJSON(ctx context.Context, data *api.ZettelDataJSON) (api.ZettelID, error) { var buf bytes.Buffer if err := encodeZettelData(&buf, data); err != nil { return api.InvalidZID, err } ub := c.newURLBuilder('j') resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf, nil) if err != nil { return api.InvalidZID, err } defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { return api.InvalidZID, statusToError(resp) |
︙ | ︙ | |||
268 269 270 271 272 273 274 | func (c *Client) ListZettel(ctx context.Context, query string) ([][]byte, error) { ub := c.newURLBuilder('z').AppendQuery(query) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() | | < < < | | 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | func (c *Client) ListZettel(ctx context.Context, query string) ([][]byte, error) { ub := c.newURLBuilder('z').AppendQuery(query) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } data, err := io.ReadAll(resp.Body) if err != nil { return nil, err } lines := bytes.Split(data, bsLF) if len(lines[len(lines)-1]) == 0 { lines = lines[:len(lines)-1] } return lines, nil } // ListZettelJSON returns a list of zettel. func (c *Client) ListZettelJSON(ctx context.Context, query string) (string, string, []api.ZidMetaJSON, error) { ub := c.newURLBuilder('j').AppendQuery(query) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return "", "", nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return "", "", nil, statusToError(resp) |
︙ | ︙ | |||
316 317 318 319 320 321 322 | ub.AppendKVQuery(api.QueryKeyPart, part) } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() | | < < < | < < | | | | > > > > > > > > > > > > > > > > > > > > > > > > | | | < < < | > | | | | | | | | | < < < | | < < | 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | ub.AppendKVQuery(api.QueryKeyPart, part) } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } return io.ReadAll(resp.Body) } // GetZettelJSON returns a zettel as a JSON struct. func (c *Client) GetZettelJSON(ctx context.Context, zid api.ZettelID) (*api.ZettelDataJSON, error) { ub := c.newURLBuilder('j').SetZid(zid) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } dec := json.NewDecoder(resp.Body) var out api.ZettelDataJSON err = dec.Decode(&out) if err != nil { return nil, err } return &out, nil } // GetParsedZettel return a parsed zettel in a defined encoding. func (c *Client) GetParsedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, 'p', zid, enc) } // GetEvaluatedZettel return an evaluated zettel in a defined encoding. func (c *Client) GetEvaluatedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, 'v', zid, enc) } func (c *Client) getZettelString(ctx context.Context, key byte, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { ub := c.newURLBuilder(key).SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, enc.String()) ub.AppendKVQuery(api.QueryKeyPart, api.PartContent) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } return io.ReadAll(resp.Body) } // GetParsedZettelZJSON returns an parsed zettel as a JSON-decoded data structure. func (c *Client) GetParsedSexpr(ctx context.Context, zid api.ZettelID, part string) (sxpf.Value, error) { return c.getSexpr(ctx, 'p', zid, part) } // GetEvaluatedZettelZJSON returns an evaluated zettel as a JSON-decoded data structure. func (c *Client) GetEvaluatedSexpr(ctx context.Context, zid api.ZettelID, part string) (sxpf.Value, error) { return c.getSexpr(ctx, 'v', zid, part) } func (c *Client) getSexpr(ctx context.Context, key byte, zid api.ZettelID, part string) (sxpf.Value, error) { ub := c.newURLBuilder(key).SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSexpr) if part != "" { ub.AppendKVQuery(api.QueryKeyPart, part) } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } return sxpf.ParseValue(sexpr.Smk, bufio.NewReaderSize(resp.Body, 8)) } // GetParsedZettelZJSON returns an parsed zettel as a JSON-decoded data structure. func (c *Client) GetParsedZJSON(ctx context.Context, zid api.ZettelID, part string) (zjson.Value, error) { return c.getZJSON(ctx, 'p', zid, part) } // GetEvaluatedZettelZJSON returns an evaluated zettel as a JSON-decoded data structure. func (c *Client) GetEvaluatedZJSON(ctx context.Context, zid api.ZettelID, part string) (zjson.Value, error) { return c.getZJSON(ctx, 'v', zid, part) } func (c *Client) getZJSON(ctx context.Context, key byte, zid api.ZettelID, part string) (zjson.Value, error) { ub := c.newURLBuilder(key).SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingZJSON) if part != "" { ub.AppendKVQuery(api.QueryKeyPart, part) } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } return zjson.Decode(resp.Body) } // GetMeta returns the metadata of a zettel. func (c *Client) GetMeta(ctx context.Context, zid api.ZettelID) (api.ZettelMeta, error) { ub := c.newURLBuilder('m').SetZid(zid) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) |
︙ | ︙ | |||
539 540 541 542 543 544 545 | // UpdateZettelJSON updates an existing zettel. func (c *Client) UpdateZettelJSON(ctx context.Context, zid api.ZettelID, data *api.ZettelDataJSON) error { var buf bytes.Buffer if err := encodeZettelData(&buf, data); err != nil { return err } | | | 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | // UpdateZettelJSON updates an existing zettel. func (c *Client) UpdateZettelJSON(ctx context.Context, zid api.ZettelID, data *api.ZettelDataJSON) error { var buf bytes.Buffer if err := encodeZettelData(&buf, data); err != nil { return err } ub := c.newURLBuilder('j').SetZid(zid) resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf, nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) |
︙ | ︙ | |||
616 617 618 619 620 621 622 | // QueryMapMeta returns a map of all metadata values with the given query action to the // list of zettel IDs containing this value. func (c *Client) QueryMapMeta(ctx context.Context, query string) (api.MapMeta, error) { err := c.updateToken(ctx) if err != nil { return nil, err } | | | 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | // QueryMapMeta returns a map of all metadata values with the given query action to the // list of zettel IDs containing this value. func (c *Client) QueryMapMeta(ctx context.Context, query string) (api.MapMeta, error) { err := c.updateToken(ctx) if err != nil { return nil, err } req, err := c.newRequest(ctx, http.MethodGet, c.newURLBuilder('q').AppendQuery(query), nil) if err != nil { return nil, err } resp, err := c.executeRequest(req) if err != nil { return nil, err } |
︙ | ︙ |
Changes to client/client_test.go.
1 | //----------------------------------------------------------------------------- | | > | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package client_test import ( "context" "flag" "log" "net/http" "net/url" "testing" "codeberg.org/t73fde/sxpf" "zettelstore.de/c/api" "zettelstore.de/c/client" "zettelstore.de/c/zjson" ) func TestZettelList(t *testing.T) { c := getClient() _, err := c.ListZettel(context.Background(), "") if err != nil { t.Error(err) |
︙ | ︙ | |||
41 42 43 44 45 46 47 | } else { t.Error(err) } return } } | | > > > > > > > > > > > > | > | > > > > > > > > > > > > > > > > > > > > > | > > | | > > > > > | > > > > > > > > > > > | 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 | } else { t.Error(err) } return } } func TestGetZJSONZettel(t *testing.T) { c := getClient() data, err := c.GetEvaluatedZJSON(context.Background(), api.ZidDefaultHome, api.PartContent) if err != nil { t.Error(err) return } if data == nil { t.Error("No data") } var v vis zjson.WalkBlock(&v, data.(zjson.Array), -1) // t.Error("Argh") } type vis struct{} func (*vis) BlockArray(a zjson.Array, pos int) zjson.CloseFunc { log.Println("SBLO", pos, a) return nil } func (*vis) InlineArray(a zjson.Array, pos int) zjson.CloseFunc { log.Println("SINL", pos, a) return nil } func (*vis) ItemArray(a zjson.Array, pos int) zjson.CloseFunc { log.Println("SITE", pos, a) return nil } func (*vis) BlockObject(t string, obj zjson.Object, pos int) (bool, zjson.CloseFunc) { log.Println("BOBJ", pos, t, obj) return true, nil } func (*vis) InlineObject(t string, obj zjson.Object, pos int) (bool, zjson.CloseFunc) { log.Println("IOBJ", pos, t, obj) return true, nil } func (*vis) Unexpected(val zjson.Value, pos int, exp string) { log.Println("Expect", pos, exp, val) } func TestGetSexprZettel(t *testing.T) { c := getClient() value, err := c.GetEvaluatedSexpr(context.Background(), api.ZidDefaultHome, api.PartContent) if err != nil { t.Error(err) return } if value == nil { t.Error("No data") } var env testEnv env.t = t res, err := sxpf.Eval(&env, value) if err != nil { t.Error(res, err) } } type testEnv struct{ t *testing.T } func noneFn(sxpf.Environment, *sxpf.Pair, int) (sxpf.Value, error) { return sxpf.Nil(), nil } func (*testEnv) LookupForm(*sxpf.Symbol) (sxpf.Form, error) { return sxpf.NewBuiltin("none", false, 0, -1, noneFn), nil } func (*testEnv) EvalSymbol(sym *sxpf.Symbol) (sxpf.Value, error) { return sym, nil } func (*testEnv) EvalOther(val sxpf.Value) (sxpf.Value, error) { return val, nil } func (te *testEnv) EvalPair(p *sxpf.Pair) (sxpf.Value, error) { return sxpf.EvalCallOrSeq(te, p) } var baseURL string func init() { flag.StringVar(&baseURL, "base-url", "http://localhost:23123/", "Base URL") } |
︙ | ︙ |
Changes to go.mod.
1 2 | module zettelstore.de/c | | < < | < | 1 2 3 4 5 | module zettelstore.de/c go 1.19 require codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0 |
Changes to go.sum.
|
| < < | | | 1 2 | codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0 h1:viya/OgeF16+i8caBPJmcLQhGpZodPh+/nxtJzSSO1s= codeberg.org/t73fde/sxpf v0.0.0-20220719090054-749a39d0a7a0/go.mod h1:4fAHEF3VH+ofbZkF6NzqiItTNy2X11tVCnZX99jXouA= |
Added html/html.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- // Package html provides types, constants and function to work with HTML. package html import ( "io" "strings" ) const ( htmlQuot = """ // longer than """, but often requested in standards htmlAmp = "&" htmlLt = "<" htmlGt = ">" htmlNull = "\uFFFD" htmlLitSpace = "\u00a0" htmlVisSpace = "\u2423" ) var ( htmlEscapes = []string{`&`, htmlAmp, `<`, htmlLt, `>`, htmlGt, `"`, htmlQuot, "\000", htmlNull, } htmlEscaper = strings.NewReplacer(htmlEscapes...) htmlVisEscapes = append(append([]string{}, htmlEscapes...), " ", htmlVisSpace, htmlLitSpace, htmlVisSpace, ) htmlVisEscaper = strings.NewReplacer(htmlVisEscapes...) ) // Escape writes to w the escaped HTML equivalent of the given string. func Escape(w io.Writer, s string) (int, error) { return htmlEscaper.WriteString(w, s) } // EscapeVisible writes to w the escaped HTML equivalent of the given string. // Each space is written as U-2423. func EscapeVisible(w io.Writer, s string) (int, error) { return htmlVisEscaper.WriteString(w, s) } var ( escQuot = []byte(htmlQuot) // longer than """, but often requested in standards escAmp = []byte(htmlAmp) escNull = []byte(htmlNull) ) // AttributeEscape writes to w the escaped HTML equivalent of the given string to be used // in attributes. func AttributeEscape(w io.Writer, s string) (int, error) { length := 0 last := 0 var html []byte lenS := len(s) for i := 0; i < lenS; i++ { switch s[i] { case '\000': html = escNull case '"': html = escQuot case '&': html = escAmp default: continue } l, err := io.WriteString(w, s[last:i]) length += l if err != nil { return length, err } l, err = w.Write(html) length += l if err != nil { return length, err } last = i + 1 } l, err := io.WriteString(w, s[last:]) return length + l, err } var unsafeSnippets = []string{ "<script", "</script", "<iframe", "</iframe", } // IsSafe returns true if the given string does not contain unsafe HTML elements. func IsSafe(s string) bool { lower := strings.ToLower(s) for _, snippet := range unsafeSnippets { if strings.Contains(lower, snippet) { return false } } return true } |
Added html/html_test.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package html_test import ( "bytes" "testing" "zettelstore.de/c/html" ) func TestEscape(t *testing.T) { testcases := []struct { in, exp string }{ {"", ""}, {"<", "<"}, } for _, tc := range testcases { var buf bytes.Buffer _, err := html.Escape(&buf, tc.in) if err != nil { t.Errorf("Escape(%q) got error: %v", tc.in, err) } if got := buf.String(); tc.exp != got { t.Errorf("Escape(%q) == %q, but got %q", tc.in, tc.exp, got) } } } func TestEscapeVisible(t *testing.T) { testcases := []struct { in, exp string }{ {"", ""}, {"<", "<"}, {" a b ", "␣a␣␣b␣"}, } for _, tc := range testcases { var buf bytes.Buffer _, err := html.EscapeVisible(&buf, tc.in) if err != nil { t.Errorf("Escape(%q) got error: %v", tc.in, err) } if got := buf.String(); tc.exp != got { t.Errorf("Escape(%q) == %q, but got %q", tc.in, tc.exp, got) } } } |
Added html/sencoder.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package html import ( "bytes" "fmt" "io" "log" "net/url" "strconv" "codeberg.org/t73fde/sxpf" "zettelstore.de/c/api" "zettelstore.de/c/attrs" "zettelstore.de/c/sexpr" "zettelstore.de/c/text" ) // EncEnvironment represent the encoding environment. // It is itself a sxpf.Environment. // // Builtins is public, so that HTML encoders based on this one can modify some // functionality. Builtins should not be updated, but can be used as a parent // map when creating a new one. type EncEnvironment struct { err error Builtins *sxpf.SymbolMap w io.Writer headingOffset int unique string footnotes []sfootnodeInfo writeFootnotes bool // true iff output should include footnotes and marks noLinks bool // true iff output must not include links visibleSpace bool // true iff space should be "visible" by using EscapeVisible } type sfootnodeInfo struct { note *sxpf.Pair attrs attrs.Attributes } func NewEncEnvironment(w io.Writer, headingOffset int) *EncEnvironment { return &EncEnvironment{ Builtins: buildBuiltins(), w: w, headingOffset: headingOffset, footnotes: nil, writeFootnotes: true, } } func buildBuiltins() *sxpf.SymbolMap { builtins := sxpf.NewSymbolMap(sexpr.Smk, nil) for _, b := range defaultEncodingFunctions { name := b.sym.GetValue() primFunc := b.fn builtins.Set(b.sym, sxpf.NewBuiltin( name, true, b.minArgs, -1, func(env sxpf.Environment, args *sxpf.Pair, _ int) (sxpf.Value, error) { primFunc(env.(*EncEnvironment), args) return nil, nil }, )) } return builtins } // SetError marks the environment with an error, if there is not already a marked error. // The effect is that all future output is not written. func (env *EncEnvironment) SetError(err error) { if env.err == nil { env.err = err } } // GetError returns the first encountered error during encoding. func (env *EncEnvironment) GetError() error { return env.err } // ReplaceWriter flushes the previous writer and installs the new one. func (env *EncEnvironment) ReplaceWriter(w io.Writer) { env.w = w } // SetUnique sets a string that maked footnote, heading, and mark fragments unique. func (env *EncEnvironment) SetUnique(s string) { if s == "" { env.unique = "" } else { env.unique = ":" + s } } // IgnoreLinks returns true, if HTML links must not be encoded. This happens if // the encoded HTML is used in a link itself. func (env *EncEnvironment) IgnoreLinks() bool { return env.noLinks } // WriteString encodes a string literally. func (env *EncEnvironment) Write(b []byte) (l int, err error) { if env.err == nil { l, env.err = env.w.Write(b) } return l, env.err } // WriteString encodes a string literally. func (env *EncEnvironment) WriteString(s string) (l int, err error) { if env.err == nil { l, env.err = io.WriteString(env.w, s) } return l, env.err } // WriteStrings encodes many string literally. func (env *EncEnvironment) WriteStrings(sl ...string) { if env.err == nil { for _, s := range sl { _, env.err = io.WriteString(env.w, s) if env.err != nil { return } } } } // WriteEscape encodes a string so that it cannot interfere with other HTML code. func (env *EncEnvironment) WriteEscaped(s string) { if env.err == nil { _, env.err = Escape(env.w, s) } } func (env *EncEnvironment) WriteEscapedOrVisible(s string) { if env.err == nil { if env.visibleSpace { _, env.err = EscapeVisible(env.w, s) } else { _, env.err = Escape(env.w, s) } } } func (env *EncEnvironment) GetSymbol(p *sxpf.Pair) (res *sxpf.Symbol) { if env.err != nil { return nil } res, env.err = p.GetSymbol() return res } func (env *EncEnvironment) GetString(p *sxpf.Pair) (res string) { if env.err != nil { return "" } res, env.err = p.GetString() return res } func (env *EncEnvironment) GetInteger(p *sxpf.Pair) (res int64) { if env.err != nil { return 0 } res, env.err = p.GetInteger() return res } func (env *EncEnvironment) GetPair(p *sxpf.Pair) (res *sxpf.Pair) { if env.err != nil { return nil } res, env.err = p.GetPair() return res } func (env *EncEnvironment) GetAttributes(p *sxpf.Pair) attrs.Attributes { if env.err != nil { return nil } return sexpr.GetAttributes(env.GetPair(p)) } func (env *EncEnvironment) WriteAttributes(a attrs.Attributes) { if len(a) == 0 { return } for _, key := range a.Keys() { if key == "" || key == attrs.DefaultAttribute { continue } val, found := a.Get(key) if !found { continue } env.WriteString(" ") env.WriteOneAttribute(key, val) } } func (env *EncEnvironment) WriteOneAttribute(key, val string) { if env.err == nil { env.WriteString(key) if val != "" { env.WriteString(`="`) _, env.err = AttributeEscape(env.w, val) env.WriteString(`"`) } } } func (env *EncEnvironment) WriteStartTag(tag string, a attrs.Attributes) { env.WriteStrings("<", tag) env.WriteAttributes(a) env.WriteString(">") } func (env *EncEnvironment) WriteEndTag(tag string) { env.WriteStrings("</", tag, ">") } func (env *EncEnvironment) WriteImage(args *sxpf.Pair) { ref := env.GetPair(args.GetTail()) env.WriteImageWithSource(args, env.GetString(ref.GetTail())) } func (env *EncEnvironment) WriteImageWithSource(args *sxpf.Pair, src string) { a := env.GetAttributes(args) a = a.Set("src", src) if title := args.GetTail().GetTail().GetTail(); !title.IsNil() { a = a.Set("title", text.EvaluateInlineString(title)) } env.WriteStartTag("img", a) } func (env *EncEnvironment) LookupForm(sym *sxpf.Symbol) (sxpf.Form, error) { return env.Builtins.LookupForm(sym) } func (env *EncEnvironment) EvalOther(val sxpf.Value) (sxpf.Value, error) { if strVal, ok := val.(*sxpf.String); ok { env.WriteEscaped(strVal.GetValue()) return nil, nil } return val, nil } func (env *EncEnvironment) EvalSymbol(val *sxpf.Symbol) (sxpf.Value, error) { env.WriteEscaped(val.GetValue()) return nil, nil } func (env *EncEnvironment) EvalPair(p *sxpf.Pair) (sxpf.Value, error) { return sxpf.EvalCallOrSeq(env, p) } func EvaluateInline(baseEnv *EncEnvironment, value sxpf.Value, withFootnotes, noLinks bool) string { var buf bytes.Buffer env := EncEnvironment{w: &buf, noLinks: noLinks} if baseEnv != nil { env.Builtins = baseEnv.Builtins env.writeFootnotes = withFootnotes && baseEnv.writeFootnotes env.footnotes = baseEnv.footnotes } else { env.Builtins = buildBuiltins() } _, err := sxpf.Eval(&env, value) if err != nil { return err.Error() } if baseEnv != nil { baseEnv.footnotes = env.footnotes } return buf.String() } func (env *EncEnvironment) WriteEndnotes() { if len(env.footnotes) == 0 { return } env.WriteString("<ol class=\"zs-endnotes\">") for i := 0; i < len(env.footnotes); i++ { fni := env.footnotes[i] n := strconv.Itoa(i + 1) un := env.unique + n a := fni.attrs.Clone().AddClass("zs-endnote").Set("value", n) if _, found := a.Get("id"); !found { a = a.Set("id", "fn:"+un) } if _, found := a.Get("role"); !found { a = a.Set("role", "doc-endnote") } env.WriteStartTag("li", a) sxpf.EvalSequence(env, fni.note) // may add more footnotes env.WriteStrings( ` <a class="zs-endnote-backref" href="#fnref:`, un, "\" role=\"doc-backlink\">↩︎</a></li>") } env.footnotes = nil env.WriteString("</ol>") } type encodingFunc func(env *EncEnvironment, args *sxpf.Pair) var defaultEncodingFunctions = []struct { sym *sxpf.Symbol minArgs int fn encodingFunc }{ {sexpr.SymPara, 0, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteString("<p>") sxpf.EvalSequence(env, args) env.WriteString("</p>") }}, {sexpr.SymHeading, 5, func(env *EncEnvironment, args *sxpf.Pair) { nLevel := env.GetInteger(args) if nLevel <= 0 { return } level := strconv.FormatInt(nLevel+int64(env.headingOffset), 10) argAttr := args.GetTail() a := env.GetAttributes(argAttr) argFragment := argAttr.GetTail().GetTail() if fragment := env.GetString(argFragment); fragment != "" { a = a.Set("id", fragment) } env.WriteStrings("<h", level) env.WriteAttributes(a) env.WriteString(">") sxpf.EvalSequence(env, argFragment.GetTail()) env.WriteStrings("</h", level, ">") }}, {sexpr.SymThematic, 0, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteString("<hr") if !sxpf.IsNil(args) { env.WriteAttributes(env.GetAttributes(args)) } env.WriteString(">") }}, {sexpr.SymListUnordered, 0, makeListFn("ul")}, {sexpr.SymListOrdered, 0, makeListFn("ol")}, {sexpr.SymListQuote, 0, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteString("<blockquote>") if !args.IsNil() && args.GetFirst().IsNil() { sxpf.Eval(env, env.GetPair(args)) } else { for elem := args; !elem.IsNil(); elem = elem.GetTail() { env.WriteString("<p>") sxpf.Eval(env, env.GetPair(elem)) env.WriteString("</p>") } } env.WriteString("</blockquote>") }}, {sexpr.SymDescription, 0, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteString("<dl>") for elem := args; !elem.IsNil(); elem = elem.GetTail() { env.WriteString("<dt>") sxpf.Eval(env, elem.GetFirst()) env.WriteString("</dt>") elem = elem.GetTail() if elem.IsNil() { break } ddlist, err := elem.GetPair() if err != nil { continue } for dditem := ddlist; !dditem.IsNil(); dditem = dditem.GetTail() { env.WriteString("<dd>") sxpf.Eval(env, dditem.GetFirst()) env.WriteString("</dd>") } } env.WriteString("</dl>") }}, {sexpr.SymTable, 1, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteString("<table>") if header := env.GetPair(args); !header.IsNil() { env.WriteString("<thead>") env.writeTableRow(header) env.WriteString("</thead>") } if argBody := args.GetTail(); !argBody.IsNil() { env.WriteString("<tbody>") for row := argBody; !row.IsNil(); row = row.GetTail() { env.writeTableRow(env.GetPair(row)) } env.WriteString("</tbody>") } env.WriteString("</table>") }}, {sexpr.SymCell, 0, makeCellFn("")}, {sexpr.SymCellCenter, 0, makeCellFn("center")}, {sexpr.SymCellLeft, 0, makeCellFn("left")}, {sexpr.SymCellRight, 0, makeCellFn("right")}, {sexpr.SymRegionBlock, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := env.GetAttributes(args) if val, found := a.Get(""); found { a = a.Remove("").AddClass(val) } env.writeRegion(args, a, "div") }}, {sexpr.SymRegionQuote, 2, func(env *EncEnvironment, args *sxpf.Pair) { env.writeRegion(args, nil, "blockquote") }}, {sexpr.SymRegionVerse, 2, func(env *EncEnvironment, args *sxpf.Pair) { env.writeRegion(args, nil, "div") }}, {sexpr.SymVerbatimComment, 1, func(env *EncEnvironment, args *sxpf.Pair) { if env.GetAttributes(args).HasDefault() { if s := env.GetString(args.GetTail()); s != "" { env.WriteString("<!--\n") env.WriteEscaped(s) env.WriteString("\n-->") } } }}, {sexpr.SymVerbatimEval, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := env.GetAttributes(args).AddClass("zs-eval") env.writeVerbatim(args, a) }}, {sexpr.SymVerbatimHTML, 2, execHTML}, {sexpr.SymVerbatimMath, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := env.GetAttributes(args).AddClass("zs-math") env.writeVerbatim(args, a) }}, {sexpr.SymVerbatimProg, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := setProgLang(env.GetAttributes(args)) oldVisible := env.visibleSpace if a.HasDefault() { a = a.RemoveDefault() env.visibleSpace = true } env.writeVerbatim(args, a) env.visibleSpace = oldVisible }}, {sexpr.SymVerbatimZettel, 0, DoNothingFn}, {sexpr.SymBLOB, 3, func(env *EncEnvironment, args *sxpf.Pair) { argSyntax := args.GetTail() env.writeBLOB(env.GetString(args), env.GetString(argSyntax), env.GetString(argSyntax.GetTail())) }}, {sexpr.SymTransclude, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := sexpr.GetAttributes(env.GetPair(args)) ref := env.GetPair(args.GetTail()) refKind := env.GetSymbol(ref) if refKind == nil { return } if refValue := env.GetString(ref.GetTail()); refValue != "" { if sexpr.SymRefStateExternal.Equal(refKind) { a = a.Set("src", refValue).AddClass("external") env.WriteString("<p><img") env.WriteAttributes(a) env.WriteString("></p>") return } env.WriteStrings("<!-- transclude ", refKind.GetValue(), ": ") env.WriteEscaped(refValue) env.WriteString(" -->") return } if env.err == nil { _, env.err = fmt.Fprintf(env.w, "%v\n", args) } log.Println("TRAN", args) }}, {sexpr.SymText, 0, func(env *EncEnvironment, args *sxpf.Pair) { if !sxpf.IsNil(args) { env.WriteEscaped(env.GetString(args)) } }}, {sexpr.SymSpace, 0, func(env *EncEnvironment, args *sxpf.Pair) { if sxpf.IsNil(args) { env.WriteString(" ") return } env.WriteEscaped(env.GetString(args)) }}, {sexpr.SymSoft, 0, func(env *EncEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }}, {sexpr.SymHard, 0, func(env *EncEnvironment, _ *sxpf.Pair) { env.WriteString("<br>") }}, {sexpr.SymLinkInvalid, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteAsSpan(env, args) }}, {sexpr.SymLinkZettel, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }}, {sexpr.SymLinkSelf, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }}, {sexpr.SymLinkFound, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }}, {sexpr.SymLinkBroken, 2, func(env *EncEnvironment, args *sxpf.Pair) { if a, refValue, ok := PrepareLink(env, args); ok { WriteLink(env, args, a.AddClass("broken"), refValue, "") } }}, {sexpr.SymLinkHosted, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }}, {sexpr.SymLinkBased, 2, func(env *EncEnvironment, args *sxpf.Pair) { WriteHRefLink(env, args) }}, {sexpr.SymLinkQuery, 2, func(env *EncEnvironment, args *sxpf.Pair) { if a, refValue, ok := PrepareLink(env, args); ok { query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue) WriteLink(env, args, a.Set("href", query), refValue, "") } }}, {sexpr.SymLinkExternal, 2, func(env *EncEnvironment, args *sxpf.Pair) { if a, refValue, ok := PrepareLink(env, args); ok { WriteLink(env, args, a.Set("href", refValue).AddClass("external"), refValue, "") } }}, {sexpr.SymEmbed, 3, func(env *EncEnvironment, args *sxpf.Pair) { argRef := args.GetTail() if syntax := env.GetString(argRef.GetTail()); syntax == api.ValueSyntaxSVG { ref := env.GetPair(argRef) env.WriteStrings( `<figure><embed type="image/svg+xml" src="`, "/", env.GetString(ref.GetTail()), ".svg", "\" /></figure>") } else { env.WriteImage(args) } }}, {sexpr.SymEmbedBLOB, 3, func(env *EncEnvironment, args *sxpf.Pair) { argSyntax := args.GetTail() a, syntax, data := env.GetAttributes(args), env.GetString(argSyntax), env.GetString(argSyntax.GetTail()) title, _ := a.Get("title") env.writeBLOB(title, syntax, data) }}, {sexpr.SymCite, 2, func(env *EncEnvironment, args *sxpf.Pair) { env.WriteStartTag("span", env.GetAttributes(args)) argKey := args.GetTail() if key := env.GetString(argKey); key != "" { env.WriteEscaped(key) if text := argKey.GetTail(); !text.IsNil() { env.WriteString(", ") sxpf.EvalSequence(env, text) } } env.WriteString("</span>") }}, {sexpr.SymMark, 3, func(env *EncEnvironment, args *sxpf.Pair) { if env.noLinks { sxpf.Eval(env, sxpf.NewPair(sexpr.SymFormatSpan, args)) return } argFragment := args.GetTail().GetTail() if fragment := env.GetString(argFragment); fragment != "" { env.WriteString(`<a id="`) env.WriteString(env.unique) env.WriteString(fragment) env.WriteString(`">`) sxpf.EvalSequence(env, argFragment.GetTail()) env.WriteString("</a>") } else { sxpf.EvalSequence(env, argFragment.GetTail()) } }}, {sexpr.SymFootnote, 1, func(env *EncEnvironment, args *sxpf.Pair) { if env.writeFootnotes { a := env.GetAttributes(args) env.footnotes = append(env.footnotes, sfootnodeInfo{args.GetTail(), a}) n := strconv.Itoa(len(env.footnotes)) un := env.unique + n env.WriteStrings( `<sup id="fnref:`, un, `"><a class="zs-noteref" href="#fn:`, un, `" role="doc-noteref">`, n, `</a></sup>`) } }}, {sexpr.SymFormatDelete, 1, makeFormatFn("del")}, {sexpr.SymFormatEmph, 1, makeFormatFn("em")}, {sexpr.SymFormatInsert, 1, makeFormatFn("ins")}, {sexpr.SymFormatQuote, 1, writeQuote}, {sexpr.SymFormatSpan, 1, makeFormatFn("span")}, {sexpr.SymFormatStrong, 1, makeFormatFn("strong")}, {sexpr.SymFormatSub, 1, makeFormatFn("sub")}, {sexpr.SymFormatSuper, 1, makeFormatFn("sup")}, {sexpr.SymLiteralComment, 1, func(env *EncEnvironment, args *sxpf.Pair) { if env.GetAttributes(args).HasDefault() { if s := env.GetString(args.GetTail()); s != "" { env.WriteString("<!-- ") env.WriteEscaped(s) env.WriteString(" -->") } } }}, {sexpr.SymLiteralHTML, 2, execHTML}, {sexpr.SymLiteralInput, 2, func(env *EncEnvironment, args *sxpf.Pair) { env.writeLiteral(args, nil, "kbd") }}, {sexpr.SymLiteralMath, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := env.GetAttributes(args).AddClass("zs-math") env.writeLiteral(args, a, "code") }}, {sexpr.SymLiteralOutput, 2, func(env *EncEnvironment, args *sxpf.Pair) { env.writeLiteral(args, nil, "samp") }}, {sexpr.SymLiteralProg, 2, func(env *EncEnvironment, args *sxpf.Pair) { a := setProgLang(env.GetAttributes(args)) env.writeLiteral(args, a, "code") }}, {sexpr.SymLiteralZettel, 0, DoNothingFn}, } // DoNothingFn is a function that does nothing. func DoNothingFn(*EncEnvironment, *sxpf.Pair) { /* Should really do nothing */ } func makeListFn(tag string) encodingFunc { return func(env *EncEnvironment, args *sxpf.Pair) { env.WriteStartTag(tag, nil) for elem := args; !elem.IsNil(); elem = elem.GetTail() { env.WriteStartTag("li", nil) sxpf.Eval(env, elem.GetFirst()) env.WriteEndTag("li") } env.WriteEndTag(tag) } } func (env *EncEnvironment) writeTableRow(cells *sxpf.Pair) { if !cells.IsNil() { env.WriteString("<tr>") for cell := cells; !cell.IsNil(); cell = cell.GetTail() { sxpf.Eval(env, cell.GetFirst()) } env.WriteString("</tr>") } } func makeCellFn(align string) encodingFunc { return func(env *EncEnvironment, args *sxpf.Pair) { if align == "" { env.WriteString("<td>") } else { env.WriteStrings(`<td class="`, align, `">`) } sxpf.EvalSequence(env, args) env.WriteString("</td>") } } func (env *EncEnvironment) writeRegion(args *sxpf.Pair, a attrs.Attributes, tag string) { if a == nil { a = env.GetAttributes(args) } env.WriteStartTag(tag, a) sxpf.Eval(env, env.GetPair(args.GetTail())) if cite := env.GetPair(args.GetTail().GetTail()); !cite.IsNil() { env.WriteString("<cite>") sxpf.EvalSequence(env, cite) env.WriteString("</cite>") } env.WriteEndTag(tag) } func (env *EncEnvironment) writeVerbatim(args *sxpf.Pair, a attrs.Attributes) { env.WriteString("<pre>") env.WriteStartTag("code", a) env.WriteEscapedOrVisible(env.GetString(args.GetTail())) env.WriteString("</code></pre>") } func execHTML(env *EncEnvironment, args *sxpf.Pair) { if s := env.GetString(args.GetTail()); s != "" && IsSafe(s) { env.WriteString(s) } } func (env *EncEnvironment) writeBLOB(title, syntax, data string) { if data == "" { return } switch syntax { case "": case api.ValueSyntaxSVG: // TODO: add title as description env.WriteStrings("<p>", data, "</p>") default: env.WriteStrings(`<p><img src="data:image/`, syntax, ";base64,", data) if title != "" { env.WriteString(`" `) env.WriteOneAttribute("title", title) env.WriteString(`></p>`) } else { env.WriteString(`"></p>`) } } } func PrepareLink(env *EncEnvironment, args *sxpf.Pair) (attrs.Attributes, string, bool) { if env.noLinks { WriteAsSpan(env, args) return nil, "", false } return env.GetAttributes(args), env.GetString(args.GetTail()), true } func WriteAsSpan(env *EncEnvironment, args *sxpf.Pair) { if args.Length() > 2 { sxpf.Eval(env, sxpf.NewPair(sexpr.SymFormatSpan, sxpf.NewPair(args.GetFirst(), args.GetTail().GetTail()))) } } func WriteLink(env *EncEnvironment, args *sxpf.Pair, a attrs.Attributes, refValue, suffix string) { env.WriteString("<a") env.WriteAttributes(a) env.WriteString(">") if args.Length() > 2 { sxpf.EvalSequence(env, args.GetTail().GetTail()) } else { env.WriteString(refValue) } env.WriteStrings("</a>", suffix) } func WriteHRefLink(env *EncEnvironment, args *sxpf.Pair) { if a, refValue, ok := PrepareLink(env, args); ok { WriteLink(env, args, a.Set("href", refValue), refValue, "") } } func makeFormatFn(tag string) encodingFunc { return func(env *EncEnvironment, args *sxpf.Pair) { a := env.GetAttributes(args) if val, found := a.Get(""); found { a = a.Remove("").AddClass(val) } env.WriteStartTag(tag, a) sxpf.EvalSequence(env, args.GetTail()) env.WriteEndTag(tag) } } func writeQuote(env *EncEnvironment, args *sxpf.Pair) { const langAttr = "lang" a := env.GetAttributes(args) lang, hasLang := a.Get(langAttr) if hasLang { a = a.Remove(langAttr) env.WriteStartTag("span", attrs.Attributes{}.Set(langAttr, lang)) } env.WriteStartTag("q", a) sxpf.EvalSequence(env, args.GetTail()) env.WriteEndTag("q") if hasLang { env.WriteEndTag("span") } } func (env *EncEnvironment) writeLiteral(args *sxpf.Pair, a attrs.Attributes, tag string) { if a == nil { a = env.GetAttributes(args) } oldVisible := env.visibleSpace if a.HasDefault() { env.visibleSpace = true a = a.RemoveDefault() } env.WriteStartTag(tag, a) env.WriteEscapedOrVisible(env.GetString(args.GetTail())) env.visibleSpace = oldVisible env.WriteEndTag(tag) } func setProgLang(a attrs.Attributes) attrs.Attributes { if val, found := a.Get(""); found { a = a.AddClass("language-" + val).Remove("") } return a } |
Changes to maps/maps.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ |
Changes to maps/maps_test.go.
1 | //----------------------------------------------------------------------------- | | | 1 2 3 4 5 6 7 8 9 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- |
︙ | ︙ |
Added sexpr/attrs.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package sexpr import ( "codeberg.org/t73fde/sxpf" "zettelstore.de/c/attrs" ) // GetAttributes traverses a s-expression list and returns an attribute structure. func GetAttributes(seq *sxpf.Pair) (result attrs.Attributes) { for elem := seq; !elem.IsNil(); elem = elem.GetTail() { attr, err := elem.GetPair() if err != nil { continue } key, err := attr.GetString() if err != nil { continue } val, err := attr.GetTail().GetString() if err != nil { continue } result = result.Set(key, val) } return result } |
Changes to sexpr/const.go.
1 | //----------------------------------------------------------------------------- | | < | < < < < < | | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < < | | | | | | | < | < < < < | < < < < < < < < < < | < < < | | | < < < | | | < < | < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | | | | | | | | | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | > | < | | | < < | < < < < < < < | < < < < < < < < < < < < < | | < < < < | < | | < > | | | | | | < < < < < < < < < < < < < < < < < < > < < < < < < < < < < < < < < < < < < < < < | > | | | | | | | | | | | | < > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package sexpr import "codeberg.org/t73fde/sxpf" // Various constants for Zettel data. Some of them are technically variables. // Symbols for Zettel node types. var ( Smk = sxpf.NewTrivialSymbolMaker() SymBLOB = Smk.MakeSymbol("BLOB") SymCell = Smk.MakeSymbol("CELL") SymCellCenter = Smk.MakeSymbol("CELL-CENTER") SymCellLeft = Smk.MakeSymbol("CELL-LEFT") SymCellRight = Smk.MakeSymbol("CELL-RIGHT") SymCite = Smk.MakeSymbol("CITE") SymDescription = Smk.MakeSymbol("DESCRIPTION") SymEmbed = Smk.MakeSymbol("EMBED") SymEmbedBLOB = Smk.MakeSymbol("EMBED-BLOB") SymFootnote = Smk.MakeSymbol("FOOTNOTE") SymFormatEmph = Smk.MakeSymbol("FORMAT-EMPH") SymFormatDelete = Smk.MakeSymbol("FORMAT-DELETE") SymFormatInsert = Smk.MakeSymbol("FORMAT-INSERT") SymFormatQuote = Smk.MakeSymbol("FORMAT-QUOTE") SymFormatSpan = Smk.MakeSymbol("FORMAT-SPAN") SymFormatSub = Smk.MakeSymbol("FORMAT-SUB") SymFormatSuper = Smk.MakeSymbol("FORMAT-SUPER") SymFormatStrong = Smk.MakeSymbol("FORMAT-STRONG") SymHard = Smk.MakeSymbol("HARD") SymHeading = Smk.MakeSymbol("HEADING") SymLinkInvalid = Smk.MakeSymbol("LINK-INVALID") SymLinkZettel = Smk.MakeSymbol("LINK-ZETTEL") SymLinkSelf = Smk.MakeSymbol("LINK-SELF") SymLinkFound = Smk.MakeSymbol("LINK-FOUND") SymLinkBroken = Smk.MakeSymbol("LINK-BROKEN") SymLinkHosted = Smk.MakeSymbol("LINK-HOSTED") SymLinkBased = Smk.MakeSymbol("LINK-BASED") SymLinkQuery = Smk.MakeSymbol("LINK-QUERY") SymLinkExternal = Smk.MakeSymbol("LINK-EXTERNAL") SymListOrdered = Smk.MakeSymbol("ORDERED") SymListUnordered = Smk.MakeSymbol("UNORDERED") SymListQuote = Smk.MakeSymbol("QUOTATION") SymLiteralProg = Smk.MakeSymbol("LITERAL-CODE") SymLiteralComment = Smk.MakeSymbol("LITERAL-COMMENT") SymLiteralHTML = Smk.MakeSymbol("LITERAL-HTML") SymLiteralInput = Smk.MakeSymbol("LITERAL-INPUT") SymLiteralMath = Smk.MakeSymbol("LITERAL-MATH") SymLiteralOutput = Smk.MakeSymbol("LITERAL-OUTPUT") SymLiteralZettel = Smk.MakeSymbol("LITERAL-ZETTEL") SymMark = Smk.MakeSymbol("MARK") SymPara = Smk.MakeSymbol("PARA") SymRegionBlock = Smk.MakeSymbol("REGION-BLOCK") SymRegionQuote = Smk.MakeSymbol("REGION-QUOTE") SymRegionVerse = Smk.MakeSymbol("REGION-VERSE") SymSoft = Smk.MakeSymbol("SOFT") SymSpace = Smk.MakeSymbol("SPACE") SymTable = Smk.MakeSymbol("TABLE") SymText = Smk.MakeSymbol("TEXT") SymThematic = Smk.MakeSymbol("THEMATIC") SymTransclude = Smk.MakeSymbol("TRANSCLUDE") SymUnknown = Smk.MakeSymbol("UNKNOWN-NODE") SymVerbatimComment = Smk.MakeSymbol("VERBATIM-COMMENT") SymVerbatimEval = Smk.MakeSymbol("VERBATIM-EVAL") SymVerbatimHTML = Smk.MakeSymbol("VERBATIM-HTML") SymVerbatimMath = Smk.MakeSymbol("VERBATIM-MATH") SymVerbatimProg = Smk.MakeSymbol("VERBATIM-CODE") SymVerbatimZettel = Smk.MakeSymbol("VERBATIM-ZETTEL") ) // Constant symbols for reference states. var ( SymRefStateInvalid = Smk.MakeSymbol("INVALID") SymRefStateZettel = Smk.MakeSymbol("ZETTEL") SymRefStateSelf = Smk.MakeSymbol("SELF") SymRefStateFound = Smk.MakeSymbol("FOUND") SymRefStateBroken = Smk.MakeSymbol("BROKEN") SymRefStateHosted = Smk.MakeSymbol("HOSTED") SymRefStateBased = Smk.MakeSymbol("BASED") SymRefStateQuery = Smk.MakeSymbol("QUERY") SymRefStateExternal = Smk.MakeSymbol("EXTERNAL") ) // Symbols for metadata types var ( SymTypeCredential = Smk.MakeSymbol("CREDENTIAL") SymTypeEmpty = Smk.MakeSymbol("EMPTY-STRING") SymTypeID = Smk.MakeSymbol("ZID") SymTypeIDSet = Smk.MakeSymbol("ZID-SET") SymTypeNumber = Smk.MakeSymbol("NUMBER") SymTypeString = Smk.MakeSymbol("STRING") SymTypeTagSet = Smk.MakeSymbol("TAG-SET") SymTypeTimestamp = Smk.MakeSymbol("TIMESTAMP") SymTypeURL = Smk.MakeSymbol("URL") SymTypeWord = Smk.MakeSymbol("WORD") SymTypeWordSet = Smk.MakeSymbol("WORD-SET") SymTypeZettelmarkup = Smk.MakeSymbol("ZETTELMARKUP") ) |
Deleted sexpr/const_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to sexpr/sexpr.go.
1 | //----------------------------------------------------------------------------- | | < | < | | < < < < < < < < < < < < | < < < | | < < | | | | | > | | > | | | | | | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package sexpr import "codeberg.org/t73fde/sxpf" func MakeString(val sxpf.Value) string { if strVal, ok := val.(*sxpf.String); ok { return strVal.GetValue() } return "" } // GetMetaContent returns the metadata and the content of a sexpr encoded zettel. func GetMetaContent(zettel sxpf.Value) (Meta, *sxpf.Pair) { if pair, ok := zettel.(*sxpf.Pair); ok { m := pair.GetFirst() if s := pair.GetSecond(); s != nil { if p, ok := s.(*sxpf.Pair); ok { if content, err := p.GetPair(); err == nil { return MakeMeta(m), content } } } return MakeMeta(m), nil } return nil, nil } type Meta map[string]MetaValue type MetaValue struct { Type string Key string Value sxpf.Value } func MakeMeta(val sxpf.Value) Meta { if result := doMakeMeta(val); len(result) > 0 { return result } return nil } func doMakeMeta(val sxpf.Value) Meta { result := make(map[string]MetaValue) for { if val == nil { return result } pair, ok := val.(*sxpf.Pair) if !ok { return result } if mv, ok := makeMetaValue(pair); ok { result[mv.Key] = mv } val = pair.GetSecond() } } func makeMetaValue(pair *sxpf.Pair) (MetaValue, bool) { var result MetaValue typePair, ok := pair.GetFirst().(*sxpf.Pair) if !ok { return result, false } typeVal, ok := typePair.GetFirst().(*sxpf.Symbol) if !ok { return result, false } keyPair, ok := typePair.GetSecond().(*sxpf.Pair) if !ok { return result, false } keyStr, ok := keyPair.GetFirst().(*sxpf.String) if !ok { return result, false } valPair, ok := keyPair.GetSecond().(*sxpf.Pair) if !ok { return result, false } result.Type = typeVal.GetValue() result.Key = keyStr.GetValue() result.Value = valPair.GetFirst() return result, true } func (m Meta) GetString(key string) string { if v, found := m[key]; found { return MakeString(v.Value) } return "" } func (m Meta) GetPair(key string) *sxpf.Pair { if mv, found := m[key]; found { if seq, ok := mv.Value.(*sxpf.Pair); ok && !seq.IsEmpty() { return seq } } return nil } |
Deleted shtml/shtml.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Added text/stext.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- // Package text provides types, constants and function to work with text output. package text import ( "bytes" "io" "codeberg.org/t73fde/sxpf" "zettelstore.de/c/sexpr" ) // EvaluateInlineString returns the text content of the given inline list as a string. func EvaluateInlineString(pl *sxpf.Pair) string { var buf bytes.Buffer env := newTextEnvironment(&buf) env.EvalPair(pl) return buf.String() } type textEnvironment struct { err error w io.Writer sm *sxpf.SymbolMap } func newTextEnvironment(w io.Writer) textEnvironment { sm := sxpf.NewSymbolMap(sexpr.Smk, nil) for _, bFn := range builtins { sym := bFn.sym minArgs := bFn.minArgs fn := bFn.fn sm.Set(sym, sxpf.NewBuiltin( sym.GetValue(), true, minArgs, -1, func(env sxpf.Environment, args *sxpf.Pair, _ int) (sxpf.Value, error) { fn(env.(*textEnvironment), args) return sxpf.Nil(), nil }, )) } return textEnvironment{ w: w, sm: sm, } } func (env *textEnvironment) GetString(p *sxpf.Pair) (res string) { if env.err == nil { res, env.err = p.GetString() return res } return "" } func (env *textEnvironment) WriteString(s string) { if env.err == nil { _, env.err = io.WriteString(env.w, s) } } func (env *textEnvironment) LookupForm(sym *sxpf.Symbol) (sxpf.Form, error) { return env.sm.LookupForm(sym) } func (*textEnvironment) EvalSymbol(*sxpf.Symbol) (sxpf.Value, error) { return nil, nil } func (env *textEnvironment) EvalPair(p *sxpf.Pair) (sxpf.Value, error) { return sxpf.EvalCallOrSeq(env, p) } func (env *textEnvironment) EvalOther(val sxpf.Value) (sxpf.Value, error) { if strVal, ok := val.(*sxpf.String); ok { env.WriteString(strVal.GetValue()) return nil, nil } return val, nil } var builtins = []struct { sym *sxpf.Symbol minArgs int fn func(env *textEnvironment, args *sxpf.Pair) }{ {sexpr.SymText, 1, func(env *textEnvironment, args *sxpf.Pair) { env.WriteString(env.GetString(args)) }}, {sexpr.SymSpace, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }}, {sexpr.SymSoft, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString(" ") }}, {sexpr.SymHard, 0, func(env *textEnvironment, _ *sxpf.Pair) { env.WriteString("\n") }}, } |
Deleted text/text.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to text/text_test.go.
1 | //----------------------------------------------------------------------------- | | < | | | | | | 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 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package text_test import ( "testing" "codeberg.org/t73fde/sxpf" "zettelstore.de/c/sexpr" "zettelstore.de/c/text" ) func TestSexprText(t *testing.T) { testcases := []struct { src string exp string }{ {"()", ""}, {`(TEXT "a")`, "a"}, {`(SPACE "a")`, " "}, } for i, tc := range testcases { sval, err := sxpf.ParseString(sexpr.Smk, tc.src) if err != nil { t.Error(err) continue } seq, ok := sval.(*sxpf.Pair) if !ok { t.Errorf("%d: not a list: %v", i, sval) } got := text.EvaluateInlineString(seq) if got != tc.exp { t.Errorf("%d: EncodeBlock(%q) == %q, but got %q", i, tc.src, tc.exp, got) } } } |
Changes to www/changes.wiki.
1 2 | <title>Change Log</title> | < < < < < < < < < < < < < < < < < | 1 2 3 4 5 6 7 8 9 | <title>Change Log</title> <a name="0_9"></a> <h2>Changes for Version 0.9.0 (2022-12-12)</h2> * Rename <tt>api.QueryKeyDepth</tt> to <tt>api.QueryKeyCost</tt> * Update encode / syntax names <a name="0_6"></a> <h2>Changes for Version 0.6.0 (2022-08-11)</h2> |
︙ | ︙ |
Changes to www/index.wiki.
1 2 3 4 5 | <title>Home</title> This repository contains Go client software to access [https://zettelstore.de|Zettelstore] via its API. | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 | <title>Home</title> This repository contains Go client software to access [https://zettelstore.de|Zettelstore] via its API. <h3>Latest Release: 0.9.0 (2022-12-12)</h3> * [./changes.wiki#0_9|Change summary] * [/timeline?p=v0.9.0&bt=v0.8.0&y=ci|Check-ins for version 0.9.0], [/vdiff?to=v0.9.0&from=v0.8.0|content diff] * [/timeline?df=v0.9.0&y=ci|Check-ins derived from the 0.9.0 release], [/vdiff?from=v0.9.0&to=trunk|content diff] * [/timeline?t=release|Timeline of all past releases] |
Added zjson/const.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package zjson // Values for Zettelmarkup element object names const ( NameType = "" NameAttribute = "a" NameBLOB = "j" NameBinary = "o" NameBlock = "b" NameDescrList = "d" NameDescription = "e" NameInline = "i" NameList = "c" NameNumeric = "n" NameSet = "y" NameString = "s" NameString2 = "q" NameString3 = "v" NameTable = "p" ) // Values to specify the Zettelmarkup element type const ( TypeBLOB = "BLOB" TypeBlock = "Block" TypeBreakHard = "Hard" TypeBreakSoft = "Soft" TypeBreakThematic = "Thematic" TypeCitation = "Cite" TypeDescrList = "Description" TypeEmbed = "Embed" TypeEmbedBLOB = "EmbedBLOB" TypeExcerpt = "Excerpt" TypeFootnote = "Footnote" TypeFormatDelete = "Delete" TypeFormatEmph = "Emph" TypeFormatInsert = "Insert" TypeFormatQuote = "Quote" TypeFormatSpan = "Span" TypeFormatStrong = "Strong" TypeFormatSub = "Sub" TypeFormatSuper = "Super" TypeHeading = "Heading" TypeLink = "Link" TypeListBullet = "Bullet" TypeListOrdered = "Ordered" TypeListQuotation = "Quotation" TypeLiteralCode = "Code" TypeLiteralComment = "Comment" TypeLiteralHTML = "HTML" TypeLiteralInput = "Input" TypeLiteralOutput = "Output" TypeLiteralMath = "Math" TypeLiteralZettel = "Zettel" TypeMark = "Mark" TypeParagraph = "Para" TypePoem = "Poem" TypeSpace = "Space" TypeTable = "Table" TypeText = "Text" TypeTransclude = "Transclude" TypeVerbatimCode = "CodeBlock" TypeVerbatimComment = "CommentBlock" TypeVerbatimEval = "EvalBlock" TypeVerbatimHTML = "HTMLBlock" TypeVerbatimMath = "MathBlock" TypeVerbatimZettel = "ZettelBlock" ) // Values to specify the state of a reference const ( RefStateBased = "based" RefStateBroken = "broken" RefStateExternal = "external" RefStateFound = "found" RefStateHosted = "local" RefStateInvalid = "invalid" RefStateQuery = "query" RefStateSelf = "self" RefStateZettel = "zettel" ) // Values for table cell alignment const ( AlignDefault = "" AlignLeft = "<" AlignCenter = ":" AlignRight = ">" ) |
Added zjson/meta.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- package zjson type Meta map[string]MetaValue type MetaValue struct { Type string Key string Value Value } func MakeMeta(val Value) Meta { obj := MakeObject(val) if len(obj) == 0 { return nil } result := make(Meta, len(obj)) for k, v := range obj { mvObj := MakeObject(v) if len(mvObj) == 0 { continue } mv := makeMetaValue(mvObj) if mv.Type != "" { result[k] = mv } } return result } func makeMetaValue(mvObj Object) MetaValue { mv := MetaValue{} for n, val := range mvObj { if n == NameType { if t, ok := val.(string); ok { mv.Type = t } } else { mv.Key = n mv.Value = val } } return mv } func (m Meta) GetArray(key string) Array { if v, found := m[key]; found { return MakeArray(v.Value) } return nil } func (m Meta) GetString(key string) string { if v, found := m[key]; found { return MakeString(v.Value) } return "" } |
Added zjson/zjson.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | //----------------------------------------------------------------------------- // Copyright (c) 2022 Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. //----------------------------------------------------------------------------- // Package zjson provides types, constants and function to work with the ZJSON // encoding of zettel. package zjson import ( "encoding/json" "fmt" "io" "zettelstore.de/c/attrs" ) // Value is the gerneric JSON value. type Value = interface{} // Array represents a JSON array. type Array = []Value // Object represents a JSON object. type Object = map[string]Value // Decode some JSON data and return the tree. func Decode(data io.Reader) (Value, error) { var result interface{} dec := json.NewDecoder(data) dec.UseNumber() err := dec.Decode(&result) if err != nil { return nil, err } return result, nil } // GetMetaContent returns the metadata and the content of a zettel ZJSON. func GetMetaContent(zjZettel Value) (Meta, Array) { if zettel, ok := zjZettel.(Object); ok { meta := MakeMeta(zettel["meta"]) content := MakeArray(zettel["content"]) return meta, content } return nil, nil } // Visitor provides functionality when a Value is traversed. type Visitor interface { BlockArray(a Array, pos int) CloseFunc InlineArray(a Array, pos int) CloseFunc ItemArray(a Array, pos int) CloseFunc BlockObject(t string, obj Object, pos int) (bool, CloseFunc) InlineObject(t string, obj Object, pos int) (bool, CloseFunc) Unexpected(val Value, pos int, exp string) } // CloseFunc is a function that executes after a ZJSON element is visited. type CloseFunc func() // WalkBlock traverses a block array. func WalkBlock(v Visitor, a Array, pos int) { ef := v.BlockArray(a, pos) for i, elem := range a { WalkBlockObject(v, elem, i) } if ef != nil { ef() } } // WalkInline traverses an inline array. func WalkInline(v Visitor, a Array, pos int) { ef := v.InlineArray(a, pos) for i, elem := range a { WalkInlineObject(v, elem, i) } if ef != nil { ef() } } // WalkBlockObject traverses a value as a JSON object in a block array. func WalkBlockObject(v Visitor, val Value, pos int) { walkObject(v, val, pos, v.BlockObject) } // WalkInlineObject traverses a value as a JSON object in an inline array. func WalkInlineObject(v Visitor, val Value, pos int) { walkObject(v, val, pos, v.InlineObject) } func walkObject(v Visitor, val Value, pos int, objFunc func(string, Object, int) (bool, CloseFunc)) { obj, ok := val.(Object) if !ok { v.Unexpected(val, pos, "Object") return } tVal, ok := obj[NameType] if !ok { v.Unexpected(obj, pos, "Object type") return } t, ok := tVal.(string) if !ok { v.Unexpected(obj, pos, "Object type value") return } doChilds, ef := objFunc(t, obj, pos) if doChilds { WalkBlockChild(v, obj, pos) WalkItemChild(v, obj, pos) WalkInlineChild(v, obj, pos) walkDescriptionList(v, obj) walkTable(v, obj, pos) } if ef != nil { ef() } } // WalkInlineChild traverses the array found at the name NameInline ('i'). func WalkInlineChild(v Visitor, obj Object, pos int) { if iVal, ok := obj[NameInline]; ok { if il, ok := iVal.(Array); ok { WalkInline(v, il, 0) } else { v.Unexpected(iVal, pos, "Inline array") } } } // WalkBlockChild traverses the array found at the name NameBlock ('b'). func WalkBlockChild(v Visitor, obj Object, pos int) { if bVal, ok := obj[NameBlock]; ok { if bl, ok := bVal.(Array); ok { WalkBlock(v, bl, 0) } else { v.Unexpected(bVal, pos, "Block array") } } } // WalkItemChild traverses the arrays found at the name NameList ('c'). func WalkItemChild(v Visitor, obj Object, pos int) { iVal, ok := obj[NameList] if !ok { return } it, ok := iVal.(Array) if !ok { v.Unexpected(iVal, pos, "Item array") return } for i, l := range it { ef := v.ItemArray(it, i) if bl, ok := l.(Array); ok { WalkBlock(v, bl, i) } else { v.Unexpected(l, i, "Item block array") } if ef != nil { ef() } } } func walkDescriptionList(v Visitor, obj Object) { descrs := GetArray(obj, NameDescrList) if len(descrs) == 0 { return } for i, elem := range descrs { dObj := MakeObject(elem) if dObj == nil { continue } WalkInlineChild(v, dObj, i) descr := GetArray(dObj, NameDescription) if len(descr) == 0 { continue } for j, ddv := range descr { dd := MakeArray(ddv) if len(dd) == 0 { continue } WalkBlock(v, dd, j) } } } func walkTable(v Visitor, obj Object, pos int) { tdata := GetArray(obj, NameTable) if len(tdata) == 0 { return } if len(tdata) != 2 { v.Unexpected(obj, pos, "Table header/rows") return } walkRow(v, MakeArray(tdata[0])) if bArray := MakeArray(tdata[1]); len(bArray) > 0 { for _, row := range bArray { if rArray := MakeArray(row); rArray != nil { walkRow(v, rArray) } } } } func walkRow(v Visitor, row Array) { if len(row) > 0 { for _, cell := range row { if cObj := MakeObject(cell); cObj != nil { WalkInlineChild(v, cObj, 0) } } } } // GetArray returns the array-typed value under the given name. func GetArray(obj Object, name string) Array { if v, ok := obj[name]; ok && v != nil { return MakeArray(v) } return nil } // GetNumber returns the numeric value at NameNumberic ('n') as a string. func GetNumber(obj Object) string { if v, ok := obj[NameNumeric]; ok { if n, ok := v.(json.Number); ok { return string(n) } if f, ok := v.(float64); ok { return fmt.Sprint(f) } } return "" } // GetString returns the string value at the given name. func GetString(obj Object, name string) string { if v, ok := obj[name]; ok { return MakeString(v) } return "" } // MakeArray returns the given value as a JSON array. func MakeArray(val Value) Array { if a, ok := val.(Array); ok { return a } return nil } // MakeString returns the given value as a string. func MakeString(val Value) string { if s, ok := val.(string); ok { return s } return "" } // GetAttribute returns a copy of the attributes of the given object. func GetAttributes(obj Object) attrs.Attributes { a := GetObject(obj, NameAttribute) if len(a) == 0 { return nil } result := make(attrs.Attributes, len(a)) for n, v := range a { if val, ok := v.(string); ok { result[n] = val } } return result } // SetAttributes copies the attributes to the given object. func SetAttributes(obj Object, a attrs.Attributes) { if len(a) == 0 { delete(obj, NameAttribute) } val := make(Object) for k, v := range a { val[k] = v } obj[NameAttribute] = val } // GetObject returns the object found at the given object with the given name. func GetObject(obj Object, name string) Object { if v, ok := obj[name]; ok && v != nil { return MakeObject(v) } return nil } // MakeObject returns the given value as a JSON object. func MakeObject(val Value) Object { if o, ok := val.(Object); ok { return o } return nil } // GetParagraphInline return the inline list of the first paragraph (or nil if there is no such thing) func GetParagraphInline(a Array) Array { if len(a) != 1 { return nil } if o := MakeObject(a[0]); o != nil { if GetString(o, NameType) == TypeParagraph { return GetArray(o, NameInline) } } return nil } |