6 function createXSLT(xsl) {
7 var p = new XSLTProcessor();
8 p.importStylesheet(xsl);
12 var xml2htmlStylesheet = null;
14 // Wykonuje block z załadowanymi arkuszami stylów
15 function withStylesheets(code_block, onError)
17 if (!xml2htmlStylesheet) {
18 $.blockUI({message: 'Ładowanie arkuszy stylów...'});
20 url: STATIC_URL + 'xsl/wl2html_client.xsl',
22 success: function(data) {
23 xml2htmlStylesheet = createXSLT(data);
36 var canonThemes = null;
38 // Wykonuje block z załadowanymi kanonicznymi motywami
39 function withThemes(code_block, onError)
42 $.blockUI({message: 'Ładowanie motywów...'});
46 success: function(data) {
48 themes = data.split('\n');
50 canonThemes[themes[i]] = 1;
65 function xml2html(options) {
66 withStylesheets(function() {
67 withThemes(function() {
68 var xml = options.xml.replace(/\/\s+/g, '<br />');
69 var parser = new DOMParser();
70 var serializer = new XMLSerializer();
71 var doc = parser.parseFromString(xml, 'text/xml');
72 var error = $('parsererror', doc);
74 if (error.length == 0) {
75 doc = xml2htmlStylesheet.transformToFragment(doc, document);
76 console.log(doc.firstChild);
78 if(doc.firstChild === null) {
79 options.error("Błąd w przetwarzaniu XML.");
83 error = $('parsererror', doc);
86 if (error.length > 0 && options.error) {
87 options.error(error.text());
89 $('.theme-text-list', doc.firstChild).each(function(){
90 var themes = $(this).html().split(',');
92 themes[i] = $.trim(themes[i]);
93 if (!(themes[i] in canonThemes))
94 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
96 $(this).html(themes.join(', '));
98 options.success(doc.firstChild);
100 }, function() { options.error && options.error('Nie udało się załadować motywów'); });
101 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
104 /* USEFULL CONSTANTS */
105 const ELEMENT_NODE = 1;
106 const ATTRIBUTE_NODE = 2;
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-(.*)$/;
119 const ELEM_START = 1;
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"
131 * PADDING for pretty-printing
134 dramat_wierszowany_l: 4,
135 dramat_wierszowany_lp: 4,
136 dramat_wspolczesny: 4,
144 naglowek_rozdzial: 4,
172 "rdf:Description": 1,
175 function getPadding(name) {
177 if(name.match(/^dc:.*$/))
181 return PADDING[name];
186 function HTMLSerializer() {
192 HTMLSerializer.prototype._prepare = function() {
195 // XML namespace is implicit
196 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
202 HTMLSerializer.prototype._pushElement = function(element) {
209 HTMLSerializer.prototype._pushChildren = function(element) {
210 for(var i = element.childNodes.length-1; i >= 0; i--)
211 this._pushElement(element.childNodes.item(i));
214 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
221 HTMLSerializer.prototype._verseBefore = function(node) {
222 var prev = node.previousSibling;
224 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
225 prev = prev.previousSibling;
228 return (prev !== null) && prev.hasAttribute('x-verse');
231 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
237 self._pushElement(rootElement);
239 self._pushChildren(rootElement);
241 while(self.stack.length > 0) {
242 var token = self.stack.pop();
244 if(token.type === ELEM_END) {
245 self.result += "</" + token.tagName + ">";
246 for(var padding = getPadding(token.tagName); padding > 0; padding--) {
252 if(token.type === NS_END) {
253 self._unassignNamespace(token.namespace);
258 switch(token.node.nodeType) {
260 if(token.node.hasAttribute('x-pass-thru')
261 || token.node.hasAttribute('data-pass-thru')) {
262 self._pushChildren(token.node);
266 if(!token.node.hasAttribute('x-node'))
269 var xnode = token.node.getAttribute('x-node');
271 if(xnode === 'wers') {
273 if(self._verseBefore(token.node))
274 self.result += '/\n';
275 self._pushChildren(token.node);
279 if(xnode === 'out-of-flow-text') {
280 self._pushChildren(token.node);
284 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
285 self.result += '/\n';
288 self._serializeElement(token.node);
291 self.result += token.node.nodeValue;
300 * TODO: this doesn't support prefix redefinitions
302 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
303 this.nsMap[nsData.uri] = undefined;
306 HTMLSerializer.prototype._assignNamespace = function(uri) {
309 return ({"prefix": "", "uri": "", "fresh": false});
312 if(this.nsMap[uri] === undefined) {
313 // this prefix hasn't been defined yet in current context
314 var prefix = NAMESPACES[uri];
316 if (prefix === undefined) { // not predefined
317 prefix = "ns" + this.nsCounter;
321 this.nsMap[uri] = prefix;
329 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
332 HTMLSerializer.prototype._join = function(prefix, name) {
334 return prefix + ":" + name;
338 HTMLSerializer.prototype._rjoin = function(prefix, name) {
340 return prefix + ":" + name;
344 HTMLSerializer.prototype._serializeElement = function(node) {
347 var ns = node.getAttribute('x-ns');
349 var newNamespaces = [];
351 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
354 newNamespaces.push(nsData);
361 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
363 /* retrieve attributes */
364 var attributeIDs = [];
365 for (var i = 0; i < node.attributes.length; i++) {
366 var attr = node.attributes.item(i);
368 // check if name starts with "x-attr-name"
369 var m = attr.name.match(XATTR_RE);
371 attributeIDs.push(m[1]);
375 if (getPadding(tagName))
378 self.result += '<' + tagName;
380 $.each(attributeIDs, function() {
381 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
384 newNamespaces.push(nsData);
391 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
392 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
395 /* print new namespace declarations */
396 $.each(newNamespaces, function() {
397 self.result += " " + self._rjoin("xmlns", this.prefix);
398 self.result += '="' + this.uri + '"';
401 if (node.childNodes.length > 0) {
403 self._pushTagEnd(tagName);
404 self._pushChildren(node);
411 function html2text(params) {
413 var s = new HTMLSerializer();
414 params.success( s.serialize(params.element, params.stripOuter) );
416 params.error("Nie udało się zserializować tekstu:" + e)