This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
172 lines (134 loc) · 4.54 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using static html;
using static System.Console;
namespace html_gen
{
class Program
{
static void Main(string[] args)
{
Func<HtmlDocument> doc = default;
// doc = document();
doc = document(
head(
title("Lorem ipsum di amet.")
),
tag("body",
tag(
"div",
tag("p", text("#1")),
tag("p", text("#2"))
)
)
);
// WriteLine(doc().html());
WriteLine(document(
// tag("body", text("lorem ipsum di amet")),
body(
text("lorem ipsum di amet")
)
)().html());
}
}
}
public abstract class HtmlElement
{
public virtual string html()
{
if (SelfClosing) return $"<{Tag}/>";
return $"<{Tag}>{string.Join(Environment.NewLine, Children.Select(x => x().html()))}</{Tag}>";
}
public abstract string Tag { get; }
public virtual bool SelfClosing { get; } = false;
protected List<Func<HtmlElement>> Children { get; } = new List<Func<HtmlElement>>();
}
public class HtmlDocument : HtmlElement
{
private const string DEFAULT_DOC_TYPE = "html";
public string DocType { get; protected set; } = DEFAULT_DOC_TYPE;
public override string Tag => throw new NotImplementedException();
public override bool SelfClosing => throw new NotImplementedException();
public override string html()
{
return $"<!DOCTYPE {DocType}>{string.Join(Environment.NewLine, Children.Select(x => x().html()))}";
}
public HtmlDocument() : this(DEFAULT_DOC_TYPE) { }
public HtmlDocument(string docType) => DocType = docType;
public HtmlDocument(Func<HtmlElement> child) : this(DEFAULT_DOC_TYPE)
{
Children.Add(child);
}
public HtmlDocument(Func<HtmlElement>[] children)
{
Children.AddRange(children);
}
}
public class Head : HtmlElement
{
public override string Tag => "head";
public override bool SelfClosing => false;
public Head(Func<HtmlElement> child) => this.Children.Add(child);
public Head() { }
}
public class HtmlTag : HtmlElement
{
public override string Tag { get; }
public HtmlTag(string tag, Func<HtmlElement> child) : this(child)
{
Tag = tag;
}
HtmlTag() { }
protected HtmlTag(Func<HtmlElement> child) => Children.Add(child);
public HtmlTag(string tag, Func<HtmlElement>[] children)
{
Tag = tag;
Children.AddRange(children);
}
}
public class Body : HtmlTag
{
public Body(Func<HtmlElement> child) : base("body", child)
{
}
public Body(Func<HtmlElement>[] children) : base("body", children)
{
}
}
public class HtmlText : HtmlElement
{
private readonly string Text;
public override string Tag => throw new NotImplementedException();
public override string html() => Text;
public HtmlText(string text) => Text = text;
}
public class Title : HtmlTag
{
public Title(Func<HtmlText> child) : base("title", child) {}
}
public static class html
{
public static Func<HtmlDocument> document() => () => new HtmlDocument();
public static Func<HtmlDocument> document(Func<HtmlElement> child)
{
return () => new HtmlDocument(child);
}
public static Func<HtmlDocument> document(params Func<HtmlElement>[] children)
{
return () => new HtmlDocument(children);
}
public static Func<Head> head() => () => new Head();
public static Func<Head> head(Func<HtmlElement> child) => () => new Head(child);
public static Func<Title> title() => () => new Title(() => new HtmlText(string.Empty));
public static Func<Title> title(string title) => () => new Title(() => new HtmlText(title));
public static Func<HtmlText> text(string text) => () => new HtmlText(text);
public static Func<HtmlTag> tag(string tag, Func<HtmlElement> child) => () => new HtmlTag(tag, child);
public static Func<HtmlTag> tag(string tag, params Func<HtmlElement>[] children) => () =>
{
return new HtmlTag(tag, children);
};
public static Func<Body> body(params Func<HtmlElement>[] children){
return () => new Body(children);
}
}