Support for <wers>.
[redakcja.git] / src / 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?2019121801',
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 function xml2html(options) {
39     withStylesheets(function() {
40         var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
41         xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
42         var parser = new DOMParser();
43         var serializer = new XMLSerializer();
44         var doc = parser.parseFromString(xml, 'text/xml');
45         var error = $('parsererror', doc);
46
47         if (error.length == 0) {
48             doc = xml2htmlStylesheet.transformToFragment(doc, document);
49
50         if(doc.firstChild === null) {
51             options.error("Błąd w przetwarzaniu XML.");
52                 return;
53             }
54
55             error = $('parsererror', doc);
56         }
57
58         if (error.length > 0 && options.error) {
59             source = $('sourcetext', doc);
60             source_text = source.text();
61             source.text('');
62             options.error(error.text(), source_text);
63         } else {
64             options.success(doc.childNodes);
65
66             $.themes.withCanon(function(canonThemes) {
67                 if (canonThemes != null) {
68                     $('.theme-text-list').addClass('canon').each(function(){
69                         var themes = $(this).html().split(',');
70                         for (i in themes) {
71                             themes[i] = $.trim(themes[i]);
72                             if (canonThemes.indexOf(themes[i]) == -1)
73                                 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
74                         }
75                         $(this).html(themes.join(', '));
76                     });
77                 }
78             });
79         }
80     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
81 }
82
83 /* USEFULL CONSTANTS */
84 const ELEMENT_NODE                                       = 1;
85 const ATTRIBUTE_NODE                 = 2;
86 const TEXT_NODE                      = 3;
87 const CDATA_SECTION_NODE             = 4;
88 const ENTITY_REFERENCE_NODE          = 5;
89 const ENTITY_NODE                    = 6;
90 const PROCESSING_INSTRUCTION_NODE    = 7;
91 const COMMENT_NODE                   = 8;
92 const DOCUMENT_NODE                  = 9;
93 const DOCUMENT_TYPE_NODE             = 10;
94 const DOCUMENT_FRAGMENT_NODE         = 11;
95 const NOTATION_NODE                  = 12;
96 const XATTR_RE = /^x-attr-name-(.*)$/;
97
98 const ELEM_START = 1;
99 const ELEM_END = 2;
100 const NS_END = 3;
101
102 const NAMESPACES = {
103         // namespaces not listed here will be assigned random names
104         "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
105         "http://purl.org/dc/elements/1.1/": "dc",
106         "http://www.w3.org/XML/1998/namespace": "xml"
107 };
108
109 function HTMLSerializer() {
110         // empty constructor
111 }
112
113
114
115 HTMLSerializer.prototype._prepare = function() {
116         this.stack = [];
117
118         // XML namespace is implicit
119         this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
120
121         this.result = "";
122         this.nsCounter = 1;
123 }
124
125 HTMLSerializer.prototype._pushElement = function(element) {
126         this.stack.push({
127                 "type": ELEM_START,
128                 "node": element
129         });
130 }
131
132 HTMLSerializer.prototype._pushChildren = function(element) {
133         for(var i = element.childNodes.length-1; i >= 0; i--)
134                 this._pushElement(element.childNodes.item(i));
135 }
136
137 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
138         this.stack.push({
139                 "type": ELEM_END,
140                 "tagName": tagName
141         });
142 }
143
144 HTMLSerializer.prototype._verseBefore = function(node) {
145     /* true if previous element is a previous verse of a stanza */
146     var parent = node.parentNode;
147     if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
148         return false;
149
150         var prev = node.previousSibling;
151
152         while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
153                 prev = prev.previousSibling;
154         }
155
156         return (prev !== null) && prev.hasAttribute('x-verse');
157 }
158
159 HTMLSerializer.prototype._nodeIgnored = function(node) {
160     return node.getAttribute('x-auto-node') == 'true';
161 }
162
163 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
164     while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
165         node = node.childNodes[0];
166     if (node.nodeType == TEXT_NODE)
167         return node.nodeValue.match(/^\s/)
168     else return false;
169 }
170
171
172 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
173 {
174         var self = this;
175         self._prepare();
176
177         if(!stripOuter)
178                 self._pushElement(rootElement);
179         else
180                 self._pushChildren(rootElement);
181
182     var text_buffer = '';
183
184         while(self.stack.length > 0) {
185                 var token = self.stack.pop();
186
187         if(token.type === ELEM_END) {
188             self.result += text_buffer;
189             text_buffer = '';
190             if (token.tagName != '')
191                 self.result += "</" + token.tagName + ">";
192             continue;
193         };
194
195                 if(token.type === NS_END) {
196                         self._unassignNamespace(token.namespace);
197                         continue;
198                 }
199
200
201                 switch(token.node.nodeType) {
202                         case ELEMENT_NODE:
203                                 if(token.node.hasAttribute('x-pass-thru')
204                                  || token.node.hasAttribute('data-pass-thru')) {
205                                         self._pushChildren(token.node);
206                                         break;
207                                 }
208
209                                 if(!token.node.hasAttribute('x-node'))
210                                         break;
211
212                                 var xnode = token.node.getAttribute('x-node');
213
214                                 if(xnode === 'out-of-flow-text') {
215                                         self._pushChildren(token.node);
216                                         break;
217                                 }
218
219                 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
220                     self.result += '/';
221                     // add whitespace if there's none
222                     if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
223                         self.result += ' ';
224                 }
225
226                 self.result += text_buffer;
227                 text_buffer = '';
228                                 self._serializeElement(token.node);
229                                 break;
230                         case TEXT_NODE:
231                                 self.result += text_buffer;
232                                 text_buffer = token.node.nodeValue.replace(/&/g, '&amp;').replace(/</g, '&lt;');
233                                 break;
234             case COMMENT_NODE:
235                 self.result += text_buffer;
236                 text_buffer = '';
237                 self.result += '<!--' + token.node.nodeValue + '-->';
238                 break;
239                 };
240         };
241     self.result += text_buffer;
242
243         return this.result;
244 }
245
246 /*
247  * TODO: this doesn't support prefix redefinitions
248  */
249 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
250         this.nsMap[nsData.uri] = undefined;
251 };
252
253 HTMLSerializer.prototype._assignNamespace = function(uri) {
254         if(uri === null) {
255                 // default namespace
256                 return ({"prefix": "", "uri": "", "fresh": false});
257         }
258
259         if(this.nsMap[uri] === undefined) {
260                 // this prefix hasn't been defined yet in current context
261                 var prefix = NAMESPACES[uri];
262
263                 if (prefix === undefined) { // not predefined
264                         prefix = "ns" + this.nsCounter;
265                         this.nsCounter += 1;
266                 }
267
268                 this.nsMap[uri] = prefix;
269                 return ({
270                         "prefix": prefix,
271                         "uri": uri,
272                         "fresh": true
273                 });
274         }
275
276         return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
277 };
278
279 HTMLSerializer.prototype._join = function(prefix, name) {
280         if(!!prefix)
281                 return prefix + ":" + name;
282         return name;
283 };
284
285 HTMLSerializer.prototype._rjoin = function(prefix, name) {
286         if(!!name)
287                 return prefix + ":" + name;
288         return prefix;
289 };
290
291 HTMLSerializer.prototype._serializeElement = function(node) {
292     var self = this;
293
294     if (self._nodeIgnored(node)) {
295         self._pushTagEnd('');
296         self._pushChildren(node);
297     }
298     else {
299         var ns = node.getAttribute('x-ns');
300         var nsPrefix = null;
301         var newNamespaces = [];
302
303         var nsData = self._assignNamespace(node.getAttribute('x-ns'));
304
305         if(nsData.fresh) {
306                 newNamespaces.push(nsData);
307                 self.stack.push({
308                         "type": NS_END,
309                         "namespace": nsData
310                 });
311         }
312
313         var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
314
315         /* retrieve attributes */
316         var attributeIDs = [];
317         for (var i = 0; i < node.attributes.length; i++) {
318                 var attr = node.attributes.item(i);
319
320                 // check if name starts with "x-attr-name"
321                 var m = attr.name.match(XATTR_RE);
322                 if (m !== null)
323                         attributeIDs.push(m[1]);
324         };
325
326         /* print out */
327
328         self.result += '<' + tagName;
329
330         $.each(attributeIDs, function() {
331                 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
332
333                 if(nsData.fresh) {
334                         newNamespaces.push(nsData);
335                         self.stack.push({
336                                 "type": NS_END,
337                                 "namespace": nsData
338                         });
339                 };
340
341                 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
342                 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
343         });
344
345         /* print new namespace declarations */
346         $.each(newNamespaces, function() {
347                 self.result += " " + self._rjoin("xmlns", this.prefix);
348                 self.result += '="' + this.uri + '"';
349         });
350
351         if (node.childNodes.length > 0) {
352                 self.result += ">";
353                 self._pushTagEnd(tagName);
354                 self._pushChildren(node);
355         }
356         else {
357                 self.result += "/>";
358         };
359     }
360 };
361
362 function html2text(params) {
363         try {
364                 var s = new HTMLSerializer();
365                 params.success( s.serialize(params.element, params.stripOuter) );
366         } catch(e) {
367                 params.error("Nie udało się zserializować tekstu:" + e)
368         }
369 }