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 // collapse previous element's padding
293 while (token.node.nodeValue[i] == '\n' && self.result[self.result.length - 1] == '\n')
295 self.result += token.node.nodeValue.substr(i);
304 * TODO: this doesn't support prefix redefinitions
306 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
307 this.nsMap[nsData.uri] = undefined;
310 HTMLSerializer.prototype._assignNamespace = function(uri) {
313 return ({"prefix": "", "uri": "", "fresh": false});
316 if(this.nsMap[uri] === undefined) {
317 // this prefix hasn't been defined yet in current context
318 var prefix = NAMESPACES[uri];
320 if (prefix === undefined) { // not predefined
321 prefix = "ns" + this.nsCounter;
325 this.nsMap[uri] = prefix;
333 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
336 HTMLSerializer.prototype._join = function(prefix, name) {
338 return prefix + ":" + name;
342 HTMLSerializer.prototype._rjoin = function(prefix, name) {
344 return prefix + ":" + name;
348 HTMLSerializer.prototype._serializeElement = function(node) {
351 var ns = node.getAttribute('x-ns');
353 var newNamespaces = [];
355 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
358 newNamespaces.push(nsData);
365 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
367 /* retrieve attributes */
368 var attributeIDs = [];
369 for (var i = 0; i < node.attributes.length; i++) {
370 var attr = node.attributes.item(i);
372 // check if name starts with "x-attr-name"
373 var m = attr.name.match(XATTR_RE);
375 attributeIDs.push(m[1]);
380 // at least one newline before padded elements
381 if (getPadding(tagName) && self.result[self.result.length - 1] != '\n')
384 self.result += '<' + tagName;
386 $.each(attributeIDs, function() {
387 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
390 newNamespaces.push(nsData);
397 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
398 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
401 /* print new namespace declarations */
402 $.each(newNamespaces, function() {
403 self.result += " " + self._rjoin("xmlns", this.prefix);
404 self.result += '="' + this.uri + '"';
407 if (node.childNodes.length > 0) {
409 self._pushTagEnd(tagName);
410 self._pushChildren(node);
417 function html2text(params) {
419 var s = new HTMLSerializer();
420 params.success( s.serialize(params.element, params.stripOuter) );
422 params.error("Nie udało się zserializować tekstu:" + e)