-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathHtmlGenerator.fs
More file actions
386 lines (304 loc) · 13.3 KB
/
HtmlGenerator.fs
File metadata and controls
386 lines (304 loc) · 13.3 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
#nowarn "10001" // Disable "This method is intended for use in generated code only." triggered by nameof references.
// --------------------------------------------------------------------------------------
// HTML type provider - generate code for accessing inferred elements
// --------------------------------------------------------------------------------------
namespace ProviderImplementation
open System
open System.Collections.Generic
open FSharp.Quotations
open FSharp.Reflection
open ProviderImplementation.ProvidedTypes
open ProviderImplementation.QuotationBuilder
open FSharp.Data
open FSharp.Data.Runtime
open FSharp.Data.Runtime.BaseTypes
open FSharp.Data.Runtime.StructuralTypes
module internal HtmlGenerator =
type private FieldInfo =
{
/// The representation type that is part of the tuple we extract the field from
TypeForTuple: Type
/// The provided property corresponding to the field
ProvidedProperty: ProvidedProperty
Convert: Expr -> Expr
}
let private getPropertyName = NameUtils.capitalizeFirstLetter
let private typeNameGenerator () =
NameUtils.uniqueGenerator (fun s ->
HtmlParser.invalidTypeNameRegex.Value.Replace(s, " ")
|> NameUtils.nicePascalName)
let private createTableType
getTableTypeName
(inferenceParameters, missingValuesStr, cultureStr)
supportsNet6Types
(table: HtmlTable)
=
let rawColumns =
match table.InferedProperties with
| Some inferedProperties -> inferedProperties
| None ->
HtmlInference.inferColumns
inferenceParameters
table.HeaderNamesAndUnits.Value
(if table.HasHeaders.Value then
table.Rows.[1..]
else
table.Rows)
let columns =
#if NET6_0_OR_GREATER
if supportsNet6Types then
rawColumns
else
rawColumns |> List.map StructuralInference.downgradeNet6PrimitiveProperty
#else
rawColumns
#endif
let fields =
columns
|> List.mapi (fun index field ->
let typ, typWithoutMeasure, conv, _convBack =
ConversionsGenerator.convertStringValue missingValuesStr cultureStr false field
{ TypeForTuple = typWithoutMeasure
ProvidedProperty =
ProvidedProperty(
field.Name,
typ,
getterCode =
fun (Singleton row) ->
if columns.Length = 1 then
row
else
Expr.TupleGet(row, index)
)
Convert = fun rowVarExpr -> conv <@ TextConversions.AsString((%%rowVarExpr: string[]).[index]) @> })
// The erased row type will be a tuple of all the field types (without the units of measure)
let rowErasedType =
(match fields with
| [ x ] -> x.TypeForTuple
| _ -> FSharpType.MakeTupleType [| for field in fields -> field.TypeForTuple |])
let rowType =
ProvidedTypeDefinition("Row", Some rowErasedType, hideObjectMethods = true, nonNullable = true)
// Each property of the generated row type will simply be a tuple get
for field in fields do
rowType.AddMember field.ProvidedProperty
let tableErasedWithRowErasedType =
typedefof<HtmlTable<_>>.MakeGenericType(rowErasedType)
let tableErasedTypeWithGeneratedRow =
typedefof<HtmlTable<_>>.MakeGenericType(rowType)
let rowConverter =
let rowVar = Var("row", typeof<string[]>)
let rowVarExpr = Expr.Var rowVar
let body =
if fields.Length = 1 then
fields.Head.Convert rowVarExpr
else
Expr.NewTuple [ for field in fields -> field.Convert rowVarExpr ]
let delegateType =
typedefof<Func<_, _>>.MakeGenericType(typeof<string[]>, rowErasedType)
Expr.NewDelegate(delegateType, [ rowVar ], body)
let create (htmlDoc: Expr) =
let rowConverterVar = Var("rowConverter", rowConverter.Type)
let body =
tableErasedWithRowErasedType?(nameof (HtmlDocument.Create))
()
(Expr.Var rowConverterVar, htmlDoc, table.Name, table.HasHeaders.Value)
Expr.Let(rowConverterVar, rowConverter, body)
let tableType =
ProvidedTypeDefinition(
getTableTypeName table.Name,
Some tableErasedTypeWithGeneratedRow,
hideObjectMethods = true,
nonNullable = true
)
tableType.AddMember rowType
create, tableType
let private createListType
getListTypeName
(inferenceParameters, missingValuesStr, cultureStr)
supportsNet6Types
(list: HtmlList)
=
let rawColumns = HtmlInference.inferListType inferenceParameters list.Values
let columns =
#if NET6_0_OR_GREATER
if supportsNet6Types then
rawColumns
else
StructuralInference.downgradeNet6Types rawColumns
#else
rawColumns
#endif
let listItemType, conv =
match columns with
| InferedType.Primitive(typ, _, optional, _) ->
let typ, _, conv, _convBack =
ConversionsGenerator.convertStringValue
missingValuesStr
cultureStr
false
(StructuralTypes.PrimitiveInferedProperty.Create("", typ, optional, None))
typ, conv
| _ ->
let typ, _, conv, _convBack =
ConversionsGenerator.convertStringValue
missingValuesStr
cultureStr
false
(StructuralTypes.PrimitiveInferedProperty.Create("", typeof<string>, false, None))
typ, conv
let listTypeWithErasedType = typedefof<HtmlList<_>>.MakeGenericType(listItemType)
let rowConverter =
let rowVar = Var("row", typeof<string>)
let rowVarExpr = Expr.Var rowVar
let body = conv <@ TextConversions.AsString(%%rowVarExpr: string) @>
let delegateType =
typedefof<Func<_, _>>.MakeGenericType(typeof<string>, listItemType)
Expr.NewDelegate(delegateType, [ rowVar ], body)
let create (htmlDoc: Expr) =
let rowConverterVar = Var("rowConverter", rowConverter.Type)
let body =
listTypeWithErasedType?(nameof (HtmlDocument.Create)) () (Expr.Var rowConverterVar, htmlDoc, list.Name)
Expr.Let(rowConverterVar, rowConverter, body)
let listType =
ProvidedTypeDefinition(
getListTypeName list.Name,
Some listTypeWithErasedType,
hideObjectMethods = true,
nonNullable = true
)
create, listType
let private createDefinitionListType
getDefinitionListTypeName
(inferenceParameters, missingValuesStr, cultureStr)
supportsNet6Types
(definitionList: HtmlDefinitionList)
=
let getListTypeName = typeNameGenerator ()
let createListType index (list: HtmlList) =
let rawColumns = HtmlInference.inferListType inferenceParameters list.Values
let columns =
#if NET6_0_OR_GREATER
if supportsNet6Types then
rawColumns
else
StructuralInference.downgradeNet6Types rawColumns
#else
rawColumns
#endif
let listItemType, conv =
match columns with
| StructuralTypes.InferedType.Primitive(typ, _, optional, _) ->
let typ, _, conv, _convBack =
ConversionsGenerator.convertStringValue
missingValuesStr
cultureStr
false
(StructuralTypes.PrimitiveInferedProperty.Create("", typ, optional, None))
typ, conv
| _ ->
let typ, _, conv, _convBack =
ConversionsGenerator.convertStringValue
missingValuesStr
cultureStr
false
(StructuralTypes.PrimitiveInferedProperty.Create("", typeof<String>, false, None))
typ, conv
let listTypeWithErasedType = typedefof<HtmlList<_>>.MakeGenericType(listItemType)
let rowConverter =
let rowVar = Var("row", typeof<string>)
let rowVarExpr = Expr.Var rowVar
let body = conv <@ TextConversions.AsString(%%rowVarExpr: string) @>
let delegateType =
typedefof<Func<_, _>>.MakeGenericType(typeof<string>, listItemType)
Expr.NewDelegate(delegateType, [ rowVar ], body)
let create doc =
let rowConverterVar = Var("rowConverter", rowConverter.Type)
let body =
listTypeWithErasedType?(nameof (HtmlList.CreateNested))
()
(Expr.Var rowConverterVar, doc, definitionList.Name, index)
Expr.Let(rowConverterVar, rowConverter, body)
let listType =
ProvidedTypeDefinition(
getListTypeName list.Name,
Some listTypeWithErasedType,
hideObjectMethods = true,
nonNullable = true
)
let prop =
ProvidedProperty(getPropertyName list.Name, listType, getterCode = (fun (Singleton doc) -> create doc))
prop, listType
let definitionListType =
ProvidedTypeDefinition(
getDefinitionListTypeName definitionList.Name,
Some typeof<HtmlDocument>,
hideObjectMethods = true,
nonNullable = true
)
for prop, listType in List.mapi createListType definitionList.Definitions do
definitionListType.AddMember listType
definitionListType.AddMember prop
definitionListType
let generateTypes asm ns typeName parameters supportsNet6Types htmlObjects =
let htmlType =
ProvidedTypeDefinition(
asm,
ns,
typeName,
Some typeof<HtmlDocument>,
hideObjectMethods = true,
nonNullable = true
)
let containerTypes = Dictionary<string, ProvidedTypeDefinition>()
let getTypeName = typeNameGenerator ()
let getOrCreateContainer name =
match containerTypes.TryGetValue(name) with
| true, t -> t
| false, _ ->
let containerType =
ProvidedTypeDefinition(
name + "Container",
Some typeof<HtmlDocument>,
hideObjectMethods = true,
nonNullable = true
)
htmlType.AddMember
<| ProvidedProperty(name, containerType, getterCode = (fun (Singleton doc) -> doc))
htmlType.AddMember containerType
containerTypes.Add(name, containerType)
containerType
for htmlObj in htmlObjects do
match htmlObj with
| Table table ->
let containerType = getOrCreateContainer "Tables"
let create, tableType =
createTableType getTypeName parameters supportsNet6Types table
htmlType.AddMember tableType
containerType.AddMember
<| ProvidedProperty(
getPropertyName table.Name,
tableType,
getterCode = fun (Singleton doc) -> create doc
)
| List list ->
let containerType = getOrCreateContainer "Lists"
let create, tableType = createListType getTypeName parameters supportsNet6Types list
htmlType.AddMember tableType
containerType.AddMember
<| ProvidedProperty(
getPropertyName list.Name,
tableType,
getterCode = fun (Singleton doc) -> create doc
)
| DefinitionList definitionList ->
let containerType = getOrCreateContainer "DefinitionLists"
let tableType =
createDefinitionListType getTypeName parameters supportsNet6Types definitionList
htmlType.AddMember tableType
containerType.AddMember
<| ProvidedProperty(
getPropertyName definitionList.Name,
tableType,
getterCode = fun (Singleton doc) -> doc
)
htmlType