am2wiki.html 9.39 KB
Newer Older
piks3l's avatar
piks3l committed
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
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html>
<head>
	<title>Am2Wiki</title>
	<meta charset="UTF-8">
</head>

<!--
	Am2Wiki by Oncela
	Last modification: 12-04-2017
	
	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"	];
	
	// 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"
		+ "{| border='1' style='border-spacing:0;' cellpadding='30'\n"
		+ "|-\n"
		+ "| colspan='2' style='background-color: %color; text-align:center;' |\n"
		+ "'''%number'''<br/>\n"
		+ "%article\n"
		+ "|-\n"
		+ "|width='50%' style='vertical-align:top;'|\n"
		+ "%left\n"
		+ "|width='50%' style='vertical-align:top;'|\n"
		+ "%right\n"
		+ "%comment" // If the amendment is commented, the templateComment (defined bellow) is inserted here
		+ "|}\n\n\n";
				
	var templateComment = 
		  "|-\n"
		+ "| colspan='2' |\n"
		+ "'''Comment:''' %comment\n";

</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");

		// Use the internal tags to retrieve the 4 relevant information for each amendment (number, article, left, right)
		var parser 		= [];		
			parser[0] 	= cleanFile.match(/<NumAm>.*?<\/NumAm> *<RepeatBlock-By>/g);
			parser[1]	= cleanFile.match(/<\/DocAmend> *<Article>.*?<\/Article> *\[\[/g);
			parser[2]	= cleanFile.match(/\[\[.*?\|\|/g);
			parser[3]	= cleanFile.match(/\|\|.*?\]\]/g);
	
		// Put the information into the amendments array
		for (var i = 0; i < parser[0].length ; i++)
		{
			var am = {};
				am.number 	= parser[0][i].replace(/<NumAm> *(.*?) *<\/NumAm> *<RepeatBlock-By>/, "Amendment $1");
				am.article 	= parser[1][i].replace(/<\/DocAmend> *<Article> *(.*?) *<\/Article> *\[\[/, "$1");
				am.left 	= parser[2][i].replace(/\[\[ *(.*?) *\|\|/, "$1");
				am.right 	= parser[3][i].replace(/\|\| *(.*?) *\]\]/, "$1");
				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;
				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);
			}
			
			// 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);	
		}
		
		// Change the display
		document.getElementById('wikify').style.display = "block";
		document.getElementById('intro').innerHTML = "Rate each amendment (non-rated amendments won't be converted):<br/><br/>";
	}
	
	// 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)
			{
				var amResult = template.replace(/\%number/g, am.number);
					amResult = amResult.replace(/\%rate/g, rates[am.value]);
					amResult = amResult.replace("%article", am.article);
					amResult = amResult.replace("%color", colors[am.value]);
					amResult = amResult.replace("%left", am.left);
					amResult = amResult.replace("%right", am.right);
					amResult = amResult.replace("%comment", am.comment!="" ? templateComment.replace("%comment", am.comment) : "");
					
					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;
	}
	
	.form_am, .form_radio
	{
		padding: .5em;
	}
	
	.form_line input[type=text]
	{
		margin-left: 1em;
		width: 40em;
	}
	
	#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>