-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.exs
executable file
·111 lines (101 loc) · 2.67 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
defmodule App do
@map_layout %{
} #_MAP_LAYOUT
@map_widget %{
} #_MAP_WIDGET
@map_page %{
} #_MAP_PAGE
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