-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
244 lines (204 loc) · 6.25 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSON to Hermes Builder Java code</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Satisfy&family=Source+Sans+3:wght@300;600&display=swap"
rel="stylesheet">
</head>
<body>
<header>
<h1>JSON to <span id="hermes-name">Hermes Builder</span></h1>
<small>Get your Java builder from your JSON file!</small>
</header>
<div id="github">
<a href="" class="sm:inline-block"><iframe
src="https://ghbtns.com/github-btn.html?user=victorhsr&repo=hermes-builder&type=star&count=true"
frameborder="0" scrolling="0" width="120" height="20" title="Hermes Builder on Github"></iframe></a>
</div>
<div id="parse_wrapper">
<button id="parse_btn" onclick="onParse()"
title="Click to parse the JSON file to Hermes Builder Java code">Parse</button>
</div>
<div class="code_wrapper">
<div class="code_area_wapper">
<div class="code_area_title">JSON</div>
<textarea id="input" wrap="off" placeholder="Insert your JSON here and click on 'Parse'"></textarea>
</div>
<div class="code_area_wapper">
<div class="code_area_title right">Hermes Builder - Java code</div>
<textarea id="output" wrap="off" placeholder="Hermes Builder Java code will be placed here"
disabled></textarea>
<button id="copy_btn" title="Copy code" onclick="onCopy()">Copy code</button>
<div id="copy_feedback">Code copied!</div>
</div>
</div>
</body>
</html>
<style>
#copy_btn {
position: absolute;
top: 0;
right: 15px;
cursor: pointer;
visibility: hidden;
}
#copy_feedback {
position: absolute;
top: 30px;
right: 15px;
background-color: #FFFFE0;
border-color: white;
font-weight: bolder;
padding: 0px 3px 0px 3px;
visibility: hidden;
}
.material-symbols-outlined {
font-variation-settings:
'FILL' 0,
'wght' 400,
'GRAD' 0,
'opsz' 24
}
#github {
position: absolute;
top: 10px;
right: 0;
}
body {
background-color: #EEEEEE;
font-family: 'Source Sans 3', sans-serif;
}
header {
/* display: flex;
justify-content: center;
flex-direction: row; */
text-align: center;
margin-bottom: 50px;
}
header small {
font-size: 17px;
}
.code_wrapper {
display: flex;
width: 100%;
height: 80vh;
justify-content: space-around;
align-items: center;
}
.code_area_wapper {
height: 100%;
display: flex;
align-items: center;
width: 48%;
position: relative;
}
.code_area_title {
position: absolute;
top: -20px;
background-color: #FFFFE0;
border-color: white;
font-weight: bolder;
padding: 0px 3px 0px 3px;
}
.code_area_title.right {
right: 0;
}
.code_area_wapper textarea {
height: 90%;
width: 100%;
top: 0;
margin-top: 0;
padding-top: 0;
position: absolute;
padding: 0;
margin: 0;
}
#parse_wrapper {
width: 100%;
display: flex;
justify-content: center;
margin-bottom: 10px;
}
#parse_btn {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
#parse_btn:hover {
background-color: #45a049;
}
#hermes-name {
font-family: 'Satisfy', cursive;
letter-spacing: 0.5px;
color: blue;
font-size: 1.2em;
}
</style>
<script>
function onCopy() {
var copyText = document.getElementById("output");
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
navigator.clipboard.writeText(copyText.value);
document.getElementById("copy_feedback").style.visibility = "visible"
setTimeout(() => {
document.getElementById("copy_feedback").style.visibility = "hidden"
}, 1500);
}
function onParse() {
const jsonObject = getInputValue();
if (!jsonObject) return;
const result = convertJSONToPattern(jsonObject);
document.getElementById("output").value = result
document.getElementById("copy_btn").style.visibility = "visible"
}
function getInputValue() {
try {
return JSON.parse(document.getElementById("input").value);
} catch (error) {
alert('Inform a valid JSON as input')
return null;
}
}
function convertJSONToPattern(obj) {
let pattern = '';
function buildPattern(obj, indent = 1) {
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = obj[key];
const addComma = i < (keys.length - 1);
if (!Array.isArray(value) && typeof value === 'object') {
pattern += `${buildIndentation(indent)}${key}(\n`;
buildPattern(value, indent + 1);
pattern += `${buildIndentation(indent)})${addComma ? ',' : ''}\n`;
} else {
const formattedValue = formatValue(value);
pattern += `${buildIndentation(indent)}${key}(${formattedValue})${addComma ? ',' : ''}\n`;
}
}
}
buildPattern(obj);
return `(\n${pattern})`;
}
function buildIndentation(size) {
return " ".repeat(size)
}
function formatValue(value) {
if (typeof value === 'string') {
return `\"${value}\"`;
}
if (Array.isArray(value)) {
return "your_list";
}
return value;
}
</script>