aboutsummaryrefslogtreecommitdiff
path: root/svg.bqn
blob: f419acd3d8a4a832252589b4d90311c540d5b8bc (plain)
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
# Utilities for creating SVG diagrams as strings

# Snag the code highlighter from md.bqn
Highlight  {
  AddT  (↑∾"t"∾↓)¨˜(12˜)  # span to tspan
  (1¨ < ·AddT⊑GetHighlights)Modify 𝕩
}

IsLines  1<≡
DeNest  {(3⌊≡)!0,⥊<,,∾𝕊¨ 𝕩}
Indent  "  "¨ DeNest

# Create a node from a tag name and interior text.
Enc  {
  open  "<",𝕨,">"
  close "</", (𝕨" ")𝕨, ">"
  l  IsLines 𝕩
   open (Indentl 𝕩){𝕨𝕗𝕩}(<l) close
}
Void  {"<",𝕩,"/>"}

FmtNum  {
  ! 0=≡𝕩
  ! (¯∞<  <) 𝕩
  d  3 ˜ b10            # Base 10, up to 3 decimals
  _base  {b|⌊÷b(↕𝔽)}  # 𝔽 is the number of digits
  fl   abs  (÷2×d) + |𝕩
   
    (𝕩<0) / "-"
    '0' + (1+·b1⌈⊢)_base fl
    0<""("."  '0' + · `×/ 3 _base)  d × abs-fl
  
}
Fmt  (2⌊≡)FmtNum(1·(' '∾FmtNum)¨){𝕩!0}

Attr  {
  𝕨   {<∾" ",𝕨,"='",𝕩,"'"}´1 𝕩
}
At  {
  _s  {((+`׬)-𝕗=)}
  𝕨 Attr >'='_s¨ '|'_s 𝕩
}
Pos  "x","y" ˘ FmtNum¨
Elt  VoidAttr

SVG  {
  (∾∾(10+@)¨)IsLines ("svg" Attr "viewBox",Fmt 𝕨) Enc 𝕩
}