generated from cbw-dev/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_generator.html
More file actions
377 lines (331 loc) · 21.3 KB
/
json_generator.html
File metadata and controls
377 lines (331 loc) · 21.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON & Sitemap Generator for Bookdown</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
}
/* Simple transition for feedback */
.feedback-button {
transition: background-color 0.3s ease;
}
/* Hide element by default */
.hidden-initially {
display: none;
}
</style>
</head>
<body class="bg-gray-50 text-gray-800">
<div class="container mx-auto p-4 sm:p-6 lg:p-8 max-w-6xl">
<div class="bg-white p-8 rounded-2xl shadow-lg">
<header class="mb-8 text-center">
<h1 class="text-3xl font-bold text-gray-900">Metadata Generator</h1>
<p class="text-gray-600 mt-2">Fill in the form below and click "Generate" to create the JSON and Sitemap snippets.</p>
</header>
<!-- Main form for user input -->
<form id="json-form" class="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6">
<!-- Column 1 -->
<div class="space-y-6">
<div>
<label for="url" class="block text-sm font-medium text-gray-700 mb-1">Workshop Code</label>
<input type="text" id="url" name="url" placeholder="INR_MonVir-2611" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1">Your workshop code</p>
</div>
<div>
<label for="title" class="block text-sm font-medium text-gray-700 mb-1">Title</label>
<input type="text" id="title" name="title" placeholder="Introduction to R" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1 invisible">placeholder</p>
</div>
<div>
<label for="instructors" class="block text-sm font-medium text-gray-700 mb-1">Instructors</label>
<input type="text" id="instructors" name="instructors" placeholder="Jane Doe, John Smith" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1">Separate multiple names with a comma.</p>
</div>
<div>
<label for="tags" class="block text-sm font-medium text-gray-700 mb-1">Tags</label>
<input type="text" id="tags" name="tags" placeholder="Data Science, R, Statistics" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1">Separate multiple tags with a comma.</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Regions</label>
<div class="grid grid-cols-2 sm:grid-cols-3 gap-2 mt-2">
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="BC" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>BC</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Alberta" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Alberta</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Prairies" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Prairies</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Ontario" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Ontario</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Quebec" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Quebec</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Atlantic" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Atlantic</span>
</label>
<label class="flex items-center space-x-2">
<input type="checkbox" name="regions" value="Online" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span>Online</span>
</label>
</div>
</div>
</div>
<!-- Column 2 -->
<div class="space-y-6">
<div>
<label for="startDate" class="block text-sm font-medium text-gray-700 mb-1">Start Date</label>
<input type="date" id="startDate" name="startDate" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1 invisible">placeholder</p>
</div>
<div>
<label for="endDate" class="block text-sm font-medium text-gray-700 mb-1">End Date</label>
<input type="date" id="endDate" name="endDate" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1 invisible">placeholder</p>
</div>
<div>
<label for="year" class="block text-sm font-medium text-gray-700 mb-1">Year</label>
<input type="number" id="year" name="year" placeholder="YYYY" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1">Auto-filled from Start Date.</p>
</div>
<div>
<label for="image" class="block text-sm font-medium text-gray-700 mb-1">Image Filename</label>
<input type="text" id="image" name="image" placeholder="INR_icon.png" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<p class="text-xs text-gray-500 mt-1 invisible">placeholder</p>
</div>
<!-- NEW DYNAMIC LOCATION INPUTS -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Location(s)</label>
<!-- Online Checkbox (Updated ID) -->
<label class="flex items-center space-x-2 mb-4">
<input type="checkbox" id="online-location-checkbox" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 focus:ring-opacity-50">
<span class="text-sm font-medium text-gray-700">Online</span>
</label>
<!-- Container for dynamic city inputs -->
<div id="location-inputs-container" class="space-y-2 mb-4">
<!-- JS will populate initial input here -->
</div>
<!-- Button to add new city/province input (MOVED DOWN) -->
<button type="button" id="add-location-button" class="mb-4 px-3 py-1.5 text-sm bg-gray-200 text-gray-700 font-medium rounded-lg shadow-sm hover:bg-gray-300 transition duration-150">
+ Add City/Province
</button>
<p class="text-xs text-gray-500 mt-1">Enter locations in [City, Province Abbreviation] format (e.g., "Toronto, ON").</p>
</div>
<!-- END NEW DYNAMIC LOCATION INPUTS -->
<div>
<label for="format" class="block text-sm font-medium text-gray-700 mb-1">Format</label>
<select id="format" name="format" class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="" disabled selected>Select a format</option>
<option value="Online">Online</option>
<option value="In-person">In-person</option>
<option value="Distributed">Distributed</option>
<option value="Hybrid">Hybrid</option>
<option value="Asynchronous">Asynchronous</option>
<option value="Flipped">Flipped</option>
</select>
<p class="text-xs text-gray-500 mt-1 invisible">placeholder</p>
</div>
</div>
</form>
<!-- Generate Button -->
<div class="mt-8 text-center">
<button id="generate-button" class="px-6 py-3 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Generate Snippets
</button>
</div>
<!-- JSON Output section -->
<div id="output-container" class="mt-10 hidden-initially">
<div class="flex justify-between items-center mb-2">
<h2 class="text-xl font-semibold text-gray-900">Generated JSON</h2>
<button id="copy-button" class="feedback-button px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Copy
</button>
</div>
<div class="bg-gray-900 rounded-lg p-4">
<pre><code id="output" class="text-sm text-gray-100 whitespace-pre-wrap break-all"></code></pre>
</div>
</div>
<!-- Sitemap XML Output section -->
<div id="sitemap-output-container" class="mt-8 hidden-initially">
<div class="flex justify-between items-center mb-2">
<h2 class="text-xl font-semibold text-gray-900">Generated Sitemap XML</h2>
<button id="copy-sitemap-button" class="feedback-button px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Copy
</button>
</div>
<div class="bg-gray-900 rounded-lg p-4">
<pre><code id="sitemap-output" class="text-sm text-gray-100 whitespace-pre-wrap break-all"></code></pre>
</div>
</div>
</div>
</div>
<script>
// Get references to all the necessary DOM elements
const form = document.getElementById('json-form');
const generateButton = document.getElementById('generate-button');
// JSON elements
const outputContainer = document.getElementById('output-container');
const outputElement = document.getElementById('output');
const copyButton = document.getElementById('copy-button');
// Sitemap XML elements
const sitemapOutputContainer = document.getElementById('sitemap-output-container');
const sitemapOutputElement = document.getElementById('sitemap-output');
const copySitemapButton = document.getElementById('copy-sitemap-button');
// Dynamic Location elements
const locationContainer = document.getElementById('location-inputs-container');
const addLocationButton = document.getElementById('add-location-button');
const onlineCheckbox = document.getElementById('online-location-checkbox'); // Renamed variable
let locationCounter = 0;
// Helper function to dynamically add a city/province input field
const addLocationInput = (placeholder = "City, Prov. Abbreviation") => {
locationCounter++;
const id = `city-location-${locationCounter}`;
const div = document.createElement('div');
div.className = 'flex items-center space-x-2';
div.setAttribute('data-location-id', id);
div.innerHTML = `
<input type="text" id="${id}" name="city_locations[]" placeholder="${placeholder}"
class="w-full px-3 py-2 bg-gray-50 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" />
<button type="button" data-id="${id}" class="remove-location-button flex-shrink-0 p-2 text-red-500 rounded-full hover:bg-red-50 transition duration-150" aria-label="Remove location">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm4 0a1 1 0 10-2 0v6a1 1 0 102 0V8z" clip-rule="evenodd" />
</svg>
</button>
`;
locationContainer.appendChild(div);
// Add listener to the remove button
div.querySelector('.remove-location-button').addEventListener('click', (e) => {
// Remove the parent div of the button
div.remove();
});
};
// Initialize with one location input
addLocationInput("Montréal, QC");
// Event listener for adding new inputs
addLocationButton.addEventListener('click', () => {
addLocationInput();
});
// Helper function to parse comma-separated strings (used for instructors/tags)
const parseToArray = (str) => {
if (!str) return [];
return str.split(',').map(item => item.trim()).filter(item => item);
};
// Function to generate the JSON output
const generateJson = () => {
const formData = new FormData(form);
const selectedRegions = Array.from(form.querySelectorAll('input[name="regions"]:checked')).map(cb => cb.value);
// Collect physical locations from dynamic inputs
const physicalLocations = Array.from(locationContainer.querySelectorAll('input[name="city_locations[]"]'))
.map(input => input.value.trim())
.filter(value => value.length > 0);
// Add 'Online' if the checkbox is checked
if (onlineCheckbox.checked) { // Updated variable name
physicalLocations.push("Online");
}
const data = {
url: formData.get('url') || "",
title: formData.get('title') || "",
instructors: parseToArray(formData.get('instructors')),
tags: parseToArray(formData.get('tags')),
regions: selectedRegions,
year: parseInt(formData.get('year'), 10) || null,
startDate: formData.get('startDate') || "",
endDate: formData.get('endDate') || "",
image: formData.get('image') ? `img/${formData.get('image')}` : "img/YOURIMAGE.png",
locations: physicalLocations, // Use the collected dynamic array
format: formData.get('format') || ""
};
// Define keys for arrays that should remain on a single line (minified)
const MINIFIED_KEYS = ['instructors', 'tags', 'regions', 'locations'];
const placeholders = {};
// 1. Create a working copy and replace the arrays with placeholders
const dataWithPlaceholders = JSON.parse(JSON.stringify(data));
for (const key of MINIFIED_KEYS) {
if (dataWithPlaceholders[key] && Array.isArray(dataWithPlaceholders[key])) {
// Create a unique placeholder key
const placeholderKey = `__MINIFIED_ARRAY_${key.toUpperCase()}__`;
// Store the minified array string (no line breaks)
placeholders[placeholderKey] = JSON.stringify(dataWithPlaceholders[key]);
// Replace the array in the object with the placeholder string
dataWithPlaceholders[key] = placeholderKey;
}
}
// 2. Pretty print the overall structure using 4 spaces for indentation.
let jsonString = JSON.stringify(dataWithPlaceholders, null, 4) + ',';
// 3. Replace the placeholder quoted strings with the raw, minified array strings.
for (const [placeholderKey, minifiedArrayString] of Object.entries(placeholders)) {
jsonString = jsonString.replace(`"${placeholderKey}"`, minifiedArrayString);
}
outputElement.textContent = jsonString;
};
// Function to generate the Sitemap XML output
const generateSitemapXml = () => {
const formData = new FormData(form);
const urlPath = formData.get('url') || "";
const fullUrl = `https://bioinformaticsdotca.github.io/${urlPath}`;
// Keep minified XML output
const xmlString = `<url><loc>${fullUrl}</loc></url>`;
sitemapOutputElement.textContent = xmlString;
};
// Auto-update the year field when the start date changes
const startDateInput = document.getElementById('startDate');
const yearInput = document.getElementById('year');
startDateInput.addEventListener('input', () => {
const dateValue = startDateInput.value;
if (dateValue) {
yearInput.value = new Date(dateValue).getFullYear();
}
});
// Event listener for the generate button
generateButton.addEventListener('click', () => {
generateJson();
generateSitemapXml();
// Show both output containers
outputContainer.style.display = 'block';
sitemapOutputContainer.style.display = 'block';
});
// Generic copy function for buttons
const setupCopyButton = (button, output) => {
button.addEventListener('click', () => {
const textToCopy = output.textContent;
const tempTextArea = document.createElement('textarea');
tempTextArea.value = textToCopy;
document.body.appendChild(tempTextArea);
tempTextArea.select();
try {
document.execCommand('copy');
const originalText = button.textContent;
button.textContent = 'Copied!';
button.classList.remove('bg-blue-600', 'hover:bg-blue-700');
button.classList.add('bg-green-500');
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('bg-green-500');
button.classList.add('bg-blue-600', 'hover:bg-blue-700');
}, 2000);
} catch (err) {
console.error('Failed to copy text: ', err);
}
document.body.removeChild(tempTextArea);
});
};
// Set up both copy buttons
setupCopyButton(copyButton, outputElement);
setupCopyButton(copySitemapButton, sitemapOutputElement);
</script>
</body>
</html>