86c6d362d0d3ed0dbae1c42ab5e071217eef24ee
[redakcja.git] / redakcja / static / js / wiki / xslt.js
1 /*
2  *
3  * XSLT STUFF
4  *
5  */
6 function createXSLT(xsl) {
7     var p = new XSLTProcessor();
8     p.importStylesheet(xsl);
9     return p;
10 }
11
12 var xml2htmlStylesheet = null;
13
14 // Wykonuje block z załadowanymi arkuszami stylów
15 function withStylesheets(code_block, onError)
16 {
17     if (!xml2htmlStylesheet) {
18         $.blockUI({message: 'Ładowanie arkuszy stylów...'});
19         $.ajax({
20                 url: STATIC_URL + 'xsl/wl2html_client.xsl',
21                 dataType: 'xml',
22                 timeout: 10000,
23                 success: function(data) {
24                 xml2htmlStylesheet = createXSLT(data);
25                 $.unblockUI();
26                                 code_block();
27
28             },
29                         error: onError
30         })
31     }
32         else {
33                 code_block();
34         }
35 }
36
37
38 // Wykonuje block z załadowanymi kanonicznymi motywami
39 function withThemes(code_block, onError)
40 {
41     if (typeof withThemes.canon == 'undefined') {
42         $.ajax({
43             url: '/themes',
44             dataType: 'text',
45             success: function(data) {
46                 withThemes.canon = data.split('\n');
47                 code_block(withThemes.canon);
48             },
49             error: function() {
50                 withThemes.canon = null;
51                 code_block(withThemes.canon);
52             }
53         })
54     }
55     else {
56         code_block(withThemes.canon);
57     }
58 }
59
60
61
62 function xml2html(options) {
63     withStylesheets(function() {
64         var xml = options.xml.replace(/\/\s+/g, '<br />');
65         var parser = new DOMParser();
66         var serializer = new XMLSerializer();
67         var doc = parser.parseFromString(xml, 'text/xml');
68         var error = $('parsererror', doc);
69
70         if (error.length == 0) {
71             doc = xml2htmlStylesheet.transformToFragment(doc, document);
72             console.log(doc.firstChild);
73
74         if(doc.firstChild === null) {
75             options.error("Błąd w przetwarzaniu XML.");
76                 return;
77             }
78
79             error = $('parsererror', doc);
80         }
81
82         if (error.length > 0 && options.error) {
83             options.error(error.text());
84         } else {
85             options.success(doc.firstChild);
86
87             withThemes(function(canonThemes) {
88                 if (canonThemes != null) {
89                     $('.theme-text-list').addClass('canon').each(function(){
90                         var themes = $(this).html().split(',');
91                         for (i in themes) {
92                             themes[i] = $.trim(themes[i]);
93                             if (canonThemes.indexOf(themes[i]) == -1)
94                                 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
95                         }
96                         $(this).html(themes.join(', '));
97                     });
98                 }
99             });
100         }
101     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
102 }
103
104 /* USEFULL CONSTANTS */
105 const ELEMENT_NODE                                       = 1;
106 const ATTRIBUTE_NODE                 = 2;
107 const TEXT_NODE                      = 3;
108 const CDATA_SECTION_NODE             = 4;
109 const ENTITY_REFERENCE_NODE          = 5;
110 const ENTITY_NODE                    = 6;
111 const PROCESSING_INSTRUCTION_NODE    = 7;
112 const COMMENT_NODE                   = 8;
113 const DOCUMENT_NODE                  = 9;
114 const DOCUMENT_TYPE_NODE             = 10;
115 const DOCUMENT_FRAGMENT_NODE         = 11;
116 const NOTATION_NODE                  = 12;
117 const XATTR_RE = /^x-attr-name-(.*)$/;
118
119 const ELEM_START = 1;
120 const ELEM_END = 2;
121 const NS_END = 3;
122
123 const NAMESPACES = {
124         // namespaces not listed here will be assigned random names
125         "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
126         "http://purl.org/dc/elements/1.1/": "dc",
127         "http://www.w3.org/XML/1998/namespace": "xml"
128 };
129
130 /*
131  * PADDING for pretty-printing
132  */
133 const PADDING = {
134     dramat_wierszowany_l: 4,
135     dramat_wierszowany_lp: 4,
136     dramat_wspolczesny: 4,
137     wywiad: 4,
138     opowiadanie: 4,
139     powiesc: 4,
140     liryka_l: 4,
141     liryka_lp: 4,
142     naglowek_czesc: 4,
143     naglowek_akt: 4,
144     naglowek_rozdzial: 4,
145     naglowek_osoba: 4,
146     lista_osob: 4,
147
148     akap: 3,
149     akap_cd: 3,
150     akap_dialog: 3,
151     strofa: 3,
152     motto: 3,
153     miejsce_czas: 3,
154
155     autor_utworu: 2,
156     nazwa_utworu: 2,
157     dzielo_nadrzedne: 2,
158
159     didaskalia: 2,
160     motto_podpis: 2,
161     naglowek_listy: 2,
162     kwestia: 1,
163     lista_osoba: 1,
164
165         "podpis": 1,
166         "wers": 0,
167         "wers_cd": 0,
168         "wers_akap": 0,
169         "wers_wciety": 0,
170
171         "rdf:RDF": 3,
172         "rdf:Description": 1,
173 };
174
175 function getPadding(name) {
176
177         if(name.match(/^dc:.*$/))
178                 return -1;
179
180         if(PADDING[name])
181                 return PADDING[name];
182
183         return 0;
184 }
185
186 function HTMLSerializer() {
187         // empty constructor
188 }
189
190
191
192 HTMLSerializer.prototype._prepare = function() {
193         this.stack = [];
194
195         // XML namespace is implicit
196         this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
197
198         this.result = "";
199         this.nsCounter = 1;
200 }
201
202 HTMLSerializer.prototype._pushElement = function(element) {
203         this.stack.push({
204                 "type": ELEM_START,
205                 "node": element
206         });
207 }
208
209 HTMLSerializer.prototype._pushChildren = function(element) {
210         for(var i = element.childNodes.length-1; i >= 0; i--)
211                 this._pushElement(element.childNodes.item(i));
212 }
213
214 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
215         this.stack.push({
216                 "type": ELEM_END,
217                 "tagName": tagName
218         });
219 }
220
221 HTMLSerializer.prototype._verseBefore = function(node) {
222         var prev = node.previousSibling;
223
224         while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
225                 prev = prev.previousSibling;
226         }
227
228         return (prev !== null) && prev.hasAttribute('x-verse');
229 }
230
231 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
232 {
233         var self = this;
234         self._prepare();
235
236         if(!stripOuter)
237                 self._pushElement(rootElement);
238         else
239                 self._pushChildren(rootElement);
240
241         while(self.stack.length > 0) {
242                 var token = self.stack.pop();
243
244                 if(token.type === ELEM_END) {
245                         self.result += "</" + token.tagName + ">";
246                         for(var padding = getPadding(token.tagName); padding > 0; padding--) {
247                                 self.result += "\n";
248                         }
249                         continue;
250                 };
251
252                 if(token.type === NS_END) {
253                         self._unassignNamespace(token.namespace);
254                         continue;
255                 }
256
257
258                 switch(token.node.nodeType) {
259                         case ELEMENT_NODE:
260                                 if(token.node.hasAttribute('x-pass-thru')
261                                  || token.node.hasAttribute('data-pass-thru')) {
262                                         self._pushChildren(token.node);
263                                         break;
264                                 }
265
266                                 if(!token.node.hasAttribute('x-node'))
267                                         break;
268
269                                 var xnode = token.node.getAttribute('x-node');
270
271                                 if(xnode === 'wers') {
272                                         /* push children */
273                                         if(self._verseBefore(token.node))
274                                                 self.result += '/\n';
275                                         self._pushChildren(token.node);
276                                         break;
277                                 };
278
279                                 if(xnode === 'out-of-flow-text') {
280                                         self._pushChildren(token.node);
281                                         break;
282                                 }
283
284                                 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
285                                         self.result += '/\n';
286                                 };
287
288                                 self._serializeElement(token.node);
289                                 break;
290                         case TEXT_NODE:
291                                 // collapse previous element's padding
292                                 var i = 0;
293                                 while (token.node.nodeValue[i] == '\n' && self.result[self.result.length - 1] == '\n')
294                                   i ++;
295                                 self.result += token.node.nodeValue.substr(i);
296                                 break;
297                 };
298         };
299
300         return this.result;
301 }
302
303 /*
304  * TODO: this doesn't support prefix redefinitions
305  */
306 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
307         this.nsMap[nsData.uri] = undefined;
308 };
309
310 HTMLSerializer.prototype._assignNamespace = function(uri) {
311         if(uri === null) {
312                 // default namespace
313                 return ({"prefix": "", "uri": "", "fresh": false});
314         }
315
316         if(this.nsMap[uri] === undefined) {
317                 // this prefix hasn't been defined yet in current context
318                 var prefix = NAMESPACES[uri];
319
320                 if (prefix === undefined) { // not predefined
321                         prefix = "ns" + this.nsCounter;
322                         this.nsCounter += 1;
323                 }
324
325                 this.nsMap[uri] = prefix;
326                 return ({
327                         "prefix": prefix,
328                         "uri": uri,
329                         "fresh": true
330                 });
331         }
332
333         return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
334 };
335
336 HTMLSerializer.prototype._join = function(prefix, name) {
337         if(!!prefix)
338                 return prefix + ":" + name;
339         return name;
340 };
341
342 HTMLSerializer.prototype._rjoin = function(prefix, name) {
343         if(!!name)
344                 return prefix + ":" + name;
345         return prefix;
346 };
347
348 HTMLSerializer.prototype._serializeElement = function(node) {
349         var self = this;
350
351         var ns = node.getAttribute('x-ns');
352         var nsPrefix = null;
353         var newNamespaces = [];
354
355         var nsData = self._assignNamespace(node.getAttribute('x-ns'));
356
357         if(nsData.fresh) {
358                 newNamespaces.push(nsData);
359                 self.stack.push({
360                         "type": NS_END,
361                         "namespace": nsData
362                 });
363         }
364
365         var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
366
367         /* retrieve attributes */
368         var attributeIDs = [];
369         for (var i = 0; i < node.attributes.length; i++) {
370                 var attr = node.attributes.item(i);
371
372                 // check if name starts with "x-attr-name"
373                 var m = attr.name.match(XATTR_RE);
374                 if (m !== null)
375                         attributeIDs.push(m[1]);
376         };
377
378         /* print out */
379
380         // at least one newline before padded elements
381         if (getPadding(tagName) && self.result[self.result.length - 1] != '\n')
382                 self.result += '\n';
383
384         self.result += '<' + tagName;
385
386         $.each(attributeIDs, function() {
387                 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
388
389                 if(nsData.fresh) {
390                         newNamespaces.push(nsData);
391                         self.stack.push({
392                                 "type": NS_END,
393                                 "namespace": nsData
394                         });
395                 };
396
397                 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
398                 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
399         });
400
401         /* print new namespace declarations */
402         $.each(newNamespaces, function() {
403                 self.result += " " + self._rjoin("xmlns", this.prefix);
404                 self.result += '="' + this.uri + '"';
405         });
406
407         if (node.childNodes.length > 0) {
408                 self.result += ">";
409                 self._pushTagEnd(tagName);
410                 self._pushChildren(node);
411         }
412         else {
413                 self.result += "/>";
414         };
415 };
416
417 function html2text(params) {
418         try {
419                 var s = new HTMLSerializer();
420                 params.success( s.serialize(params.element, params.stripOuter) );
421         } catch(e) {
422                 params.error("Nie udało się zserializować tekstu:" + e)
423         }
424 }