-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.exs
executable file
·204 lines (189 loc) · 6.33 KB
/
template.exs
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
Mix.install([:jason])
defmodule App do
@map_layout %{
} #_MAP_LAYOUT
@map_widget %{
} #_MAP_WIDGET
@map_page %{
} #_MAP_PAGE
def listen() do
input = IO.read(:line) |> String.trim() |> String.split(",")
cond do
length(input) == 1 ->
[arg1] = input
if arg1 == "stop" do
System.halt()
end
length(input) == 2 ->
[arg1, arg2] = input
if arg1 == ":widget" do
IO.puts apply(App, :widget, [arg2, %{}])
else if arg1 == "widget" do
IO.puts apply(App, :widget, [arg2, %{}]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
else if arg1 == ":layout" do
IO.puts apply(App, :layout, [arg2, %{}, %{}])
else if arg1 == "layout" do
IO.puts apply(App, :layout, [arg2, %{}, %{}]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
else if arg1 == ":render" do
IO.puts apply(App, :render, [arg2, "layout", %{}])
else
IO.puts apply(App, :render, [arg2, "layout", %{}]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end end end end end
length(input) == 3 ->
[arg1, arg2, arg3] = input
if arg1 == ":widget" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode() do
IO.puts apply(App, :widget, [arg2, json])
end
else if arg1 == "widget" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode() do
IO.puts apply(App, :widget, [arg2, json]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end
else if arg1 == ":layout" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode() do
IO.puts apply(App, :layout, [arg2, json, %{}])
end
else if arg1 == "layout" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode() do
IO.puts apply(App, :layout, [arg2, json, %{}]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end
else if arg1 == ":render" do
IO.puts apply(App, :render, [arg2, arg3, %{}])
else
IO.puts apply(App, :render, [arg2, arg3, %{}]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end end end end end
length(input) == 4 ->
[arg1, arg2, arg3, arg4] = input
if arg1 == ":layout" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode(), {:ok, cont} <- Base.decode64!(arg4) |> Jason.decode() do
IO.puts apply(App, :layout, [arg2, json, cont])
end
else if arg1 == "layout" do
with {:ok, json} <- Base.decode64!(arg3) |> Jason.decode(), {:ok, cont} <- Base.decode64!(arg4) |> Jason.decode() do
IO.puts apply(App, :layout, [arg2, json, cont]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end
else if arg1 == ":render" do
with {:ok, json} <- Base.decode64!(arg4) |> Jason.decode() do
IO.puts apply(App, :render, [arg2, arg3, json])
end
else
with {:ok, json} <- Base.decode64!(arg4) |> Jason.decode() do
IO.puts apply(App, :render, [arg2, arg3, json]) |> String.replace(~r/<<(.*?)>>/, fn(c) ->
String.slice(c, 2..-3) |> Base.decode64!
end)
end
end end end
true ->
IO.puts "<h1>Error 500</h1><h2>Internal Server Error!</h2>"
end
App.listen()
end #_LISTEN
def render(name, layout, args) do
cond do
@map_page[name] ->
apply(PAGE, @map_page[name], [layout, args])
@map_page["#{name}/index"] ->
apply(PAGE, @map_page["#{name}/index"], [layout, args])
@map_page["#{name}/404"] ->
apply(PAGE, @map_page["#{name}/404"], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
@map_page["#{name}/error"] ->
apply(PAGE, @map_page["#{name}/error"], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
@map_page[String.replace(name, ~r/\/[^\/]+$/, "/404")] ->
apply(PAGE, @map_page[String.replace(name, ~r/\/[^\/]+$/, "/404")], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
@map_page[String.replace(name, ~r/\/[^\/]+$/, "/error")] ->
apply(PAGE, @map_page[String.replace(name, ~r/\/[^\/]+$/, "/error")], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
@map_page["404"] ->
apply(PAGE, @map_page["404"], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
@map_page["error"] ->
apply(PAGE, @map_page["error"], [layout, Map.merge(args, %{
status: 404,
error: "Page Not Found!"
})])
true ->
"<h1>Error 404</h1>\n<h2>Page Not Found!</h2>"
end
end
def widget(widget, args) do
if @map_widget[widget] do
apply(WIDGET, @map_widget[widget], [args])
else
"{Error 500: Widget Not Found!}"
end
end
def layout(layout, args, cont) do
if @map_layout[layout] do
apply(LAYOUT, @map_layout[layout], [args, cont])
else
Enum.reduce(cont, "", fn {_, val}, a ->
"#{a} #{val}"
end)
end
end
def escapeHTML(arg) do
if is_bitstring(arg) do
String.replace(arg, ~r/[<>&]/, fn (c) ->
cond do
c == "<" ->
"<"
c == ">" ->
">"
c == "&" ->
"&"
true ->
""
end
end) |> String.replace(~r/&(amp;)*/, "&")
else
arg
end
end
def escapeArg(arg) do
if is_bitstring(arg) do
String.replace(arg, ~r/([\\]*)([\\"'])/, fn (c) ->
if rem(String.length(c), 2) == 0 do
"#{c}"
else
"\\#{c}"
end
end)
else
arg
end
end
end
defmodule LAYOUT do
end #_LAYOUT
defmodule WIDGET do
end #_WIDGET
defmodule PAGE do
end #_PAGE
App.listen()