Zettelstore Client

Check-in Differences
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From v0.15.0 To v0.16.0

2023-12-08
14:08
shtml: export some methods to be used in Zettel Presenter ... (check-in: 0a3d4044cd user: stern tags: trunk)
2023-11-30
15:15
Version 0.16.0 ... (check-in: 751754d40c user: stern tags: trunk, release, v0.16.0)
2023-11-29
16:50
Add zettel identifier for predefined role zettel ... (check-in: d47ca9287a user: stern tags: trunk)
2023-10-27
07:16
Adapt to sx changes ... (check-in: d99c7556b9 user: stern tags: trunk)
2023-10-26
15:57
Version 0.15.0 ... (check-in: 8c6fa07a0d user: stern tags: trunk, release, v0.15.0)
2023-10-22
13:09
Fix shtml generation when description term has no description data ... (check-in: d8f567812a user: stern tags: trunk)

Changes to api/const.go.

51
52
53
54
55
56
57






58
59
60

61
62
63
64
65
66
67

	// WebUI image zettel are in the range 40000..49999
	ZidEmoji = ZettelID("00000000040001")

	// Other sxn code zettel are in the range 50000..59999
	ZidSxnPrelude = ZettelID("00000000059900")







	// Range 90000...99999 is reserved for zettel templates
	ZidTOCNewTemplate    = ZettelID("00000000090000")
	ZidTemplateNewZettel = ZettelID("00000000090001")

	ZidTemplateNewTag    = ZettelID("00000000090003")
	ZidTemplateNewUser   = ZettelID("00000000090002")

	ZidDefaultHome = ZettelID("00010000000000")
)

// LengthZid factors the constant length of a zettel identifier







>
>
>
>
>
>



>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

	// WebUI image zettel are in the range 40000..49999
	ZidEmoji = ZettelID("00000000040001")

	// Other sxn code zettel are in the range 50000..59999
	ZidSxnPrelude = ZettelID("00000000059900")

	// Predefined Zettelmarkup zettel are in the range 60000..69999
	ZidRoleZettelZettel        = ZettelID("00000000060010")
	ZidRoleConfigurationZettel = ZettelID("00000000060020")
	ZidRoleRoleZettel          = ZettelID("00000000060030")
	ZidRoleTagZettel           = ZettelID("00000000060040")

	// Range 90000...99999 is reserved for zettel templates
	ZidTOCNewTemplate    = ZettelID("00000000090000")
	ZidTemplateNewZettel = ZettelID("00000000090001")
	ZidTemplateNewRole   = ZettelID("00000000090004")
	ZidTemplateNewTag    = ZettelID("00000000090003")
	ZidTemplateNewUser   = ZettelID("00000000090002")

	ZidDefaultHome = ZettelID("00010000000000")
)

// LengthZid factors the constant length of a zettel identifier
124
125
126
127
128
129
130

131
132
133
134
135
136
137
// Predefined Metadata values
const (
	ValueFalse             = "false"
	ValueTrue              = "true"
	ValueLangEN            = "en"
	ValueRoleConfiguration = "configuration"
	ValueRoleTag           = "tag"

	ValueRoleZettel        = "zettel"
	ValueSyntaxCSS         = "css"
	ValueSyntaxDraw        = "draw"
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"







>







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Predefined Metadata values
const (
	ValueFalse             = "false"
	ValueTrue              = "true"
	ValueLangEN            = "en"
	ValueRoleConfiguration = "configuration"
	ValueRoleTag           = "tag"
	ValueRoleRole          = "role"
	ValueRoleZettel        = "zettel"
	ValueSyntaxCSS         = "css"
	ValueSyntaxDraw        = "draw"
	ValueSyntaxGif         = "gif"
	ValueSyntaxHTML        = "html"
	ValueSyntaxMarkdown    = "markdown"
	ValueSyntaxMD          = "md"
165
166
167
168
169
170
171

172
173
174
175
176
177
178
const (
	QueryKeyCommand   = "cmd"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"

	QueryKeySeed      = "_seed"
	QueryKeyTag       = "tag"
)

// Supported encoding values.
const (
	EncodingHTML  = "html"







>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const (
	QueryKeyCommand   = "cmd"
	QueryKeyEncoding  = "enc"
	QueryKeyParseOnly = "parseonly"
	QueryKeyPart      = "part"
	QueryKeyPhrase    = "phrase"
	QueryKeyQuery     = "q"
	QueryKeyRole      = "role"
	QueryKeySeed      = "_seed"
	QueryKeyTag       = "tag"
)

// Supported encoding values.
const (
	EncodingHTML  = "html"

Changes to client/client.go.

431
432
433
434
435
436
437











438
439
440
441
442
443
444
445
446
447
448
	return agg, nil
}

// TagZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) TagZettel(ctx context.Context, tag string) (api.ZettelID, error) {











	if c.client.CheckRedirect == nil {
		panic("client does not allow to track redirect")
	}
	ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyTag, tag)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {







>
>
>
>
>
>
>
>
>
>
>



|







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
	return agg, nil
}

// TagZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) TagZettel(ctx context.Context, tag string) (api.ZettelID, error) {
	return c.fetchTagOrRoleZettel(ctx, api.QueryKeyTag, tag)
}

// RoleZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) RoleZettel(ctx context.Context, role string) (api.ZettelID, error) {
	return c.fetchTagOrRoleZettel(ctx, api.QueryKeyRole, role)
}

func (c *Client) fetchTagOrRoleZettel(ctx context.Context, key, val string) (api.ZettelID, error) {
	if c.client.CheckRedirect == nil {
		panic("client does not allow to track redirect")
	}
	ub := c.newURLBuilder('z').AppendKVQuery(key, val)
	resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
	if err != nil {
		return api.InvalidZID, err
	}
	defer resp.Body.Close()
	data, err := io.ReadAll(resp.Body)
	if err != nil {

Changes to go.mod.

1
2
3
4
5
module zettelstore.de/client.fossil

go 1.21

require zettelstore.de/sx.fossil v0.0.0-20231026154942-e6a183740a4f




|
1
2
3
4
5
module zettelstore.de/client.fossil

go 1.21

require zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207

Changes to go.sum.

1
2
zettelstore.de/sx.fossil v0.0.0-20231026154942-e6a183740a4f h1:NFblKWyhNnXDDcF7C6zx7cDyIJ/7GGAusIwR6uZrfkM=
zettelstore.de/sx.fossil v0.0.0-20231026154942-e6a183740a4f/go.mod h1:Uw3OLM1ufOM4Xe0G51mvkTDUv2okd+HyDBMx+0ZG7ME=
|
|
1
2
zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207 h1:8ch54z0w53bps6a00NDofEqo3AJ1l7ITXyC3XyLmlY4=
zettelstore.de/sx.fossil v0.0.0-20231130150648-05ef116ba207/go.mod h1:Uw3OLM1ufOM4Xe0G51mvkTDUv2okd+HyDBMx+0ZG7ME=

Changes to shtml/shtml.go.

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
890
891
892
893
894
895
896
897
898
	"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"
	"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(128)
	}
	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.String(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(1024)
	}
	astEnv := sxeval.MakeRootEnvironment(128) // approx: number of bindings in te.initialize()
	engine := sxeval.MakeEngine(astSF, astEnv)
	engine.BindSyntax(&sxbuiltins.QuoteS)
	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.String("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.String("zs-endnote"))).
			Cons(sx.Cons(tr.Make("value"), sx.String(noteNum))).
			Cons(sx.Cons(tr.Make("id"), sx.String("fn:"+noteID))).
			Cons(sx.Cons(tr.Make("role"), sx.String("doc-endnote"))).
			Cons(tr.symAttr)

		backref := sx.Nil().Cons(sx.String("\u21a9\ufe0e")).
			Cons(sx.Nil().
				Cons(sx.Cons(tr.symClass, sx.String("zs-endnote-backref"))).
				Cons(sx.Cons(tr.Make("href"), sx.String("#fnref:"+noteID))).
				Cons(sx.Cons(tr.Make("role"), sx.String("doc-backlink"))).
				Cons(tr.symAttr)).
			Cons(tr.symA)

		li := sx.Nil().Cons(tr.Make("li"))
		li.AppendBang(attrs).
			ExtendBang(fni.noteHx).
			AppendBang(sx.String(" ")).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      sxeval.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(sx.ListName, 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.String("<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 {
				continue
			}
			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.String(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.String(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.String("transclude"),
				refKind,
				sx.String("->"),
				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.String(" ")
		}
		return te.getString(args[0])
	})
	te.bind(sz.NameSymSoft, 0, func([]sx.Object) sx.Object { return sx.String(" ") })
	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.String("image/svg+xml")),
				sx.Cons(te.Make("src"), sx.String("/"+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.String(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.String(", "))
			}
			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.String("doc-noteref"))).
			Cons(sx.Cons(te.Make("href"), sx.String("#fn:"+noteID))).
			Cons(sx.Cons(te.tr.symClass, sx.String("zs-noteref"))).
			Cons(te.symAttr)
		href := sx.Nil().Cons(sx.String(noteNum)).Cons(hrefAttr).Cons(te.symA)
		supAttr := sx.Nil().Cons(sx.Cons(te.Make("id"), sx.String("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.String(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.String("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.String(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 int16, fn transformFn) {
	te.astEnv.Bind(te.astSF.MustMake(name), &sxeval.Builtin{

		Name:     name,
		MinArity: minArity,
		MaxArity: -1,



		IsPure:   true,
		Fn: func(_ *sxeval.Frame, args []sx.Object) (sx.Object, error) {

			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.Builtin{
		Name:     name,
		MinArity: 0,
		MaxArity: -1,
		IsPure:   true,
		Fn: func(_ *sxeval.Frame, 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",
}








<
<



|

|

<


<

>
|
>
>
|
|
|
>
>
>
|
|
|
|
<
<
<


|
|



|

<


>
>


>
>

>

|
>
>
|
>
>
>
>
>
|
<
<


|

>
>
>

|


|

<
<
<
<
<
<
|
|

















<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|


|

|

>
|

>
|
>
>
>
>
|
|

|
|
|

|

<

>



|
|


>
|
>
>
|
|

<
<
|
|
|
|
|



|
|
|
|
|

|





<



|
<
<
<
|
|
>
|
>
>
>
>
>
|
>
>
|
>
>
>
>
>
|
|
|
>
|
>
>
>
>

>
|
<
|
|
<
|
|
|
|
>
>
>
>
>
>
>
|
>
|
>


>
>
|
>

|
<
|
|
>
|
>
>
>
>
|
>
>
>
|
>
>
>
|
<
>
>
|
|
<
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
>
|

>
|

|






|

|

|
|
|
>
>
>
>
>
>


>
>
>
>
>
|
|
|
<
<
<
<
<
<
|

|
|

|


|

|
>
>
|
|


|

|

|

|

|


|
|


|

>
|
|
|



|


|
|




|




|
|




<
|



|


|
|





|

|
|




|


|













|

|
|
|
|

>
|
|
|

|
|

|

|





<
|
|

|
|
|

|
|
|



|

|
<
|
|

<
|
|







|
|
|
|


|






|



|
|
|



|
|







>
|
|









<
|
|
|

|

|



|
|
|
>
>







|



|



|
|






|


|


|
|


|
|
|
|



|

|
|
|

|
|
>
>


|


|

|

|
|
>
>
|
|

|
|
|
|
|
>
>
|
|

|
|
|
|
>
>
|

|

|
|
>
>
|
|


|
|
|


|
|
|


|

|




|
|
>
|
|
|
|
|
>
|

|
|
|
>
>
>
|
|





|
>
>
>

|

|



|
|




|

<
|
|
|
|
|
|


|

<
|
>
>
>

|
|








<
|
|
>
>
|
|
|
|
|
|
|


|
|
|
>
|
|
|
|
|

|
|

|
|





|
|
|
|

|
|
|
|

|
|
|

|
|


|


|
|
|
|
>
>
|


|

|




|
>
>
>
>
>
|
>
>
>
>
>
>
|
>
>
|
>
>
|
>
>
>
>
>
>
|
>
>
>

>
>
>
>
>
>
>
>
|


>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
>

<
<
<
<
|




|

|


|






|



<







|
|
|




|







|

|

|

|

|



|




>
>
>
>
>

|




|
>
|
>
|
<
>
|
<
<
>
>
>
|
<
>
|
>
|
<
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
|
<
|
<
>

<
|
<
|
|
<
|
|
|
|
|
>
>
>
>
>
|
<
<


|
|
>
>
>
|
|

>
>
>
>
>
|
|
|
|
>
|

|
|





|


>
>
>
>
>
>
>
>
>
|
|





|


<
<
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

916
917


918
919
920
921

922
923
924
925


926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951

952
953

954

955
956

957

958
959

960
961
962
963
964
965
966
967
968
969
970


971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021









1022
1023



















1024
1025
1026
1027
1028
1029
1030
	"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/sxhtml"
)

// Evaluator will transform a s-expression that encodes the zettel AST into an s-expression
// that represents HTML.
type Evaluator struct {
	sf            sx.SymbolFactory

	headingOffset int64
	unique        string

	noLinks       bool // true iff output must not include links

	symAttr     *sx.Symbol
	symList     *sx.Symbol
	symNoEscape *sx.Symbol
	symClass    *sx.Symbol
	symMeta     *sx.Symbol
	symP        *sx.Symbol
	symLI       *sx.Symbol
	symA        *sx.Symbol
	symHREF     *sx.Symbol
	symSpan     *sx.Symbol

	fns     map[string]EvalFn
	minArgs map[string]int



}

// NewEvaluator creates a new Evaluator object.
func NewEvaluator(headingOffset int, sf sx.SymbolFactory) *Evaluator {
	if sf == nil {
		sf = sx.MakeMappedFactory(128)
	}
	ev := &Evaluator{
		sf:            sf,

		headingOffset: int64(headingOffset),
		symAttr:       sf.MustMake(sxhtml.NameSymAttr),
		symList:       sf.MustMake(sxhtml.NameSymList),
		symNoEscape:   sf.MustMake(sxhtml.NameSymNoEscape),
		symClass:      sf.MustMake("class"),
		symMeta:       sf.MustMake("meta"),
		symP:          sf.MustMake("p"),
		symLI:         sf.MustMake("li"),
		symA:          sf.MustMake("a"),
		symHREF:       sf.MustMake("href"),
		symSpan:       sf.MustMake("span"),

		fns:     make(map[string]EvalFn, 128),
		minArgs: make(map[string]int, 128),
	}
	ev.bindCommon()
	ev.bindMetadata()
	ev.bindBlocks()
	ev.bindInlines()
	return ev
}



// SetUnique sets a prefix to make several HTML ids unique.
func (tr *Evaluator) SetUnique(s string) { tr.unique = s }

// SymbolFactory returns the symbol factory of this evaluator.
func (ev *Evaluator) SymbolFactory() sx.SymbolFactory { return ev.sf }

// IsValidName returns true, if name is a valid symbol name.
func (tr *Evaluator) IsValidName(s string) bool { return tr.sf.IsValidName(s) }

// Make a new HTML symbol.
func (tr *Evaluator) Make(s string) *sx.Symbol { return tr.sf.MustMake(s) }







// EvaluateAttrbute transforms the given attributes into a HTML s-expression.
func (tr *Evaluator) EvaluateAttrbute(a attrs.Attributes) *sx.Pair {
	if len(a) == 0 {
		return nil
	}
	plist := sx.Nil()
	keys := a.Keys()
	for i := len(keys) - 1; i >= 0; i-- {
		key := keys[i]
		if key != attrs.DefaultAttribute && tr.IsValidName(key) {
			plist = plist.Cons(sx.Cons(tr.Make(key), sx.String(a[key])))
		}
	}
	if plist == nil {
		return nil
	}
	return plist.Cons(tr.symAttr)
}






// Evaluate a metadata s-expression into a list of HTML s-expressions.
func (ev *Evaluator) Evaluate(lst *sx.Pair, env *Environment) (*sx.Pair, error) {






















	result := ev.eval(lst, env)

	if err := env.err; err != nil {
		return nil, err
	}
	pair, isPair := sx.GetPair(result)
	if !isPair {
		return nil, fmt.Errorf("evaluation does not result in a pair, but %T/%v", result, result)
	}

	for i := 0; i < len(env.endnotes); i++ {
		// May extend tr.endnotes

		if env.endnotes[i].noteHx != nil {
			continue
		}

		objHx := ev.eval(env.endnotes[i].noteAST, env)
		if env.err != nil {
			break
		}
		noteHx, isHx := sx.GetPair(objHx)
		if !isHx {
			return nil, fmt.Errorf("endnote evaluation does not result in pair, but %T/%v", objHx, objHx)
		}
		env.endnotes[i].noteHx = noteHx
	}


	return pair, nil
}

// Endnotes returns a SHTML object with all collected endnotes.
func (ev *Evaluator) Endnotes(env *Environment) *sx.Pair {
	if env.err != nil || len(env.endnotes) == 0 {
		return nil
	}

	result := sx.Nil().Cons(ev.Make("ol"))
	symValue, symId, symRole := ev.Make("value"), ev.Make("id"), ev.Make("role")

	currResult := result.AppendBang(sx.Nil().Cons(sx.Cons(ev.symClass, sx.String("zs-endnotes"))).Cons(ev.symAttr))
	for i, fni := range env.endnotes {
		noteNum := strconv.Itoa(i + 1)


		attrs := fni.attrs.Cons(sx.Cons(ev.symClass, sx.String("zs-endnote"))).
			Cons(sx.Cons(symValue, sx.String(noteNum))).
			Cons(sx.Cons(symId, sx.String("fn:"+fni.noteID))).
			Cons(sx.Cons(symRole, sx.String("doc-endnote"))).
			Cons(ev.symAttr)

		backref := sx.Nil().Cons(sx.String("\u21a9\ufe0e")).
			Cons(sx.Nil().
				Cons(sx.Cons(ev.symClass, sx.String("zs-endnote-backref"))).
				Cons(sx.Cons(ev.symHREF, sx.String("#fnref:"+fni.noteID))).
				Cons(sx.Cons(symRole, sx.String("doc-backlink"))).
				Cons(ev.symAttr)).
			Cons(ev.symA)

		li := sx.Nil().Cons(ev.symLI)
		li.AppendBang(attrs).
			ExtendBang(fni.noteHx).
			AppendBang(sx.String(" ")).AppendBang(backref)
		currResult = currResult.AppendBang(li)
	}

	return result
}

// Environment where sz objects are evaluated to shtml objects



type Environment struct {
	err          error
	langStack    []string
	endnotes     []endnoteInfo
	quoteNesting uint
}
type endnoteInfo struct {
	noteID  string    // link id
	noteAST sx.Object // Endnote as AST
	attrs   *sx.Pair  // attrs a-list
	noteHx  *sx.Pair  // Endnote as SxHTML
}

// MakeEnvironment builds a new evaluation environment.
func MakeEnvironment(lang string) Environment {
	langStack := make([]string, 1, 16)
	langStack[0] = lang
	return Environment{
		err:          nil,
		langStack:    langStack,
		endnotes:     nil,
		quoteNesting: 0,
	}
}

// GetError returns the last error found.
func (env *Environment) GetError() error { return env.err }

// Reset the environment.
func (env *Environment) Reset() {

	env.langStack = env.langStack[0:1]
	env.endnotes = nil

	env.quoteNesting = 0
}

// PushAttribute adds the current attributes to the environment.
func (env *Environment) pushAttributes(a attrs.Attributes) {
	if value, ok := a.Get("lang"); ok {
		env.langStack = append(env.langStack, value)
	} else {
		env.langStack = append(env.langStack, env.getLanguage())
	}
}

// popAttributes removes the current attributes from the envrionment
func (env *Environment) popAttributes() {
	env.langStack = env.langStack[0 : len(env.langStack)-1]
}

// getLanguage returns the current language
func (env *Environment) getLanguage() string {
	return env.langStack[len(env.langStack)-1]
}

// EvalFn is a function to be called for evaluation.

type EvalFn func([]sx.Object, *Environment) sx.Object

func (ev *Evaluator) bind(name string, minArgs int, fn EvalFn) {
	ev.fns[name] = fn
	if minArgs > 0 {
		ev.minArgs[name] = minArgs
	}
}

// ResolveBinding returns the function bound to the given name.
func (ev *Evaluator) ResolveBinding(name string) EvalFn {
	if fn, found := ev.fns[name]; found {
		return fn
	}
	return nil
}


// Rebind overwrites a binding, but leaves the minimum number of arguments intact.
func (ev *Evaluator) Rebind(name string, fn EvalFn) {
	if _, found := ev.fns[name]; !found {
		panic(name)

	}
	ev.fns[name] = fn
}

func (ev *Evaluator) bindCommon() {
	ev.bind(sx.ListName, 0, ev.evalList)
	ev.bind("quote", 1, func(args []sx.Object, _ *Environment) sx.Object { return args[0] })
}

func (ev *Evaluator) bindMetadata() {
	ev.bind(sz.NameSymMeta, 0, ev.evalList)
	evalMetaString := func(args []sx.Object, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).Name()).
			Set("content", getString(args[1], env).String())
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.NameSymTypeCredential, 2, evalMetaString)
	ev.bind(sz.NameSymTypeEmpty, 2, evalMetaString)
	ev.bind(sz.NameSymTypeID, 2, evalMetaString)
	ev.bind(sz.NameSymTypeNumber, 2, evalMetaString)
	ev.bind(sz.NameSymTypeString, 2, evalMetaString)
	ev.bind(sz.NameSymTypeTimestamp, 2, evalMetaString)
	ev.bind(sz.NameSymTypeURL, 2, evalMetaString)
	ev.bind(sz.NameSymTypeWord, 2, evalMetaString)

	evalMetaSet := func(args []sx.Object, env *Environment) sx.Object {
		var sb strings.Builder
		lst := ev.eval(args[1], env)
		for elem := getList(lst, env); elem != nil; elem = elem.Tail() {
			sb.WriteByte(' ')
			sb.WriteString(getString(elem.Car(), env).String())
		}
		s := sb.String()
		if len(s) > 0 {
			s = s[1:]
		}
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).Name()).
			Set("content", s)
		return ev.EvaluateMeta(a)
	}
	ev.bind(sz.NameSymTypeIDSet, 2, evalMetaSet)
	ev.bind(sz.NameSymTypeTagSet, 2, evalMetaSet)
	ev.bind(sz.NameSymTypeWordSet, 2, evalMetaSet)
	ev.bind(sz.NameSymTypeZettelmarkup, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := make(attrs.Attributes, 2).
			Set("name", ev.getSymbol(ev.eval(args[0], env), env).String()).
			Set("content", text.EvaluateInlineString(getList(args[1], env)))
		return ev.EvaluateMeta(a)
	})
}

// EvaluateMeta returns HTML meta object for an attribute.
func (ev *Evaluator) EvaluateMeta(a attrs.Attributes) *sx.Pair {
	return sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(ev.symMeta)
}

func (ev *Evaluator) bindBlocks() {
	ev.bind(sz.NameSymBlock, 0, ev.evalList)
	ev.bind(sz.NameSymPara, 0, func(args []sx.Object, env *Environment) sx.Object {






		return ev.evalSlice(args, env).Cons(ev.symP)
	})
	ev.bind(sz.NameSymHeading, 5, func(args []sx.Object, env *Environment) sx.Object {
		nLevel := getInt64(args[0], env)
		if nLevel <= 0 {
			env.err = fmt.Errorf("%v is a negative level", nLevel)
			return sx.Nil()
		}
		level := strconv.FormatInt(nLevel+ev.headingOffset, 10)

		a := ev.getAttributes(args[1], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if fragment := getString(args[3], env).String(); fragment != "" {
			a = a.Set("id", ev.unique+fragment)
		}

		if result, isPair := sx.GetPair(ev.eval(args[4], env)); isPair && result != nil {
			if len(a) > 0 {
				result = result.Cons(ev.EvaluateAttrbute(a))
			}
			return result.Cons(ev.Make("h" + level))
		}
		return sx.MakeList(ev.Make("h"+level), sx.String("<MISSING TEXT>"))
	})
	ev.bind(sz.NameSymThematic, 0, func(args []sx.Object, env *Environment) sx.Object {
		result := sx.Nil()
		if len(args) > 0 {
			if attrList := getList(ev.eval(args[0], env), env); attrList != nil {
				result = result.Cons(ev.EvaluateAttrbute(sz.GetAttributes(attrList)))
			}
		}
		return result.Cons(ev.Make("hr"))
	})

	ev.bind(sz.NameSymListOrdered, 0, ev.makeListFn("ol"))
	ev.bind(sz.NameSymListUnordered, 0, ev.makeListFn("ul"))
	ev.bind(sz.NameSymDescription, 0, func(args []sx.Object, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.Nil()
		}
		items := sx.Nil().Cons(ev.Make("dl"))
		curItem := items
		for pos := 0; pos < len(args); pos++ {
			term := getList(ev.eval(args[pos], env), env)
			curItem = curItem.AppendBang(term.Cons(ev.Make("dt")))
			pos++
			if pos >= len(args) {
				break
			}
			ddBlock := getList(ev.eval(args[pos], env), env)
			if ddBlock == nil {
				continue
			}
			for ddlst := ddBlock; ddlst != nil; ddlst = ddlst.Tail() {
				dditem := getList(ddlst.Car(), env)
				curItem = curItem.AppendBang(dditem.Cons(ev.Make("dd")))
			}
		}
		return items
	})

	ev.bind(sz.NameSymListQuote, 0, func(args []sx.Object, env *Environment) sx.Object {
		if args == nil {
			return sx.Nil()
		}
		result := sx.Nil().Cons(ev.Make("blockquote"))
		currResult := result
		for _, elem := range args {
			if quote, isPair := sx.GetPair(ev.eval(elem, env)); isPair {
				currResult = currResult.AppendBang(quote.Cons(ev.symP))
			}
		}
		return result
	})

	ev.bind(sz.NameSymTable, 1, func(args []sx.Object, env *Environment) sx.Object {
		thead := sx.Nil()
		if header := getList(ev.eval(args[0], env), env); !sx.IsNil(header) {
			thead = sx.Nil().Cons(ev.evalTableRow(header)).Cons(ev.Make("thead"))
		}

		tbody := sx.Nil()
		if len(args) > 1 {
			tbody = sx.Nil().Cons(ev.Make("tbody"))
			curBody := tbody
			for _, row := range args[1:] {
				curBody = curBody.AppendBang(ev.evalTableRow(getList(ev.eval(row, env), env)))
			}
		}

		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(ev.Make("table"))
	})
	ev.bind(sz.NameSymCell, 0, ev.makeCellFn(""))
	ev.bind(sz.NameSymCellCenter, 0, ev.makeCellFn("center"))
	ev.bind(sz.NameSymCellLeft, 0, ev.makeCellFn("left"))
	ev.bind(sz.NameSymCellRight, 0, ev.makeCellFn("right"))

	symDiv := ev.Make("div")
	ev.bind(sz.NameSymRegionBlock, 2, ev.makeRegionFn(symDiv, true))
	ev.bind(sz.NameSymRegionQuote, 2, ev.makeRegionFn(ev.Make("blockquote"), false))
	ev.bind(sz.NameSymRegionVerse, 2, ev.makeRegionFn(symDiv, false))

	ev.bind(sz.NameSymVerbatimComment, 1, func(args []sx.Object, env *Environment) sx.Object {
		if ev.getAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(args[1], env); s != "" {
					t := sx.String(s.String())
					return sx.Nil().Cons(t).Cons(ev.Make(sxhtml.NameSymBlockComment))
				}
			}
		}
		return nil
	})

	ev.bind(sz.NameSymVerbatimEval, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.getAttributes(args[0], env).AddClass("zs-eval"), getString(args[1], env))
	})
	ev.bind(sz.NameSymVerbatimHTML, 2, ev.evalHTML)
	ev.bind(sz.NameSymVerbatimMath, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalVerbatim(ev.getAttributes(args[0], env).AddClass("zs-math"), getString(args[1], env))
	})
	ev.bind(sz.NameSymVerbatimProg, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		content := getString(args[1], env)
		if a.HasDefault() {
			content = sx.String(visibleReplacer.Replace(content.String()))
		}
		return ev.evalVerbatim(a, content)
	})
	ev.bind(sz.NameSymVerbatimZettel, 0, nilFn)

	ev.bind(sz.NameSymBLOB, 3, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalBLOB(getList(args[0], env), getString(args[1], env), getString(args[2], env))
	})

	ev.bind(sz.NameSymTransclude, 2, func(args []sx.Object, env *Environment) sx.Object {
		ref, isPair := sx.GetPair(ev.eval(args[1], env))
		if !isPair {
			return sx.Nil()
		}
		refKind := ref.Car()
		if sx.IsNil(refKind) {
			return sx.Nil()
		}
		if refValue := getString(ref.Tail().Car(), env); refValue != "" {
			if refSym, isRefSym := sx.GetSymbol(refKind); isRefSym && refSym.Name() == sz.NameSymRefStateExternal {
				a := ev.getAttributes(args[0], env).Set("src", refValue.String()).AddClass("external")
				return sx.Nil().Cons(sx.Nil().Cons(ev.EvaluateAttrbute(a)).Cons(ev.Make("img"))).Cons(ev.symP)
			}
			return sx.MakeList(
				ev.Make(sxhtml.NameSymInlineComment),
				sx.String("transclude"),
				refKind,
				sx.String("->"),
				refValue,
			)
		}
		return ev.evalSlice(args, env)
	})
}

func (ev *Evaluator) makeListFn(tag string) EvalFn {
	sym := ev.Make(tag)
	return func(args []sx.Object, env *Environment) sx.Object {
		result := sx.Nil().Cons(sym)
		last := result
		for _, elem := range args {
			item := sx.Nil().Cons(ev.symLI)
			if res, isPair := sx.GetPair(ev.eval(elem, env)); isPair {
				item.ExtendBang(res)
			}
			last = last.AppendBang(item)
		}
		return result
	}
}

func (ev *Evaluator) evalTableRow(pairs *sx.Pair) *sx.Pair {
	row := sx.Nil().Cons(ev.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 (ev *Evaluator) makeCellFn(align string) EvalFn {
	return func(args []sx.Object, env *Environment) sx.Object {
		tdata := ev.evalSlice(args, env)
		if align != "" {
			tdata = tdata.Cons(ev.EvaluateAttrbute(attrs.Attributes{"class": align}))
		}
		return tdata.Cons(ev.Make("td"))
	}
}

func (ev *Evaluator) makeRegionFn(sym *sx.Symbol, genericToClass bool) EvalFn {
	return func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if genericToClass {
			if val, found := a.Get(""); found {
				a = a.Remove("").AddClass(val)
			}
		}
		result := sx.Nil()
		if len(a) > 0 {
			result = result.Cons(ev.EvaluateAttrbute(a))
		}
		result = result.Cons(sym)
		currResult := result.LastPair()
		if region, isPair := sx.GetPair(ev.eval(args[1], env)); isPair {
			currResult = currResult.ExtendBang(region)
		}
		if len(args) > 2 {
			if cite, isPair := sx.GetPair(ev.eval(args[2], env)); isPair && cite != nil {
				currResult.AppendBang(cite.Cons(ev.Make("cite")))
			}
		}
		return result
	}
}

func (ev *Evaluator) evalVerbatim(a attrs.Attributes, s sx.String) sx.Object {
	a = setProgLang(a)
	code := sx.Nil().Cons(s)
	if al := ev.EvaluateAttrbute(a); al != nil {
		code = code.Cons(al)
	}
	code = code.Cons(ev.Make("code"))
	return sx.Nil().Cons(code).Cons(ev.Make("pre"))
}

func (ev *Evaluator) bindInlines() {
	ev.bind(sz.NameSymInline, 0, ev.evalList)
	ev.bind(sz.NameSymText, 1, func(args []sx.Object, env *Environment) sx.Object { return getString(args[0], env) })
	ev.bind(sz.NameSymSpace, 0, func(args []sx.Object, env *Environment) sx.Object {
		if len(args) == 0 {
			return sx.String(" ")
		}
		return getString(args[0], env)
	})
	ev.bind(sz.NameSymSoft, 0, func([]sx.Object, *Environment) sx.Object { return sx.String(" ") })
	symBR := ev.Make("br")
	ev.bind(sz.NameSymHard, 0, func([]sx.Object, *Environment) sx.Object { return sx.Nil().Cons(symBR) })

	ev.bind(sz.NameSymLinkInvalid, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		var inline *sx.Pair
		if len(args) > 2 {
			inline = ev.evalSlice(args[2:], env)
		}
		if inline == nil {
			inline = sx.Nil().Cons(ev.eval(args[1], env))
		}
		return inline.Cons(ev.symSpan)
	})
	evalHREF := func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.String()), refValue, args[2:], env)
	}
	ev.bind(sz.NameSymLinkZettel, 2, evalHREF)
	ev.bind(sz.NameSymLinkSelf, 2, evalHREF)
	ev.bind(sz.NameSymLinkFound, 2, evalHREF)
	ev.bind(sz.NameSymLinkBroken, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.AddClass("broken"), refValue, args[2:], env)
	})
	ev.bind(sz.NameSymLinkHosted, 2, evalHREF)
	ev.bind(sz.NameSymLinkBased, 2, evalHREF)
	ev.bind(sz.NameSymLinkQuery, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		query := "?" + api.QueryKeyQuery + "=" + url.QueryEscape(refValue.String())
		return ev.evalLink(a.Set("href", query), refValue, args[2:], env)
	})
	ev.bind(sz.NameSymLinkExternal, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		refValue := getString(args[1], env)
		return ev.evalLink(a.Set("href", refValue.String()).AddClass("external"), refValue, args[2:], env)
	})

	ev.bind(sz.NameSymEmbed, 3, func(args []sx.Object, env *Environment) sx.Object {
		ref := getList(ev.eval(args[1], env), env)
		syntax := getString(args[2], env)
		if syntax == api.ValueSyntaxSVG {
			embedAttr := sx.MakeList(
				ev.symAttr,
				sx.Cons(ev.Make("type"), sx.String("image/svg+xml")),
				sx.Cons(ev.Make("src"), sx.String("/"+getString(ref.Tail(), env).String()+".svg")),
			)
			return sx.MakeList(
				ev.Make("figure"),
				sx.MakeList(
					ev.Make("embed"),
					embedAttr,
				),
			)
		}
		a := ev.getAttributes(args[0], env)
		a = a.Set("src", getString(ref.Tail().Car(), env).String())
		if len(args) > 3 {
			var sb strings.Builder
			flattenText(&sb, sx.MakeList(args[3:]...))
			if d := sb.String(); d != "" {
				a = a.Set("alt", d)
			}
		}
		return sx.MakeList(ev.Make("img"), ev.EvaluateAttrbute(a))
	})
	ev.bind(sz.NameSymEmbedBLOB, 3, func(args []sx.Object, env *Environment) sx.Object {
		a, syntax, data := ev.getAttributes(args[0], env), getString(args[1], env), getString(args[2], env)
		summary, hasSummary := a.Get(api.KeySummary)
		if !hasSummary {
			summary = ""
		}
		return ev.evalBLOB(
			sx.MakeList(ev.Make(sx.ListName), sx.String(summary)),
			syntax,
			data,
		)
	})

	ev.bind(sz.NameSymCite, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		result := sx.Nil()
		if key := getString(args[1], env); key != "" {
			if len(args) > 2 {
				result = ev.evalSlice(args[2:], env).Cons(sx.String(", "))
			}
			result = result.Cons(key)
		}
		if len(a) > 0 {
			result = result.Cons(ev.EvaluateAttrbute(a))
		}
		if result == nil {
			return nil
		}
		return result.Cons(ev.symSpan)
	})

	ev.bind(sz.NameSymMark, 3, func(args []sx.Object, env *Environment) sx.Object {
		result := ev.evalSlice(args[3:], env)
		if !ev.noLinks {
			if fragment := getString(args[2], env); fragment != "" {
				a := attrs.Attributes{"id": fragment.String() + ev.unique}
				return result.Cons(ev.EvaluateAttrbute(a)).Cons(ev.symA)
			}
		}
		return result.Cons(ev.symSpan)
	})

	ev.bind(sz.NameSymEndnote, 1, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		attrPlist := sx.Nil()
		if len(a) > 0 {
			if attrs := ev.EvaluateAttrbute(a); attrs != nil {
				attrPlist = attrs.Tail()
			}
		}

		text, isPair := sx.GetPair(args[1])
		if !isPair {
			return sx.Nil()
		}

		noteNum := strconv.Itoa(len(env.endnotes) + 1)
		noteID := ev.unique + noteNum
		env.endnotes = append(env.endnotes, endnoteInfo{
			noteID: noteID, noteAST: ev.eval(text, env), noteHx: nil, attrs: attrPlist})
		hrefAttr := sx.Nil().Cons(sx.Cons(ev.Make("role"), sx.String("doc-noteref"))).
			Cons(sx.Cons(ev.symHREF, sx.String("#fn:"+noteID))).
			Cons(sx.Cons(ev.symClass, sx.String("zs-noteref"))).
			Cons(ev.symAttr)
		href := sx.Nil().Cons(sx.String(noteNum)).Cons(hrefAttr).Cons(ev.symA)
		supAttr := sx.Nil().Cons(sx.Cons(ev.Make("id"), sx.String("fnref:"+noteID))).Cons(ev.symAttr)
		return sx.Nil().Cons(href).Cons(supAttr).Cons(ev.Make("sup"))
	})

	ev.bind(sz.NameSymFormatDelete, 1, ev.makeFormatFn("del"))
	ev.bind(sz.NameSymFormatEmph, 1, ev.makeFormatFn("em"))
	ev.bind(sz.NameSymFormatInsert, 1, ev.makeFormatFn("ins"))
	ev.bind(sz.NameSymFormatMark, 1, ev.makeFormatFn("mark"))
	ev.bind(sz.NameSymFormatQuote, 1, ev.evalQuote)
	ev.bind(sz.NameSymFormatSpan, 1, ev.makeFormatFn("span"))
	ev.bind(sz.NameSymFormatStrong, 1, ev.makeFormatFn("strong"))
	ev.bind(sz.NameSymFormatSub, 1, ev.makeFormatFn("sub"))
	ev.bind(sz.NameSymFormatSuper, 1, ev.makeFormatFn("sup"))

	ev.bind(sz.NameSymLiteralComment, 1, func(args []sx.Object, env *Environment) sx.Object {
		if ev.getAttributes(args[0], env).HasDefault() {
			if len(args) > 1 {
				if s := getString(ev.eval(args[1], env), env); s != "" {
					return sx.Nil().Cons(s).Cons(ev.Make(sxhtml.NameSymInlineComment))
				}
			}
		}
		return sx.Nil()
	})
	ev.bind(sz.NameSymLiteralHTML, 2, ev.evalHTML)
	kbdSym := ev.Make("kbd")
	ev.bind(sz.NameSymLiteralInput, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, kbdSym, env)
	})
	codeSym := ev.Make("code")
	ev.bind(sz.NameSymLiteralMath, 2, func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env).AddClass("zs-math")
		return ev.evalLiteral(args, a, codeSym, env)
	})
	sampSym := ev.Make("samp")
	ev.bind(sz.NameSymLiteralOutput, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, sampSym, env)
	})
	ev.bind(sz.NameSymLiteralProg, 2, func(args []sx.Object, env *Environment) sx.Object {
		return ev.evalLiteral(args, nil, codeSym, env)
	})

	ev.bind(sz.NameSymLiteralZettel, 0, nilFn)
}

func (ev *Evaluator) makeFormatFn(tag string) EvalFn {
	sym := ev.Make(tag)
	return func(args []sx.Object, env *Environment) sx.Object {
		a := ev.getAttributes(args[0], env)
		env.pushAttributes(a)
		defer env.popAttributes()
		if val, hasClass := a.Get(""); hasClass {
			a = a.Remove("").AddClass(val)
		}
		res := ev.evalSlice(args[1:], env)
		if len(a) > 0 {
			res = res.Cons(ev.EvaluateAttrbute(a))
		}
		return res.Cons(sym)
	}
}

type quoteData struct {
	primLeft, primRight string
	secLeft, secRight   string
	nbsp                bool
}

var langQuotes = map[string]quoteData{
	"":              {"&quot;", "&quot;", "&quot;", "&quot;", false},
	api.ValueLangEN: {"&ldquo;", "&rdquo;", "&lsquo;", "&rsquo;", false},
	"de":            {"&bdquo;", "&ldquo;", "&sbquo;", "&lsquo;", false},
	"fr":            {"&laquo;", "&raquo;", "&lsaquo;", "&rsaquo;", true},
}

func getQuoteData(lang string) quoteData {
	langFields := strings.FieldsFunc(lang, func(r rune) bool { return r == '-' || r == '_' })
	for len(langFields) > 0 {
		langSup := strings.Join(langFields, "-")
		quotes, ok := langQuotes[langSup]
		if ok {
			return quotes
		}
		langFields = langFields[0 : len(langFields)-1]
	}
	return langQuotes[""]
}

func getQuotes(data *quoteData, env *Environment) (string, string) {
	if env.quoteNesting%2 == 0 {
		return data.primLeft, data.primRight
	}
	return data.secLeft, data.secRight
}

func (ev *Evaluator) evalQuote(args []sx.Object, env *Environment) sx.Object {
	a := ev.getAttributes(args[0], env)
	env.pushAttributes(a)
	defer env.popAttributes()

	if val, hasClass := a.Get(""); hasClass {
		a = a.Remove("").AddClass(val)
	}
	quotes := getQuoteData(env.getLanguage())
	leftQ, rightQ := getQuotes(&quotes, env)

	env.quoteNesting++
	res := ev.evalSlice(args[1:], env)
	env.quoteNesting--

	lastPair := res.LastPair()
	if lastPair.IsNil() {
		res = sx.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ), sx.String(rightQ)), sx.Nil())
	} else {
		if quotes.nbsp {
			lastPair.AppendBang(sx.MakeList(ev.symNoEscape, sx.String("&nbsp;"), sx.String(rightQ)))
			res = res.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ), sx.String("&nbsp;")))
		} else {
			lastPair.AppendBang(sx.MakeList(ev.symNoEscape, sx.String(rightQ)))
			res = res.Cons(sx.MakeList(ev.symNoEscape, sx.String(leftQ)))
		}
	}
	if len(a) > 0 {
		res = res.Cons(ev.EvaluateAttrbute(a))
		return res.Cons(ev.symSpan)
	}




	return res.Cons(ev.symList)
}

var visibleReplacer = strings.NewReplacer(" ", "\u2423")

func (ev *Evaluator) evalLiteral(args []sx.Object, a attrs.Attributes, sym *sx.Symbol, env *Environment) sx.Object {
	if a == nil {
		a = ev.getAttributes(args[0], env)
	}
	a = setProgLang(a)
	literal := getString(args[1], env).String()
	if a.HasDefault() {
		a = a.RemoveDefault()
		literal = visibleReplacer.Replace(literal)
	}
	res := sx.Nil().Cons(sx.String(literal))
	if len(a) > 0 {
		res = res.Cons(ev.EvaluateAttrbute(a))
	}
	return res.Cons(sym)
}

func setProgLang(a attrs.Attributes) attrs.Attributes {
	if val, found := a.Get(""); found {
		a = a.AddClass("language-" + val).Remove("")
	}
	return a
}

func (ev *Evaluator) evalHTML(args []sx.Object, env *Environment) sx.Object {
	if s := getString(ev.eval(args[1], env), env); s != "" && IsSafe(s.String()) {
		return sx.Nil().Cons(s).Cons(ev.symNoEscape)
	}
	return nil
}

func (ev *Evaluator) evalBLOB(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(ev.symNoEscape)).Cons(ev.symP)
	default:
		imgAttr := sx.Nil().Cons(sx.Cons(ev.Make("src"), sx.String("data:image/"+syntax.String()+";base64,"+data.String())))
		var sb strings.Builder
		flattenText(&sb, description)
		if d := sb.String(); d != "" {
			imgAttr = imgAttr.Cons(sx.Cons(ev.Make("alt"), sx.String(d)))
		}
		return sx.Nil().Cons(sx.Nil().Cons(imgAttr.Cons(ev.symAttr)).Cons(ev.Make("img"))).Cons(ev.symP)
	}
}

func 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.Symbol:
			if obj.Name() == sz.NameSymSpace {
				sb.WriteByte(' ')
				break
			}
		case *sx.Pair:
			flattenText(sb, obj)
		}
	}
}

func (ev *Evaluator) evalList(args []sx.Object, env *Environment) sx.Object {
	return ev.evalSlice(args, env)
}
func nilFn([]sx.Object, *Environment) sx.Object { return sx.Nil() }


func (ev *Evaluator) eval(obj sx.Object, env *Environment) sx.Object {
	if env.err != nil {


		return sx.Nil()
	}
	if sx.IsNil(obj) {
		return obj

	}
	lst, isLst := sx.GetPair(obj)
	if !isLst {
		return obj


	}
	sym, found := sx.GetSymbol(lst.Car())
	if !found {
		env.err = fmt.Errorf("symbol expected, but got %T/%v", lst.Car(), lst.Car())
		return sx.Nil()
	}
	name := sym.Name()
	fn, found := ev.fns[name]
	if !found {
		env.err = fmt.Errorf("symbol %q not bound", name)
		return sx.Nil()
	}
	var args []sx.Object
	for cdr := lst.Cdr(); !sx.IsNil(cdr); {
		pair, isPair := sx.GetPair(cdr)
		if !isPair {
			break
		}
		args = append(args, pair.Car())
		cdr = pair.Cdr()
	}
	if minArgs, hasMinArgs := ev.minArgs[name]; hasMinArgs {
		if minArgs > len(args) {
			env.err = fmt.Errorf("%v needs at least %d arguments, but got only %d", name, minArgs, len(args))
			return sx.Nil()
		}

	}
	result := fn(args, env)

	if env.err != nil {

		return sx.Nil()
	}

	return result

}


func (ev *Evaluator) evalSlice(args []sx.Object, env *Environment) *sx.Pair {
	result := sx.Cons(sx.Nil(), sx.Nil())
	curr := result
	for _, arg := range args {
		elem := ev.eval(arg, env)
		if env.err != nil {
			return nil
		}
		curr = curr.AppendBang(elem)
	}
	return result.Tail()


}

func (ev *Evaluator) evalLink(a attrs.Attributes, refValue sx.String, inline []sx.Object, env *Environment) sx.Object {
	result := ev.evalSlice(inline, env)
	if len(inline) == 0 {
		result = sx.Nil().Cons(refValue)
	}
	if ev.noLinks {
		return result.Cons(ev.symSpan)
	}
	return result.Cons(ev.EvaluateAttrbute(a)).Cons(ev.symA)
}

func (ev *Evaluator) getSymbol(val sx.Object, env *Environment) *sx.Symbol {
	if env.err == nil {
		if sym, ok := sx.GetSymbol(val); ok {
			return sym
		}
		env.err = fmt.Errorf("%v/%T is not a symbol", val, val)
	}
	return ev.Make("???")
}
func getString(val sx.Object, env *Environment) sx.String {
	if env.err != nil {
		return ""
	}
	if s, ok := sx.GetString(val); ok {
		return s
	}
	env.err = fmt.Errorf("%v/%T is not a string", val, val)
	return ""
}
func getList(val sx.Object, env *Environment) *sx.Pair {
	if env.err == nil {
		if res, isPair := sx.GetPair(val); isPair {
			return res
		}
		env.err = fmt.Errorf("%v/%T is not a list", val, val)
	}
	return nil
}
func getInt64(val sx.Object, env *Environment) int64 {
	if env.err != nil {
		return -1017
	}
	if num, ok := sx.GetNumber(val); ok {
		return int64(num.(sx.Int64))
	}
	env.err = fmt.Errorf("%v/%T is not a number", val, val)
	return -1017
}









func (ev *Evaluator) getAttributes(arg sx.Object, env *Environment) attrs.Attributes {
	return sz.GetAttributes(getList(ev.eval(arg, env), env))



















}

var unsafeSnippets = []string{
	"<script", "</script",
	"<iframe", "</iframe",
}

Changes to sz/const.go.

30
31
32
33
34
35
36

37
38
39
40
41
42
43
	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"







>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	NameSymDescription     = "DESCRIPTION"
	NameSymEmbed           = "EMBED"
	NameSymEmbedBLOB       = "EMBED-BLOB"
	NameSymEndnote         = "ENDNOTE"
	NameSymFormatEmph      = "FORMAT-EMPH"
	NameSymFormatDelete    = "FORMAT-DELETE"
	NameSymFormatInsert    = "FORMAT-INSERT"
	NameSymFormatMark      = "FORMAT-MARK"
	NameSymFormatQuote     = "FORMAT-QUOTE"
	NameSymFormatSpan      = "FORMAT-SPAN"
	NameSymFormatSub       = "FORMAT-SUB"
	NameSymFormatSuper     = "FORMAT-SUPER"
	NameSymFormatStrong    = "FORMAT-STRONG"
	NameSymHard            = "HARD"
	NameSymHeading         = "HEADING"
124
125
126
127
128
129
130

131
132
133
134
135
136
137
	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







>







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	SymDescription     *sx.Symbol
	SymEmbed           *sx.Symbol
	SymEmbedBLOB       *sx.Symbol
	SymEndnote         *sx.Symbol
	SymFormatEmph      *sx.Symbol
	SymFormatDelete    *sx.Symbol
	SymFormatInsert    *sx.Symbol
	SymFormatMark      *sx.Symbol
	SymFormatQuote     *sx.Symbol
	SymFormatSpan      *sx.Symbol
	SymFormatSub       *sx.Symbol
	SymFormatSuper     *sx.Symbol
	SymFormatStrong    *sx.Symbol
	SymHard            *sx.Symbol
	SymHeading         *sx.Symbol
219
220
221
222
223
224
225

226
227
228
229
230
231
232
	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)







>







221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	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.SymFormatMark = sf.MustMake(NameSymFormatMark)
	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)

Changes to www/changes.wiki.

1
2



3
4





5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<title>Change Log</title>




<a name="0_16"></a>
<h2>Changes for Version 0.16.0 (pending)</h2>






<a name="0_15"></a>
<h2>Changes for Version 0.15.0 (2023-10-26)</h2>
  * Tag zettel: API, constant values
  * Refactorings b/c Sx

<a name="0_14"></a>
<h2>Changes for Version 0.14.0 (2023-09-23)</h2>
  * Remove support for JSON encoding

<a name="0_13"></a>
<h2>Changes for Version 0.13.0 (2023-08-07)</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>


>
>
>

|
>
>
>
>
>



|
|



|







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
<title>Change Log</title>

<a name="0_17"></a>
<h2>Changes for Version 0.17.0 (pending)</h2>

<a name="0_16"></a>
<h2>Changes for Version 0.16.0 (2023-11-30)</h2>
  *  Refactor shtml transformator to support evaluating the language tree of a
     zettel AST. Its API has been changes, since evaluation is now top-down,
     where previous transformation was bottom.up.
  *  Add API call to retrieve role zettel.
  *  Added constants for role zettel and to mark text within zettelmarkup.

<a name="0_15"></a>
<h2>Changes for Version 0.15.0 (2023-10-26)</h2>
  *  Tag zettel: API, constant values
  *  Refactorings b/c Sx

<a name="0_14"></a>
<h2>Changes for Version 0.14.0 (2023-09-23)</h2>
  *  Remove support for JSON encoding

<a name="0_13"></a>
<h2>Changes for Version 0.13.0 (2023-08-07)</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>

Changes to www/index.wiki.

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.15.0 (2023-10-26)</h3>
  *  [./changes.wiki#0_15|Change summary]
  *  [/timeline?p=v0.15.0&bt=v0.13.0&y=ci|Check-ins for version 0.15.0],
     [/vdiff?to=v0.15.0&from=v0.13.0|content diff]
  *  [/timeline?df=v0.15.0&y=ci|Check-ins derived from the 0.15.0 release],
     [/vdiff?from=v0.15.0&to=trunk|content diff]
  *  [/timeline?t=release|Timeline of all past releases]





























|
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<title>Home</title>

This repository contains Go client software to
access [https://zettelstore.de|Zettelstore] via its API.

<h3>Latest Release: 0.16.0 (2023-11-30)</h3>
  *  [./changes.wiki#0_16|Change summary]
  *  [/timeline?p=v0.16.0&bt=v0.15.0&y=ci|Check-ins for version 0.16.0],
     [/vdiff?to=v0.16.0&from=v0.15.0|content diff]
  *  [/timeline?df=v0.16.0&y=ci|Check-ins derived from the 0.16.0 release],
     [/vdiff?from=v0.16.0&to=trunk|content diff]
  *  [/timeline?t=release|Timeline of all past releases]

<h3>Use instructions</h3>

If you want to import this library into your own [https://go.dev/|Go] software,
you must execute a <code>go get</code> command. Since Go treats non-standard
software and non-standard platforms quite badly, you must use some non-standard
commands.

First, you must install the version control system
[https://fossil-scm.org|Fossil], which is a superior solution compared to Git,
in too many use cases. It is just a single executable, nothing more. Make sure,
it is in your search path for commands.

How you can execute the following Go command to retrieve a given version of
this library:

<code>GOVCS=zettelstore.de:fossil go get -x
zettelstore.de/client.fossil@HASH</code>

where <code>HASH</code> is the hash value of the commit you want to use.

Go currently seems not to support software versions when the software is
managed by Fossil. This explains the need for the hash value. However, this
methods works and you can use the client software to access a Zettelstore.