am2wiki.html 12 KB
Newer Older
piks3l's avatar
piks3l committed
1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head>
	<title>Am2Wiki</title>
	<meta charset="UTF-8">
</head>

<!--
	Am2Wiki by Oncela
Oncela's avatar
Oncela committed
10
	Last modification: 13-04-2017
piks3l's avatar
piks3l committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	
	This program intends to convert amendments drafted by Members of European 
	Parliament into reusable formats.
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version. Get a copy of the GNU General 
    Public License at <http://www.gnu.org/licenses/>.
-->


<!----- For users 	----->

<script>

	// Colors and symbols used to rate the amendments (you can add or remove some)
	var rates 	= [		"unrated", 	"--", 		"-", 		"/", 		"+", 		"++"		];
	var colors 	= [		"white", 	"#A60000", 	"#F55E33", 	"#BABABA", 	"#AEF279", 	"#44C92C"	];
	
Oncela's avatar
Oncela committed
31
32
33
	// Political groups of the EP
	var groups  = [ "", "EPP", "S&D", "ALDE", "Verts/ALE", "ECR", "GUE/NGL", "NI", "EFD" ];
	
piks3l's avatar
piks3l committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	// Template for conversion (how the amendments are displayed once processed: you can change it at will) 
	/* 
		%number 	= amendmend number
		%rate 		= amendment rating
		%article	= amended article
		%color		= amendment color (according to its rating)
		%left		= original text
		%right		= amended text
		%comment 	= comment, if any
	*/
	var template = 
	
		  "=== %number %rate ===\n"
		+ "\n"
Oncela's avatar
Oncela committed
48
		+ "{| border='1' style='border-spacing:0;  width:100%;' cellpadding='30'\n"
piks3l's avatar
piks3l committed
49
50
51
		+ "|-\n"
		+ "| colspan='2' style='background-color: %color; text-align:center;' |\n"
		+ "'''%number'''<br/>\n"
Oncela's avatar
Oncela committed
52
53
		+ "%member<br/>\n"
		+ "%group"		// If a group has been set, the templateGroup (defined bellow) is inserted here
piks3l's avatar
piks3l committed
54
55
56
57
58
59
		+ "%article\n"
		+ "|-\n"
		+ "|width='50%' style='vertical-align:top;'|\n"
		+ "%left\n"
		+ "|width='50%' style='vertical-align:top;'|\n"
		+ "%right\n"
Oncela's avatar
Oncela committed
60
61
62
63
64
65
66
67
		+ "%justif" 	// If the amendment is justified, the templateJustif  (defined bellow) is inserted here
		+ "|}\n\n"
		+ "%comment"	// If the amendment is commented, the templateComment (defined bellow) is inserted here
		+ "\n";
	
	var templateGroup = "'''%group'''<br/>\n";
	
	var templateJustif = 
piks3l's avatar
piks3l committed
68
69
		  "|-\n"
		+ "| colspan='2' |\n"
Oncela's avatar
Oncela committed
70
71
72
		+ "''Justification: %justif''\n";
		
	var templateComment = "\n%comment\n\n";
piks3l's avatar
piks3l committed
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

</script>


<!-----    HTML 	----->

<body>

	<!-- Title -->
	<div id="title">Am2Wiki</div>
	
	<!-- Instructions (change during processing) -->
	<div id="intro">Select an HTML document containing EP amendments:<br/><br/></div>

	<!-- File selector (disappears once a file is selected) -->
	<input id = "input" type="file" onchange="read(this.files[0])">
	
	<!-- After processing, contains the checkboxes used for rating the amendments -->	
	<div id="form"></div>

	<!-- Button opening the output box (appears after processing) -->	
	<div id="wikify" onclick="convert()">Convert</div>

	<!-- The output box -->	
	<div id="output">
		<textarea></textarea>
		<div id="refresh" onclick="convert();">Refresh</div>
		<div id="close" onclick="this.parentNode.style.display='none';">Close</div>
	</div>
	
	<!-- Hidden buffer used for DOM manipulation (deleted after processing) -->
	<div id="buffer"></div>
	
</body>


<!----- Javascript 	----->

<script>

	// Array of the processed amendments
	var amendments 	= [];
		
	// Open and read the file selected by the user
	function read(file)
	{
		// Change the info displayed on the page
    	document.getElementById('input').style.display 	= "none";
    	document.getElementById('intro').innerHTML 		= "Loading...";
    	
    	// Open and read the file
		var reader = new FileReader();
    	reader.onload = function(){process(reader.result);};
    	reader.readAsText(file);
	}
	
	// Process the file content
	function process(file)
	{
		
		// Put the content of the file into a <div> buffer (for DOM manipulation)
		var buffer = document.getElementById('buffer');
		buffer.innerHTML = file;
		
		// Simplify the tables
		var elements = "";
		elements = buffer.getElementsByTagName("table");
		for (var i = elements.length; i > 0; i--)
		{
			// Isolate the third line
			var trl = elements[0].getElementsByTagName("tr");
			var tr = trl[trl.length-1];
			if (trl.length > 3)
				tr = trl[2];
			
			// Replace HTML by wiki
			tr.innerHTML = tr.innerHTML.replace(/(<i>\n?<b>)|(<\/b>\n?<\/i>)/gm, "'''''");
		
			// Create a simplified table
			var newTable = document.createElement("p");
				newTable.innerHTML = 
					"[[" 
					+ tr.getElementsByTagName("td")[0].innerText 
					+ "||" 
					+ tr.getElementsByTagName("td")[1].innerText
					+ "]]";
			
			// Replace the old tables by the simplified one
			elements[0].parentNode.replaceChild(newTable, elements[0]);
		}
	
		// Remove HTML from the file
		var cleanFile = "";
		elements = buffer.getElementsByTagName("p");
		for (var i = 0; i < elements.length; i++)
		{
			cleanFile += elements[i].innerText;;
		}
		
		// Delete the buffer
		buffer.parentNode.removeChild(buffer);

		// Remove tabs and new-lines
		cleanFile = cleanFile.replace(/\t/g, " ");
		cleanFile = cleanFile.replace(/\n/g, " ");
		while (cleanFile.search(/  /g) > -1)
			cleanFile = cleanFile.replace(/  /g, " ");
			
		// Remove the begining of the file
		cleanFile = cleanFile.replace(/^.*?(<NumAm>)/, "$1");

Oncela's avatar
Oncela committed
184
185
186
187
188
		// Use the internal tags to isolate each amendment in an array
		var parser = cleanFile.match(/Am>.*?<\/NumAm>.*?<Members>.*?\[\[.*?\]\].*?((<Num)|$)/g);
	
		// Use the internal tags to retrive each info and put them into the amendments array
		for (var i = 0; i < parser.length ; i++)
piks3l's avatar
piks3l committed
189
190
		{
			var am = {};
Oncela's avatar
Oncela committed
191
192
193
194
195
196
197
				am.number 	= parser[i].replace(/Am> *(.*?) *<\/NumAm>.*/, "Amendment $1");
				am.member	= parser[i].replace(/.*<RepeatBlock-By> *<Members> *(.*?) *<\/Members>.*/, "$1");
				am.article 	= parser[i].replace(/.*<Article> *(.*?) *<\/Article>.*/, "$1");
				am.left 	= parser[i].replace(/.*\[\[ *(.*?) *\|\|.*/, "$1");
				am.right 	= parser[i].replace(/.*\|\| *(.*?) *\]\].*/, "$1");
				am.justif 	= (parser[i].search(/TitreJust/)>0) ? parser[i].replace(/.*<\/TitreJust> *(.*?) *<.*/, "$1") : "";
				am.group	= 0;
piks3l's avatar
piks3l committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
				am.comment  = "";
				am.value    = 0;
				
			amendments.push(am);
		}
		
		// Create the form
		var form = document.getElementById('form');
		
		// For each amendment
		for (var i = 0; i < amendments.length; i++)
		{			
			// Create a line
			var line = document.createElement("div");
				line.className = "form_line";
			
			//	Attribute the amendment to the line
				line.am = amendments[i];
				
			// Display the amendmend number
			var span = document.createElement('span');
				span.className = "form_am";
				span.innerHTML = amendments[i].number;
Oncela's avatar
Oncela committed
221
222
223
224
225
226
227
228
229
230
				
				// Display or hide the content on click
				span.onclick = function()
				{
					var cnt = this.parentNode.nextSibling;
					if (cnt.style.display == "none")
						cnt.style.display = "block";
					else
						cnt.style.display = "none";
				}
piks3l's avatar
piks3l committed
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
				line.appendChild(span);
			
			// Create the checkboxes
			var checkboxes = [];
			
			// For each color
			for (var j = 0; j < colors.length; j++)
			{
				// Create a background
				var span = document.createElement('span');
					span.className = "form_radio";
					span.style.background = colors[j];
			
				// Create a checkbox
				var cb = document.createElement('input');
					cb.type = "radio";
					cb.name = "radio"+i;
					cb.value = j;
					
				// On click, change the displayed color and the value of the amendment
				cb.onclick = function()
				{
					this.parentNode.parentNode.am.value=this.value; 
					this.parentNode.parentNode.firstChild.style.background=colors[this.value];
				};
				
				// By default, the first radio is checked
				if (j==0) cb.checked=true;
				
				span.appendChild(cb);
				line.appendChild(span);
			}
			
Oncela's avatar
Oncela committed
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
			// Create the input for selecting the Group
			var group = document.createElement('select');
			
			var opt = document.createElement('option');
				opt.value = 0;
				opt.innerText = "Group";
				opt.className = "firstOpt";
			group.appendChild(opt);
		
			for (var j = 1; j < groups.length; j++)
			{
				var opt = document.createElement('option');
					opt.value = j;
					opt.innerText = groups[j];
				group.appendChild(opt);
			}

			group.onchange = function()
			{
				this.parentNode.am.group=this.value;
			}
			group.onclick = function()
			{
			this.style.color="black"; 
			}
			line.appendChild(group);
			
piks3l's avatar
piks3l committed
291
292
293
294
295
296
297
298
299
300
301
			// Create the input for adding a comment
			var comment = document.createElement('input');
				comment.type = "text";
				comment.placeholder="Comment"
				comment.onchange = function()
				{
					this.parentNode.am.comment=this.value; 
				}
				line.appendChild(comment);
				
			form.appendChild(line);	
Oncela's avatar
Oncela committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
			
			// Add the content of the amendment bellow
			var content = document.createElement("div");
				content.className = "form_content";
				content.style.display = "none";
				content.innerHTML = 
					  amendments[i].member  + "<br/><br/><b>" 
					+ amendments[i].article + "</b><br/><br/>" 
					+ "<table><tr><td>" 
					+ amendments[i].left 
					+ "</td><td>" 
					+ amendments[i].right 
					+ "</td></tr></table>";
					
				if (amendments[i].justif != "")
					content.innerHTML += "<br/><i>Justification: " + amendments[i].justif + "</i>";
					
				content.innerHTML = content.innerHTML.replace(/'''''(.*?)'''''/g, "<b>$1</b>");
			
			form.appendChild(content);
			
				
			
piks3l's avatar
piks3l committed
325
326
327
328
		}
		
		// Change the display
		document.getElementById('wikify').style.display = "block";
Oncela's avatar
Oncela committed
329
330
		document.getElementById('intro').innerHTML = "Rate each amendment (non-rated amendments won't be converted).<br/>"
		+ "Cick on the number of an amendment to show/hide its content.<br/><br/>";
piks3l's avatar
piks3l committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	}
	
	// Convert and display the rated amendments
	function convert()
	{
		var result = "";
		
		// For each amendment
		for (var i = 0; i < amendments.length; i++)
		{
			// Convert the amendment if rated
			var am = amendments[i];
			if (am.value > 0)
			{
Oncela's avatar
Oncela committed
345
346
347
348
349
350
351
352
353
354
				var amResult = template.replace(/\%number/g, 	am.number);
					amResult = amResult.replace(/\%rate/g, 		rates[am.value]);
					amResult = amResult.replace(/\%member/g, 	am.member);
					amResult = amResult.replace(/\%group/g, 	am.group!=0  ? templateGroup.replace(/\%group/g, groups[am.group]) : "");
					amResult = amResult.replace(/\%article/g, 	am.article);
					amResult = amResult.replace(/\%color/g, 	colors[am.value]);
					amResult = amResult.replace(/\%left/g, 		am.left);
					amResult = amResult.replace(/\%right/g, 	am.right);
					amResult = amResult.replace(/\%justif/g, 	am.justif!=""  ? templateJustif.replace(/\%justif/g, am.justif) : "");
					amResult = amResult.replace(/\%comment/g, 	am.comment!="" ? templateComment.replace(/\%comment/g, am.comment) : "");
piks3l's avatar
piks3l committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
					
					result += amResult;
			}		
		}
		
		// Display the converted amendmends
		var output 	= document.getElementById('output');
			output.childNodes[1].value = result;
			output.style.display = "block";
	}

</script>


<!-----     CSS	 	----->

<style>

	body
	{
		padding: 2em;
	}
	
	#title
	{
		font-size: 2em;
		font-weight: bold;
		margin: 1em;
		margin-top: 0;
		color: #486A8C;
	
	}
	.form_line
	{
		height: 2.5em;
	}
	
Oncela's avatar
Oncela committed
392
393
394
395
396
397
	.form_line span:hover
	{
		cursor: pointer;
		text-decoration: underline;
	}
	
piks3l's avatar
piks3l committed
398
399
400
401
402
403
404
405
406
407
408
	.form_am, .form_radio
	{
		padding: .5em;
	}
	
	.form_line input[type=text]
	{
		margin-left: 1em;
		width: 40em;
	}
	
Oncela's avatar
Oncela committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
	select
	{
		margin-left: 1em;
		color: gray;
	}
	
	.firstOpt
	{
		color: gray;
	}
	
	.form_content
	{
		margin: 1em 4em 4em;
	}
	
	.form_content td
	{
		width: 50%;
		padding-right: 1em;
		vertical-align: top;
	}
	
piks3l's avatar
piks3l committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
	#wikify
	{
		display: none;
		margin: 2em;
		padding: 1em 2em;
		position: fixed;
		top:2px;
		right: 2px;
		border: 1px black solid;
		cursor: pointer;
		background: white;
		font-weight: bold;		
	}
	
	#output
	{
		display: none;
		position: fixed;
		margin: 2em;
		top: 2px;
		right: 2px;
		background:white;
		border: 1px solid black;
	}
	
	#output textarea
	{
		width: 30em;
		height: 40em;
		margin-left: auto;
		margin-top: auto;
		border: none;
	}
	
	#close, #refresh
	{
		padding:1em 2em;
		cursor: pointer;
		font-weight: bold;
	}
	
	#refresh
	{
		border-bottom: 1px solid black;
		border-top: 1px solid black;
	}
	
	#wikify:hover, #close:hover, #refresh:hover
	{
		background: #E8E8E8;
	}
	
	#buffer
	{
		display: none;
	}
	
</style>
</html>