-
Notifications
You must be signed in to change notification settings - Fork 58
/
svg.bqn
44 lines (39 loc) · 1.52 KB
/
svg.bqn
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
# Utilities for creating SVG diagrams as strings
# Snag the code highlighter from md.bqn
Highlight ← {
AddT ← (↑∾"t"∾↓)¨˜⟜(1‿2⥊˜≢)⍟(𝕨⊣1) # span to tspan
(1¨ <⊸∾ ·AddT⌾⊑GetHighlights)⊸Modify 𝕩
}
# Create a node from a tag name and interior text.
Enc ← {
DeNest ← {(3⌊≡)◶⟨!∘0,⋈,⊢,∾𝕊¨⟩ ⥊𝕩}
open ← ∾⟨"<",𝕨,">"⟩
close← ∾⟨"</", (∧`𝕨≠' ')/𝕨, ">"⟩
l ← 1 < d←≡𝕩
∾ open ({" "⊸∾¨(∾DeNest¨)⍟(3≤d)⥊𝕩}⍟l 𝕩){𝕨‿𝕗‿𝕩}○(⥊∘<⍟l) close
}
Void ← {∾⟨"<",𝕩,"/>"⟩}
FmtNums ← {
! ∧´ (¯∞⊸< ∧ <⟜∞) 𝕩
k←3⋆˜b←10
m‿p←"-."-'0'
s←𝕩<0⋄a←|𝕩⋄v←⌊0.5+k×a
⊔⟜('0'⊸+)´<∘∾˘⍉∾⟨
≍⋈⟜(m¨)/s
↕∘≠⊸{f←b≤𝕩⋄(∨´f)◶⟨≍,𝕊⟜(⌊÷⟜b)○(f⊸/)´∾b⊸|⌾(⊑⌽)⟩𝕨‿𝕩}⌊v÷k
p¨⌾(⊑⌽)∘⊏⊸∾⍟(0<≠)↕∘≠⊸{f←×𝕩↩k|𝕩⋄𝕨(∨´f)◶⟨↕0‿2,(⋈⟜(⌊÷⟜k)∾𝕊)⟜(b⊸×)○(f⊸/)⟩𝕩}v
⟩
}
FmtNum ← (2⌊≡)◶(⊑FmtNums∘⥊)‿(FmtNums⌾⥊)‿{𝕩⋄!0}
Fmt ← (2⌊≡)◶FmtNum‿(1↓·∾' '∾¨FmtNum)‿{𝕩⋄!0}
At1 ← " " ∾ {∾⟨𝕨,"='",𝕩,"'"⟩}´
Attr ← ∾⟜(∾ <∘At1⎉1)
At ← {
_s ← {((+`׬)⊸-𝕗⊸=)⊸⊔}
𝕨 >⊘(∾⟜(∾At1¨)) '='_s¨ '|'_s 𝕩
}
Pos ← ⟨"x","y"⟩ ≍˘ FmtNum
Elt ← Void∘Attr
SVG ← {
("svg" Attr ⟨"viewBox",Fmt 𝕨⟩) Enc 𝕩
}