-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc.go
More file actions
1474 lines (1005 loc) · 29.5 KB
/
doc.go
File metadata and controls
1474 lines (1005 loc) · 29.5 KB
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
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
/*
Package decksh -- a domain-specific language for presentations, visualizations, and information displays
`decksh` is a domain-specific language (DSL) for generating [`deck`](https://github.com/ajstarks/deck) markup.

`decksh` elements
## Install
```
go get github.com/ajstarks/decksh # install the package
go install github.com/ajstarks/decksh/cmd/decksh@latest # install the decksh command
```
The current version is 2026-01-10-1.0.0
## References and Examples
- [`decksh` overview](https://speakerdeck.com/ajstarks/decksh-a-little-language-for-decks)
- [`decksh` object reference](https://speakerdeck.com/ajstarks/decksh-object-reference)
- [Installing and Running decksh/pdfdeck](https://speakerdeck.com/ajstarks/pdfdeck)
- [Repository of decksh projects and visualizations](https://github.com/ajstarks/deckviz "Repository of decksh projects and visualizations")
## Package use
The `Process` function reads decksh commands from an `io.Reader` and writes deck markup to an `io.Writer`, returning an error.
For example:
```
package main
import (
"fmt"
"os"
"strings"
"github.com/ajstarks/decksh"
)
func main() {
input := `
deck
slide
ctext "hello, world" 50 50 10
eslide
edeck
`
err := decksh.Process(os.Stdout, strings.NewReader(input))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
```
Produces:
```
<deck>
<slide>
<text align="c" xp="50" yp="50" sp="10" >hello, world</text>
</slide>
</deck>
````
## Running the command line client
This repository also contains `cmd/decksh`, a client decksh command:
`decksh` reads from the specified input, and writes deck markup to the specified output destination:
```
$ decksh # input from stdin, output to stdout
$ decksh -o foo.xml # input from stdin, output to foo.xml
$ decksh foo.sh # input from foo.sh output to stdout
$ decksh -o foo.xml foo.sh # input from foo.sh output to foo.xml
$ decksh -version # show decksh version
$ decksh -dump ... # show decksh variables
```
Typically, `decksh` acts as the head of a rendering pipeline, where another `deck` client renders the markup. This example uses `pdfdeck`
```
$ decksh text.dsh | pdfdeck -stdout -pagesize 1200,900 - > text.pdf
```
## Example input
decksh hello, world:
```
// hello world
deck
slide "black" "white"
ctext "hello world" 50 25 15
circle 0 0 120 "blue"
eslide
edeck
```
produces:

This deck script:
```
// Example deck
midx=50
midy=50
iw=640
ih=480
imfile="follow.jpg"
imlink="https://budnitzbicycles.com"
imscale=58
dtop=87
opts="-fulldeck=f -textsize 1 -xlabel=2 -barwidth 1.5"
deck
slide "white" "black"
ctext "Deck elements" midx dtop 5
cimage "follow.jpg" "Dreams" 72 midy iw ih imscale imlink
textblock "Budnitz #1, Plainfield, NJ, May 10, 2015" 55 35 10 1 "serif" "white"
// List
blist 10 75 3
li "text, image, list"
li "rect, ellipse, polygon"
li "line, arc, curve"
elist
// Graphics
gy=10
c1="red"
c2="blue"
c3="green"
rect 15 gy 8 6 c1
ellipse 27.5 gy 8 6 c2
polygon "37 37 45" "7 13 10" c3
line 50 gy 60 gy 0.25 c1
arc 70 gy 10 8 0 180 0.25 c2
curve 80 gy 95 25 90 gy 0.25 c3
// Chart
chleft=10
chright=45
chtop=42
chtbottom=28
dchart -left chleft -right chright -top chtop -bottom chbottom opts AAPL.d
eslide
edeck
```
Produces:

Text, font, color, caption and link arguments follow Go conventions (surrounded by double quotes).
## Coordinate System
`decksh` (like the deck markup it produces) uses a traditional Cartesian
coordinate system: The origin (0,0) is at the lower left, x increases to the
right, and y increases upwards. The coordinate system is expliticly based on
the percentages, with x and y ranging from 0-100. For example (50, 50) is the
middle of the canvas, (100,100) is the upper right, (100,0) is the lower
right, and (0,100) is the upper left.

## Rulers
# Make rulers with an interval (default 5), with optional color
```ruler [increment] [color]```

## Colors
Colors formats are:
- [RGB](https://en.wikipedia.org/wiki/RGB_color_model "RGB"): "rgb(n,n,n)", where n ranges from 0-255, for example "`"rgb(128,0,128)"` .
- hex: "#rrggbb", for example `"#aa00aa"`,
- [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV "HSV"): hsv(hue,saturation,value), hue ranges from 0-360, saturation and value range from 0-100, for example `"hsv(360,30,30)"` (pdfdeck and pngdeck support this syntax)
- [SVG color names](https://www.w3.org/TR/SVG11/types.html#ColorKeywords "SVG color names").
Color gradients (used for slide backgrounds and rectangle and square fills) are specified as color1/color2/percent, for example, `"blue/white/90"`
## Coordinates, dimensions, scales, opacity and fonts
Coordinates, dimensions, scales and opacities are floating point numbers
ranging from from 0-100 (representing percentages of the canvas width and
percent opacity). Some arguments are optional, and if omitted defaults are
applied (black for text, gray for graphics, 100% opacity).
Canvas size and image dimensions are in pixels.
Fonts may be:
- "sans"
- "serif"
- "mono"
- symbol"

## Begin or end a deck.
```
deck
edeck
```
## Begin, end a slide with optional background and text colors.
```
slide [bgcolor] [fgcolor]
eslide
```
Note that doc/edoc and page/epage are may also be used as synomyms for deck/edeck and slide/eslide.
## Specify the size of the canvas.
```
canvas w h
```
## Simple assignments
`id=<number>` defines a constant, which may be then subtitited. For example:
```
x=10
y=20
text "hello, world" x y 5
```
The special identifier, `deckshVersion` contains the string denoting the version of decksh.
## Assignment operations
`id+=<number>` increment the value of `id` by `<number>`
```
x+=5
```
`id-=<number>` decrement the value of `id` by `<number>`
```
x-=10
```
`id*=<number>` multiply the value of `id` by `<number>`
```
x*=50
```
`id*=<number>` divide the value of `id` by `<number>`
```
x/=100
```
## Binary operations
Addition `id=<id> + number or <id>`
```
tx=10
spacing=1.2
sx=tx-10
vx=tx+spacing
```
Subtraction `id=<id> - number or <id>`
```
a=x-10
```
Muliplication `id=<id> * number or <id>`
```
a=x*10
```
Division `id=<id> / number or <id>`
```
a=x/10
```
Modulo `id=<id> % number or <id>`
a=x%10
## Coordinate assignments
Assign (x,y) coordinates to the specified identifier. The x coordinate is
`id_x` and the y coordinate is `id_y`. The expression with the parentheses
may be a constant, variable or binary expression.
This code:
```
a=40
b=40
c=20
p0=(50,50)
p1=(a,b)
p2=(a+c,b)
p3=(a+c,b+c)
p4=(a,b+c)
circle p0_x p0_x 3
line p1_x p1_y p2_x p2_y 0.2 "blue"
line p2_x p2_y p3_x p3_y 0.2 "red"
line p3_x p3_y p4_x p4_y 0.2 "green"
line p4_x p4_y p1_x p1_y 0.2 "orange"
```
makes this:

## Polar Coordinates
```
x=polarx cx cy r theta
y=polary cx cy r theta
```
Return the polar coordinate given the center at `(cx, cy)`, radius `r`, and angle `theta` (in degrees)

## Polar Coordinates (composite)
```
p=polar cx cy r theta
```
Return the polar coordinates `(p_x)` and `(p_y)` given the center at `(cx, cy)`, radius `r`, and angle `theta` (in degrees)
## Dump
The keyword `dump` causes a sorted list of variables and their values to
be printed on standard error.
For example, given:
```
deck
slide
x=50
y=50
s=5
message="Hello, decksh"
ctext message x y s
eslide
dump
edeck
```
Produces:
```
deckshVersion = "2024-12-15-1.0.0"
message = "Hello, decksh"
s = 5
x = 50
y = 50
```
You can also specify that specific variables are shown, `dump x y` shows:
```
x = 50
y = 50
```
## Area
```
a=area d
c=area a+b
```
return the circular area, `a` for the diameter `d`.
## Formatted Text
Assign a string variable with formatted text (using package fmt floating point format strings). Up to five variables may be used.
```
w1=10
w2=20+100
s0=format "Widget 1: %.2f" w1
s1=format "Widget 2: %.3f" w2
st=format "Total Widgets: %v" s1+w2
```
Large numbers may also be formatted with commas using the `%,` format string. For example:
```
s=format "%," 123456789 // s contains 123,456,789
```
## Random Number
```
x=random min max
```

assign a random number in the specified range
## Math Functions
decksh supports these math functions:
- cosine
- sine
- square root
- tangent
### Cosine
return the coine of the number of expression (`id` or binary operation)
```
a=4
b=10
x=cosine 4
x=cosine a+b
x=cosine b
```
### Sine
return the sine of the number of expression (`id` or binary operation)
```
a=4
b=10
x=sine 4
x=sine a+b
x=sine b
```
### Square Root
return the square root of the number of expression (`id` or binary operation)
```
a=4
b=10
x=sqrt 4
x=sqrt a+b
x=sqrt b
```
### Tangent
return the square root of the number of expression (`id` or binary operation)
```
a=4
b=10
x=tangent 4
x=tangent a+b
x=tangent b
```
## Range Value Mapping
```
x=vmap v vmin vmax min max
```

For value `v`, map the range `vmin-vmax` to `min-max`.
## Substring
```
x=substr string begin end
```
assigns a substring given beginning and ending indicies.
`-` may be used as a shorthand for the beginning and end.
```
s="hello, world"
a=substr s - - // a="hello, world"
b=substr s - 4 // b="hello"
c=substr s 7 - // c="world"
d=substr s 3 8 // d="lo, wo"
e=substr "This is a test" 5 8 // e="is a"
```
## Loops
Loop over `statements`, with `x` starting at `begin`, ending at `end` with an
optional `increment` (if omitted the increment is 1). Substitution of `x`
will occur in statements.
```
for x=begin end [increment]
statements
efor
```
Loop over `statements`, with `x` ranging over the contents of items within `
[]`. Substitution of `x` will occur in statements.
```
for x=["abc" "def" "ghi"]
statements
efor
```
Loop over `statements`, with `x` ranging over the contents `"file"`.
Substitution of `x` will occur in statements.
```
for x="file"
statements
efor
```
## Conditionals
Specify the conditional execution of decksh statements with `if condition`,
`else` and `eif`. The else block is optional. The values for ```v1``` and
```v2``` may be either numbers of strings. (For strings only ```==``` and
```!=``` apply). The conditions are:
```
if v1 condition v2
where condition is:
== or eq equals if x == y
!= or ne not equals if x != y
< or lt less than if x > y
> or gt greater than if x < y
>= or ge greater than or equal if x >= y
<= or ge less than or equal if x <= y
>< or bt between if x >< y z
```
For example:
```
x=10
y=20
if x > y
text "x is greater than y" x y 5
else
text "x is not greater than y" x y 5
eif
```
The else block may be omitted:
```
if x < 10
text "x is less than 10" x y 5
eif
```
For strings:
```
c1="red"
c2="blue"
if c1 != c2
text "red is not blue" 50 50 2
eif
```
## Include decksh markup from a file
```
include "file"
```
places the contents of `"file"` inline.
## Functions
Functions have a defined `name` and arguments, and are specifed with
statements between the `def` and `edef` keywords
```
def name arg1 arg2 ... argn
statements
edef
```
## Importing function defintions
Functions may be imported once, and then called by name.
For example, given a file `redcircle.dsh`:
```
def redcircle X Y
circle X Y 10 "red"
edef
```
which is referenced:
```
import "redcircle.dsh"
x=50
y=50
x2=x-20
y2=y+20
redcircle x y
redcircle x2 y2
```
makes:

Functions may also be called with the `func` keyword:
```
func "file" arg1 ... argn
```
For example, given a file "ftest.dsh"
```
def ftest funx funy funs funt
funs*=2
ctext funt funx funy funs
edef
```
calling the function:
```
func "ftest.dsh" 50 30 2.5 "hello"
```
produces:
```
funx=50
funy=30
funs=5.0
funt="hello"
ctext "hello" 50 30 5.0
```
## Data: Make a file
```
data "foo.d"
uno 100
dos 200
tres 300
edata
```
makes a file named `foo.d` with the lines between `data` and `edata`.
## Grid: Place objects on a grid
```
grid "file.dsh" x y xskip yskip limit
```

The first file argument (`"file.dsh"` above) specifies a file with decksh
commands; each item in the file must include the arguments "x" and "y".
Normal variable substitution occurs for other arguments. For example if the
contents of `file.dsh` has six items:
```
circle x y 5
circle x y 10
circle x y 15
square x y 5
square x y 10
square x y 15
```
The line:
```
grid "file.dsh" 10 80 20 30 50
```
creates two rows: three circles and then three squares
`x, y` specify the beginning location of the items, `xskip` is the horizontal
spacing between items. `yinternal` is the vertical spacing between items and
`limit` the the horizontal limit. When the `limit` is reached, a new row is
created.
## Text
Left, centered, end, or block-aligned text or file contents (`x` and `y` are the text's reference point), with optional font ("sans", "serif", "mono", or "symbol"), color and opacity.
### left-aligned text
```
text "text" x y size [font] [color] [opacity] [link]
btext "text" x y size [font] [color] [opacity] [link]
```

### centered text
```
ctext "text" x y size [font] [color] [opacity] [link]
```

### right-aligned text
```
etext "text" x y size [font] [color] [opacity] [link]
```

### a text block (from either a string or the contents of a file)
```
textblock "text" x y width size [font] [color] [opacity] [link]
textblockfile "filename" x y width size [font] [color] [opacity] [link]
```

### paragraphs
```
para x y width size [font] [color] [opacity] [link]
....
epara
````

Text rotated along the specified angle (in degrees)
```
rtext "text" x y angle size [font] [color] [opacity] [link]
```
### rotated text

### text on an arc
Text on an arc centered at `(x,y)`, with specified radius, between begin and
ending angles (in degrees). if the beginning angle is less than the ending
angle the text is rendered counter-clockwise. if the beginning angle is
greater than the ending angle, the text is rendered clockwise.
```
arctext "text" x y radius begin-angle end-angle size [font] [color] [opacity] [link]
```

### text from file contents (plain and code formatted)
Place the contents of "filename" at (x,y). Place the contents of "filename" in
gray box, using a monospaced font.
```
textfile "filename" x y size [font] [color] [opacity] [linespacing]
```

```
textcode "filename" x y width size [color]
```

## Images
Plain and captioned, with optional scales, links and caption size. `(x, y)` is
the center of the image, and `width` and `height` are the image dimensions
in pixels.
```
image "file" x y width height [scale] [link]
cimage "file" "caption" x y width height [scale] [link] [size]
```

## Lists
(plain, bulleted, numbered, centered). Optional arguments specify the color,
opacity, line spacing, link and rotation (degrees)
```
list x y size [font] [color] [opacity] [linespacing] [link] [rotation]
```

```
blist x y size [font] [color] [opacity] [linespacing] [link] [rotation]
```

```
nlist x y size [font] [color] [opacity] [linespacing] [link] [rotation]
```

```
clist x y size [font] [color] [opacity] [linespacing] [link] [rotation]
```

### list items, and ending the list
```
li "text"
elist
```
## Graphics
Rectangles, ellipses, squares, circles: specify the center location `(x, y)`
and dimensions `(w,h)` with optional color and opacity. The default color
and opacity is gray, 100%. In the case of the `acircle` keyword, the `a`
argument is the area, not the diameter.
```
rect x y w h [color] [opacity]
ellipse x y w h [color] [opacity]
```


```
square x y w [color] [opacity]
circle x y w [color] [opacity]
```


```
acircle x y a [color] [opacity]
```

Rounded rectangles are similar, with the added radius for the corners:
(solid colors only)
```
rrect x y w h r [color]
```

For polygons, specify the x and y coordinates as a series of numbers, with
optional color and opacity.
```
polygon "xcoords" "ycoords" [color] [opacity]
```

Note that the coordinates may be either discrete:
```
polygon "10 20 30" "50 60 50"
```
or use substitution:
```
x1=10
x2=20
x3=30
y1=50
y2=y1+10
y3=y1
polygon "x1 x2 x3" "y1 y2 y3"
```
A combination of constants and substitution is also allowed.
```
polygon "20 x2 30" "50 y2 50"
```
Polyline is similar to polygon, except line segments are used instead of a filled polygon, and you may specify a line width.
```
polyline "xcoords" "ycoords" [lw] [color] [opacity]
```

For lines, specify the coordinates for the beginning `(x1,y1)` and end points
`(x2, y2)`. For horizontal and vertical lines specify the initial point and
the length. Line thickness, color and opacity are optional, with defaults
(0.2, gray, 100%).
A "pill" shape has is a horizontal line with rounded ends.
```
line x1 y1 x2 y2 [size] [color] [opacity]
```

```
hline x y length [size] [color] [opacity]
```

```
vline x y length [size] [color] [opacity]
```

```
pill x w length size [color]
```

Curve is a quadratic Bezier curve: specify the beginning location `(bx, by)`,
the control point `(cx, cy)`, and ending location `(ex, ey)`.
For arcs, specify the location of the center point `(x,y)`, the width and