Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Difference From trunk To v0.13.0
2024-08-26
| ||
12:46 | Fix generating external links in shtml; some refactorings ... (Leaf check-in: 97640fce44 user: stern tags: trunk) | |
2024-08-19
| ||
09:25 | SHTML: generate (th... for table cell in header ... (check-in: 6ca051ffec user: stern tags: trunk) | |
2023-08-07
| ||
15:17 | Remove support for JSON encoding ... (check-in: 0628873da8 user: stern tags: trunk) | |
13:44 | Version 0.13.0 ... (check-in: 92d8dd7df8 user: stern tags: release, trunk, v0.13.0) | |
10:22 | client: use sx for creating/updating zettel ... (check-in: 4ce083f28b user: stern tags: trunk) | |
Added .github/dependabot.yml.
> > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 | # To get started with Dependabot version updates, you'll need to specify which # package ecosystems to update and where the package manifests are located. # Please see the documentation for all configuration options: # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates version: 2 updates: - package-ecosystem: "gomod" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "daily" rebase-strategy: "disabled" |
Changes to api/api.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | //----------------------------------------------------------------------------- // Copyright (c) 2021-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. //----------------------------------------------------------------------------- // Package api contains common definitions used for client and server. package api // ZettelID contains the identifier of a zettel. It is a string with 14 digits. type ZettelID string // InvalidZID is an invalif zettel identifier const InvalidZID = "" // IsValid returns true, if the idenfifier contains 14 digits. func (zid ZettelID) IsValid() bool { if len(zid) != 14 { return false } for i := 0; i < 14; i++ { ch := zid[i] if ch < '0' || '9' < ch { return false } } return true } // ZettelMeta is a map containg the 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 ZettelCanRename // Requesting user is allowed to provide the zettel with a new identifier 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 |
︙ | ︙ |
Changes to api/const.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | | | | | < < < < < < | < < < | | | | < < | | | | | | > | | | < | | > | < < < < < < < | | < < | < < < | > | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package api import "fmt" // Predefined Zettel Identifier const ( // System zettel ZidVersion = ZettelID("00000000000001") ZidHost = ZettelID("00000000000002") ZidOperatingSystem = ZettelID("00000000000003") ZidLicense = ZettelID("00000000000004") ZidAuthors = ZettelID("00000000000005") ZidDependencies = ZettelID("00000000000006") ZidLog = ZettelID("00000000000007") ZidBoxManager = ZettelID("00000000000020") ZidMetadataKey = ZettelID("00000000000090") ZidParser = ZettelID("00000000000092") ZidStartupConfiguration = ZettelID("00000000000096") ZidConfiguration = ZettelID("00000000000100") // 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") ZidRenameTemplate = ZettelID("00000000010404") ZidDeleteTemplate = ZettelID("00000000010405") ZidErrorTemplate = ZettelID("00000000010700") // WebUI sxn code zettel are in the range 19000..19999 ZidSxnTemplate = ZettelID("00000000019100") // CSS-related zettel are in the range 20000..29999 ZidBaseCSS = ZettelID("00000000020001") ZidUserCSS = ZettelID("00000000025001") ZidRoleCSSMap = ZettelID("00000000029000") // Maps roles to CSS zettel, which should be in the range 29001..29999. // 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 // Range 90000...99999 is reserved for zettel templates ZidTOCNewTemplate = ZettelID("00000000090000") ZidTemplateNewZettel = ZettelID("00000000090001") ZidTemplateNewUser = ZettelID("00000000090002") ZidDefaultHome = ZettelID("00010000000000") ) // LengthZid factors the constant length of a zettel identifier const LengthZid = len(ZidDefaultHome) // Values of the metadata key/value type. const ( MetaCredential = "Credential" MetaEmpty = "EString" MetaID = "Identifier" MetaIDSet = "IdentifierSet" MetaNumber = "Number" MetaString = "String" MetaTagSet = "TagSet" MetaTimestamp = "Timestamp" MetaURL = "URL" MetaWord = "Word" MetaWordSet = "WordSet" MetaZettelmarkup = "Zettelmarkup" ) // Predefined general Metadata keys const ( KeyID = "id" KeyTitle = "title" |
︙ | ︙ | |||
145 146 147 148 149 150 151 | // Predefined Metadata values const ( ValueFalse = "false" ValueTrue = "true" ValueLangEN = "en" ValueRoleConfiguration = "configuration" | < < | 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | // Predefined Metadata values const ( ValueFalse = "false" ValueTrue = "true" ValueLangEN = "en" ValueRoleConfiguration = "configuration" ValueRoleZettel = "zettel" ValueSyntaxCSS = "css" ValueSyntaxDraw = "draw" ValueSyntaxGif = "gif" ValueSyntaxHTML = "html" ValueSyntaxMarkdown = "markdown" ValueSyntaxMD = "md" |
︙ | ︙ | |||
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | ValueVisibilityLogin = "login" ValueVisibilityOwner = "owner" ValueVisibilityPublic = "public" ) // Additional HTTP constants. const ( HeaderAccept = "Accept" HeaderContentType = "Content-Type" HeaderDestination = "Destination" HeaderLocation = "Location" ) // Values for HTTP query parameter. const ( QueryKeyCommand = "cmd" QueryKeyEncoding = "enc" QueryKeyParseOnly = "parseonly" QueryKeyPart = "part" QueryKeyPhrase = "phrase" QueryKeyQuery = "q" | > > < < > > | 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 | ValueVisibilityLogin = "login" ValueVisibilityOwner = "owner" ValueVisibilityPublic = "public" ) // Additional HTTP constants. const ( MethodMove = "MOVE" // HTTP method for renaming a zettel HeaderAccept = "Accept" HeaderContentType = "Content-Type" HeaderDestination = "Destination" HeaderLocation = "Location" ) // Values for HTTP query parameter. const ( QueryKeyCommand = "cmd" QueryKeyEncoding = "enc" QueryKeyParseOnly = "parseonly" QueryKeyPart = "part" QueryKeyPhrase = "phrase" QueryKeyQuery = "q" QueryKeySeed = "_seed" ) // Supported encoding values. const ( EncodingHTML = "html" EncodingMD = "md" EncodingSHTML = "shtml" EncodingSz = "sz" EncodingText = "text" EncodingZMK = "zmk" EncodingPlain = "plain" EncodingData = "data" EncodingJson = "json" ) var mapEncodingEnum = map[string]EncodingEnum{ EncodingHTML: EncoderHTML, EncodingMD: EncoderMD, EncodingSHTML: EncoderSHTML, EncodingSz: EncoderSz, EncodingText: EncoderText, EncodingZMK: EncoderZmk, EncodingPlain: EncoderPlain, EncodingData: EncoderData, EncodingJson: EncoderJson, } var mapEnumEncoding = map[EncodingEnum]string{} func init() { for k, v := range mapEncodingEnum { mapEnumEncoding[v] = k } |
︙ | ︙ | |||
246 247 248 249 250 251 252 253 254 255 256 257 258 259 | EncoderSHTML EncoderSz EncoderText EncoderZmk EncoderPlain EncoderData ) // String representation of an encoder key. func (e EncodingEnum) String() string { if f, ok := mapEnumEncoding[e]; ok { return f } | > | 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | EncoderSHTML EncoderSz EncoderText EncoderZmk EncoderPlain EncoderData EncoderJson ) // String representation of an encoder key. func (e EncodingEnum) String() string { if f, ok := mapEnumEncoding[e]; ok { return f } |
︙ | ︙ | |||
278 279 280 281 282 283 284 | // Supported search operator representations const ( BackwardDirective = "BACKWARD" ContextDirective = "CONTEXT" CostDirective = "COST" ForwardDirective = "FORWARD" | < < < < < < < < < < < < < < | 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 | // Supported search operator representations const ( BackwardDirective = "BACKWARD" ContextDirective = "CONTEXT" CostDirective = "COST" ForwardDirective = "FORWARD" IdentDirective = "IDENT" ItemsDirective = "ITEMS" MaxDirective = "MAX" LimitDirective = "LIMIT" OffsetDirective = "OFFSET" OrDirective = "OR" OrderDirective = "ORDER" PhraseDirective = "PHRASE" PickDirective = "PICK" RandomDirective = "RANDOM" ReverseDirective = "REVERSE" UnlinkedDirective = "UNLINKED" ActionSeparator = "|" ExistOperator = "?" ExistNotOperator = "!?" SearchOperatorNot = "!" SearchOperatorEqual = "=" SearchOperatorNotEqual = "!=" SearchOperatorHas = ":" SearchOperatorHasNot = "!:" SearchOperatorPrefix = "[" SearchOperatorNoPrefix = "![" SearchOperatorSuffix = "]" SearchOperatorNoSuffix = "!]" SearchOperatorMatch = "~" SearchOperatorNoMatch = "!~" SearchOperatorLess = "<" SearchOperatorNotLess = "!<" SearchOperatorGreater = ">" SearchOperatorNotGreater = "!>" ) |
Changes to api/urlbuilder.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | > > > > > < | > > > > > < < < | < < < < | > > > > > > > > | > > > > > > > > > > > > > > > > | | > > > > > > > | > | > | > > | > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package api import ( "net/url" "strings" ) type urlQuery struct{ key, val string } // URLBuilder should be used to create zettelstore URLs. type URLBuilder struct { prefix string key byte rawLocal string path []string query []urlQuery fragment string } // NewURLBuilder creates a new URL builder with the given prefix and key. func NewURLBuilder(prefix string, key byte) *URLBuilder { return &URLBuilder{prefix: prefix, key: key} } // Clone an URLBuilder func (ub *URLBuilder) Clone() *URLBuilder { cpy := new(URLBuilder) cpy.key = ub.key if len(ub.path) > 0 { cpy.path = make([]string, 0, len(ub.path)) cpy.path = append(cpy.path, ub.path...) } if len(ub.query) > 0 { cpy.query = make([]urlQuery, 0, len(ub.query)) cpy.query = append(cpy.query, ub.query...) } cpy.fragment = ub.fragment return cpy } // SetRawLocal sets everything that follows the prefix / key. func (ub *URLBuilder) SetRawLocal(rawLocal string) *URLBuilder { for len(rawLocal) > 0 && rawLocal[0] == '/' { rawLocal = rawLocal[1:] } ub.rawLocal = rawLocal ub.path = nil ub.query = nil ub.fragment = "" return ub } // SetZid sets the zettel identifier. func (ub *URLBuilder) SetZid(zid ZettelID) *URLBuilder { if len(ub.path) > 0 { panic("Cannot add Zid") } ub.rawLocal = "" ub.path = append(ub.path, string(zid)) return ub } // AppendPath adds a new path element func (ub *URLBuilder) AppendPath(p string) *URLBuilder { ub.rawLocal = "" for len(p) > 0 && p[0] == '/' { p = p[1:] } if p != "" { ub.path = append(ub.path, p) } return ub } // AppendKVQuery adds a new key/value query parameter func (ub *URLBuilder) AppendKVQuery(key, value string) *URLBuilder { ub.rawLocal = "" ub.query = append(ub.query, urlQuery{key, value}) return ub } // AppendQuery adds a new query func (ub *URLBuilder) AppendQuery(value string) *URLBuilder { if value != "" { ub.rawLocal = "" ub.query = append(ub.query, urlQuery{QueryKeyQuery, value}) } return ub } // ClearQuery removes all query parameters. func (ub *URLBuilder) ClearQuery() *URLBuilder { ub.rawLocal = "" ub.query = nil ub.fragment = "" return ub } // SetFragment stores the fragment func (ub *URLBuilder) SetFragment(s string) *URLBuilder { ub.rawLocal = "" ub.fragment = s return ub } // String produces a string value. func (ub *URLBuilder) String() string { var sb strings.Builder sb.WriteString(ub.prefix) if ub.key != '/' { sb.WriteByte(ub.key) } if ub.rawLocal != "" { sb.WriteString(ub.rawLocal) return sb.String() } for i, p := range ub.path { if i > 0 || ub.key != '/' { sb.WriteByte('/') } sb.WriteString(url.PathEscape(p)) } if len(ub.fragment) > 0 { sb.WriteByte('#') sb.WriteString(ub.fragment) } for i, q := range ub.query { if i == 0 { sb.WriteByte('?') } else { sb.WriteByte('&') } sb.WriteString(q.key) if val := q.val; val != "" { sb.WriteByte('=') sb.WriteString(url.QueryEscape(val)) } } return sb.String() } |
Changes to attrs/attrs.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < < | | 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) 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. //----------------------------------------------------------------------------- // Package attrs stores attributes of zettel parts. package attrs import ( "strings" "zettelstore.de/client.fossil/maps" ) // Attributes store additional information about some node types. type Attributes map[string]string // IsEmpty returns true if there are no attributes. func (a Attributes) IsEmpty() bool { return len(a) == 0 } |
︙ | ︙ | |||
84 85 86 87 88 89 90 | func (a Attributes) Remove(key string) Attributes { if a != nil { delete(a, key) } return a } | | | | | | | | | > > > | | | < | | > > > | | | > | | | < > | | | < | < < | 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 | func (a Attributes) Remove(key string) Attributes { if a != nil { delete(a, key) } return a } // AddClass adds a value to the class attribute. func (a Attributes) AddClass(class string) Attributes { if a == nil { return map[string]string{"class": class} } classes := a.GetClasses() for _, cls := range classes { if cls == class { return a } } classes = append(classes, class) a["class"] = strings.Join(classes, " ") return a } // GetClasses returns the class values as a string slice func (a Attributes) GetClasses() []string { if a == nil { return nil } classes, ok := a["class"] if !ok { return nil } return strings.Fields(classes) } // HasClass returns true, if attributes contains the given class. func (a Attributes) HasClass(s string) bool { if a == nil { return false } classes, found := a["class"] if !found { return false } return strings.Contains(" "+classes+" ", " "+s+" ") } |
Changes to attrs/attrs_test.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //----------------------------------------------------------------------------- // Copyright (c) 2020-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. //----------------------------------------------------------------------------- package attrs_test import ( "testing" "zettelstore.de/client.fossil/attrs" ) func TestHasDefault(t *testing.T) { t.Parallel() attr := attrs.Attributes{} if attr.HasDefault() { t.Error("Should not have default attr") |
︙ | ︙ | |||
53 54 55 56 57 58 59 | func TestHasClass(t *testing.T) { t.Parallel() testcases := []struct { classes string class string exp bool }{ | | | 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | func TestHasClass(t *testing.T) { t.Parallel() testcases := []struct { classes string class string exp bool }{ {"", "", true}, {"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.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | | < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | //----------------------------------------------------------------------------- // Copyright (c) 2021-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. //----------------------------------------------------------------------------- // Package client provides a client for accessing the Zettelstore via its API. package client import ( "bufio" "bytes" "context" "fmt" "io" "net" "net/http" "net/url" "strconv" "strings" "time" "zettelstore.de/client.fossil/api" "zettelstore.de/client.fossil/sexp" "zettelstore.de/sx.fossil" "zettelstore.de/sx.fossil/sxreader" ) // Client contains all data to execute requests. type Client struct { base string username string password string |
︙ | ︙ | |||
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | myURL := *u myURL.User = nil myURL.ForceQuery = false myURL.RawQuery = "" myURL.Fragment = "" myURL.RawFragment = "" base := myURL.String() c := Client{ base: base, client: http.Client{ Timeout: 10 * time.Second, Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 5 * time.Second, // TCP connect timeout }).DialContext, TLSHandshakeTimeout: 5 * time.Second, }, }, } return &c } | > > > < < < < < < < < < < < < < | 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 | myURL := *u myURL.User = nil myURL.ForceQuery = false myURL.RawQuery = "" myURL.Fragment = "" myURL.RawFragment = "" base := myURL.String() if !strings.HasSuffix(base, "/") { base += "/" } c := Client{ base: base, client: http.Client{ Timeout: 10 * time.Second, Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 5 * time.Second, // TCP connect timeout }).DialContext, TLSHandshakeTimeout: 5 * time.Second, }, }, } return &c } // Error encapsulates the possible client call errors. type Error struct { StatusCode int Message string Body []byte } |
︙ | ︙ | |||
122 123 124 125 126 127 128 | return &Error{ StatusCode: resp.StatusCode, Message: resp.Status[4:], Body: body, } } | < | | | < < < < > > > | 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 | return &Error{ StatusCode: resp.StatusCode, Message: resp.Status[4:], Body: body, } } 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, h http.Header) (*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 } for key, val := range h { req.Header[key] = append(req.Header[key], val...) } return c.executeRequest(req) } // SetAuth sets authentication data. func (c *Client) SetAuth(username, password string) { c.username = username c.password = password |
︙ | ︙ | |||
188 189 190 191 192 193 194 | if err != nil { return err } vals, err := sexp.ParseList(obj, "ssi") if err != nil { return err } | | | | | | | | 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 | if err != nil { return err } vals, err := sexp.ParseList(obj, "ssi") if err != nil { return err } token := vals[1].(sx.String).String() if len(token) < 4 { return fmt.Errorf("no valid token found: %q", token) } c.token = token c.tokenType = vals[0].(sx.String).String() c.expires = time.Now().Add(time.Duration(vals[2].(sx.Int64)*9/10) * time.Second) return nil } func (c *Client) updateToken(ctx context.Context) error { if c.username == "" { return nil } 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') resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data), nil) if err != nil { return api.InvalidZID, err } defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { return api.InvalidZID, statusToError(resp) } |
︙ | ︙ | |||
255 256 257 258 259 260 261 | // CreateZettelData creates a new zettel and returns its URL. 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 } | | | | | | 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 | // CreateZettelData creates a new zettel and returns its URL. 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, nil) 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) } var bsLF = []byte{'\n'} // QueryZettel returns a list of all Zettel. 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, nil) if err != nil { return nil, err } defer resp.Body.Close() data, err := io.ReadAll(resp.Body) switch resp.StatusCode { case http.StatusOK: |
︙ | ︙ | |||
302 303 304 305 306 307 308 | lines = lines[:len(lines)-1] } return lines, nil } // QueryZettelData returns a list of zettel metadata. func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) { | | | | 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | lines = lines[:len(lines)-1] } return lines, nil } // QueryZettelData returns a list of zettel metadata. 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, nil) if err != nil { return "", "", nil, err } defer resp.Body.Close() rdr := sxreader.MakeReader(resp.Body) obj, err := rdr.Read() switch resp.StatusCode { |
︙ | ︙ | |||
333 334 335 336 337 338 339 | return "", "", nil, err } hVals, err := sexp.ParseList(vals[2], "ys") if err != nil { return "", "", nil, err } metaList, err := parseMetaList(vals[3].(*sx.Pair)) | | | 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | return "", "", nil, err } hVals, err := sexp.ParseList(vals[2], "ys") if err != nil { return "", "", nil, err } metaList, err := parseMetaList(vals[3].(*sx.Pair)) return qVals[1].String(), hVals[1].String(), 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 { |
︙ | ︙ | |||
407 408 409 410 411 412 413 | return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val) } return zid, 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 | | | 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val) } return zid, 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, WordSet, or TagSet. 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 |
︙ | ︙ | |||
430 431 432 433 434 435 436 | } } } } return agg, nil } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | 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 | } } } } return agg, nil } // GetZettel returns a zettel as a string. 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, 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, 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() |
︙ | ︙ | |||
526 527 528 529 530 531 532 | // GetEvaluatedZettel return an evaluated zettel in a defined encoding. func (c *Client) GetEvaluatedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, zid, enc, false) } func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) { | | | | | | | | | | | | | | 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | // GetEvaluatedZettel return an evaluated zettel in a defined encoding. func (c *Client) GetEvaluatedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) { return c.getZettelString(ctx, 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, 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 an parsed zettel as a Sexpr-decoded data structure. func (c *Client) GetParsedSz(ctx context.Context, zid api.ZettelID, part string, sf sx.SymbolFactory) (sx.Object, error) { return c.getSz(ctx, zid, part, true, sf) } // GetEvaluatedSz returns an evaluated zettel as a Sexpr-decoded data structure. func (c *Client) GetEvaluatedSz(ctx context.Context, zid api.ZettelID, part string, sf sx.SymbolFactory) (sx.Object, error) { return c.getSz(ctx, zid, part, false, sf) } func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sx.SymbolFactory) (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, 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), sxreader.WithSymbolFactory(sf)).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, 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 { |
︙ | ︙ | |||
620 621 622 623 624 625 626 | Meta: meta, Rights: rights, }, nil } // UpdateZettel updates an existing zettel. func (c *Client) UpdateZettel(ctx context.Context, zid api.ZettelID, data []byte) error { | | | | | > > > > > > > > > > > > > > > > > | | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | Meta: meta, Rights: rights, }, nil } // UpdateZettel updates an existing zettel. 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), nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // UpdateZettelData updates an existing zettel. 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, nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // RenameZettel renames a zettel. func (c *Client) RenameZettel(ctx context.Context, oldZid, newZid api.ZettelID) error { ub := c.newURLBuilder('z').SetZid(oldZid) h := http.Header{ api.HeaderDestination: {c.newURLBuilder('z').SetZid(newZid).String()}, } resp, err := c.buildAndExecuteRequest(ctx, api.MethodMove, ub, nil, h) 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, 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. 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, nil) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return statusToError(resp) } return nil } // GetVersionInfo returns version information.. func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) { resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.newURLBuilder('x'), nil, 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).String(), Hash: vals[4].(sx.String).String(), }, nil } } return VersionInfo{}, err } // VersionInfo contains version information. type VersionInfo struct { Major int Minor int Patch int Info string Hash string } |
Changes to client/client_test.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | > | > | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package client_test import ( "context" "flag" "net/http" "net/url" "testing" "zettelstore.de/client.fossil/api" "zettelstore.de/client.fossil/client" "zettelstore.de/client.fossil/sz" "zettelstore.de/sx.fossil" ) func TestZettelList(t *testing.T) { c := getClient() _, err := c.QueryZettel(context.Background(), "") if err != nil { t.Error(err) |
︙ | ︙ | |||
44 45 46 47 48 49 50 | } return } } func TestGetSzZettel(t *testing.T) { c := getClient() | > > > | | 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | } return } } func TestGetSzZettel(t *testing.T) { c := getClient() sf := sx.MakeMappedFactory() var zetSyms sz.ZettelSymbols zetSyms.InitializeZettelSymbols(sf) value, err := c.GetEvaluatedSz(context.Background(), api.ZidDefaultHome, api.PartContent, sf) if err != nil { t.Error(err) return } if value.IsNil() { t.Error("No data") } |
︙ | ︙ |
Changes to go.mod.
|
| | | | < < < < | 1 2 3 4 5 | module zettelstore.de/client.fossil go 1.20 require zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284 |
Changes to go.sum.
|
| | < < | < < | 1 2 | zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284 h1:26xwZWEjdyL3wObczc/PKugv0EY6mgSH5NUe5kYFCt4= zettelstore.de/sx.fossil v0.0.0-20230727172325-adec5a7ba284/go.mod h1:nsWXVrQG8RNKtoXzEMrWXNMdnpfIDU6Hb0pk56KpVKE= |
Deleted input/entity.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted input/entity_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted input/input.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted input/input_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted input/runes.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to maps/maps.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package maps import "sort" func Keys[T any](m map[string]T) []string { |
︙ | ︙ |
Changes to maps/maps_test.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //----------------------------------------------------------------------------- // Copyright (c) 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. //----------------------------------------------------------------------------- package maps_test import ( "testing" "zettelstore.de/client.fossil/maps" ) func isSorted(seq []string) bool { for i := 1; i < len(seq); i++ { if seq[i] < seq[i-1] { return false } |
︙ | ︙ |
Changes to sexp/sexp.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | > | | | | | | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- // Package sexp contains helper function to work with s-expression in an alien // environment. package sexp import ( "errors" "fmt" "sort" "zettelstore.de/client.fossil/api" "zettelstore.de/sx.fossil" ) // EncodeZettel transforms zettel data into a sx object. func EncodeZettel(zettel api.ZettelData) sx.Object { sf := sx.MakeMappedFactory() return sx.MakeList( sf.MustMake("zettel"), meta2sz(zettel.Meta, sf), sx.MakeList(sf.MustMake("rights"), sx.Int64(int64(zettel.Rights))), sx.MakeList(sf.MustMake("encoding"), sx.MakeString(zettel.Encoding)), sx.MakeList(sf.MustMake("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 |
︙ | ︙ | |||
69 70 71 72 73 74 75 | if errSym := CheckSymbol(contentVals[0], "content"); errSym != nil { return api.ZettelData{}, errSym } return api.ZettelData{ Meta: meta, Rights: rights, | | | > | | | | | | | | | | | 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 | if errSym := CheckSymbol(contentVals[0], "content"); errSym != nil { return api.ZettelData{}, errSym } return api.ZettelData{ Meta: meta, Rights: rights, Encoding: encVals[1].(sx.String).String(), Content: contentVals[1].(sx.String).String(), }, nil } // EncodeMetaRights translates metadata/rights into a sx object. func EncodeMetaRights(mr api.MetaRights) *sx.Pair { sf := sx.MakeMappedFactory() return sx.MakeList( sf.MustMake("list"), meta2sz(mr.Meta, sf), sx.MakeList(sf.MustMake("rights"), sx.Int64(int64(mr.Rights))), ) } func meta2sz(m api.ZettelMeta, sf sx.SymbolFactory) sx.Object { result := sx.Nil().Cons(sf.MustMake("meta")) curr := result keys := make([]string, 0, len(m)) for k := range m { keys = append(keys, k) } sort.Strings(keys) for _, k := range keys { val := sx.MakeList(sf.MustMake(k), sx.MakeString(m[k])) curr = curr.AppendBang(val) } return result } // ParseMeta translates the given list to metadata. func ParseMeta(pair *sx.Pair) (api.ZettelMeta, error) { if err := CheckSymbol(pair.Car(), "meta"); err != nil { return nil, err } res := api.ZettelMeta{} for node := pair.Tail(); node != nil; node = node.Tail() { mVals, err := ParseList(node.Car(), "ys") if err != nil { return nil, err } res[mVals[0].(*sx.Symbol).Name()] = mVals[1].(sx.String).String() } return res, nil } // ParseRights returns the rights values of the given object. func ParseRights(obj sx.Object) (api.ZettelRights, error) { rVals, err := ParseList(obj, "yi") |
︙ | ︙ | |||
131 132 133 134 135 136 137 | if i64 < 0 && i64 >= int64(api.ZettelMaxRight) { return api.ZettelMaxRight, fmt.Errorf("invalid zettel right value: %v", i64) } return api.ZettelRights(i64), nil } // ParseList parses the given object as a proper list, based on a type specification. | | | | | 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 | if i64 < 0 && i64 >= int64(api.ZettelMaxRight) { return api.ZettelMaxRight, fmt.Errorf("invalid zettel right value: %v", i64) } return api.ZettelRights(i64), nil } // ParseList parses the given object as a proper list, based on a type specification. func ParseList(obj sx.Object, spec string) ([]sx.Object, error) { pair, isPair := sx.GetPair(obj) if !isPair { return nil, fmt.Errorf("not a list: %T/%v", obj, obj) } if pair == nil { if spec == "" { return nil, nil } return nil, ErrElementsMissing } result := make([]sx.Object, 0, len(spec)) node, i := pair, 0 for ; node != nil; i++ { if i >= len(spec) { return nil, ErrNoSpec } var val sx.Object var ok bool car := node.Car() switch spec[i] { case 'b': val, ok = sx.GetBoolean(car) case 'i': val, ok = car.(sx.Int64) case 'o': val, ok = car, true case 'p': val, ok = sx.GetPair(car) case 's': |
︙ | ︙ | |||
193 194 195 196 197 198 199 | // 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) } | | | 192 193 194 195 196 197 198 199 200 201 202 203 | // 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.Name(); got != name { return fmt.Errorf("symbol %q expected, but got: %q", name, got) } return nil } |
Changes to sexp/sexp_test.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | 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) 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. //----------------------------------------------------------------------------- package sexp_test import ( "testing" "zettelstore.de/client.fossil/sexp" "zettelstore.de/sx.fossil" ) func TestParseObject(t *testing.T) { if elems, err := sexp.ParseList(sx.MakeString("a"), "s"); err == nil { t.Error("expected an error, but got: ", elems) } if elems, err := sexp.ParseList(sx.Nil(), ""); err != nil { |
︙ | ︙ | |||
35 36 37 38 39 40 41 | if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "ss"); err == nil { t.Error("expected error, but got: ", elems) } if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), ""); err == nil { t.Error("expected error, but got: ", elems) } | | | | 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "ss"); err == nil { t.Error("expected error, but got: ", elems) } if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), ""); err == nil { t.Error("expected error, but got: ", elems) } if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "b"); err == nil { t.Error("expected error, but got: ", elems) } if elems, err := sexp.ParseList(sx.Cons(sx.Nil(), sx.MakeString("a")), "ps"); err == nil { t.Error("expected error, but got: ", elems) } if elems, err := sexp.ParseList(sx.MakeList(sx.MakeString("a")), "s"); err != nil { t.Error(err) |
︙ | ︙ |
Deleted shtml/const.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted shtml/lang.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to shtml/shtml.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | > | | | > | | | | > > > > > > > > | | | > > > | | | > > > > > > > > > > | < < | < < < < | > > | | > > > > > > > > > | | | | | | | < < | | < < < | > > > > > > | < | > | > > > > > > > | | | > > > | | > | > > > > | > > > > | | > > > | < < < < | < < | | < | | | < < | | > > | | | | | | | | | | | < | | | < | > | < < < < < < < < < < < < < | | < < > > > | | > > | | < < < | < < | < < < < < < < < < < < < | < | < < | < < | < < < < > > > | | < < > > > | < < < < < | > | | > > | | | | | < < < < < < < < < < < < < < | | | | | | | | | | | | < | | | | | | | < < < < < < < | < < < | | | > > > > > > | | | | | < | < < | | | | | | | | | | < | | | | | | | | | | | | > | | | | | | | | | | > | | | | | | | | | | | | | | | > | > | | | | | | | | | | | | > | > | | | | | | | | > | | | | | | | | < < < < < < < < | < | < | | > | | | | | | | | < < < | | > > < | | < | | | | | | | | | > > > > > | > | > | | | < < | | | | | < < | | | | | | | < < | | | | | | < < | | | > > > > > > | | > > | | | > > | > > > > | | < < > | | < | | | | | < | | | | < < < | | | < < < | | | | | | > | | | | | | | < | < | | > > > > > | | < < | | | | | | | | | | < | | | | | | | | | | > | | > | | | > | | | | | | > | | < < | | | | | | | > | | | < | < < < | < < < < < < < < < | | < < | < > > | | | | | > | | | | | | | | | | | | | | | < | < | < | | | | < > | | < < < | < < < < | > | | < | < < | | < < < | < > | < | > | | < > > | < < < < < < < < < < < < < < | | < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | < | | | | | | < < < < < < < < < | | | > > > > | > > | < > | | > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 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 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- // Package shtml transforms a s-expr encoded zettel AST into a s-expr representation of HTML. package shtml import ( "fmt" "net/url" "strconv" "strings" "zettelstore.de/client.fossil/api" "zettelstore.de/client.fossil/attrs" "zettelstore.de/client.fossil/sz" "zettelstore.de/client.fossil/text" "zettelstore.de/sx.fossil" "zettelstore.de/sx.fossil/sxbuiltins/quote" "zettelstore.de/sx.fossil/sxeval" "zettelstore.de/sx.fossil/sxhtml" ) // Transformer will transform a s-expression that encodes the zettel AST into an s-expression // that represents HTML. type Transformer struct { sf sx.SymbolFactory rebinder RebindProc headingOffset int64 unique string endnotes []endnoteInfo noLinks bool // true iff output must not include links symAttr *sx.Symbol symClass *sx.Symbol symMeta *sx.Symbol symA *sx.Symbol symSpan *sx.Symbol } type endnoteInfo struct { noteAST *sx.Pair // Endnote as AST noteHx *sx.Pair // Endnote as SxHTML attrs *sx.Pair // attrs a-list } // NewTransformer creates a new transformer object. func NewTransformer(headingOffset int, sf sx.SymbolFactory) *Transformer { if sf == nil { sf = sx.MakeMappedFactory() } return &Transformer{ sf: sf, rebinder: nil, headingOffset: int64(headingOffset), symAttr: sf.MustMake(sxhtml.NameSymAttr), symClass: sf.MustMake("class"), symMeta: sf.MustMake("meta"), symA: sf.MustMake("a"), symSpan: sf.MustMake("span"), } } // SymbolFactory returns the symbol factory to create HTML symbols. func (tr *Transformer) SymbolFactory() sx.SymbolFactory { return tr.sf } // SetUnique sets a prefix to make several HTML ids unique. func (tr *Transformer) SetUnique(s string) { tr.unique = s } // IsValidName returns true, if name is a valid symbol name. func (tr *Transformer) IsValidName(s string) bool { return tr.sf.IsValidName(s) } // Make a new HTML symbol. func (tr *Transformer) Make(s string) *sx.Symbol { return tr.sf.MustMake(s) } // RebindProc is a procedure which is called every time before a tranformation takes place. type RebindProc func(*TransformEnv) // SetRebinder sets the rebinder procedure. func (tr *Transformer) SetRebinder(rb RebindProc) { tr.rebinder = rb } // TransformAttrbute transforms the given attributes into a HTML s-expression. func (tr *Transformer) TransformAttrbute(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 && tr.IsValidName(key) { plist = plist.Cons(sx.Cons(tr.Make(key), sx.MakeString(a[key]))) } } if plist == nil { return nil } return plist.Cons(tr.symAttr) } // TransformMeta creates a HTML meta s-expression func (tr *Transformer) TransformMeta(a attrs.Attributes) *sx.Pair { return sx.Nil().Cons(tr.TransformAttrbute(a)).Cons(tr.symMeta) } // Transform an AST s-expression into a list of HTML s-expressions. func (tr *Transformer) Transform(lst *sx.Pair) (*sx.Pair, error) { astSF := sx.FindSymbolFactory(lst) if astSF != nil { if astSF == tr.sf { panic("Invalid AST SymbolFactory") } } else { astSF = sx.MakeMappedFactory() } astEnv := sx.MakeRootEnvironment() engine := sxeval.MakeEngine(astSF, astEnv) quote.InstallQuoteSyntax(astEnv, astSF.MustMake(sz.NameSymQuote)) te := TransformEnv{ tr: tr, astSF: astSF, astEnv: astEnv, err: nil, textEnc: text.NewEncoder(astSF), } te.initialize() if rb := tr.rebinder; rb != nil { rb(&te) } val, err := engine.Eval(te.astEnv, lst) if err != nil { return nil, err } res, isPair := sx.GetPair(val) if !isPair { panic("Result is not a list") } for i := 0; i < len(tr.endnotes); i++ { // May extend tr.endnotes val, err = engine.Eval(te.astEnv, tr.endnotes[i].noteAST) if err != nil { return res, err } en, ok := sx.GetPair(val) if !ok { panic("Endnote is not a list") } tr.endnotes[i].noteHx = en } return res, err } // Endnotes returns a SHTML object with all collected endnotes. func (tr *Transformer) Endnotes() *sx.Pair { if len(tr.endnotes) == 0 { return nil } result := sx.Nil().Cons(tr.Make("ol")) currResult := result.AppendBang(sx.Nil().Cons(sx.Cons(tr.symClass, sx.MakeString("zs-endnotes"))).Cons(tr.symAttr)) for i, fni := range tr.endnotes { noteNum := strconv.Itoa(i + 1) noteID := tr.unique + noteNum attrs := fni.attrs.Cons(sx.Cons(tr.symClass, sx.MakeString("zs-endnote"))). Cons(sx.Cons(tr.Make("value"), sx.MakeString(noteNum))). Cons(sx.Cons(tr.Make("id"), sx.MakeString("fn:"+noteID))). Cons(sx.Cons(tr.Make("role"), sx.MakeString("doc-endnote"))). Cons(tr.symAttr) backref := sx.Nil().Cons(sx.MakeString("\u21a9\ufe0e")). Cons(sx.Nil(). Cons(sx.Cons(tr.symClass, sx.MakeString("zs-endnote-backref"))). Cons(sx.Cons(tr.Make("href"), sx.MakeString("#fnref:"+noteID))). Cons(sx.Cons(tr.Make("role"), sx.MakeString("doc-backlink"))). Cons(tr.symAttr)). Cons(tr.symA) li := sx.Nil().Cons(tr.Make("li")) li.AppendBang(attrs). ExtendBang(fni.noteHx). AppendBang(sx.MakeString(" ")).AppendBang(backref) currResult = currResult.AppendBang(li) } tr.endnotes = nil return result } // TransformEnv is the environment where the actual transformation takes places. type TransformEnv struct { tr *Transformer astSF sx.SymbolFactory astEnv sx.Environment err error textEnc *text.Encoder symNoEscape *sx.Symbol symAttr *sx.Symbol symA *sx.Symbol symSpan *sx.Symbol symP *sx.Symbol } func (te *TransformEnv) initialize() { te.symNoEscape = te.Make(sxhtml.NameSymNoEscape) te.symAttr = te.tr.symAttr te.symA = te.tr.symA te.symSpan = te.tr.symSpan te.symP = te.Make("p") te.bind(sz.NameSymList, 0, listArgs) te.bindMetadata() te.bindBlocks() te.bindInlines() } func listArgs(args []sx.Object) sx.Object { return sx.MakeList(args...) } func (te *TransformEnv) bindMetadata() { te.bind(sz.NameSymMeta, 0, listArgs) te.bind(sz.NameSymTypeZettelmarkup, 2, func(args []sx.Object) sx.Object { a := make(attrs.Attributes, 2). Set("name", te.getSymbol(args[0]).String()). Set("content", te.textEnc.Encode(te.getList(args[1]))) return te.transformMeta(a) }) metaString := func(args []sx.Object) sx.Object { a := make(attrs.Attributes, 2). Set("name", te.getSymbol(args[0]).Name()). Set("content", te.getString(args[1]).String()) return te.transformMeta(a) } te.bind(sz.NameSymTypeCredential, 2, metaString) te.bind(sz.NameSymTypeEmpty, 2, metaString) te.bind(sz.NameSymTypeID, 2, metaString) te.bind(sz.NameSymTypeNumber, 2, metaString) te.bind(sz.NameSymTypeString, 2, metaString) te.bind(sz.NameSymTypeTimestamp, 2, metaString) te.bind(sz.NameSymTypeURL, 2, metaString) te.bind(sz.NameSymTypeWord, 2, metaString) metaSet := func(args []sx.Object) sx.Object { var sb strings.Builder for elem := te.getList(args[1]); elem != nil; elem = elem.Tail() { sb.WriteByte(' ') sb.WriteString(te.getString(elem.Car()).String()) } s := sb.String() if len(s) > 0 { s = s[1:] } a := make(attrs.Attributes, 2). Set("name", te.getSymbol(args[0]).Name()). Set("content", s) return te.transformMeta(a) } te.bind(sz.NameSymTypeIDSet, 2, metaSet) te.bind(sz.NameSymTypeTagSet, 2, metaSet) te.bind(sz.NameSymTypeWordSet, 2, metaSet) } func (te *TransformEnv) bindBlocks() { te.bind(sz.NameSymBlock, 0, listArgs) te.bind(sz.NameSymPara, 0, func(args []sx.Object) sx.Object { // for ; args != nil; args = args.Tail() { // lst, ok := sx.GetList(args.Car()) // if !ok || lst != nil { // break // } // } return sx.MakeList(args...).Cons(te.symP) }) te.bind(sz.NameSymHeading, 5, func(args []sx.Object) sx.Object { nLevel := te.getInt64(args[0]) if nLevel <= 0 { te.err = fmt.Errorf("%v is a negative level", nLevel) return sx.Nil() } level := strconv.FormatInt(nLevel+te.tr.headingOffset, 10) a := te.getAttributes(args[1]) if fragment := te.getString(args[3]).String(); fragment != "" { a = a.Set("id", te.tr.unique+fragment) } if result, isPair := sx.GetPair(args[4]); isPair && result != nil { if len(a) > 0 { result = result.Cons(te.transformAttribute(a)) } return result.Cons(te.Make("h" + level)) } return sx.MakeList(te.Make("h"+level), sx.MakeString("<MISSING TEXT>")) }) te.bind(sz.NameSymThematic, 0, func(args []sx.Object) sx.Object { result := sx.Nil() if len(args) > 0 { if attrList := te.getList(args[0]); attrList != nil { result = result.Cons(te.transformAttribute(sz.GetAttributes(attrList))) } } return result.Cons(te.Make("hr")) }) te.bind(sz.NameSymListOrdered, 0, te.makeListFn("ol")) te.bind(sz.NameSymListUnordered, 0, te.makeListFn("ul")) te.bind(sz.NameSymDescription, 0, func(args []sx.Object) sx.Object { if len(args) == 0 { return sx.Nil() } items := sx.Nil().Cons(te.Make("dl")) curItem := items for pos := 0; pos < len(args); pos++ { term := te.getList(args[pos]) curItem = curItem.AppendBang(term.Cons(te.Make("dt"))) pos++ if pos >= len(args) { break } ddBlock := te.getList(args[pos]) if ddBlock == nil { break } for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() { dditem := te.getList(ddlst.Car()) curItem = curItem.AppendBang(dditem.Cons(te.Make("dd"))) } } return items }) te.bind(sz.NameSymListQuote, 0, func(args []sx.Object) sx.Object { if args == nil { return sx.Nil() } result := sx.Nil().Cons(te.Make("blockquote")) currResult := result for _, elem := range args { if quote, isPair := sx.GetPair(elem); isPair { currResult = currResult.AppendBang(quote.Cons(te.symP)) } } return result }) te.bind(sz.NameSymTable, 1, func(args []sx.Object) sx.Object { thead := sx.Nil() if header := te.getList(args[0]); !sx.IsNil(header) { thead = sx.Nil().Cons(te.transformTableRow(header)).Cons(te.Make("thead")) } tbody := sx.Nil() if len(args) > 1 { tbody = sx.Nil().Cons(te.Make("tbody")) curBody := tbody for _, row := range args[1:] { curBody = curBody.AppendBang(te.transformTableRow(te.getList(row))) } } table := sx.Nil() if tbody != nil { table = table.Cons(tbody) } if thead != nil { table = table.Cons(thead) } if table == nil { return sx.Nil() } return table.Cons(te.Make("table")) }) te.bind(sz.NameSymCell, 0, te.makeCellFn("")) te.bind(sz.NameSymCellCenter, 0, te.makeCellFn("center")) te.bind(sz.NameSymCellLeft, 0, te.makeCellFn("left")) te.bind(sz.NameSymCellRight, 0, te.makeCellFn("right")) te.bind(sz.NameSymRegionBlock, 2, te.makeRegionFn(te.Make("div"), true)) te.bind(sz.NameSymRegionQuote, 2, te.makeRegionFn(te.Make("blockquote"), false)) te.bind(sz.NameSymRegionVerse, 2, te.makeRegionFn(te.Make("div"), false)) te.bind(sz.NameSymVerbatimComment, 1, func(args []sx.Object) sx.Object { if te.getAttributes(args[0]).HasDefault() { if len(args) > 1 { if s := te.getString(args[1]); s != "" { t := sx.MakeString(s.String()) return sx.Nil().Cons(t).Cons(te.Make(sxhtml.NameSymBlockComment)) } } } return nil }) te.bind(sz.NameSymVerbatimEval, 2, func(args []sx.Object) sx.Object { return te.transformVerbatim(te.getAttributes(args[0]).AddClass("zs-eval"), te.getString(args[1])) }) te.bind(sz.NameSymVerbatimHTML, 2, te.transformHTML) te.bind(sz.NameSymVerbatimMath, 2, func(args []sx.Object) sx.Object { return te.transformVerbatim(te.getAttributes(args[0]).AddClass("zs-math"), te.getString(args[1])) }) te.bind(sz.NameSymVerbatimProg, 2, func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) content := te.getString(args[1]) if a.HasDefault() { content = sx.MakeString(visibleReplacer.Replace(content.String())) } return te.transformVerbatim(a, content) }) te.bind(sz.NameSymVerbatimZettel, 0, func([]sx.Object) sx.Object { return sx.Nil() }) te.bind(sz.NameSymBLOB, 3, func(args []sx.Object) sx.Object { return te.transformBLOB(te.getList(args[0]), te.getString(args[1]), te.getString(args[2])) }) te.bind(sz.NameSymTransclude, 2, func(args []sx.Object) 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 := te.getString(ref.Tail().Car()); refValue != "" { if te.astSF.MustMake(sz.NameSymRefStateExternal).IsEqual(refKind) { a := te.getAttributes(args[0]).Set("src", refValue.String()).AddClass("external") return sx.Nil().Cons(sx.Nil().Cons(te.transformAttribute(a)).Cons(te.Make("img"))).Cons(te.symP) } return sx.MakeList( te.Make(sxhtml.NameSymInlineComment), sx.MakeString("transclude"), refKind, sx.MakeString("->"), refValue, ) } return sx.MakeList(args...) }) } func (te *TransformEnv) makeListFn(tag string) transformFn { sym := te.Make(tag) return func(args []sx.Object) sx.Object { result := sx.Nil().Cons(sym) last := result for _, elem := range args { item := sx.Nil().Cons(te.Make("li")) if res, isPair := sx.GetPair(elem); isPair { item.ExtendBang(res) } last = last.AppendBang(item) } return result } } func (te *TransformEnv) transformTableRow(pairs *sx.Pair) *sx.Pair { row := sx.Nil().Cons(te.Make("tr")) if pairs == nil { return nil } curRow := row for pair := pairs; pair != nil; pair = pair.Tail() { curRow = curRow.AppendBang(pair.Car()) } return row } func (te *TransformEnv) makeCellFn(align string) transformFn { return func(args []sx.Object) sx.Object { tdata := sx.MakeList(args...) if align != "" { tdata = tdata.Cons(te.transformAttribute(attrs.Attributes{"class": align})) } return tdata.Cons(te.Make("td")) } } func (te *TransformEnv) makeRegionFn(sym *sx.Symbol, genericToClass bool) transformFn { return func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) if genericToClass { if val, found := a.Get(""); found { a = a.Remove("").AddClass(val) } } result := sx.Nil() if len(a) > 0 { result = result.Cons(te.transformAttribute(a)) } result = result.Cons(sym) currResult := result.LastPair() if region, isPair := sx.GetPair(args[1]); isPair { currResult = currResult.ExtendBang(region) } if len(args) > 2 { if cite, isPair := sx.GetPair(args[2]); isPair && cite != nil { currResult.AppendBang(cite.Cons(te.Make("cite"))) } } return result } } func (te *TransformEnv) transformVerbatim(a attrs.Attributes, s sx.String) sx.Object { a = setProgLang(a) code := sx.Nil().Cons(s) if al := te.transformAttribute(a); al != nil { code = code.Cons(al) } code = code.Cons(te.Make("code")) return sx.Nil().Cons(code).Cons(te.Make("pre")) } func (te *TransformEnv) bindInlines() { te.bind(sz.NameSymInline, 0, listArgs) te.bind(sz.NameSymText, 1, func(args []sx.Object) sx.Object { return te.getString(args[0]) }) te.bind(sz.NameSymSpace, 0, func(args []sx.Object) sx.Object { if len(args) == 0 { return sx.MakeString(" ") } return te.getString(args[0]) }) te.bind(sz.NameSymSoft, 0, func([]sx.Object) sx.Object { return sx.MakeString(" ") }) brSym := te.Make("br") te.bind(sz.NameSymHard, 0, func([]sx.Object) sx.Object { return sx.Nil().Cons(brSym) }) te.bind(sz.NameSymLinkInvalid, 2, func(args []sx.Object) sx.Object { // a := te.getAttributes(args) var inline *sx.Pair if len(args) > 2 { inline = sx.MakeList(args[2:]...) } if inline == nil { inline = sx.Nil().Cons(args[1]) } return inline.Cons(te.symSpan) }) transformHREF := func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) refValue := te.getString(args[1]) return te.transformLink(a.Set("href", refValue.String()), refValue, args[2:]) } te.bind(sz.NameSymLinkZettel, 2, transformHREF) te.bind(sz.NameSymLinkSelf, 2, transformHREF) te.bind(sz.NameSymLinkFound, 2, transformHREF) te.bind(sz.NameSymLinkBroken, 2, func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) refValue := te.getString(args[1]) return te.transformLink(a.AddClass("broken"), refValue, args[2:]) }) te.bind(sz.NameSymLinkHosted, 2, transformHREF) te.bind(sz.NameSymLinkBased, 2, transformHREF) te.bind(sz.NameSymLinkQuery, 2, func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) refValue := te.getString(args[1]) query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String()) return te.transformLink(a.Set("href", query), refValue, args[2:]) }) te.bind(sz.NameSymLinkExternal, 2, func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) refValue := te.getString(args[1]) return te.transformLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args[2:]) }) te.bind(sz.NameSymEmbed, 3, func(args []sx.Object) sx.Object { ref := te.getList(args[1]) syntax := te.getString(args[2]) if syntax == api.ValueSyntaxSVG { embedAttr := sx.MakeList( te.symAttr, sx.Cons(te.Make("type"), sx.MakeString("image/svg+xml")), sx.Cons(te.Make("src"), sx.MakeString("/"+te.getString(ref.Tail()).String()+".svg")), ) return sx.MakeList( te.Make("figure"), sx.MakeList( te.Make("embed"), embedAttr, ), ) } a := te.getAttributes(args[0]) a = a.Set("src", string(te.getString(ref.Tail().Car()))) var sb strings.Builder te.flattenText(&sb, ref.Tail().Tail().Tail()) if d := sb.String(); d != "" { a = a.Set("alt", d) } return sx.MakeList(te.Make("img"), te.transformAttribute(a)) }) te.bind(sz.NameSymEmbedBLOB, 3, func(args []sx.Object) sx.Object { a, syntax, data := te.getAttributes(args[0]), te.getString(args[1]), te.getString(args[2]) summary, _ := a.Get(api.KeySummary) return te.transformBLOB( sx.MakeList(te.astSF.MustMake(sz.NameSymInline), sx.MakeString(summary)), syntax, data, ) }) te.bind(sz.NameSymCite, 2, func(args []sx.Object) sx.Object { result := sx.Nil() if key := te.getString(args[1]); key != "" { if len(args) > 2 { result = sx.MakeList(args[2:]...).Cons(sx.MakeString(", ")) } result = result.Cons(key) } if a := te.getAttributes(args[0]); len(a) > 0 { result = result.Cons(te.transformAttribute(a)) } if result == nil { return nil } return result.Cons(te.symSpan) }) te.bind(sz.NameSymMark, 3, func(args []sx.Object) sx.Object { result := sx.MakeList(args[3:]...) if !te.tr.noLinks { if fragment := te.getString(args[2]); fragment != "" { a := attrs.Attributes{"id": fragment.String() + te.tr.unique} return result.Cons(te.transformAttribute(a)).Cons(te.symA) } } return result.Cons(te.symSpan) }) te.bind(sz.NameSymEndnote, 1, func(args []sx.Object) sx.Object { attrPlist := sx.Nil() if a := te.getAttributes(args[0]); len(a) > 0 { if attrs := te.transformAttribute(a); attrs != nil { attrPlist = attrs.Tail() } } text, isPair := sx.GetPair(args[1]) if !isPair { return sx.Nil() } te.tr.endnotes = append(te.tr.endnotes, endnoteInfo{noteAST: text, noteHx: nil, attrs: attrPlist}) noteNum := strconv.Itoa(len(te.tr.endnotes)) noteID := te.tr.unique + noteNum hrefAttr := sx.Nil().Cons(sx.Cons(te.Make("role"), sx.MakeString("doc-noteref"))). Cons(sx.Cons(te.Make("href"), sx.MakeString("#fn:"+noteID))). Cons(sx.Cons(te.tr.symClass, sx.MakeString("zs-noteref"))). Cons(te.symAttr) href := sx.Nil().Cons(sx.MakeString(noteNum)).Cons(hrefAttr).Cons(te.symA) supAttr := sx.Nil().Cons(sx.Cons(te.Make("id"), sx.MakeString("fnref:"+noteID))).Cons(te.symAttr) return sx.Nil().Cons(href).Cons(supAttr).Cons(te.Make("sup")) }) te.bind(sz.NameSymFormatDelete, 1, te.makeFormatFn("del")) te.bind(sz.NameSymFormatEmph, 1, te.makeFormatFn("em")) te.bind(sz.NameSymFormatInsert, 1, te.makeFormatFn("ins")) te.bind(sz.NameSymFormatQuote, 1, te.transformQuote) te.bind(sz.NameSymFormatSpan, 1, te.makeFormatFn("span")) te.bind(sz.NameSymFormatStrong, 1, te.makeFormatFn("strong")) te.bind(sz.NameSymFormatSub, 1, te.makeFormatFn("sub")) te.bind(sz.NameSymFormatSuper, 1, te.makeFormatFn("sup")) te.bind(sz.NameSymLiteralComment, 1, func(args []sx.Object) sx.Object { if te.getAttributes(args[0]).HasDefault() { if len(args) > 1 { if s := te.getString(args[1]); s != "" { return sx.Nil().Cons(s).Cons(te.Make(sxhtml.NameSymInlineComment)) } } } return sx.Nil() }) te.bind(sz.NameSymLiteralHTML, 2, te.transformHTML) kbdSym := te.Make("kbd") te.bind(sz.NameSymLiteralInput, 2, func(args []sx.Object) sx.Object { return te.transformLiteral(args, nil, kbdSym) }) codeSym := te.Make("code") te.bind(sz.NameSymLiteralMath, 2, func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]).AddClass("zs-math") return te.transformLiteral(args, a, codeSym) }) sampSym := te.Make("samp") te.bind(sz.NameSymLiteralOutput, 2, func(args []sx.Object) sx.Object { return te.transformLiteral(args, nil, sampSym) }) te.bind(sz.NameSymLiteralProg, 2, func(args []sx.Object) sx.Object { return te.transformLiteral(args, nil, codeSym) }) te.bind(sz.NameSymLiteralZettel, 0, func([]sx.Object) sx.Object { return sx.Nil() }) } func (te *TransformEnv) makeFormatFn(tag string) transformFn { sym := te.Make(tag) return func(args []sx.Object) sx.Object { a := te.getAttributes(args[0]) if val, found := a.Get(""); found { a = a.Remove("").AddClass(val) } res := sx.MakeList(args[1:]...) if len(a) > 0 { res = res.Cons(te.transformAttribute(a)) } return res.Cons(sym) } } func (te *TransformEnv) transformQuote(args []sx.Object) sx.Object { const langAttr = "lang" a := te.getAttributes(args[0]) langVal, found := a.Get(langAttr) if found { a = a.Remove(langAttr) } if val, found2 := a.Get(""); found2 { a = a.Remove("").AddClass(val) } res := sx.MakeList(args[1:]...) if len(a) > 0 { res = res.Cons(te.transformAttribute(a)) } res = res.Cons(te.Make("q")) if found { res = sx.Nil().Cons(res).Cons(te.transformAttribute(attrs.Attributes{}.Set(langAttr, langVal))).Cons(te.symSpan) } return res } var visibleReplacer = strings.NewReplacer(" ", "\u2423") func (te *TransformEnv) transformLiteral(args []sx.Object, a attrs.Attributes, sym *sx.Symbol) sx.Object { if a == nil { a = te.getAttributes(args[0]) } a = setProgLang(a) literal := te.getString(args[1]).String() if a.HasDefault() { a = a.RemoveDefault() literal = visibleReplacer.Replace(literal) } res := sx.Nil().Cons(sx.MakeString(literal)) if len(a) > 0 { res = res.Cons(te.transformAttribute(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 (te *TransformEnv) transformHTML(args []sx.Object) sx.Object { if s := te.getString(args[1]); s != "" && IsSafe(s.String()) { return sx.Nil().Cons(s).Cons(te.symNoEscape) } return nil } func (te *TransformEnv) transformBLOB(description *sx.Pair, syntax, data sx.String) sx.Object { if data == "" { return sx.Nil() } switch syntax { case "": return sx.Nil() case api.ValueSyntaxSVG: return sx.Nil().Cons(sx.Nil().Cons(data).Cons(te.symNoEscape)).Cons(te.symP) default: imgAttr := sx.Nil().Cons(sx.Cons(te.Make("src"), sx.MakeString("data:image/"+syntax.String()+";base64,"+data.String()))) var sb strings.Builder te.flattenText(&sb, description) if d := sb.String(); d != "" { imgAttr = imgAttr.Cons(sx.Cons(te.Make("alt"), sx.MakeString(d))) } return sx.Nil().Cons(sx.Nil().Cons(imgAttr.Cons(te.symAttr)).Cons(te.Make("img"))).Cons(te.symP) } } func (te *TransformEnv) flattenText(sb *strings.Builder, lst *sx.Pair) { for elem := lst; elem != nil; elem = elem.Tail() { switch obj := elem.Car().(type) { case sx.String: sb.WriteString(obj.String()) case *sx.Pair: te.flattenText(sb, obj) } } } type transformFn func([]sx.Object) sx.Object func (te *TransformEnv) bind(name string, minArity int, fn transformFn) { te.astEnv.Bind(te.astSF.MustMake(name), sxeval.BuiltinA(func(args []sx.Object) (sx.Object, error) { if nArgs := len(args); nArgs < minArity { return sx.Nil(), fmt.Errorf("not enough arguments (%d) for form %v (%d)", nArgs, name, minArity) } res := fn(args) return res, te.err })) } func (te *TransformEnv) Rebind(name string, fn func([]sx.Object, sxeval.Callable) sx.Object) { sym := te.astSF.MustMake(name) obj, found := te.astEnv.Lookup(sym) if !found { panic(sym.String()) } preFn, ok := sxeval.GetCallable(obj) if !ok { panic(sym.String()) } te.astEnv.Bind(sym, sxeval.BuiltinA(func(args []sx.Object) (sx.Object, error) { res := fn(args, preFn) return res, te.err })) } func (te *TransformEnv) Make(name string) *sx.Symbol { return te.tr.Make(name) } func (te *TransformEnv) getSymbol(val sx.Object) *sx.Symbol { if te.err != nil { return nil } if sym, ok := sx.GetSymbol(val); ok { return sym } te.err = fmt.Errorf("%v/%T is not a symbol", val, val) return nil } func (te *TransformEnv) getString(val sx.Object) sx.String { if te.err != nil { return "" } if s, ok := sx.GetString(val); ok { return s } te.err = fmt.Errorf("%v/%T is not a string", val, val) return "" } func (te *TransformEnv) getInt64(val sx.Object) int64 { if te.err != nil { return -1017 } if num, ok := sx.GetNumber(val); ok { return int64(num.(sx.Int64)) } te.err = fmt.Errorf("%v/%T is not a number", val, val) return -1017 } func (te *TransformEnv) getList(val sx.Object) *sx.Pair { if te.err == nil { if res, isPair := sx.GetPair(val); isPair { return res } te.err = fmt.Errorf("%v/%T is not a list", val, val) } return nil } func (te *TransformEnv) getAttributes(args sx.Object) attrs.Attributes { return sz.GetAttributes(te.getList(args)) } func (te *TransformEnv) transformLink(a attrs.Attributes, refValue sx.String, inline []sx.Object) sx.Object { result := sx.MakeList(inline...) if len(inline) == 0 { result = sx.Nil().Cons(refValue) } if te.tr.noLinks { return result.Cons(te.symSpan) } return result.Cons(te.transformAttribute(a)).Cons(te.symA) } func (te *TransformEnv) transformAttribute(a attrs.Attributes) *sx.Pair { return te.tr.TransformAttrbute(a) } func (te *TransformEnv) transformMeta(a attrs.Attributes) *sx.Pair { return te.tr.TransformMeta(a) } var unsafeSnippets = []string{ "<script", "</script", "<iframe", "</iframe", } |
︙ | ︙ |
Changes to sz/const.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | | > | > | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | < | | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > | < > | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package sz import "zettelstore.de/sx.fossil" // Various constants for Zettel data. Some of them are technically variables. const ( // Symbols for Metanodes NameSymBlock = "BLOCK" NameSymInline = "INLINE" NameSymList = "list" NameSymMeta = "META" NameSymQuote = "quote" // Symbols for Zettel node types. NameSymBLOB = "BLOB" NameSymCell = "CELL" NameSymCellCenter = "CELL-CENTER" NameSymCellLeft = "CELL-LEFT" NameSymCellRight = "CELL-RIGHT" NameSymCite = "CITE" NameSymDescription = "DESCRIPTION" NameSymEmbed = "EMBED" NameSymEmbedBLOB = "EMBED-BLOB" NameSymEndnote = "ENDNOTE" NameSymFormatEmph = "FORMAT-EMPH" NameSymFormatDelete = "FORMAT-DELETE" NameSymFormatInsert = "FORMAT-INSERT" NameSymFormatQuote = "FORMAT-QUOTE" NameSymFormatSpan = "FORMAT-SPAN" NameSymFormatSub = "FORMAT-SUB" NameSymFormatSuper = "FORMAT-SUPER" NameSymFormatStrong = "FORMAT-STRONG" NameSymHard = "HARD" NameSymHeading = "HEADING" NameSymLinkInvalid = "LINK-INVALID" NameSymLinkZettel = "LINK-ZETTEL" NameSymLinkSelf = "LINK-SELF" NameSymLinkFound = "LINK-FOUND" NameSymLinkBroken = "LINK-BROKEN" NameSymLinkHosted = "LINK-HOSTED" NameSymLinkBased = "LINK-BASED" NameSymLinkQuery = "LINK-QUERY" NameSymLinkExternal = "LINK-EXTERNAL" NameSymListOrdered = "ORDERED" NameSymListUnordered = "UNORDERED" NameSymListQuote = "QUOTATION" NameSymLiteralProg = "LITERAL-CODE" NameSymLiteralComment = "LITERAL-COMMENT" NameSymLiteralHTML = "LITERAL-HTML" NameSymLiteralInput = "LITERAL-INPUT" NameSymLiteralMath = "LITERAL-MATH" NameSymLiteralOutput = "LITERAL-OUTPUT" NameSymLiteralZettel = "LITERAL-ZETTEL" NameSymMark = "MARK" NameSymPara = "PARA" NameSymRegionBlock = "REGION-BLOCK" NameSymRegionQuote = "REGION-QUOTE" NameSymRegionVerse = "REGION-VERSE" NameSymSoft = "SOFT" NameSymSpace = "SPACE" NameSymTable = "TABLE" NameSymText = "TEXT" NameSymThematic = "THEMATIC" NameSymTransclude = "TRANSCLUDE" NameSymUnknown = "UNKNOWN-NODE" NameSymVerbatimComment = "VERBATIM-COMMENT" NameSymVerbatimEval = "VERBATIM-EVAL" NameSymVerbatimHTML = "VERBATIM-HTML" NameSymVerbatimMath = "VERBATIM-MATH" NameSymVerbatimProg = "VERBATIM-CODE" NameSymVerbatimZettel = "VERBATIM-ZETTEL" // Constant symbols for reference states. NameSymRefStateInvalid = "INVALID" NameSymRefStateZettel = "ZETTEL" NameSymRefStateSelf = "SELF" NameSymRefStateFound = "FOUND" NameSymRefStateBroken = "BROKEN" NameSymRefStateHosted = "HOSTED" NameSymRefStateBased = "BASED" NameSymRefStateQuery = "QUERY" NameSymRefStateExternal = "EXTERNAL" // Symbols for metadata types. NameSymTypeCredential = "CREDENTIAL" NameSymTypeEmpty = "EMPTY-STRING" NameSymTypeID = "ZID" NameSymTypeIDSet = "ZID-SET" NameSymTypeNumber = "NUMBER" NameSymTypeString = "STRING" NameSymTypeTagSet = "TAG-SET" NameSymTypeTimestamp = "TIMESTAMP" NameSymTypeURL = "URL" NameSymTypeWord = "WORD" NameSymTypeWordSet = "WORD-SET" NameSymTypeZettelmarkup = "ZETTELMARKUP" ) // ZettelSymbols collect all symbols needed to represent zettel data. type ZettelSymbols struct { // Symbols for Metanodes SymBlock *sx.Symbol SymInline *sx.Symbol SymList *sx.Symbol SymMeta *sx.Symbol SymQuote *sx.Symbol // Symbols for Zettel node types. SymBLOB *sx.Symbol SymCell *sx.Symbol SymCellCenter *sx.Symbol SymCellLeft *sx.Symbol SymCellRight *sx.Symbol SymCite *sx.Symbol SymDescription *sx.Symbol SymEmbed *sx.Symbol SymEmbedBLOB *sx.Symbol SymEndnote *sx.Symbol SymFormatEmph *sx.Symbol SymFormatDelete *sx.Symbol SymFormatInsert *sx.Symbol SymFormatQuote *sx.Symbol SymFormatSpan *sx.Symbol SymFormatSub *sx.Symbol SymFormatSuper *sx.Symbol SymFormatStrong *sx.Symbol SymHard *sx.Symbol SymHeading *sx.Symbol SymLinkInvalid *sx.Symbol SymLinkZettel *sx.Symbol SymLinkSelf *sx.Symbol SymLinkFound *sx.Symbol SymLinkBroken *sx.Symbol SymLinkHosted *sx.Symbol SymLinkBased *sx.Symbol SymLinkQuery *sx.Symbol SymLinkExternal *sx.Symbol SymListOrdered *sx.Symbol SymListUnordered *sx.Symbol SymListQuote *sx.Symbol SymLiteralProg *sx.Symbol SymLiteralComment *sx.Symbol SymLiteralHTML *sx.Symbol SymLiteralInput *sx.Symbol SymLiteralMath *sx.Symbol SymLiteralOutput *sx.Symbol SymLiteralZettel *sx.Symbol SymMark *sx.Symbol SymPara *sx.Symbol SymRegionBlock *sx.Symbol SymRegionQuote *sx.Symbol SymRegionVerse *sx.Symbol SymSoft *sx.Symbol SymSpace *sx.Symbol SymTable *sx.Symbol SymText *sx.Symbol SymThematic *sx.Symbol SymTransclude *sx.Symbol SymUnknown *sx.Symbol SymVerbatimComment *sx.Symbol SymVerbatimEval *sx.Symbol SymVerbatimHTML *sx.Symbol SymVerbatimMath *sx.Symbol SymVerbatimProg *sx.Symbol SymVerbatimZettel *sx.Symbol // Constant symbols for reference states. SymRefStateInvalid *sx.Symbol SymRefStateZettel *sx.Symbol SymRefStateSelf *sx.Symbol SymRefStateFound *sx.Symbol SymRefStateBroken *sx.Symbol SymRefStateHosted *sx.Symbol SymRefStateBased *sx.Symbol SymRefStateQuery *sx.Symbol SymRefStateExternal *sx.Symbol // Symbols for metadata types SymTypeCredential *sx.Symbol SymTypeEmpty *sx.Symbol SymTypeID *sx.Symbol SymTypeIDSet *sx.Symbol SymTypeNumber *sx.Symbol SymTypeString *sx.Symbol SymTypeTagSet *sx.Symbol SymTypeTimestamp *sx.Symbol SymTypeURL *sx.Symbol SymTypeWord *sx.Symbol SymTypeWordSet *sx.Symbol SymTypeZettelmarkup *sx.Symbol } func (zs *ZettelSymbols) InitializeZettelSymbols(sf sx.SymbolFactory) { // Symbols for Metanodes zs.SymBlock = sf.MustMake(NameSymBlock) zs.SymInline = sf.MustMake(NameSymInline) zs.SymList = sf.MustMake(NameSymList) zs.SymMeta = sf.MustMake(NameSymMeta) zs.SymQuote = sf.MustMake(NameSymQuote) // Symbols for Zettel node types. zs.SymBLOB = sf.MustMake(NameSymBLOB) zs.SymCell = sf.MustMake(NameSymCell) zs.SymCellCenter = sf.MustMake(NameSymCellCenter) zs.SymCellLeft = sf.MustMake(NameSymCellLeft) zs.SymCellRight = sf.MustMake(NameSymCellRight) zs.SymCite = sf.MustMake(NameSymCite) zs.SymDescription = sf.MustMake(NameSymDescription) zs.SymEmbed = sf.MustMake(NameSymEmbed) zs.SymEmbedBLOB = sf.MustMake(NameSymEmbedBLOB) zs.SymEndnote = sf.MustMake(NameSymEndnote) zs.SymFormatEmph = sf.MustMake(NameSymFormatEmph) zs.SymFormatDelete = sf.MustMake(NameSymFormatDelete) zs.SymFormatInsert = sf.MustMake(NameSymFormatInsert) zs.SymFormatQuote = sf.MustMake(NameSymFormatQuote) zs.SymFormatSpan = sf.MustMake(NameSymFormatSpan) zs.SymFormatSub = sf.MustMake(NameSymFormatSub) zs.SymFormatSuper = sf.MustMake(NameSymFormatSuper) zs.SymFormatStrong = sf.MustMake(NameSymFormatStrong) zs.SymHard = sf.MustMake(NameSymHard) zs.SymHeading = sf.MustMake(NameSymHeading) zs.SymLinkInvalid = sf.MustMake(NameSymLinkInvalid) zs.SymLinkZettel = sf.MustMake(NameSymLinkZettel) zs.SymLinkSelf = sf.MustMake(NameSymLinkSelf) zs.SymLinkFound = sf.MustMake(NameSymLinkFound) zs.SymLinkBroken = sf.MustMake(NameSymLinkBroken) zs.SymLinkHosted = sf.MustMake(NameSymLinkHosted) zs.SymLinkBased = sf.MustMake(NameSymLinkBased) zs.SymLinkQuery = sf.MustMake(NameSymLinkQuery) zs.SymLinkExternal = sf.MustMake(NameSymLinkExternal) zs.SymListOrdered = sf.MustMake(NameSymListOrdered) zs.SymListUnordered = sf.MustMake(NameSymListUnordered) zs.SymListQuote = sf.MustMake(NameSymListQuote) zs.SymLiteralProg = sf.MustMake(NameSymLiteralProg) zs.SymLiteralComment = sf.MustMake(NameSymLiteralComment) zs.SymLiteralHTML = sf.MustMake(NameSymLiteralHTML) zs.SymLiteralInput = sf.MustMake(NameSymLiteralInput) zs.SymLiteralMath = sf.MustMake(NameSymLiteralMath) zs.SymLiteralOutput = sf.MustMake(NameSymLiteralOutput) zs.SymLiteralZettel = sf.MustMake(NameSymLiteralZettel) zs.SymMark = sf.MustMake(NameSymMark) zs.SymPara = sf.MustMake(NameSymPara) zs.SymRegionBlock = sf.MustMake(NameSymRegionBlock) zs.SymRegionQuote = sf.MustMake(NameSymRegionQuote) zs.SymRegionVerse = sf.MustMake(NameSymRegionVerse) zs.SymSoft = sf.MustMake(NameSymSoft) zs.SymSpace = sf.MustMake(NameSymSpace) zs.SymTable = sf.MustMake(NameSymTable) zs.SymText = sf.MustMake(NameSymText) zs.SymThematic = sf.MustMake(NameSymThematic) zs.SymTransclude = sf.MustMake(NameSymTransclude) zs.SymUnknown = sf.MustMake(NameSymUnknown) zs.SymVerbatimComment = sf.MustMake(NameSymVerbatimComment) zs.SymVerbatimEval = sf.MustMake(NameSymVerbatimEval) zs.SymVerbatimHTML = sf.MustMake(NameSymVerbatimHTML) zs.SymVerbatimMath = sf.MustMake(NameSymVerbatimMath) zs.SymVerbatimProg = sf.MustMake(NameSymVerbatimProg) zs.SymVerbatimZettel = sf.MustMake(NameSymVerbatimZettel) // Constant symbols for reference states. zs.SymRefStateInvalid = sf.MustMake(NameSymRefStateInvalid) zs.SymRefStateZettel = sf.MustMake(NameSymRefStateZettel) zs.SymRefStateSelf = sf.MustMake(NameSymRefStateSelf) zs.SymRefStateFound = sf.MustMake(NameSymRefStateFound) zs.SymRefStateBroken = sf.MustMake(NameSymRefStateBroken) zs.SymRefStateHosted = sf.MustMake(NameSymRefStateHosted) zs.SymRefStateBased = sf.MustMake(NameSymRefStateBased) zs.SymRefStateQuery = sf.MustMake(NameSymRefStateQuery) zs.SymRefStateExternal = sf.MustMake(NameSymRefStateExternal) // Symbols for metadata types. zs.SymTypeCredential = sf.MustMake(NameSymTypeCredential) zs.SymTypeEmpty = sf.MustMake(NameSymTypeEmpty) zs.SymTypeID = sf.MustMake(NameSymTypeID) zs.SymTypeIDSet = sf.MustMake(NameSymTypeIDSet) zs.SymTypeNumber = sf.MustMake(NameSymTypeNumber) zs.SymTypeString = sf.MustMake(NameSymTypeString) zs.SymTypeTagSet = sf.MustMake(NameSymTypeTagSet) zs.SymTypeTimestamp = sf.MustMake(NameSymTypeTimestamp) zs.SymTypeURL = sf.MustMake(NameSymTypeURL) zs.SymTypeWord = sf.MustMake(NameSymTypeWord) zs.SymTypeWordSet = sf.MustMake(NameSymTypeWordSet) zs.SymTypeZettelmarkup = sf.MustMake(NameSymTypeZettelmarkup) } |
Added sz/const_test.go.
> > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package sz_test import ( "testing" "zettelstore.de/client.fossil/sz" "zettelstore.de/sx.fossil" ) func BenchmarkInitializeZettelSymbols(b *testing.B) { sf := sx.MakeMappedFactory() for i := 0; i < b.N; i++ { var zs sz.ZettelSymbols zs.InitializeZettelSymbols(sf) } } |
Deleted sz/parser.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/parser_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to sz/sz.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < < | | | < < < < < < < < < < < | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- package sz import ( "zettelstore.de/client.fossil/attrs" "zettelstore.de/sx.fossil" ) // GetAttributes traverses a s-expression list and returns an attribute structure. func GetAttributes(seq *sx.Pair) (result attrs.Attributes) { for elem := seq; elem != nil; elem = elem.Tail() { pair, isPair := sx.GetPair(elem.Car()) if !isPair || pair == nil { continue } key := pair.Car() if !key.IsAtom() { continue } val := pair.Cdr() if tail, isTailPair := sx.GetPair(val); isTailPair { val = tail.Car() } if !val.IsAtom() { continue } result = result.Set(key.String(), val.String()) } return result } // GetMetaContent returns the metadata and the content of a sz encoded zettel. func GetMetaContent(zettel sx.Object) (Meta, *sx.Pair) { if pair, isPair := sx.GetPair(zettel); isPair { m := pair.Car() if s := pair.Tail(); s != nil { if content, isContentPair := sx.GetPair(s.Car()); isContentPair { return MakeMeta(m), content |
︙ | ︙ | |||
77 78 79 80 81 82 83 | func MakeMeta(obj sx.Object) Meta { if result := doMakeMeta(obj); len(result) > 0 { return result } return nil } func doMakeMeta(obj sx.Object) Meta { | > > > > > | | | | < < | > < > > > > | | > > > > > > > > > > > | > | > > | | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 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 | func MakeMeta(obj sx.Object) Meta { if result := doMakeMeta(obj); len(result) > 0 { return result } return nil } func doMakeMeta(obj sx.Object) Meta { result := make(map[string]MetaValue) for { if sx.IsNil(obj) { return result } pair, isPair := sx.GetPair(obj) if !isPair { return result } if mv, ok2 := makeMetaValue(pair); ok2 { result[mv.Key] = mv } obj = pair.Cdr() } } func makeMetaValue(mnode *sx.Pair) (MetaValue, bool) { var result MetaValue mval, isPair := sx.GetPair(mnode.Car()) if !isPair { return result, false } typeSym, isSymbol := sx.GetSymbol(mval.Car()) if !isSymbol { return result, false } keyPair, isPair := sx.GetPair(mval.Cdr()) if !isPair { return result, false } keyList, isPair := sx.GetPair(keyPair.Car()) if !isPair { return result, false } quoteSym, isSymbol := sx.GetSymbol(keyList.Car()) if !isSymbol || quoteSym.Name() != "quote" { return result, false } keySym, isSymbol := sx.GetSymbol(keyList.Tail().Car()) if !isSymbol { return result, false } valPair, isPair := sx.GetPair(keyPair.Cdr()) if !isPair { return result, false } result.Type = typeSym.Name() result.Key = keySym.Name() result.Value = valPair.Car() return result, true } func (m Meta) GetString(key string) string { if v, found := m[key]; found { return v.Value.String() } 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 } |
Deleted sz/walk.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/block.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/inline.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/post-processor.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/ref.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/ref_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/zmk.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/zmk_fuzz_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Deleted sz/zmk/zmk_test.go.
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Changes to text/text.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | < | > | > > > > > | > | > > > > | > > > > > > | > > | < < < < | < | < < < < < < | | | | 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 | //----------------------------------------------------------------------------- // 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. //----------------------------------------------------------------------------- // Package text provides types, constants and function to work with text output. package text import ( "strings" "zettelstore.de/client.fossil/sz" "zettelstore.de/sx.fossil" ) // Encoder is the structure to hold relevant data to execute the encoding. type Encoder struct { sf sx.SymbolFactory sb strings.Builder symText *sx.Symbol symSpace *sx.Symbol symSoft *sx.Symbol symHard *sx.Symbol symQuote *sx.Symbol } func NewEncoder(sf sx.SymbolFactory) *Encoder { if sf == nil { return nil } enc := &Encoder{ sf: sf, sb: strings.Builder{}, symText: sf.MustMake(sz.NameSymText), symSpace: sf.MustMake(sz.NameSymSpace), symSoft: sf.MustMake(sz.NameSymSoft), symHard: sf.MustMake(sz.NameSymHard), symQuote: sf.MustMake(sz.NameSymQuote), } return enc } func (enc *Encoder) Encode(lst *sx.Pair) string { enc.executeList(lst) result := enc.sb.String() enc.sb.Reset() return result } // EvaluateInlineString returns the text content of the given inline list as a string. func EvaluateInlineString(lst *sx.Pair) string { if sf := sx.FindSymbolFactory(lst); sf != nil { return NewEncoder(sf).Encode(lst) } return "" } func (enc *Encoder) executeList(lst *sx.Pair) { for elem := lst; elem != nil; elem = elem.Tail() { enc.execute(elem.Car()) } } func (enc *Encoder) execute(obj sx.Object) { cmd, isPair := sx.GetPair(obj) if !isPair { return } sym := cmd.Car() if sx.IsNil(sym) { return } if sym.IsEqual(enc.symText) { args := cmd.Tail() if args == nil { return } if val, isString := sx.GetString(args.Car()); isString { enc.sb.WriteString(val.String()) } } else if sym.IsEqual(enc.symSpace) || sym.IsEqual(enc.symSoft) { enc.sb.WriteByte(' ') } else if sym.IsEqual(enc.symHard) { enc.sb.WriteByte('\n') } else if !sym.IsEqual(enc.symQuote) { enc.executeList(cmd.Tail()) } } |
Changes to text/text_test.go.
1 2 3 4 5 6 7 8 | //----------------------------------------------------------------------------- // 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. | < < < | | | | < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //----------------------------------------------------------------------------- // Copyright (c) 2022-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. //----------------------------------------------------------------------------- package text_test import ( "strings" "testing" "zettelstore.de/client.fossil/text" "zettelstore.de/sx.fossil" "zettelstore.de/sx.fossil/sxreader" ) func TestSzText(t *testing.T) { testcases := []struct { src string exp string }{ {"()", ""}, {`(INLINE (TEXT "a"))`, "a"}, {`(INLINE (SPACE "a"))`, " "}, } for i, tc := range testcases { sval, err := sxreader.MakeReader(strings.NewReader(tc.src)).Read() if err != nil { t.Error(err) continue } |
︙ | ︙ |
Changes to www/changes.wiki.
1 2 | <title>Change Log</title> | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 1 2 3 4 5 6 7 8 9 10 11 | <title>Change Log</title> <a name="0_13"></a> <h2>Changes for Version 0.13.0 (pending)</h2> * API uses plain data or sx data, but no JSON encoded data. * Dependency sx is now hosted on Fossil repository, same for this library. <a name="0_12"></a> <h2>Changes for Version 0.12.0 (2023-06-05)</h2> * Rename "sexpr" to "sz". |
︙ | ︙ |
Changes to www/index.wiki.
1 2 3 4 5 | <title>Home</title> This repository contains Go client software to access [https://zettelstore.de|Zettelstore] via its API. | | | | | | | < < < < < < < < < < < < < < < < < < < < < < < | 1 2 3 4 5 6 7 8 9 10 11 12 | <title>Home</title> This repository contains Go client software to access [https://zettelstore.de|Zettelstore] via its API. <h3>Latest Release: 0.13.0 (2023-08-07)</h3> * [./changes.wiki#0_13|Change summary] * [/timeline?p=v0.13.0&bt=v0.20.0&y=ci|Check-ins for version 0.13.0], [/vdiff?to=v0.13.0&from=v0.20.0|content diff] * [/timeline?df=v0.13.0&y=ci|Check-ins derived from the 0.13.0 release], [/vdiff?from=v0.13.0&to=trunk|content diff] * [/timeline?t=release|Timeline of all past releases] |