-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinterfaces.fsx
59 lines (44 loc) · 1.69 KB
/
interfaces.fsx
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
(** INTERFACES **)
#r "nuget:FSharp.Data"
open FSharp.Data
open System.IO
type IHtmlParser =
abstract member ParseHtml : string -> HtmlDocument
type WebParser () =
interface IHtmlParser with
member this.ParseHtml url = HtmlDocument.Load(url)
member this.ParseHtml url = (this :> IHtmlParser).ParseHtml(url)
type FileParser () =
interface IHtmlParser with
member this.ParseHtml filePath =
filePath
|> File.ReadAllText
|> fun fileContents -> HtmlDocument.Parse(fileContents)
member this.ParseHtml filePath = (this :> IHtmlParser).ParseHtml(filePath)
// Create parsers
let classWebParser = WebParser() :> IHtmlParser
let classFileParser = FileParser() :> IHtmlParser
//Function to handle parsing of HTML
let parseHtml (parser:IHtmlParser) (source:string) = parser.ParseHtml(source)
// Use web parser
parseHtml classWebParser "https://github.com/dotnet/fsharp"
// Use file parser
Path.Join(__SOURCE_DIRECTORY__, "fsharp-github-repo.html")
|> parseHtml classFileParser
(** OBJECT EXPRESSIONS **)
// Implement IHtmlParser to parse HTML contents from URL
let webParser =
{ new IHtmlParser with
member this.ParseHtml url = HtmlDocument.Load(url) }
// Implement IHtmlPraser to parse HTML contents from local file
let fileParser =
{ new IHtmlParser with
member this.ParseHtml filePath =
filePath
|> File.ReadAllText
|> fun fileContents -> HtmlDocument.Parse(fileContents) }
// Parse with webParser
parseHtml webParser "https://github.com/dotnet/fsharp"
// Read local file contents and parse with fileParser
Path.Join(__SOURCE_DIRECTORY__, "fsharp-github-repo.html")
|> parseHtml fileParser