Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From v0.18.0 To v0.19.0
2024-12-13
| ||
14:14 | Remove query action constants for rss/atom feed ... (Leaf check-in: 7902e0d349 user: stern tags: trunk) | |
12:10 | Version 0.19.0 ... (check-in: fd15ae683e user: stern tags: release, trunk, v0.19.0) | |
2024-12-02
| ||
17:00 | Remove some linter errors (revive) ... (check-in: 2b332aee9c user: stern tags: trunk) | |
2024-07-11
| ||
16:29 | Remove "rename" operation and associated constants ... (check-in: a203b14b7e user: stern tags: trunk) | |
14:40 | Version 0.18.0 ... (check-in: b141c81ad9 user: stern tags: release, trunk, v0.18.0) | |
2024-07-10
| ||
15:15 | Reserve ZID for computed zettel about zettel ... (check-in: ea5260d4a2 user: stern tags: trunk) | |
Changes to api/api.go.
︙ | ︙ | |||
30 31 32 33 34 35 36 | if ch < '0' || '9' < ch { return false } } return true } | | | > > > > > | | 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 | if ch < '0' || '9' < ch { return false } } return true } // ZettelMeta is a map containg the normalized metadata of a zettel. type ZettelMeta map[string]string // ZettelRights is an integer that encode access rights for a zettel. type ZettelRights uint8 // Values for ZettelRights, can be or-ed const ( ZettelCanNone ZettelRights = 1 << iota ZettelCanCreate // Current user is allowed to create a new zettel ZettelCanRead // Requesting user is allowed to read the zettel ZettelCanWrite // Requesting user is allowed to update the zettel placeholdergo1 // Was assigned to rename right, which is now removed ZettelCanDelete // Requesting user is allowed to delete the zettel ZettelMaxRight // Sentinel value ) // MetaRights contains the metadata of a zettel, and its rights. type MetaRights struct { Meta ZettelMeta Rights ZettelRights } // ZidMetaRights contains the identifier, the metadata of a zettel, and its rights. type ZidMetaRights struct { ID ZettelID Meta ZettelMeta Rights ZettelRights } // ZettelData contains all data for a zettel. // // - Meta is a map containing the metadata of the zettel. // - Rights is an integer specifying the access rights. // - Encoding is a string specifying the encoding of the zettel content. // - Content is the zettel content itself. type ZettelData struct { Meta ZettelMeta Rights ZettelRights Encoding string Content string // raw, uninterpreted zettel content } // Aggregate maps metadata keys to list of zettel identifier. type Aggregate map[string][]ZettelID |
Changes to api/const.go.
︙ | ︙ | |||
11 12 13 14 15 16 17 | // SPDX-FileCopyrightText: 2021-present Detlef Stern //----------------------------------------------------------------------------- package api import "fmt" | | > > > > | | | | | | | | | | | | | | | | | | | | | | < | | | | | | < | | | | | | | | | | | | | | | | | | | | > | | > > > > | > > > > | 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 | // SPDX-FileCopyrightText: 2021-present Detlef Stern //----------------------------------------------------------------------------- package api import "fmt" // Predefined zettel identifier. // // See [List of predefined zettel]. // // [List of predefined zettel]: https://zettelstore.de/manual/h/00001005090000 const ( // System zettel ZidVersion = ZettelID("00000000000001") ZidHost = ZettelID("00000000000002") ZidOperatingSystem = ZettelID("00000000000003") ZidLicense = ZettelID("00000000000004") ZidAuthors = ZettelID("00000000000005") ZidDependencies = ZettelID("00000000000006") ZidLog = ZettelID("00000000000007") ZidMemory = ZettelID("00000000000008") ZidSx = ZettelID("00000000000009") ZidHTTP = ZettelID("00000000000010") ZidAPI = ZettelID("00000000000011") ZidWebUI = ZettelID("00000000000012") ZidConsole = ZettelID("00000000000013") ZidBoxManager = ZettelID("00000000000020") ZidZettel = ZettelID("00000000000021") ZidIndex = ZettelID("00000000000022") ZidQuery = ZettelID("00000000000023") ZidMetadataKey = ZettelID("00000000000090") ZidParser = ZettelID("00000000000092") ZidStartupConfiguration = ZettelID("00000000000096") ZidConfiguration = ZettelID("00000000000100") ZidDirectory = ZettelID("00000000000101") // WebUI HTML templates are in the range 10000..19999 ZidBaseTemplate = ZettelID("00000000010100") ZidLoginTemplate = ZettelID("00000000010200") ZidListTemplate = ZettelID("00000000010300") ZidZettelTemplate = ZettelID("00000000010401") ZidInfoTemplate = ZettelID("00000000010402") ZidFormTemplate = ZettelID("00000000010403") ZidDeleteTemplate = ZettelID("00000000010405") ZidErrorTemplate = ZettelID("00000000010700") // WebUI sxn code zettel are in the range 19000..19999 ZidSxnStart = ZettelID("00000000019000") ZidSxnBase = ZettelID("00000000019990") // CSS-related zettel are in the range 20000..29999 ZidBaseCSS = ZettelID("00000000020001") ZidUserCSS = ZettelID("00000000025001") // WebUI JS zettel are in the range 30000..39999 // WebUI image zettel are in the range 40000..49999 ZidEmoji = ZettelID("00000000040001") // Other sxn code zettel are in the range 50000..59999 ZidSxnPrelude = ZettelID("00000000059900") // Predefined Zettelmarkup zettel are in the range 60000..69999 ZidRoleZettelZettel = ZettelID("00000000060010") ZidRoleConfigurationZettel = ZettelID("00000000060020") ZidRoleRoleZettel = ZettelID("00000000060030") ZidRoleTagZettel = ZettelID("00000000060040") // Range 90000...99999 is reserved for zettel templates ZidTOCNewTemplate = ZettelID("00000000090000") ZidTemplateNewZettel = ZettelID("00000000090001") ZidTemplateNewRole = ZettelID("00000000090004") ZidTemplateNewTag = ZettelID("00000000090003") ZidTemplateNewUser = ZettelID("00000000090002") // Range 00000999999900...00000999999999 are predefined zettel to be searched by content. ZidAppDirectory = ZettelID("00000999999999") // Default Home Zettel ZidDefaultHome = ZettelID("00010000000000") ) // LengthZid factors the constant length of a zettel identifier const LengthZid = len(ZidDefaultHome) // Values of the metadata key/value types. // // See [Supported Key Types]. // // [Supported Key Types]: https://zettelstore.de/manual/h/00001006030000 const ( MetaCredential = "Credential" MetaEmpty = "EString" MetaID = "Identifier" MetaIDSet = "IdentifierSet" MetaNumber = "Number" MetaString = "String" MetaTagSet = "TagSet" MetaTimestamp = "Timestamp" MetaURL = "URL" MetaWord = "Word" MetaZettelmarkup = "Zettelmarkup" ) // Predefined / supported metadata keys. // // See [Supported Metadata Keys]. // // [Supported Metadata Keys]: https://zettelstore.de/manual/h/00001006020000 const ( KeyID = "id" KeyTitle = "title" KeyRole = "role" KeyTags = "tags" KeySyntax = "syntax" KeyAuthor = "author" |
︙ | ︙ | |||
126 127 128 129 130 131 132 133 134 135 136 137 | KeyFolgeRole = "folge-role" KeyForward = "forward" KeyLang = "lang" KeyLicense = "license" KeyModified = "modified" KeyPrecursor = "precursor" KeyPredecessor = "predecessor" KeyPublished = "published" KeyQuery = "query" KeyReadOnly = "read-only" KeySubordinates = "subordinates" KeySuccessors = "successors" | > > < > | | | | | | | | | | | | | | | | | < < | 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 | KeyFolgeRole = "folge-role" KeyForward = "forward" KeyLang = "lang" KeyLicense = "license" KeyModified = "modified" KeyPrecursor = "precursor" KeyPredecessor = "predecessor" KeyPrequel = "prequel" KeyPublished = "published" KeyQuery = "query" KeyReadOnly = "read-only" KeySequel = "sequel" KeySubordinates = "subordinates" KeySuccessors = "successors" KeySummary = "summary" KeySuperior = "superior" KeyURL = "url" KeyUselessFiles = "useless-files" KeyUserID = "user-id" KeyUserRole = "user-role" KeyVisibility = "visibility" ) // Predefined metadata values. const ( ValueFalse = "false" ValueTrue = "true" ValueLangEN = "en" // Default for "lang" ValueRoleConfiguration = "configuration" // A role for internal zettel ValueRoleTag = "tag" // A role for tag zettel ValueRoleRole = "role" // A role for role zettel ValueRoleZettel = "zettel" // A role for zettel ValueSyntaxCSS = "css" // Syntax: CSS ValueSyntaxDraw = "draw" // Syntax: Drawing ValueSyntaxGif = "gif" // Syntax GIF image ValueSyntaxHTML = "html" // Syntax: HTML ValueSyntaxMarkdown = "markdown" // Syntax: Markdown / CommonMark ValueSyntaxMD = "md" // Syntax: Markdown / CommonMark ValueSyntaxNone = "none" // Syntax: no syntax / content, just metadata ValueSyntaxSVG = "svg" // Syntax: SVG ValueSyntaxSxn = "sxn" // Syntax: S-Expression ValueSyntaxText = "text" // Syntax: plain text ValueSyntaxZmk = "zmk" // Syntax: Zettelmarkup ValueUserRoleCreator = "creator" ValueUserRoleOwner = "owner" ValueUserRoleReader = "reader" ValueUserRoleWriter = "writer" ValueVisibilityCreator = "creator" ValueVisibilityExpert = "expert" ValueVisibilityLogin = "login" ValueVisibilityOwner = "owner" ValueVisibilityPublic = "public" ) // Additional HTTP constants. const ( HeaderAccept = "Accept" HeaderContentType = "Content-Type" HeaderDestination = "Destination" HeaderLocation = "Location" ) // Values for HTTP query parameter. |
︙ | ︙ | |||
196 197 198 199 200 201 202 | QueryKeyRole = "role" QueryKeySeed = "_seed" QueryKeyTag = "tag" ) // Supported encoding values. const ( | | | | | | | | | | 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | QueryKeyRole = "role" QueryKeySeed = "_seed" QueryKeyTag = "tag" ) // Supported encoding values. const ( EncodingHTML = "html" // Plain HTML EncodingMD = "md" // Markdown EncodingSHTML = "shtml" // SxHTML EncodingSz = "sz" // Structure of zettel, encoded a an S-expression EncodingText = "text" // plain text content EncodingZMK = "zmk" // Zettelmarkup EncodingPlain = "plain" // Plain zettel, no processing EncodingData = "data" // Plain zettel, metadata as S-Expression ) var mapEncodingEnum = map[string]EncodingEnum{ EncodingHTML: EncoderHTML, EncodingMD: EncoderMD, EncodingSHTML: EncoderSHTML, EncodingSz: EncoderSz, |
︙ | ︙ | |||
269 270 271 272 273 274 275 | PartContent = "content" PartZettel = "zettel" ) // Command to be executed atthe Zettelstore type Command string | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 | PartContent = "content" PartZettel = "zettel" ) // Command to be executed atthe Zettelstore type Command string // Supported command values. const ( CommandAuthenticated = Command("authenticated") CommandRefresh = Command("refresh") ) // Supported search operator representations. const ( BackwardDirective = "BACKWARD" // Backward-only context ContextDirective = "CONTEXT" // Context directive CostDirective = "COST" // Maximum cost of a context operation ForwardDirective = "FORWARD" // Forward-only context FullDirective = "FULL" // Include tags in context IdentDirective = "IDENT" // Use only specified zettel ItemsDirective = "ITEMS" // Select list elements in a zettel MaxDirective = "MAX" // Maximum number of context results LimitDirective = "LIMIT" // Maximum number of zettel OffsetDirective = "OFFSET" // Offset to start returned zettel list OrDirective = "OR" // Combine several search expression with an "or" OrderDirective = "ORDER" // Specify metadata keys for the order of returned list PhraseDirective = "PHRASE" // Only unlinked zettel with given phrase PickDirective = "PICK" // Pick some random zettel RandomDirective = "RANDOM" // Order zettel list randomly ReverseDirective = "REVERSE" // Reverse the order of a zettel list UnlinkedDirective = "UNLINKED" // Search for zettel that contain a phase(s) but do not link ActionSeparator = "|" // Separates action list of previous elements of query expression AtomAction = "ATOM" // Return an Atom web feed KeysAction = "KEYS" // Provide metadata key used MinAction = "MIN" // Return only those values with a minimum amount of zettel MaxAction = "MAX" // Return only those values with a maximum amount of zettel NumberedAction = "NUMBERED" // Return a numbered list RedirectAction = "REDIRECT" // Return the first zettel in list ReIndexAction = "REINDEX" // Ensure that zettel is/are indexed. RSSAction = "RSS" // Return a RSS web feed TitleAction = "TITLE" // Set a title for Atom or RSS web feed ExistOperator = "?" // Does zettel have metadata with given key? ExistNotOperator = "!?" // True id zettel does not have metadata with given key. SearchOperatorNot = "!" SearchOperatorEqual = "=" // True if values are equal SearchOperatorNotEqual = "!=" // False if values are equal SearchOperatorHas = ":" // True if values are equal/included SearchOperatorHasNot = "!:" // False if values are equal/included SearchOperatorPrefix = "[" // True if value is prefix of the other SearchOperatorNoPrefix = "![" // False if value is prefix of the other SearchOperatorSuffix = "]" // True if value is suffix of other SearchOperatorNoSuffix = "!]" // False if value is suffix of other SearchOperatorMatch = "~" // True if value is included in other SearchOperatorNoMatch = "!~" // False if value is included in other SearchOperatorLess = "<" // True if value is smaller than other SearchOperatorNotLess = "!<" // False if value is smaller than other SearchOperatorGreater = ">" // True if value is greater than other SearchOperatorNotGreater = "!>" // False if value is greater than other ) // QueryPrefix is the prefix that denotes a query expression within a reference. const QueryPrefix = "query:" |
Changes to api/urlbuilder.go.
︙ | ︙ | |||
29 30 31 32 33 34 35 | result := URLBuilder{prefix: prefix} if key != '/' { result.base.AddPath(string([]byte{key})) } return &result } | | | | | > > | | 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 | result := URLBuilder{prefix: prefix} if key != '/' { result.base.AddPath(string([]byte{key})) } return &result } // Clone an URLBuilder. func (ub *URLBuilder) Clone() *URLBuilder { cpy := new(URLBuilder) ub.base.Copy(&cpy.base) cpy.prefix = ub.prefix return cpy } // SetZid sets the zettel identifier. func (ub *URLBuilder) SetZid(zid ZettelID) *URLBuilder { ub.base.AddPath(string(zid)) return ub } // AppendPath adds a new path element. func (ub *URLBuilder) AppendPath(p string) *URLBuilder { ub.base.AddPath(p) return ub } // AppendKVQuery adds a new key/value query parameter. func (ub *URLBuilder) AppendKVQuery(key, value string) *URLBuilder { ub.base.AddQuery(key, value) return ub } // AppendQuery adds a new query. // // Basically the same as [URLBuilder.AppendKVQuery]([api.QueryKeyQuery], value) func (ub *URLBuilder) AppendQuery(value string) *URLBuilder { if value != "" { ub.base.AddQuery(QueryKeyQuery, value) } return ub } // ClearQuery removes all query parameters. func (ub *URLBuilder) ClearQuery() *URLBuilder { ub.base.RemoveQueries() return ub } // SetFragment sets the fragment. func (ub *URLBuilder) SetFragment(s string) *URLBuilder { ub.base.SetFragment(s) return ub } // String produces a string value. func (ub *URLBuilder) String() string { |
︙ | ︙ |
Changes to attrs/attrs.go.
︙ | ︙ | |||
11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // SPDX-FileCopyrightText: 2020-present Detlef Stern //----------------------------------------------------------------------------- // Package attrs stores attributes of zettel parts. package attrs import ( "strings" "t73f.de/r/zsc/maps" ) // Attributes store additional information about some node types. type Attributes map[string]string | > | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // SPDX-FileCopyrightText: 2020-present Detlef Stern //----------------------------------------------------------------------------- // Package attrs stores attributes of zettel parts. package attrs import ( "slices" "strings" "t73f.de/r/zsc/maps" ) // Attributes store additional information about some node types. type Attributes map[string]string |
︙ | ︙ | |||
83 84 85 86 87 88 89 | func (a Attributes) Remove(key string) Attributes { if a != nil { delete(a, key) } return a } | | | | | | | > > | | | < > | > > | | | < < < | | < < < | > > | | | < < | | < | | | < > | 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 | func (a Attributes) Remove(key string) Attributes { if a != nil { delete(a, key) } return a } // Add a value to an attribute key. func (a Attributes) Add(key, value string) Attributes { if a == nil { return map[string]string{key: value} } values := a.Values(key) if !slices.Contains(values, value) { values = append(values, value) a[key] = strings.Join(values, " ") } return a } // Values are the space separated values of an attribute. func (a Attributes) Values(key string) []string { if a != nil { if value, ok := a[key]; ok { return strings.Fields(value) } } return nil } // Has the attribute key a value? func (a Attributes) Has(key, value string) bool { return slices.Contains(a.Values(key), value) } // AddClass adds a value to the class attribute. func (a Attributes) AddClass(class string) Attributes { return a.Add("class", class) } // GetClasses returns the class values as a string slice func (a Attributes) GetClasses() []string { return a.Values("class") } // HasClass returns true, if attributes contains the given class. func (a Attributes) HasClass(s string) bool { return a.Has("class", s) } |
Changes to attrs/attrs_test.go.
︙ | ︙ | |||
53 54 55 56 57 58 59 | func TestHasClass(t *testing.T) { t.Parallel() testcases := []struct { classes string class string exp bool }{ | | | 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | func TestHasClass(t *testing.T) { t.Parallel() testcases := []struct { classes string class string exp bool }{ {"", "", false}, {"x", "", false}, {"x", "x", true}, {"x", "y", false}, {"abc def ghi", "abc", true}, {"abc def ghi", "def", true}, {"abc def ghi", "ghi", true}, {"ab de gi", "b", false}, |
︙ | ︙ |
Changes to client/client.go.
︙ | ︙ | |||
11 12 13 14 15 16 17 | // SPDX-FileCopyrightText: 2021-present Detlef Stern //----------------------------------------------------------------------------- // Package client provides a client for accessing the Zettelstore via its API. package client import ( | < < | | 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 | // SPDX-FileCopyrightText: 2021-present Detlef Stern //----------------------------------------------------------------------------- // Package client provides a client for accessing the Zettelstore via its API. package client import ( "bytes" "context" "fmt" "io" "net" "net/http" "net/url" "strconv" "strings" "time" "t73f.de/r/sx" "t73f.de/r/sx/sxreader" "t73f.de/r/zsc/api" "t73f.de/r/zsc/sexp" ) // Client contains all data to execute requests. type Client struct { base string username string password string token string tokenType string expires time.Time client http.Client } // Base returns the base part of the URLs that are used to communicate with a Zettelstore. func (c *Client) Base() string { return c.base } // NewClient creates a new client with a given base URL to a Zettelstore. func NewClient(u *url.URL) *Client { myURL := *u myURL.User = nil myURL.ForceQuery = false myURL.RawQuery = "" myURL.Fragment = "" myURL.RawFragment = "" |
︙ | ︙ | |||
69 70 71 72 73 74 75 | }, }, } return &c } // AllowRedirect will modify the client to not follow redirect status code when | | | | > > > > > | 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 | }, }, } return &c } // AllowRedirect will modify the client to not follow redirect status code when // using the Zettelstore. The original behaviour can be restored by setting // allow to false. func (c *Client) AllowRedirect(allow bool) { if allow { c.client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } } else { c.client.CheckRedirect = nil } } // Error encapsulates the possible client call errors. // // - StatusCode is the HTTP status code, e.g. 200 // - Message is the HTTP message, e.g. "OK" // - Body is the HTTP body returned by a request. type Error struct { StatusCode int Message string Body []byte } // Error returns the error as a string. func (err *Error) Error() string { var body string if err.Body == nil { body = "nil" } else if bl := len(err.Body); bl == 0 { body = "empty" } else { |
︙ | ︙ | |||
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | StatusCode: resp.StatusCode, Message: resp.Status[4:], Body: body, } } // NewURLBuilder creates a new URL builder for the client with the given key. func (c *Client) NewURLBuilder(key byte) *api.URLBuilder { return api.NewURLBuilder(c.base, key) } func (*Client) newRequest(ctx context.Context, method string, ub *api.URLBuilder, body io.Reader) (*http.Request, error) { return http.NewRequestWithContext(ctx, method, ub.String(), body) } func (c *Client) executeRequest(req *http.Request) (*http.Response, error) { if c.token != "" { req.Header.Add("Authorization", c.tokenType+" "+c.token) } resp, err := c.client.Do(req) if err != nil { if resp != nil && resp.Body != nil { | > > > > > | | > > > > < < < > > | 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 | StatusCode: resp.StatusCode, Message: resp.Status[4:], Body: body, } } // NewURLBuilder creates a new URL builder for the client with the given key. // // key is one of the defined lower case letters to specify an endpoint. // See [Endpoints used by the API] for details. // // [Endpoints used by the API]: https://zettelstore.de/manual/h/00001012920000 func (c *Client) NewURLBuilder(key byte) *api.URLBuilder { return api.NewURLBuilder(c.base, key) } func (*Client) newRequest(ctx context.Context, method string, ub *api.URLBuilder, body io.Reader) (*http.Request, error) { return http.NewRequestWithContext(ctx, method, ub.String(), body) } func (c *Client) executeRequest(req *http.Request) (*http.Response, error) { if c.token != "" { req.Header.Add("Authorization", c.tokenType+" "+c.token) } resp, err := c.client.Do(req) if err != nil { if resp != nil && resp.Body != nil { _ = resp.Body.Close() } return nil, err } return resp, err } func (c *Client) buildAndExecuteRequest( ctx context.Context, method string, ub *api.URLBuilder, body io.Reader, ) (*http.Response, error) { req, err := c.newRequest(ctx, method, ub, body) if err != nil { return nil, err } err = c.updateToken(ctx) if err != nil { return nil, err } return c.executeRequest(req) } // SetAuth sets authentication data. // // username and password are the same values that are used to authenticate via the Web-UI. func (c *Client) SetAuth(username, password string) { c.username = username c.password = password c.token = "" c.tokenType = "" c.expires = time.Time{} } |
︙ | ︙ | |||
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 | if time.Now().After(c.expires) { return c.Authenticate(ctx) } return c.RefreshToken(ctx) } // Authenticate sets a new token by sending user name and password. func (c *Client) Authenticate(ctx context.Context) error { authData := url.Values{"username": {c.username}, "password": {c.password}} req, err := c.newRequest(ctx, http.MethodPost, c.NewURLBuilder('a'), strings.NewReader(authData.Encode())) if err != nil { return err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") return c.executeAuthRequest(req) } // RefreshToken updates the access token func (c *Client) RefreshToken(ctx context.Context) error { req, err := c.newRequest(ctx, http.MethodPut, c.NewURLBuilder('a'), nil) if err != nil { return err } return c.executeAuthRequest(req) } // CreateZettel creates a new zettel and returns its URL. func (c *Client) CreateZettel(ctx context.Context, data []byte) (api.ZettelID, error) { ub := c.NewURLBuilder('z') | > > > > > > > > | > > | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < | < < < < < < | < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | < < < < < < < < < < < < < < < < < < < | > > > > | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 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 | if time.Now().After(c.expires) { return c.Authenticate(ctx) } return c.RefreshToken(ctx) } // Authenticate sets a new token by sending user name and password. // // [Client.SetAuth] should be called before. func (c *Client) Authenticate(ctx context.Context) error { authData := url.Values{"username": {c.username}, "password": {c.password}} req, err := c.newRequest(ctx, http.MethodPost, c.NewURLBuilder('a'), strings.NewReader(authData.Encode())) if err != nil { return err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") return c.executeAuthRequest(req) } // RefreshToken updates the access token // // [Client.SetAuth] should be called before. func (c *Client) RefreshToken(ctx context.Context) error { req, err := c.newRequest(ctx, http.MethodPut, c.NewURLBuilder('a'), nil) if err != nil { return err } return c.executeAuthRequest(req) } // CreateZettel creates a new zettel and returns its URL. // // data contains the zettel metadata and content, as it is stored in a file in a zettel box, // or as returned by [Client.GetZettel]. // Metadata is separated from zettel content by an empty line. func (c *Client) CreateZettel(ctx context.Context, data []byte) (api.ZettelID, error) { ub := c.NewURLBuilder('z') resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data)) if err != nil { return api.InvalidZID, err } defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { return api.InvalidZID, statusToError(resp) } b, err := io.ReadAll(resp.Body) if err != nil { return api.InvalidZID, err } if zid := api.ZettelID(b); zid.IsValid() { return zid, nil } return api.InvalidZID, err } // CreateZettelData creates a new zettel and returns its URL. // // data contains the zettel date, encoded as explicit struct. func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (api.ZettelID, error) { var buf bytes.Buffer if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil { return api.InvalidZID, err } ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData) resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf) if err != nil { return api.InvalidZID, err } defer resp.Body.Close() rdr := sxreader.MakeReader(resp.Body) obj, err := rdr.Read() if resp.StatusCode != http.StatusCreated { return api.InvalidZID, statusToError(resp) } if err != nil { return api.InvalidZID, err } return makeZettelID(obj) } func makeZettelID(obj sx.Object) (api.ZettelID, error) { val, isInt64 := obj.(sx.Int64) if !isInt64 || val <= 0 { return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val) } sVal := strconv.FormatInt(int64(val), 10) if len(sVal) < 14 { sVal = "00000000000000"[0:14-len(sVal)] + sVal } zid := api.ZettelID(sVal) if !zid.IsValid() { return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val) } return zid, nil } // UpdateZettel updates an existing zettel, specified by its zettel identifier. // // data contains the zettel metadata and content, as it is stored in a file in a zettel box, // or as returned by [Client.GetZettel]. // Metadata is separated from zettel content by an empty line. func (c *Client) UpdateZettel(ctx context.Context, zid api.ZettelID, data []byte) error { ub := c.NewURLBuilder('z').SetZid(zid) resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data)) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // UpdateZettelData updates an existing zettel, specified by its zettel identifier. func (c *Client) UpdateZettelData(ctx context.Context, zid api.ZettelID, data api.ZettelData) error { var buf bytes.Buffer if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil { return err } ub := c.NewURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData) resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // DeleteZettel deletes a zettel with the given identifier. func (c *Client) DeleteZettel(ctx context.Context, zid api.ZettelID) error { ub := c.NewURLBuilder('z').SetZid(zid) resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // ExecuteCommand will execute a given command at the Zettelstore. // // See [API commands] for a list of valid commands. // // [API commands]: https://zettelstore.de/manual/h/00001012080100 func (c *Client) ExecuteCommand(ctx context.Context, command api.Command) error { ub := c.NewURLBuilder('x').AppendKVQuery(api.QueryKeyCommand, string(command)) resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } |
Added client/retrieve.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 | //----------------------------------------------------------------------------- // Copyright (c) 2021-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2021-present Detlef Stern //----------------------------------------------------------------------------- package client import ( "bufio" "bytes" "context" "fmt" "io" "net/http" "t73f.de/r/sx" "t73f.de/r/sx/sxreader" "t73f.de/r/zsc/api" "t73f.de/r/zsc/sexp" "t73f.de/r/zsc/sz" ) var bsLF = []byte{'\n'} // QueryZettel returns a list of all Zettel based on the given query. // // query is a search expression, as described in [Query the list of all zettel]. // // The functions returns a slice of bytes slices, where each byte slice contains the // zettel identifier within its first 14 bytes. The next byte is a space character, // followed by the title of the zettel. // // [Query the list of all zettel]: https://zettelstore.de/manual/h/00001012051400 func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) { ub := c.NewURLBuilder('z').AppendQuery(query) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return nil, err } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) switch resp.StatusCode { case http.StatusOK: case http.StatusNoContent: return nil, nil default: return nil, statusToError(resp) } 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 } // QueryZettelData returns a list of zettel metadata. // // query is a search expression, as described in [Query the list of all zettel]. // // The functions returns the normalized query and its human-readable representation as // its first two result values. // // [Query the list of all zettel]: https://zettelstore.de/manual/h/00001012051400 func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) { ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData).AppendQuery(query) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return "", "", nil, err } defer resp.Body.Close() rdr := sxreader.MakeReader(resp.Body) obj, err := rdr.Read() switch resp.StatusCode { case http.StatusOK: case http.StatusNoContent: return "", "", nil, nil default: return "", "", nil, statusToError(resp) } if err != nil { return "", "", nil, err } vals, err := sexp.ParseList(obj, "yppp") if err != nil { return "", "", nil, err } qVals, err := sexp.ParseList(vals[1], "ys") if err != nil { return "", "", nil, err } hVals, err := sexp.ParseList(vals[2], "ys") if err != nil { return "", "", nil, err } metaList, err := parseMetaList(vals[3].(*sx.Pair)) return sz.GoValue(qVals[1]), sz.GoValue(hVals[1]), metaList, err } func parseMetaList(metaPair *sx.Pair) ([]api.ZidMetaRights, error) { if metaPair == nil { return nil, fmt.Errorf("no zettel list") } if errSym := sexp.CheckSymbol(metaPair.Car(), "list"); errSym != nil { return nil, errSym } var result []api.ZidMetaRights for node := metaPair.Cdr(); !sx.IsNil(node); { elem, isPair := sx.GetPair(node) if !isPair { return nil, fmt.Errorf("meta-list not a proper list: %v", metaPair.String()) } node = elem.Cdr() vals, err := sexp.ParseList(elem.Car(), "yppp") if err != nil { return nil, err } if errSym := sexp.CheckSymbol(vals[0], "zettel"); errSym != nil { return nil, errSym } idVals, err := sexp.ParseList(vals[1], "yi") if err != nil { return nil, err } if errSym := sexp.CheckSymbol(idVals[0], "id"); errSym != nil { return nil, errSym } zid, err := makeZettelID(idVals[1]) if err != nil { return nil, err } meta, err := sexp.ParseMeta(vals[2].(*sx.Pair)) if err != nil { return nil, err } rights, err := sexp.ParseRights(vals[3]) if err != nil { return nil, err } result = append(result, api.ZidMetaRights{ ID: zid, Meta: meta, Rights: rights, }) } return result, nil } // QueryAggregate returns a aggregate as a result of a query. // It is most often used in a query with an action, where the action is either // a metadata key of type Word or of type TagSet. // // query is a search expression, as described in [Query the list of all zettel]. // It must contain an aggregate action. // // [Query the list of all zettel]: https://zettelstore.de/manual/h/00001012051400 func (c *Client) QueryAggregate(ctx context.Context, query string) (api.Aggregate, error) { lines, err := c.QueryZettel(ctx, query) if err != nil { return nil, err } if len(lines) == 0 { return nil, nil } agg := make(api.Aggregate, len(lines)) for _, line := range lines { if fields := bytes.Fields(line); len(fields) > 1 { key := string(fields[0]) for _, field := range fields[1:] { if zid := api.ZettelID(string(field)); zid.IsValid() { agg[key] = append(agg[key], zid) } } } } return agg, nil } // TagZettel returns the identifier of the tag zettel for a given tag. // // This method only works if c.AllowRedirect(true) was called. func (c *Client) TagZettel(ctx context.Context, tag string) (api.ZettelID, error) { return c.fetchTagOrRoleZettel(ctx, api.QueryKeyTag, tag) } // RoleZettel returns the identifier of the tag zettel for a given role. // // This method only works if c.AllowRedirect(true) was called. func (c *Client) RoleZettel(ctx context.Context, role string) (api.ZettelID, error) { return c.fetchTagOrRoleZettel(ctx, api.QueryKeyRole, role) } func (c *Client) fetchTagOrRoleZettel(ctx context.Context, key, val string) (api.ZettelID, error) { if c.client.CheckRedirect == nil { panic("client does not allow to track redirect") } ub := c.NewURLBuilder('z').AppendKVQuery(key, val) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return api.InvalidZID, err } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) if err != nil { return api.InvalidZID, err } switch resp.StatusCode { case http.StatusNotFound: return "", nil case http.StatusFound: zid := api.ZettelID(data) if zid.IsValid() { return zid, nil } return api.InvalidZID, nil default: return api.InvalidZID, statusToError(resp) } } // GetZettel returns a zettel as a byte slice. // // part must be one of "meta", "content", or "zettel". // // The format of the byte slice is described in [Layout of a zettel]. // // [Layout of a zettel]: https://zettelstore.de/manual/h/00001006000000 func (c *Client) GetZettel(ctx context.Context, zid api.ZettelID, part string) ([]byte, error) { ub := c.NewURLBuilder('z').SetZid(zid) if part != "" && part != api.PartContent { ub.AppendKVQuery(api.QueryKeyPart, part) } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return nil, err } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) switch resp.StatusCode { case http.StatusOK: case http.StatusNoContent: return nil, nil default: return nil, statusToError(resp) } return data, err } // GetZettelData returns a zettel as a struct of its parts. func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) { ub := c.NewURLBuilder('z').SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData) ub.AppendKVQuery(api.QueryKeyPart, api.PartZettel) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err == nil { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return api.ZettelData{}, statusToError(resp) } rdr := sxreader.MakeReader(resp.Body) obj, err2 := rdr.Read() if err2 == nil { return sexp.ParseZettel(obj) } } return api.ZettelData{}, err } // GetParsedZettel return a parsed zettel in a specified text-based encoding. // // A parsed zettel is just read from its box and is not processed any further. // // Valid encoding values are given as constants. They are described in more // detail in [Encodings available via the API]. // // [Encodings available via the API]: https://zettelstore.de/manual/h/00001012920500 func (c *Client) GetParsedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, zid, enc, true) } // GetEvaluatedZettel return an evaluated zettel in a specified text-based encoding. // // An evaluated zettel was parsed, and any transclusions etc. are resolved. // This is the zettel representation you typically see on the Web UI. // // Valid encoding values are given as constants. They are described in more // detail in [Encodings available via the API]. // // [Encodings available via the API]: https://zettelstore.de/manual/h/00001012920500 func (c *Client) GetEvaluatedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, zid, enc, false) } func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) { ub := c.NewURLBuilder('z').SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, enc.String()) ub.AppendKVQuery(api.QueryKeyPart, api.PartContent) if parseOnly { ub.AppendKVQuery(api.QueryKeyParseOnly, "") } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return nil, err } defer resp.Body.Close() switch resp.StatusCode { case http.StatusOK: case http.StatusNoContent: default: return nil, statusToError(resp) } return io.ReadAll(resp.Body) } // GetParsedSz returns a part of an parsed zettel as a Sexpr-decoded data structure. // // A parsed zettel is just read from its box and is not processed any further. // // part must be one of "meta", "content", or "zettel". // // Basically, this function returns the sz encoding of a part of a zettel. func (c *Client) GetParsedSz(ctx context.Context, zid api.ZettelID, part string) (sx.Object, error) { return c.getSz(ctx, zid, part, true) } // GetEvaluatedSz returns an evaluated zettel as a Sexpr-decoded data structure. // // An evaluated zettel was parsed, and any transclusions etc. are resolved. // This is the zettel representation you typically see on the Web UI. // // part must be one of "meta", "content", or "zettel". // // Basically, this function returns the sz encoding of a part of a zettel. func (c *Client) GetEvaluatedSz(ctx context.Context, zid api.ZettelID, part string) (sx.Object, error) { return c.getSz(ctx, zid, part, false) } func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool) (sx.Object, error) { ub := c.NewURLBuilder('z').SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSz) if part != "" { ub.AppendKVQuery(api.QueryKeyPart, part) } if parseOnly { ub.AppendKVQuery(api.QueryKeyParseOnly, "") } resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, statusToError(resp) } return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8)).Read() } // GetMetaData returns the metadata of a zettel. func (c *Client) GetMetaData(ctx context.Context, zid api.ZettelID) (api.MetaRights, error) { ub := c.NewURLBuilder('z').SetZid(zid) ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData) ub.AppendKVQuery(api.QueryKeyPart, api.PartMeta) resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return api.MetaRights{}, err } defer resp.Body.Close() rdr := sxreader.MakeReader(resp.Body) obj, err := rdr.Read() if resp.StatusCode != http.StatusOK { return api.MetaRights{}, statusToError(resp) } if err != nil { return api.MetaRights{}, err } vals, err := sexp.ParseList(obj, "ypp") if err != nil { return api.MetaRights{}, err } if errSym := sexp.CheckSymbol(vals[0], "list"); errSym != nil { return api.MetaRights{}, err } meta, err := sexp.ParseMeta(vals[1].(*sx.Pair)) if err != nil { return api.MetaRights{}, err } rights, err := sexp.ParseRights(vals[2]) if err != nil { return api.MetaRights{}, err } return api.MetaRights{ Meta: meta, Rights: rights, }, nil } // GetVersionInfo returns version information of the Zettelstore that is used. func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) { resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.NewURLBuilder('x'), nil) if err != nil { return VersionInfo{}, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return VersionInfo{}, statusToError(resp) } rdr := sxreader.MakeReader(resp.Body) obj, err := rdr.Read() if err == nil { if vals, errVals := sexp.ParseList(obj, "iiiss"); errVals == nil { return VersionInfo{ Major: int(vals[0].(sx.Int64)), Minor: int(vals[1].(sx.Int64)), Patch: int(vals[2].(sx.Int64)), Info: vals[3].(sx.String).GetValue(), Hash: vals[4].(sx.String).GetValue(), }, nil } } return VersionInfo{}, err } // VersionInfo contains version information of the associated Zettelstore. // // - Major is an integer containing the major software version of Zettelstore. // If its value is greater than zero, different major versions are not compatible. // - Minor is an integer specifying the minor software version for the given major version. // If the major version is greater than zero, minor versions are backward compatible. // - Patch is an integer that specifies a change within a minor version. // A version that have equal major and minor versions and differ in patch version are // always compatible, even if the major version equals zero. // - Info contains some optional text, i.e. it may be the empty string. Typically, Info // specifies a developer version by containing the string "dev". // - Hash contains the value of the source code version stored in the Zettelstore repository. // You can use it to reproduce bugs that occured, when source code was changed since // its introduction. type VersionInfo struct { Major int Minor int Patch int Info string Hash string } // GetApplicationZid returns the zettel identifier used to configure a client // application with the given name. func (c *Client) GetApplicationZid(ctx context.Context, appname string) (api.ZettelID, error) { mr, err := c.GetMetaData(ctx, api.ZidAppDirectory) if err != nil { return api.InvalidZID, err } key := appname + "-zid" val, found := mr.Meta[key] if !found { return api.InvalidZID, fmt.Errorf("no application registered: %v", appname) } if zid := api.ZettelID(val); zid.IsValid() { return zid, nil } return api.InvalidZID, fmt.Errorf("invalid identifier for application %v: %v", appname, val) } // Get executes a GET request to the given URL and returns the read data. func (c *Client) Get(ctx context.Context, ub *api.URLBuilder) ([]byte, error) { resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil) if err != nil { return nil, err } defer resp.Body.Close() switch resp.StatusCode { case http.StatusOK: case http.StatusNoContent: return nil, nil default: return nil, statusToError(resp) } data, err := io.ReadAll(resp.Body) return data, err } |
Changes to go.mod.
1 2 | module t73f.de/r/zsc | | | | | | 1 2 3 4 5 6 7 8 9 | module t73f.de/r/zsc go 1.23 require ( t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5 t73f.de/r/sxwebs v0.0.0-20241031144449-53c3b2ed1a6f t73f.de/r/webs v0.0.0-20241031141359-cd4f76a622cd ) |
Changes to go.sum.
|
| | | | | | | | 1 2 3 4 5 6 | t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5 h1:ug4hohM6pK28M8Uo0o3+XvjBure2wfEtuCnHVIdqBZY= t73f.de/r/sx v0.0.0-20240814083626-4df0ec6454b5/go.mod h1:VRvsWoBErPKvMieDMMk1hsh1tb9sA4ijEQWGw/TbtQ0= t73f.de/r/sxwebs v0.0.0-20241031144449-53c3b2ed1a6f h1:VJ4S7YWy9tCJuFz5MckqUjjktPaf0kpnTkNBVRVXpo4= t73f.de/r/sxwebs v0.0.0-20241031144449-53c3b2ed1a6f/go.mod h1:IaM+U+LvYTYeuiIS5cwZW6kcEpdwoKBYVCU7LZr4Sgk= t73f.de/r/webs v0.0.0-20241031141359-cd4f76a622cd h1:+7cqJonXKDso+uPvsvOPl7BiLkhj8VQT/Has8qC5VIQ= t73f.de/r/webs v0.0.0-20241031141359-cd4f76a622cd/go.mod h1:NSoOON8be62MfQZzlCApK27Jt2zhIa6Vrmo9RJ4tOnQ= |
Changes to input/input.go.
︙ | ︙ | |||
93 94 95 96 97 98 99 | inp.Next() return true } return false } // IsEOLEOS returns true if char is either EOS or EOL. | | < < < < < < | 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | inp.Next() return true } return false } // IsEOLEOS returns true if char is either EOS or EOL. func IsEOLEOS(ch rune) bool { return ch == EOS || ch == '\n' || ch == '\r' } // EatEOL transforms both "\r" and "\r\n" into "\n". func (inp *Input) EatEOL() { switch inp.Ch { case '\r': if inp.Peek() == '\n' { inp.Next() |
︙ | ︙ | |||
122 123 124 125 126 127 128 129 130 131 132 133 134 135 | // SetPos allows to reset the read position. func (inp *Input) SetPos(pos int) { if inp.Pos != pos { inp.readPos = pos inp.Next() } } // SkipToEOL reads until the next end-of-line. func (inp *Input) SkipToEOL() { for { switch inp.Ch { case EOS, '\n', '\r': return | > > > > > > > | 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // SetPos allows to reset the read position. func (inp *Input) SetPos(pos int) { if inp.Pos != pos { inp.readPos = pos inp.Next() } } // SkipSpace reads while the current character is not a space character. func (inp *Input) SkipSpace() { for ch := inp.Ch; IsSpace(ch); { ch = inp.Next() } } // SkipToEOL reads until the next end-of-line. func (inp *Input) SkipToEOL() { for { switch inp.Ch { case EOS, '\n', '\r': return |
︙ | ︙ |
Changes to input/runes.go.
︙ | ︙ | |||
21 22 23 24 25 26 27 | case ' ', '\t': return true case '\n', '\r', EOS: return false } return unicode.IsSpace(ch) } | > > > | 21 22 23 24 25 26 27 28 29 30 | case ' ', '\t': return true case '\n', '\r', EOS: return false } return unicode.IsSpace(ch) } // IsSpace returns true if current character is a whitespace. func (inp *Input) IsSpace() bool { return IsSpace(inp.Ch) } |
Changes to maps/maps.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 | //----------------------------------------------------------------------------- // Copyright (c) 2022-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2022-present Detlef Stern //----------------------------------------------------------------------------- package maps import "sort" func Keys[T any](m map[string]T) []string { if len(m) == 0 { return nil } result := make([]string, 0, len(m)) for k := range m { result = append(result, k) | > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //----------------------------------------------------------------------------- // Copyright (c) 2022-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2022-present Detlef Stern //----------------------------------------------------------------------------- // Package maps provides utility functions to work with maps. package maps import "sort" // Keys returns the sorted list of string keys of the given map. func Keys[T any](m map[string]T) []string { if len(m) == 0 { return nil } result := make([]string, 0, len(m)) for k := range m { result = append(result, k) |
︙ | ︙ |
Changes to sexp/sexp.go.
︙ | ︙ | |||
31 32 33 34 35 36 37 38 39 40 41 42 43 44 | meta2sz(zettel.Meta), sx.MakeList(sx.MakeSymbol("rights"), sx.Int64(int64(zettel.Rights))), sx.MakeList(sx.MakeSymbol("encoding"), sx.MakeString(zettel.Encoding)), sx.MakeList(sx.MakeSymbol("content"), sx.MakeString(zettel.Content)), ) } func ParseZettel(obj sx.Object) (api.ZettelData, error) { vals, err := ParseList(obj, "ypppp") if err != nil { return api.ZettelData{}, err } if errSym := CheckSymbol(vals[0], "zettel"); errSym != nil { return api.ZettelData{}, errSym | > | 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | meta2sz(zettel.Meta), sx.MakeList(sx.MakeSymbol("rights"), sx.Int64(int64(zettel.Rights))), sx.MakeList(sx.MakeSymbol("encoding"), sx.MakeString(zettel.Encoding)), sx.MakeList(sx.MakeSymbol("content"), sx.MakeString(zettel.Content)), ) } // ParseZettel parses an object to contain all needed data for a zettel. func ParseZettel(obj sx.Object) (api.ZettelData, error) { vals, err := ParseList(obj, "ypppp") if err != nil { return api.ZettelData{}, err } if errSym := CheckSymbol(vals[0], "zettel"); errSym != nil { return api.ZettelData{}, errSym |
︙ | ︙ | |||
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | } if i < len(spec) { return nil, ErrElementsMissing } return result, nil } var ErrElementsMissing = errors.New("spec contains more data") var ErrNoSpec = errors.New("no spec for elements") // CheckSymbol ensures that the given object is a symbol with the given name. func CheckSymbol(obj sx.Object, name string) error { sym, isSymbol := sx.GetSymbol(obj) if !isSymbol { return fmt.Errorf("object %v/%T is not a symbol", obj, obj) } if got := sym.GetValue(); got != name { return fmt.Errorf("symbol %q expected, but got: %q", name, got) } return nil } | > > > > > | 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 | } if i < len(spec) { return nil, ErrElementsMissing } return result, nil } // ErrElementsMissing is returned, // if ParseList is called with a list smaller than the number of type specifications. var ErrElementsMissing = errors.New("spec contains more data") // ErrNoSpec is returned, // if ParseList if called with a list greater than the number of type specifications. var ErrNoSpec = errors.New("no spec for elements") // CheckSymbol ensures that the given object is a symbol with the given name. func CheckSymbol(obj sx.Object, name string) error { sym, isSymbol := sx.GetSymbol(obj) if !isSymbol { return fmt.Errorf("object %v/%T is not a symbol", obj, obj) } if got := sym.GetValue(); got != name { return fmt.Errorf("symbol %q expected, but got: %q", name, got) } return nil } |
Changes to shtml/const.go.
︙ | ︙ | |||
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | SymSTRONG = sx.MakeSymbol("strong") symSUB = sx.MakeSymbol("sub") symSUP = sx.MakeSymbol("sup") symTABLE = sx.MakeSymbol("table") symTBODY = sx.MakeSymbol("tbody") symTHEAD = sx.MakeSymbol("thead") symTD = sx.MakeSymbol("td") symTR = sx.MakeSymbol("tr") SymUL = sx.MakeSymbol("ul") ) // Symbols for HTML attribute keys var ( symAttrAlt = sx.MakeSymbol("alt") | > | 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | SymSTRONG = sx.MakeSymbol("strong") symSUB = sx.MakeSymbol("sub") symSUP = sx.MakeSymbol("sup") symTABLE = sx.MakeSymbol("table") symTBODY = sx.MakeSymbol("tbody") symTHEAD = sx.MakeSymbol("thead") symTD = sx.MakeSymbol("td") symTH = sx.MakeSymbol("th") symTR = sx.MakeSymbol("tr") SymUL = sx.MakeSymbol("ul") ) // Symbols for HTML attribute keys var ( symAttrAlt = sx.MakeSymbol("alt") |
︙ | ︙ |
Added shtml/lang.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) 2023-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2023-present Detlef Stern //----------------------------------------------------------------------------- package shtml import ( "strings" "t73f.de/r/zsc/api" ) // LangStack is a stack to store the nesting of "lang" attribute values. // It is used to generate typographically correct quotes. type LangStack []string // NewLangStack creates a new language stack. func NewLangStack(lang string) LangStack { ls := make([]string, 1, 16) ls[0] = lang return ls } // Reset restores the language stack to its initial value. func (ls *LangStack) Reset() { *ls = (*ls)[0:1] } // Push adds a new language value. func (ls *LangStack) Push(lang string) { *ls = append(*ls, lang) } // Pop removes the topmost language value. func (ls *LangStack) Pop() { *ls = (*ls)[0 : len(*ls)-1] } // Top returns the topmost language value. func (ls *LangStack) Top() string { return (*ls)[len(*ls)-1] } // Dup duplicates the topmost language value. func (ls *LangStack) Dup() { *ls = append(*ls, (*ls)[len(*ls)-1]) } // QuoteInfo contains language specific data about quotes. type QuoteInfo struct { primLeft, primRight string secLeft, secRight string nbsp bool } // GetPrimary returns the primary left and right quote entity. func (qi *QuoteInfo) GetPrimary() (string, string) { return qi.primLeft, qi.primRight } // GetSecondary returns the secondary left and right quote entity. func (qi *QuoteInfo) GetSecondary() (string, string) { return qi.secLeft, qi.secRight } // GetQuotes returns quotes based on a nesting level. func (qi *QuoteInfo) GetQuotes(level uint) (string, string) { if level%2 == 0 { return qi.GetPrimary() } return qi.GetSecondary() } // GetNBSp returns true, if there must be a non-breaking space between the // quote entities and the quoted text. func (qi *QuoteInfo) GetNBSp() bool { return qi.nbsp } var langQuotes = map[string]*QuoteInfo{ "": {""", """, """, """, false}, api.ValueLangEN: {"“", "”", "‘", "’", false}, "de": {"„", "“", "‚", "‘", false}, "fr": {"«", "»", "‹", "›", true}, } // GetQuoteInfo returns language specific data about quotes. func GetQuoteInfo(lang string) *QuoteInfo { langFields := strings.FieldsFunc(lang, func(r rune) bool { return r == '-' || r == '_' }) for len(langFields) > 0 { langSup := strings.Join(langFields, "-") quotes, ok := langQuotes[langSup] if ok { return quotes } langFields = langFields[0 : len(langFields)-1] } return langQuotes[""] } |
Changes to shtml/shtml.go.
︙ | ︙ | |||
50 51 52 53 54 55 56 | ev.bindMetadata() ev.bindBlocks() ev.bindInlines() return ev } // SetUnique sets a prefix to make several HTML ids unique. | | | | | | 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 | ev.bindMetadata() ev.bindBlocks() ev.bindInlines() return ev } // SetUnique sets a prefix to make several HTML ids unique. func (ev *Evaluator) SetUnique(s string) { ev.unique = s } // IsValidName returns true, if name is a valid symbol name. func isValidName(s string) bool { return s != "" } // EvaluateAttrbute transforms the given attributes into a HTML s-expression. func EvaluateAttrbute(a attrs.Attributes) *sx.Pair { if len(a) == 0 { return nil } plist := sx.Nil() keys := a.Keys() for i := len(keys) - 1; i >= 0; i-- { key := keys[i] if key != attrs.DefaultAttribute && isValidName(key) { plist = plist.Cons(sx.Cons(sx.MakeSymbol(key), sx.MakeString(a[key]))) } } if plist == nil { return nil } return plist.Cons(sxhtml.SymAttr) |
︙ | ︙ | |||
113 114 115 116 117 118 119 | if err := env.err; err != nil { return nil, err } return result.List(), nil } // Endnotes returns a SHTML object with all collected endnotes. | | | 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | if err := env.err; err != nil { return nil, err } return result.List(), nil } // Endnotes returns a SHTML object with all collected endnotes. func Endnotes(env *Environment) *sx.Pair { if env.err != nil || len(env.endnotes) == 0 { return nil } var result sx.ListBuilder result.Add(SymOL) result.Add(sx.Nil().Cons(sx.Cons(SymAttrClass, sx.MakeString("zs-endnotes"))).Cons(sxhtml.SymAttr)) |
︙ | ︙ | |||
151 152 153 154 155 156 157 | } return result.List() } // Environment where sz objects are evaluated to shtml objects type Environment struct { err error | | < < | | | | | | | < | < | | | > > > > | 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 | } return result.List() } // Environment where sz objects are evaluated to shtml objects type Environment struct { err error langStack LangStack endnotes []endnoteInfo quoteNesting uint } type endnoteInfo struct { noteID string // link id noteAST sx.Vector // Endnote as list of AST inline elements attrs *sx.Pair // attrs a-list noteHx *sx.Pair // Endnote as SxHTML } // MakeEnvironment builds a new evaluation environment. func MakeEnvironment(lang string) Environment { return Environment{ err: nil, langStack: NewLangStack(lang), endnotes: nil, quoteNesting: 0, } } // GetError returns the last error found. func (env *Environment) GetError() error { return env.err } // Reset the environment. func (env *Environment) Reset() { env.langStack.Reset() env.endnotes = nil env.quoteNesting = 0 } // pushAttribute adds the current attributes to the environment. func (env *Environment) pushAttributes(a attrs.Attributes) { if value, ok := a.Get("lang"); ok { env.langStack.Push(value) } else { env.langStack.Dup() } } // popAttributes removes the current attributes from the envrionment. func (env *Environment) popAttributes() { env.langStack.Pop() } // getLanguage returns the current language. func (env *Environment) getLanguage() string { return env.langStack.Top() } func (env *Environment) getQuotes() (string, string, bool) { qi := GetQuoteInfo(env.getLanguage()) leftQ, rightQ := qi.GetQuotes(env.quoteNesting) return leftQ, rightQ, qi.GetNBSp() } // EvalFn is a function to be called for evaluation. type EvalFn func(sx.Vector, *Environment) sx.Object func (ev *Evaluator) bind(sym *sx.Symbol, minArgs int, fn EvalFn) { symVal := sym.GetValue() |
︙ | ︙ | |||
235 236 237 238 239 240 241 | ev.fns[symVal] = fn } func (ev *Evaluator) bindMetadata() { ev.bind(sz.SymMeta, 0, ev.evalList) evalMetaString := func(args sx.Vector, env *Environment) sx.Object { a := make(attrs.Attributes, 2). | | | 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | ev.fns[symVal] = fn } func (ev *Evaluator) bindMetadata() { ev.bind(sz.SymMeta, 0, ev.evalList) evalMetaString := func(args sx.Vector, env *Environment) sx.Object { a := make(attrs.Attributes, 2). Set("name", getSymbol(args[0], env).GetValue()). Set("content", getString(args[1], env).GetValue()) return ev.EvaluateMeta(a) } ev.bind(sz.SymTypeCredential, 2, evalMetaString) ev.bind(sz.SymTypeEmpty, 2, evalMetaString) ev.bind(sz.SymTypeID, 2, evalMetaString) ev.bind(sz.SymTypeNumber, 2, evalMetaString) |
︙ | ︙ | |||
259 260 261 262 263 264 265 | sb.WriteString(getString(elem.Car(), env).GetValue()) } s := sb.String() if len(s) > 0 { s = s[1:] } a := make(attrs.Attributes, 2). | | | | | | | | | 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 | sb.WriteString(getString(elem.Car(), env).GetValue()) } s := sb.String() if len(s) > 0 { s = s[1:] } a := make(attrs.Attributes, 2). Set("name", getSymbol(args[0], env).GetValue()). Set("content", s) return ev.EvaluateMeta(a) } ev.bind(sz.SymTypeIDSet, 2, evalMetaSet) ev.bind(sz.SymTypeTagSet, 2, evalMetaSet) ev.bind(sz.SymTypeZettelmarkup, 2, func(args sx.Vector, env *Environment) sx.Object { a := make(attrs.Attributes, 2). Set("name", getSymbol(args[0], env).GetValue()). Set("content", text.EvaluateInlineString(getList(args[1], env))) return ev.EvaluateMeta(a) }) } // EvaluateMeta returns HTML meta object for an attribute. func (ev *Evaluator) EvaluateMeta(a attrs.Attributes) *sx.Pair { return sx.Nil().Cons(EvaluateAttrbute(a)).Cons(SymMeta) } func (ev *Evaluator) bindBlocks() { ev.bind(sz.SymBlock, 0, ev.evalList) ev.bind(sz.SymPara, 0, func(args sx.Vector, env *Environment) sx.Object { return ev.evalSlice(args, env).Cons(SymP) }) ev.bind(sz.SymHeading, 5, func(args sx.Vector, env *Environment) sx.Object { nLevel := getInt64(args[0], env) if nLevel <= 0 { env.err = fmt.Errorf("%v is a negative heading level", nLevel) return sx.Nil() } level := strconv.FormatInt(nLevel+ev.headingOffset, 10) headingSymbol := sx.MakeSymbol("h" + level) a := GetAttributes(args[1], env) env.pushAttributes(a) defer env.popAttributes() if fragment := getString(args[3], env).GetValue(); fragment != "" { a = a.Set("id", ev.unique+fragment) } if result, _ := ev.EvaluateList(args[4:], env); result != nil { if len(a) > 0 { result = result.Cons(EvaluateAttrbute(a)) } return result.Cons(headingSymbol) } return sx.MakeList(headingSymbol, sx.MakeString("<MISSING TEXT>")) }) ev.bind(sz.SymThematic, 0, func(args sx.Vector, env *Environment) sx.Object { result := sx.Nil() if len(args) > 0 { if attrList := getList(args[0], env); attrList != nil { result = result.Cons(EvaluateAttrbute(sz.GetAttributes(attrList))) } } return result.Cons(SymHR) }) ev.bind(sz.SymListOrdered, 0, ev.makeListFn(SymOL)) ev.bind(sz.SymListUnordered, 0, ev.makeListFn(SymUL)) |
︙ | ︙ | |||
360 361 362 363 364 365 366 | } return result.List() }) ev.bind(sz.SymTable, 1, func(args sx.Vector, env *Environment) sx.Object { thead := sx.Nil() if header := getList(args[0], env); !sx.IsNil(header) { | | | | 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | } return result.List() }) ev.bind(sz.SymTable, 1, func(args sx.Vector, env *Environment) sx.Object { thead := sx.Nil() if header := getList(args[0], env); !sx.IsNil(header) { thead = sx.Nil().Cons(ev.evalTableRow(symTH, header, env)).Cons(symTHEAD) } var tbody sx.ListBuilder if len(args) > 1 { tbody.Add(symTBODY) for _, row := range args[1:] { tbody.Add(ev.evalTableRow(symTD, getList(row, env), env)) } } table := sx.Nil() if !tbody.IsEmpty() { table = table.Cons(tbody.List()) } |
︙ | ︙ | |||
393 394 395 396 397 398 399 | ev.bind(sz.SymCellRight, 0, ev.makeCellFn("right")) ev.bind(sz.SymRegionBlock, 2, ev.makeRegionFn(SymDIV, true)) ev.bind(sz.SymRegionQuote, 2, ev.makeRegionFn(symBLOCKQUOTE, false)) ev.bind(sz.SymRegionVerse, 2, ev.makeRegionFn(SymDIV, false)) ev.bind(sz.SymVerbatimComment, 1, func(args sx.Vector, env *Environment) sx.Object { | | | | | | | | | | | 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 | ev.bind(sz.SymCellRight, 0, ev.makeCellFn("right")) ev.bind(sz.SymRegionBlock, 2, ev.makeRegionFn(SymDIV, true)) ev.bind(sz.SymRegionQuote, 2, ev.makeRegionFn(symBLOCKQUOTE, false)) ev.bind(sz.SymRegionVerse, 2, ev.makeRegionFn(SymDIV, false)) ev.bind(sz.SymVerbatimComment, 1, func(args sx.Vector, env *Environment) sx.Object { if GetAttributes(args[0], env).HasDefault() { if len(args) > 1 { if s := getString(args[1], env); s.GetValue() != "" { return sx.Nil().Cons(s).Cons(sxhtml.SymBlockComment) } } } return nil }) ev.bind(sz.SymVerbatimEval, 2, func(args sx.Vector, env *Environment) sx.Object { return evalVerbatim(GetAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env)) }) ev.bind(sz.SymVerbatimHTML, 2, ev.evalHTML) ev.bind(sz.SymVerbatimMath, 2, func(args sx.Vector, env *Environment) sx.Object { return evalVerbatim(GetAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env)) }) ev.bind(sz.SymVerbatimProg, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) content := getString(args[1], env) if a.HasDefault() { content = sx.MakeString(visibleReplacer.Replace(content.GetValue())) } return evalVerbatim(a, content) }) ev.bind(sz.SymVerbatimZettel, 0, nilFn) ev.bind(sz.SymBLOB, 3, func(args sx.Vector, env *Environment) sx.Object { return evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env)) }) ev.bind(sz.SymTransclude, 2, func(args sx.Vector, env *Environment) sx.Object { ref, isPair := sx.GetPair(args[1]) if !isPair { return sx.Nil() } refKind := ref.Car() if sx.IsNil(refKind) { return sx.Nil() } if refValue := getString(ref.Tail().Car(), env); refValue.GetValue() != "" { if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.IsEqualSymbol(sz.SymRefStateExternal) { a := GetAttributes(args[0], env).Set("src", refValue.GetValue()).AddClass("external") return sx.Nil().Cons(sx.Nil().Cons(EvaluateAttrbute(a)).Cons(SymIMG)).Cons(SymP) } return sx.MakeList( sxhtml.SymInlineComment, sx.MakeString("transclude"), refKind, sx.MakeString("->"), refValue, |
︙ | ︙ | |||
471 472 473 474 475 476 477 | for node := term; node != nil; node = node.Tail() { elem := ev.Eval(node.Car(), env) result.Add(elem) } return result.List() } | | | | | | | | | | | | | | | < < < < < < < < < < < < < < < | | | | | | | | | | 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 | for node := term; node != nil; node = node.Tail() { elem := ev.Eval(node.Car(), env) result.Add(elem) } return result.List() } func (ev *Evaluator) evalTableRow(sym *sx.Symbol, pairs *sx.Pair, env *Environment) *sx.Pair { if pairs == nil { return nil } var row sx.ListBuilder row.Add(symTR) for pair := pairs; pair != nil; pair = pair.Tail() { row.Add(sx.Cons(sym, ev.Eval(pair.Car(), env))) } return row.List() } func (ev *Evaluator) makeCellFn(align string) EvalFn { return func(args sx.Vector, env *Environment) sx.Object { tdata := ev.evalSlice(args, env) if align != "" { tdata = tdata.Cons(EvaluateAttrbute(attrs.Attributes{"class": align})) } return tdata } } func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn { return func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() if genericToClass { if val, found := a.Get(""); found { a = a.Remove("").AddClass(val) } } var result sx.ListBuilder result.Add(sym) if len(a) > 0 { result.Add(EvaluateAttrbute(a)) } if region, isPair := sx.GetPair(args[1]); isPair { if evalRegion := ev.EvalPairList(region, env); evalRegion != nil { result.ExtendBang(evalRegion) } } if len(args) > 2 { if cite, _ := ev.EvaluateList(args[2:], env); cite != nil { result.Add(cite.Cons(symCITE)) } } return result.List() } } func evalVerbatim(a attrs.Attributes, s sx.String) sx.Object { a = setProgLang(a) code := sx.Nil().Cons(s) if al := EvaluateAttrbute(a); al != nil { code = code.Cons(al) } code = code.Cons(symCODE) return sx.Nil().Cons(code).Cons(symPRE) } func (ev *Evaluator) bindInlines() { ev.bind(sz.SymInline, 0, ev.evalList) ev.bind(sz.SymText, 1, func(args sx.Vector, env *Environment) sx.Object { return getString(args[0], env) }) ev.bind(sz.SymSoft, 0, func(sx.Vector, *Environment) sx.Object { return sx.MakeString(" ") }) ev.bind(sz.SymHard, 0, func(sx.Vector, *Environment) sx.Object { return sx.Nil().Cons(symBR) }) ev.bind(sz.SymLinkInvalid, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() var inline *sx.Pair if len(args) > 2 { inline = ev.evalSlice(args[2:], env) } if inline == nil { inline = sx.Nil().Cons(ev.Eval(args[1], env)) } return inline.Cons(SymSPAN) }) evalHREF := func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() refValue := getString(args[1], env) return ev.evalLink(a.Set("href", refValue.GetValue()), refValue, args[2:], env) } ev.bind(sz.SymLinkZettel, 2, evalHREF) ev.bind(sz.SymLinkSelf, 2, evalHREF) ev.bind(sz.SymLinkFound, 2, evalHREF) ev.bind(sz.SymLinkBroken, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() refValue := getString(args[1], env) return ev.evalLink(a.AddClass("broken"), refValue, args[2:], env) }) ev.bind(sz.SymLinkHosted, 2, evalHREF) ev.bind(sz.SymLinkBased, 2, evalHREF) ev.bind(sz.SymLinkQuery, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() refValue := getString(args[1], env) query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.GetValue()) return ev.evalLink(a.Set("href", query), refValue, args[2:], env) }) ev.bind(sz.SymLinkExternal, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() refValue := getString(args[1], env) return ev.evalLink(a.Set("href", refValue.GetValue()).Add("rel", "external"), refValue, args[2:], env) }) ev.bind(sz.SymEmbed, 3, func(args sx.Vector, env *Environment) sx.Object { ref := getList(args[1], env) a := GetAttributes(args[0], env) a = a.Set("src", getString(ref.Tail().Car(), env).GetValue()) if len(args) > 3 { var sb strings.Builder flattenText(&sb, sx.MakeList(args[3:]...)) if d := sb.String(); d != "" { a = a.Set("alt", d) } } return sx.MakeList(SymIMG, EvaluateAttrbute(a)) }) ev.bind(sz.SymEmbedBLOB, 3, func(args sx.Vector, env *Environment) sx.Object { a, syntax, data := GetAttributes(args[0], env), getString(args[1], env), getString(args[2], env) summary, hasSummary := a.Get(api.KeySummary) if !hasSummary { summary = "" } return evalBLOB( sx.MakeList(sxhtml.SymListSplice, sx.MakeString(summary)), syntax, data, ) }) ev.bind(sz.SymCite, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() result := sx.Nil() if key := getString(args[1], env); key.GetValue() != "" { if len(args) > 2 { result = ev.evalSlice(args[2:], env).Cons(sx.MakeString(", ")) } result = result.Cons(key) } if len(a) > 0 { result = result.Cons(EvaluateAttrbute(a)) } if result == nil { return nil } return result.Cons(SymSPAN) }) ev.bind(sz.SymMark, 3, func(args sx.Vector, env *Environment) sx.Object { result := ev.evalSlice(args[3:], env) if !ev.noLinks { if fragment := getString(args[2], env).GetValue(); fragment != "" { a := attrs.Attributes{"id": fragment + ev.unique} return result.Cons(EvaluateAttrbute(a)).Cons(SymA) } } return result.Cons(SymSPAN) }) ev.bind(sz.SymEndnote, 1, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() attrPlist := sx.Nil() if len(a) > 0 { if attrs := EvaluateAttrbute(a); attrs != nil { attrPlist = attrs.Tail() } } noteNum := strconv.Itoa(len(env.endnotes) + 1) noteID := ev.unique + noteNum env.endnotes = append(env.endnotes, endnoteInfo{ |
︙ | ︙ | |||
690 691 692 693 694 695 696 | ev.bind(sz.SymFormatQuote, 1, ev.evalQuote) ev.bind(sz.SymFormatSpan, 1, ev.makeFormatFn(SymSPAN)) ev.bind(sz.SymFormatStrong, 1, ev.makeFormatFn(SymSTRONG)) ev.bind(sz.SymFormatSub, 1, ev.makeFormatFn(symSUB)) ev.bind(sz.SymFormatSuper, 1, ev.makeFormatFn(symSUP)) ev.bind(sz.SymLiteralComment, 1, func(args sx.Vector, env *Environment) sx.Object { | | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | | | | | | | | 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | ev.bind(sz.SymFormatQuote, 1, ev.evalQuote) ev.bind(sz.SymFormatSpan, 1, ev.makeFormatFn(SymSPAN)) ev.bind(sz.SymFormatStrong, 1, ev.makeFormatFn(SymSTRONG)) ev.bind(sz.SymFormatSub, 1, ev.makeFormatFn(symSUB)) ev.bind(sz.SymFormatSuper, 1, ev.makeFormatFn(symSUP)) ev.bind(sz.SymLiteralComment, 1, func(args sx.Vector, env *Environment) sx.Object { if GetAttributes(args[0], env).HasDefault() { if len(args) > 1 { if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" { return sx.Nil().Cons(s).Cons(sxhtml.SymInlineComment) } } } return sx.Nil() }) ev.bind(sz.SymLiteralHTML, 2, ev.evalHTML) ev.bind(sz.SymLiteralInput, 2, func(args sx.Vector, env *Environment) sx.Object { return evalLiteral(args, nil, symKBD, env) }) ev.bind(sz.SymLiteralMath, 2, func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env).AddClass("zs-math") return evalLiteral(args, a, symCODE, env) }) ev.bind(sz.SymLiteralOutput, 2, func(args sx.Vector, env *Environment) sx.Object { return evalLiteral(args, nil, symSAMP, env) }) ev.bind(sz.SymLiteralProg, 2, func(args sx.Vector, env *Environment) sx.Object { return evalLiteral(args, nil, symCODE, env) }) ev.bind(sz.SymLiteralZettel, 0, nilFn) } func (ev *Evaluator) makeFormatFn(sym *sx.Symbol) EvalFn { return func(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() if val, hasClass := a.Get(""); hasClass { a = a.Remove("").AddClass(val) } res := ev.evalSlice(args[1:], env) if len(a) > 0 { res = res.Cons(EvaluateAttrbute(a)) } return res.Cons(sym) } } func (ev *Evaluator) evalQuote(args sx.Vector, env *Environment) sx.Object { a := GetAttributes(args[0], env) env.pushAttributes(a) defer env.popAttributes() if val, hasClass := a.Get(""); hasClass { a = a.Remove("").AddClass(val) } leftQ, rightQ, withNbsp := env.getQuotes() env.quoteNesting++ res := ev.evalSlice(args[1:], env) env.quoteNesting-- lastPair := res.LastPair() if lastPair.IsNil() { res = sx.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString(rightQ)), sx.Nil()) } else { if withNbsp { lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(" "), sx.MakeString(rightQ))) res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ), sx.MakeString(" "))) } else { lastPair.AppendBang(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(rightQ))) res = res.Cons(sx.MakeList(sxhtml.SymNoEscape, sx.MakeString(leftQ))) } } if len(a) > 0 { res = res.Cons(EvaluateAttrbute(a)) return res.Cons(SymSPAN) } return res.Cons(sxhtml.SymListSplice) } var visibleReplacer = strings.NewReplacer(" ", "\u2423") func evalLiteral(args sx.Vector, a attrs.Attributes, sym *sx.Symbol, env *Environment) sx.Object { if a == nil { a = GetAttributes(args[0], env) } a = setProgLang(a) literal := getString(args[1], env).GetValue() if a.HasDefault() { a = a.RemoveDefault() literal = visibleReplacer.Replace(literal) } res := sx.Nil().Cons(sx.MakeString(literal)) if len(a) > 0 { res = res.Cons(EvaluateAttrbute(a)) } return res.Cons(sym) } func setProgLang(a attrs.Attributes) attrs.Attributes { if val, found := a.Get(""); found { a = a.AddClass("language-" + val).Remove("") } return a } func (ev *Evaluator) evalHTML(args sx.Vector, env *Environment) sx.Object { if s := getString(ev.Eval(args[1], env), env); s.GetValue() != "" && IsSafe(s.GetValue()) { return sx.Nil().Cons(s).Cons(sxhtml.SymNoEscape) } return nil } func evalBLOB(description *sx.Pair, syntax, data sx.String) sx.Object { if data.GetValue() == "" { return sx.Nil() } switch syntax.GetValue() { case "": return sx.Nil() case api.ValueSyntaxSVG: |
︙ | ︙ | |||
925 926 927 928 929 930 931 | } if env.err == nil { return result.List() } return nil } | | | | | 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | } if env.err == nil { return result.List() } return nil } // EvalPairList evaluates a list of lists. func (ev *Evaluator) EvalPairList(pair *sx.Pair, env *Environment) *sx.Pair { var result sx.ListBuilder for node := pair; node != nil; node = node.Tail() { elem := ev.Eval(node.Car(), env) result.Add(elem) } if env.err == nil { return result.List() } return nil } func (ev *Evaluator) evalLink(a attrs.Attributes, refValue sx.String, inline sx.Vector, env *Environment) sx.Object { result := ev.evalSlice(inline, env) if len(inline) == 0 { result = sx.Nil().Cons(refValue) } if ev.noLinks { return result.Cons(SymSPAN) } return result.Cons(EvaluateAttrbute(a)).Cons(SymA) } func getSymbol(obj sx.Object, env *Environment) *sx.Symbol { if env.err == nil { if sym, ok := sx.GetSymbol(obj); ok { return sym } env.err = fmt.Errorf("%v/%T is not a symbol", obj, obj) } return sx.MakeSymbol("???") |
︙ | ︙ | |||
990 991 992 993 994 995 996 | } env.err = fmt.Errorf("%v/%T is not a number", val, val) return -1017 } // GetAttributes evaluates the given arg in the given environment and returns // the contained attributes. | | | 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | } env.err = fmt.Errorf("%v/%T is not a number", val, val) return -1017 } // GetAttributes evaluates the given arg in the given environment and returns // the contained attributes. func GetAttributes(arg sx.Object, env *Environment) attrs.Attributes { return sz.GetAttributes(getList(arg, env)) } var unsafeSnippets = []string{ "<script", "</script", "<iframe", "</iframe", } |
︙ | ︙ |
Changes to sz/const.go.
︙ | ︙ | |||
11 12 13 14 15 16 17 | // SPDX-FileCopyrightText: 2022-present Detlef Stern //----------------------------------------------------------------------------- package sz import "t73f.de/r/sx" | | < | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // SPDX-FileCopyrightText: 2022-present Detlef Stern //----------------------------------------------------------------------------- package sz import "t73f.de/r/sx" // Various constants for Zettel data. They are technically variables. var ( // Symbols for Metanodes SymBlock = sx.MakeSymbol("BLOCK") SymInline = sx.MakeSymbol("INLINE") SymMeta = sx.MakeSymbol("META") // Symbols for Zettel noMakede types. |
︙ | ︙ |
Changes to sz/sz.go.
︙ | ︙ | |||
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | } } return MakeMeta(m), nil } return nil, nil } type Meta map[string]MetaValue type MetaValue struct { Type string Key string Value sx.Object } func MakeMeta(obj sx.Object) Meta { if result := doMakeMeta(obj); len(result) > 0 { return result } return nil } func doMakeMeta(obj sx.Object) Meta { | > > > > > > > > | 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 | } } return MakeMeta(m), nil } return nil, nil } // Meta map metadata keys to MetaValue. type Meta map[string]MetaValue // MetaValue is an extended metadata value: // // - Type: the type assiciated with the metata key // - Key: the metadata key itself // - Value: the metadata value as an (sx-) object. type MetaValue struct { Type string Key string Value sx.Object } // MakeMeta build a Meta based on a list of metadata objects. func MakeMeta(obj sx.Object) Meta { if result := doMakeMeta(obj); len(result) > 0 { return result } return nil } func doMakeMeta(obj sx.Object) Meta { |
︙ | ︙ | |||
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | next = next.Tail() result.Type = typeSym.GetValue() result.Key = keySym.GetValue() result.Value = next.Car() return result, true } func (m Meta) GetString(key string) string { if v, found := m[key]; found { return GoValue(v.Value) } return "" } func (m Meta) GetPair(key string) *sx.Pair { if mv, found := m[key]; found { if pair, isPair := sx.GetPair(mv.Value); isPair { return pair } } return nil | > > > | 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | next = next.Tail() result.Type = typeSym.GetValue() result.Key = keySym.GetValue() result.Value = next.Car() return result, true } // GetString return the metadata string value associated with the given key. func (m Meta) GetString(key string) string { if v, found := m[key]; found { return GoValue(v.Value) } return "" } // GetPair return the metadata value associated with the given key, // as a list of objects. func (m Meta) GetPair(key string) *sx.Pair { if mv, found := m[key]; found { if pair, isPair := sx.GetPair(mv.Value); isPair { return pair } } return nil |
︙ | ︙ |
Changes to sz/walk.go.
︙ | ︙ | |||
62 63 64 65 66 67 68 | SymLinkExternal: walkChildrenInlines4, SymLinkFound: walkChildrenInlines4, SymLinkHosted: walkChildrenInlines4, SymLinkInvalid: walkChildrenInlines4, SymLinkQuery: walkChildrenInlines4, SymLinkSelf: walkChildrenInlines4, SymLinkZettel: walkChildrenInlines4, | | | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | SymLinkExternal: walkChildrenInlines4, SymLinkFound: walkChildrenInlines4, SymLinkHosted: walkChildrenInlines4, SymLinkInvalid: walkChildrenInlines4, SymLinkQuery: walkChildrenInlines4, SymLinkSelf: walkChildrenInlines4, SymLinkZettel: walkChildrenInlines4, SymEmbed: walkChildrenEmbed, SymCite: walkChildrenInlines4, SymFormatDelete: walkChildrenInlines3, SymFormatEmph: walkChildrenInlines3, SymFormatInsert: walkChildrenInlines3, SymFormatMark: walkChildrenInlines3, SymFormatQuote: walkChildrenInlines3, SymFormatStrong: walkChildrenInlines3, |
︙ | ︙ | |||
161 162 163 164 165 166 167 | } n.SetCar(Walk(v, n.Head(), env)) } return dn } func walkChildrenTable(v Visitor, tn *sx.Pair, env *sx.Pair) *sx.Pair { | < < | > > > > > > > > > > > > > > > > | 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 | } n.SetCar(Walk(v, n.Head(), env)) } return dn } func walkChildrenTable(v Visitor, tn *sx.Pair, env *sx.Pair) *sx.Pair { for row := tn.Tail(); row != nil; row = row.Tail() { row.SetCar(walkChildrenList(v, row.Head(), env)) } return tn } func walkChildrenMark(v Visitor, mn *sx.Pair, env *sx.Pair) *sx.Pair { // sym := mn.Car() next := mn.Tail() // mark := next.Car() next = next.Tail() // slug := next.Car() next = next.Tail() // fragment := next.Car() next.SetCdr(walkChildrenList(v, next.Tail(), env)) return mn } func walkChildrenEmbed(v Visitor, en *sx.Pair, env *sx.Pair) *sx.Pair { // sym := en.Car() next := en.Tail() // attr := next.Car() next = next.Tail() // ref := next.Car() next = next.Tail() // syntax := next.Car() next = next.Tail() if next != nil { // text := next.Car() next.SetCar(Walk(v, next.Head(), env)) } return en } func walkChildrenInlines4(v Visitor, ln *sx.Pair, env *sx.Pair) *sx.Pair { // sym := ln.Car() next := ln.Tail() // attrs := next.Car() next = next.Tail() // val3 := next.Car() |
︙ | ︙ |
Changes to sz/zmk/block.go.
︙ | ︙ | |||
270 271 272 273 274 275 276 277 | lastPara = bn } } } // parseRegionLastLine parses the last line of a region and returns its inline text. func (cp *zmkP) parseRegionLastLine() *sx.Pair { cp.clearStacked() // remove any lists defined in the region | > | | | 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | lastPara = bn } } } // parseRegionLastLine parses the last line of a region and returns its inline text. func (cp *zmkP) parseRegionLastLine() *sx.Pair { inp := cp.inp cp.clearStacked() // remove any lists defined in the region inp.SkipSpace() var region sx.ListBuilder for { switch inp.Ch { case input.EOS, '\n', '\r': return region.List() } in := cp.parseInline() if in == nil { return region.List() } |
︙ | ︙ | |||
297 298 299 300 301 302 303 | if delims < 3 { return nil, false } if inp.Ch != ' ' { return nil, false } inp.Next() | | | 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | if delims < 3 { return nil, false } if inp.Ch != ' ' { return nil, false } inp.Next() inp.SkipSpace() if delims > 7 { delims = 7 } level := int64(delims - 2) var attrs *sx.Pair var text sx.ListBuilder for { |
︙ | ︙ | |||
347 348 349 350 351 352 353 | // parseNestedList parses a list. func (cp *zmkP) parseNestedList() (res *sx.Pair, success bool) { kinds := cp.parseNestedListKinds() if len(kinds) == 0 { return nil, false } | > | | | 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | // parseNestedList parses a list. func (cp *zmkP) parseNestedList() (res *sx.Pair, success bool) { kinds := cp.parseNestedListKinds() if len(kinds) == 0 { return nil, false } inp := cp.inp inp.SkipSpace() if !kinds[len(kinds)-1].IsEqual(sz.SymListQuote) && input.IsEOLEOS(inp.Ch) { return nil, false } if len(kinds) < len(cp.lists) { cp.lists = cp.lists[:len(kinds)] } ln, newLnCount := cp.buildNestedList(kinds) |
︙ | ︙ | |||
443 444 445 446 447 448 449 | // parseDefTerm parses a term of a definition list. func (cp *zmkP) parseDefTerm() (res *sx.Pair, success bool) { inp := cp.inp if inp.Next() != ' ' { return nil, false } inp.Next() | | | 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | // parseDefTerm parses a term of a definition list. func (cp *zmkP) parseDefTerm() (res *sx.Pair, success bool) { inp := cp.inp if inp.Next() != ' ' { return nil, false } inp.Next() inp.SkipSpace() descrl := cp.descrl if descrl == nil { descrl = sx.Cons(sz.SymDescription, nil) cp.descrl = descrl res = descrl } lastPair, pos := lastPairPos(descrl) |
︙ | ︙ | |||
482 483 484 485 486 487 488 | // parseDefDescr parses a description of a definition list. func (cp *zmkP) parseDefDescr() (res *sx.Pair, success bool) { inp := cp.inp if inp.Next() != ' ' { return nil, false } inp.Next() | | | 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | // parseDefDescr parses a description of a definition list. func (cp *zmkP) parseDefDescr() (res *sx.Pair, success bool) { inp := cp.inp if inp.Next() != ' ' { return nil, false } inp.Next() inp.SkipSpace() descrl := cp.descrl lastPair, pos := lastPairPos(descrl) if descrl == nil || pos <= 0 { // No term given return nil, false } |
︙ | ︙ |
Changes to sz/zmk/inline.go.
︙ | ︙ | |||
146 147 148 149 150 151 152 | func hasQueryPrefix(src []byte) bool { return len(src) > len(api.QueryPrefix) && string(src[:len(api.QueryPrefix)]) == api.QueryPrefix } func (cp *zmkP) parseReference(openCh, closeCh rune) (ref string, text *sx.Pair, _ bool) { inp := cp.inp inp.Next() | | | 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | func hasQueryPrefix(src []byte) bool { return len(src) > len(api.QueryPrefix) && string(src[:len(api.QueryPrefix)]) == api.QueryPrefix } func (cp *zmkP) parseReference(openCh, closeCh rune) (ref string, text *sx.Pair, _ bool) { inp := cp.inp inp.Next() inp.SkipSpace() if inp.Ch == openCh { // Additional opening chars result in a fail return "", nil, false } var is sx.Vector pos := inp.Pos if !hasQueryPrefix(inp.Src[pos:]) { |
︙ | ︙ | |||
180 181 182 183 184 185 186 | if hasSpace { return "", nil, false } inp.SetPos(pos) } } | | | 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | if hasSpace { return "", nil, false } inp.SetPos(pos) } } inp.SkipSpace() pos = inp.Pos if !cp.readReferenceToClose(closeCh) { return "", nil, false } ref = strings.TrimSpace(string(inp.Src[pos:inp.Pos])) if inp.Next() != closeCh { return "", nil, false |
︙ | ︙ | |||
326 327 328 329 330 331 332 | Cons(sz.SymMark) return mn, true // Problematisch ist, dass hier noch nicht mn.Fragment und mn.Slug gesetzt werden. // Evtl. muss es ein PreMark-Symbol geben } func (cp *zmkP) parseLinkLikeRest() (*sx.Pair, bool) { | < > | 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | Cons(sz.SymMark) return mn, true // Problematisch ist, dass hier noch nicht mn.Fragment und mn.Slug gesetzt werden. // Evtl. muss es ein PreMark-Symbol geben } func (cp *zmkP) parseLinkLikeRest() (*sx.Pair, bool) { var ins sx.Vector inp := cp.inp inp.SkipSpace() for inp.Ch != ']' { in := cp.parseInline() if in == nil { return nil, false } ins = append(ins, in) if input.IsEOLEOS(inp.Ch) && sz.IsBreakSym(in.Car()) { |
︙ | ︙ | |||
355 356 357 358 359 360 361 | if inp.Next() != '%' { return nil, false } for inp.Ch == '%' { inp.Next() } attrs := cp.parseInlineAttributes() | | | 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | if inp.Next() != '%' { return nil, false } for inp.Ch == '%' { inp.Next() } attrs := cp.parseInlineAttributes() inp.SkipSpace() pos := inp.Pos for { if input.IsEOLEOS(inp.Ch) { return sx.MakeList( sz.SymLiteralComment, attrs, sx.MakeString(string(inp.Src[pos:inp.Pos])), |
︙ | ︙ | |||
457 458 459 460 461 462 463 | s := cp.parseString() sb.WriteString(s.GetValue()) } } } func createLiteralNode(sym *sx.Symbol, attrs *sx.Pair, content string) *sx.Pair { | | | 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | s := cp.parseString() sb.WriteString(s.GetValue()) } } } func createLiteralNode(sym *sx.Symbol, attrs *sx.Pair, content string) *sx.Pair { if sym.IsEqualSymbol(sz.SymLiteralZettel) { if p := attrs.Assoc(sx.MakeString("")); p != nil { if val, isString := sx.GetString(p.Cdr()); isString && val.GetValue() == api.ValueSyntaxHTML { sym = sz.SymLiteralHTML attrs = attrs.RemoveAssoc(sx.MakeString("")) } } } |
︙ | ︙ |
Changes to sz/zmk/post-processor.go.
︙ | ︙ | |||
320 321 322 323 324 325 326 | } elem = cellTail.Head() if elem.Car().IsEqual(sz.SymText) { if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" { str := s.GetValue() cellAlign := getCellAlignment(str[len(str)-1]) | | | 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | } elem = cellTail.Head() if elem.Car().IsEqual(sz.SymText) { if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" { str := s.GetValue() cellAlign := getCellAlignment(str[len(str)-1]) if !cellAlign.IsEqualSymbol(sz.SymCell) { elem.SetCdr(sx.Cons(sx.MakeString(str[0:len(str)-1]), nil)) } align[cellCount-1] = cellAlign cell.SetCar(cellAlign) } } } |
︙ | ︙ | |||
366 367 368 369 370 371 372 | // elem is first cell inline element elem := cellTail.Head() if elem.Car().IsEqual(sz.SymText) { if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" { str := s.GetValue() cellAlign := getCellAlignment(str[0]) | | | 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | // elem is first cell inline element elem := cellTail.Head() if elem.Car().IsEqual(sz.SymText) { if s, isString := sx.GetString(elem.Tail().Car()); isString && s.GetValue() != "" { str := s.GetValue() cellAlign := getCellAlignment(str[0]) if !cellAlign.IsEqualSymbol(sz.SymCell) { elem.SetCdr(sx.Cons(sx.MakeString(str[1:]), nil)) cell.SetCar(cellAlign) } } } } |
︙ | ︙ |
Changes to sz/zmk/ref.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 | // ----------------------------------------------------------------------------- // Copyright (c) 2020-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2020-present Detlef Stern // ----------------------------------------------------------------------------- package zmk import ( "net/url" "strings" "t73f.de/r/sx" "t73f.de/r/zsc/api" "t73f.de/r/zsc/sz" ) // ParseReference parses a string and returns a reference. func ParseReference(s string) *sx.Pair { if invalidReference(s) { return makePairRef(sz.SymRefStateInvalid, s) } if strings.HasPrefix(s, api.QueryPrefix) { return makePairRef(sz.SymRefStateQuery, s[len(api.QueryPrefix):]) } if state, ok := localState(s); ok { | > | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | // ----------------------------------------------------------------------------- // Copyright (c) 2020-present Detlef Stern // // This file is part of zettelstore-client. // // Zettelstore client is licensed under the latest version of the EUPL // (European Union Public License). Please see file LICENSE.txt for your rights // and obligations under this license. // // SPDX-License-Identifier: EUPL-1.2 // SPDX-FileCopyrightText: 2020-present Detlef Stern // ----------------------------------------------------------------------------- package zmk import ( "net/url" "strings" "t73f.de/r/sx" "t73f.de/r/zsc/api" "t73f.de/r/zsc/sz" ) // ParseReference parses a string and returns a reference. func ParseReference(s string) *sx.Pair { if invalidReference(s) { return makePairRef(sz.SymRefStateInvalid, s) } if strings.HasPrefix(s, api.QueryPrefix) { return makePairRef(sz.SymRefStateQuery, s[len(api.QueryPrefix):]) } if state, ok := localState(s); ok { if state.IsEqualSymbol(sz.SymRefStateBased) { s = s[1:] } _, err := url.Parse(s) if err == nil { return makePairRef(state, s) } } |
︙ | ︙ |
Changes to sz/zmk/zmk.go.
︙ | ︙ | |||
20 21 22 23 24 25 26 27 28 29 30 31 32 33 | "unicode" "t73f.de/r/sx" "t73f.de/r/zsc/input" "t73f.de/r/zsc/sz" ) func ParseBlocks(inp *input.Input) *sx.Pair { parser := zmkP{inp: inp} var lastPara *sx.Pair var blkBuild sx.ListBuilder for inp.Ch != input.EOS { bn, cont := parser.parseBlock(lastPara) | > | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | "unicode" "t73f.de/r/sx" "t73f.de/r/zsc/input" "t73f.de/r/zsc/sz" ) // ParseBlocks tries to parse the input as a block element. func ParseBlocks(inp *input.Input) *sx.Pair { parser := zmkP{inp: inp} var lastPara *sx.Pair var blkBuild sx.ListBuilder for inp.Ch != input.EOS { bn, cont := parser.parseBlock(lastPara) |
︙ | ︙ | |||
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | var pp postProcessor if bs := pp.visitPairList(bnl, nil); bs != nil { return bs.Cons(sz.SymBlock) } return nil } func ParseInlines(inp *input.Input) *sx.Pair { parser := zmkP{inp: inp} var ins sx.Vector for inp.Ch != input.EOS { in := parser.parseInline() if in == nil { break | > | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | var pp postProcessor if bs := pp.visitPairList(bnl, nil); bs != nil { return bs.Cons(sz.SymBlock) } return nil } // ParseInlines tries to parse the input as an inline element. func ParseInlines(inp *input.Input) *sx.Pair { parser := zmkP{inp: inp} var ins sx.Vector for inp.Ch != input.EOS { in := parser.parseInline() if in == nil { break |
︙ | ︙ | |||
107 108 109 110 111 112 113 | func (attrs attrMap) asPairAssoc() *sx.Pair { names := make([]string, 0, len(attrs)) for n := range attrs { names = append(names, n) } slices.Sort(names) | | | 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | func (attrs attrMap) asPairAssoc() *sx.Pair { names := make([]string, 0, len(attrs)) for n := range attrs { names = append(names, n) } slices.Sort(names) var assoc *sx.Pair for i := len(names) - 1; i >= 0; i-- { n := names[i] assoc = assoc.Cons(sx.Cons(sx.MakeString(n), sx.MakeString(attrs[n]))) } return assoc } |
︙ | ︙ | |||
187 188 189 190 191 192 193 | inp.Next() } if pos < inp.Pos { return attrMap{"": string(inp.Src[pos:inp.Pos])}.asPairAssoc() } // No immediate name: skip spaces | | | 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | inp.Next() } if pos < inp.Pos { return attrMap{"": string(inp.Src[pos:inp.Pos])}.asPairAssoc() } // No immediate name: skip spaces inp.SkipSpace() return cp.parseInlineAttributes() } func (cp *zmkP) parseInlineAttributes() *sx.Pair { inp := cp.inp pos := inp.Pos if attrs, success := cp.doParseAttributes(); success { |
︙ | ︙ | |||
271 272 273 274 275 276 277 | inp.EatEOL() default: return } } } | < < < < < < | 273 274 275 276 277 278 279 280 281 282 | inp.EatEOL() default: return } } } func isNameRune(ch rune) bool { return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '-' || ch == '_' } |
Changes to sz/zmk/zmk_test.go.
︙ | ︙ | |||
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | func checkTcs(t *testing.T, isBlock bool, tcs TestCases) { t.Helper() for tcn, tc := range tcs { t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.source), func(st *testing.T) { st.Helper() ast := parseInput(tc.source, isBlock) got := ast.String() if tc.want != got { st.Errorf("\nwant=%q\n got=%q", tc.want, got) } }) } } | > | > > > > | 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 | func checkTcs(t *testing.T, isBlock bool, tcs TestCases) { t.Helper() for tcn, tc := range tcs { t.Run(fmt.Sprintf("TC=%02d,src=%q", tcn, tc.source), func(st *testing.T) { st.Helper() ast := parseInput(tc.source, isBlock) sz.Walk(astWalker{}, ast, nil) got := ast.String() if tc.want != got { st.Errorf("\nwant=%q\n got=%q", tc.want, got) } }) } } func parseInput(src string, asBlock bool) *sx.Pair { inp := input.NewInput([]byte(src)) if asBlock { bl := zmk.ParseBlocks(inp) return bl } il := zmk.ParseInlines(inp) return il } type astWalker struct{} func (astWalker) Visit(node *sx.Pair, env *sx.Pair) sx.Object { return sx.MakeBoolean(true) } func TestEOL(t *testing.T) { t.Parallel() for _, isBlock := range []bool{true, false} { checkTcs(t, isBlock, TestCases{ {"", "()"}, {"\n", "()"}, {"\r", "()"}, |
︙ | ︙ |
Changes to text/text.go.
︙ | ︙ | |||
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | ) // Encoder is the structure to hold relevant data to execute the encoding. type Encoder struct { sb strings.Builder } func NewEncoder() *Encoder { enc := &Encoder{ sb: strings.Builder{}, } return enc } func (enc *Encoder) Encode(lst *sx.Pair) string { enc.executeList(lst) result := enc.sb.String() enc.sb.Reset() return result } | > > | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | ) // Encoder is the structure to hold relevant data to execute the encoding. type Encoder struct { sb strings.Builder } // NewEncoder returns a new text encoder. func NewEncoder() *Encoder { enc := &Encoder{ sb: strings.Builder{}, } return enc } // Encode the object list as a string. func (enc *Encoder) Encode(lst *sx.Pair) string { enc.executeList(lst) result := enc.sb.String() enc.sb.Reset() return result } |
︙ | ︙ |
Changes to www/changes.wiki.
1 2 3 | <title>Change Log</title> <a name="0_19"></a> | > > > | > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <title>Change Log</title> <a name="0_20"></a> <h2>Changes for Version 0.20.0 (pending)</h2> <a name="0_19"></a> <h2>Changes for Version 0.19.0 (2024-12-13)</h2> * Remove support for rename operation; removed all associated constants * Make quote handling in shtml public, to be used by other encoders * shtml generates external links with rel attribute * Add some input handling methods * Enhance docs for api/client <a name="0_18"></a> <h2>Changes for Version 0.18.0 (2024-07-11)</h2> * Add client method <code>GetApplicationZid</code> to retrieve the zettel identifier of an configuration zettel for a specific application. * Rename to be package <code>t73f.de/r/zsc</code> * Reserve some zettel identifier for future use |
︙ | ︙ |
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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <title>Home</title> This repository contains Go client software to access [https://zettelstore.de|Zettelstore] via its API. <h3>Latest Release: 0.19.0 (2024-12-13)</h3> * [./changes.wiki#0_19|Change summary] * [/timeline?p=v0.19.0&bt=v0.18.0&y=ci|Check-ins for version 0.19], [/vdiff?to=v0.19.0&from=v0.18.0|content diff] * [/timeline?df=v0.19.0&y=ci|Check-ins derived from the 0.19 release], [/vdiff?from=v0.19.0&to=trunk|content diff] * [/timeline?t=release|Timeline of all past releases] <h3>Use instructions</h3> If you want to import this library into your own [https://go.dev/|Go] software, you must execute a <code>go get</code> command. Since Go treats non-standard software and non-standard platforms quite badly, you must use some non-standard commands. First, you must install the [https://fossil-scm.org|Fossil] version control system. In too many use cases it is a superior solution compared to Git, It is just a single executable, nothing more. Make sure, it is in your search path for commands. How you can execute the following Go command to retrieve a given version of this library: <code>GOVCS=zettelstore.de:fossil go get -x t73f.de/r/zsc@HASH</code> where <code>HASH</code> is the hash value of the commit you want to use. |
︙ | ︙ |