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