-
Notifications
You must be signed in to change notification settings - Fork 12
/
Main.purs
213 lines (179 loc) · 9.11 KB
/
Main.purs
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
module Main where
import Prelude
import Control.Monad.Except (ExceptT, runExceptT)
import DOM.Common (Ctor(..), Interface, TagNS(..), mkAttribute, unSnake)
import DOM.Indexed as Indexed
import DOM.Parse as Parse
import DOM.TypeStub (TypeStub(..))
import Data.Argonaut.Decode (class DecodeJson)
import Data.Array as Array
import Data.Either (blush)
import Data.Foldable (for_)
import Data.Maybe (Maybe(..))
import Data.Set as Set
import Data.String as String
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Traversable (sequence)
import Data.Tuple.Nested ((/\))
import Data.Variant (Variant, inj)
import Effect (Effect)
import Effect.Aff (Aff, Error, launchAff_, message)
import Effect.Class.Console as Console
import FS as FS
import Node.Path as Path
import Parser as Parser
import Prim.Row (class Cons)
import Type.Proxy (Proxy(..))
main :: Effect Unit
main = launchAff_ do
r <- runExceptT generate
for_ ( blush r ) \e -> do
Console.log $ message e
cachePath = "codegen/cache" :: String
fetch :: forall @label @spec r1 r2
. IsSymbol label
=> Cons label spec r1 r2
=> DecodeJson spec
=> String -> ExceptT Error Aff ( Variant r2 )
fetch url = inj ( Proxy @label ) <$> do
let urlFilename = Path.basename url
cacheName = reflectSymbol ( Proxy @label )
localFilename = Path.concat [ cachePath, cacheName, urlFilename ]
FS.cachedFetch @spec localFilename url
generate :: ExceptT Error Aff Unit
generate = do
FS.createDir $ cachePath <> "/dfn"
FS.createDir $ cachePath <> "/events"
FS.createDir $ cachePath <> "/idlparsed"
FS.createDir $ cachePath <> "/elements"
html <- Parse.parse HTML <$> sequence
[ fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/html.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/html-media-capture.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/pointerlock-2.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/selection-api.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/css-transitions-1.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/css-transitions-2.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/wai-aria-1.2.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/html.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/touch-events.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/uievents.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/pointerevents3.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/css-animations-1.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/css-transitions-1.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/mediacapture-handle-actions.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/mediacapture-streams.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/mediastream-recording.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/dom.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/html.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/wai-aria-1.2.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/html.json"
]
svg <- fixSVG <<< Parse.parse SVG <$> sequence
[ fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/SVG2.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/svg-integration.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/svg-strokes.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/fill-stroke-3.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/filter-effects-1.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/svg-animations.json"
, fetch @"events" "https://raw.githubusercontent.com/w3c/webref/curated/ed/events/svg-animations.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/SVG2.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/svg-animations.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/filter-effects-1.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/css-masking-1.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/SVG2.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/svg-animations.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/filter-effects-1.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/css-masking-1.json"
]
mathml <- Parse.parse MathML <$> sequence
[ fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/mathml-core.json"
, fetch @"dfn" "https://raw.githubusercontent.com/w3c/webref/curated/ed/dfns/mathml-aam.json"
, fetch @"idlparsed" "https://raw.githubusercontent.com/w3c/webref/curated/ed/idlparsed/mathml-core.json"
, fetch @"elements" "https://raw.githubusercontent.com/w3c/webref/curated/ed/elements/mathml-core.json"
]
Indexed.generate html svg mathml
Parser.generate
-- SVG spec is barely useful
fixSVG :: Parse.Specification -> Parse.Specification
fixSVG svgBase =
svgBase
{ interfaces =
map ( missingPresentationProperties ) svgBase.interfaces
<> [ svgText, svgPresentation ]
, attributes = do
let existing = Set.fromFoldable $ _.name <$> svgBase.attributes
patched = flip Array.mapMaybe ( svgTextMembers <> svgPresentationMembers ) \member ->
if Set.member member existing then Nothing else mkAttribute mempty member
svgBase.attributes <> patched
}
missingPresentationProperties :: Interface -> Interface
missingPresentationProperties = case _ of
txt@{ name } | name `Array.elem` [ "SVGTextElement", "SVGTextPathElement", "SVGTspanElement" ] ->
txt { bases = txt.bases <> [ svgText.ctor, svgPresentation.ctor ] }
fe@{ name } | Just _ <- String.stripPrefix ( String.Pattern "SVGFe" ) name ->
fe { bases = fe.bases <> [ svgPresentation.ctor, Ctor "SVGFilterPrimitiveElement" ] }
animate@{ name : "SVGAnimateElement" } ->
animate { members = animate.members <> [ Ctor "by" /\ TypeString ] }
animate@{ name } | Just _ <- String.stripPrefix ( String.Pattern "SVGAnimate" ) name ->
animate { bases = animate.bases <> [ svgPresentation.ctor, Ctor "SVGAnimateElement" ] }
svg@{ name : "SVGSVGElement" } ->
svg
{ members = svg.members <>
[ Ctor "height" /\ TypeString
, Ctor "width" /\ TypeString
]
, bases = svg.bases <> [ svgPresentation.ctor ]
}
clippath@{ name : "SVGClipPathElement" } ->
clippath
{ members = clippath.members <>
[ Ctor "clipPathUnits" /\ TypeString ]
}
id ->
id { bases = id.bases <> [ svgPresentation.ctor ] }
svgText :: Interface
svgText =
{ ctor : Ctor "SvgText"
, name : "SvgText"
, bases : []
, members : map ( ( _ /\ TypeString ) <<< Ctor <<< unSnake ) svgTextMembers
}
svgTextMembers :: Array String
svgTextMembers =
[ "alignment-baseline"
, "baseline-shift"
, "dominant-baseline"
, "font-family"
, "font-size"
, "font-size-adjust"
, "font-stretch"
, "font-style"
, "font-variant"
, "font-weight"
, "letter-spacing"
, "text-decoration"
, "word-spacing"
, "writing-mode"
, "unicode-bidi"
]
svgPresentation :: Interface
svgPresentation =
{ ctor : Ctor "SvgPresentation"
, name : "SvgPresentation"
, bases : []
, members : map ( ( _ /\ TypeString ) <<< Ctor <<< unSnake ) svgPresentationMembers
}
svgPresentationMembers :: Array String
svgPresentationMembers =
[ "pathLength"
, "mask"
, "opacity"
, "overflow"
, "clip-path"
, "clip-rule"
, "cursor"
, "display"
, "transform"
, "transform-origin"
, "visibility"
]