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: '/wlxml/wl2html.xsl',
23 success: function(data) {
24 xml2htmlStylesheet = createXSLT(data);
37 function xml2html(options) {
38 withStylesheets(function() {
39 var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
40 xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
41 var parser = new DOMParser();
42 var serializer = new XMLSerializer();
43 var doc = parser.parseFromString(xml, 'text/xml');
44 var error = $('parsererror', doc);
46 if (error.length == 0) {
47 doc = xml2htmlStylesheet.transformToFragment(doc, document);
49 if(doc.firstChild === null) {
50 options.error("Błąd w przetwarzaniu XML.");
54 error = $('parsererror', doc);
57 if (error.length > 0 && options.error) {
58 source = $('sourcetext', doc);
59 source_text = source.text();
61 options.error(error.text(), source_text);
63 let galleryUrl = new URL(
67 $("img", $(doc.childNodes)).each(function() {
77 options.success(doc.childNodes);
79 $.themes.withCanon(function(canonThemes) {
80 if (canonThemes != null) {
81 $('.theme-text-list').addClass('canon').each(function(){
82 var themes = $(this).html().split(',');
84 themes[i] = $.trim(themes[i]);
85 if (canonThemes.indexOf(themes[i]) == -1)
86 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
88 $(this).html(themes.join(', '));
93 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
96 /* USEFULL CONSTANTS */
97 const ELEMENT_NODE = 1;
98 const ATTRIBUTE_NODE = 2;
100 const CDATA_SECTION_NODE = 4;
101 const ENTITY_REFERENCE_NODE = 5;
102 const ENTITY_NODE = 6;
103 const PROCESSING_INSTRUCTION_NODE = 7;
104 const COMMENT_NODE = 8;
105 const DOCUMENT_NODE = 9;
106 const DOCUMENT_TYPE_NODE = 10;
107 const DOCUMENT_FRAGMENT_NODE = 11;
108 const NOTATION_NODE = 12;
109 const XATTR_RE = /^x-attr-name-(.*)$/;
110 const XATTR_KNOWN_RE = /^x-a-([a-z]+)-(.*)$/;
112 const ELEM_START = 1;
117 // namespaces not listed here will be assigned random names
118 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
119 "http://purl.org/dc/elements/1.1/": "dc",
120 "http://www.w3.org/XML/1998/namespace": "xml"
126 for (prefix in NAMESPACES) {
127 NS_PREFIXES[NAMESPACES[prefix]] = prefix
130 function HTMLSerializer() {
136 HTMLSerializer.prototype._prepare = function() {
139 // XML namespace is implicit
140 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
146 HTMLSerializer.prototype._pushElement = function(element) {
153 HTMLSerializer.prototype._pushChildren = function(element) {
154 for(var i = element.childNodes.length-1; i >= 0; i--)
155 this._pushElement(element.childNodes.item(i));
158 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
165 HTMLSerializer.prototype._verseBefore = function(node) {
166 /* true if previous element is a previous verse of a stanza */
167 var parent = node.parentNode;
168 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
171 var prev = node.previousSibling;
173 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
174 prev = prev.previousSibling;
177 return (prev !== null) && prev.hasAttribute('x-verse');
180 HTMLSerializer.prototype._nodeIgnored = function(node) {
181 return node.getAttribute('x-auto-node') == 'true';
184 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
185 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
186 node = node.childNodes[0];
187 if (node.nodeType == TEXT_NODE)
188 return node.nodeValue.match(/^\s/)
193 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
199 self._pushElement(rootElement);
201 self._pushChildren(rootElement);
203 var text_buffer = '';
205 while(self.stack.length > 0) {
206 var token = self.stack.pop();
208 if(token.type === ELEM_END) {
209 self.result += text_buffer;
211 if (token.tagName != '')
212 self.result += "</" + token.tagName + ">";
216 if(token.type === NS_END) {
217 self._unassignNamespace(token.namespace);
222 switch(token.node.nodeType) {
224 if(token.node.hasAttribute('x-pass-thru')
225 || token.node.hasAttribute('data-pass-thru')) {
226 self._pushChildren(token.node);
230 if(!token.node.hasAttribute('x-node'))
233 var xnode = token.node.getAttribute('x-node');
235 if(xnode === 'out-of-flow-text') {
236 self._pushChildren(token.node);
240 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
242 // add whitespace if there's none
243 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
247 self.result += text_buffer;
249 self._serializeElement(token.node);
252 self.result += text_buffer;
253 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
256 self.result += text_buffer;
258 self.result += '<!--' + token.node.nodeValue + '-->';
262 self.result += text_buffer;
268 * TODO: this doesn't support prefix redefinitions
270 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
271 this.nsMap[nsData.uri] = undefined;
274 HTMLSerializer.prototype._assignNamespace = function(uri) {
277 return ({"prefix": "", "uri": "", "fresh": false});
280 if(this.nsMap[uri] === undefined) {
281 // this prefix hasn't been defined yet in current context
282 var prefix = NAMESPACES[uri];
284 if (prefix === undefined) { // not predefined
285 prefix = "ns" + this.nsCounter;
289 this.nsMap[uri] = prefix;
297 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
300 HTMLSerializer.prototype._join = function(prefix, name) {
302 return prefix + ":" + name;
306 HTMLSerializer.prototype._rjoin = function(prefix, name) {
308 return prefix + ":" + name;
312 HTMLSerializer.prototype._serializeElement = function(node) {
315 if (self._nodeIgnored(node)) {
316 self._pushTagEnd('');
317 self._pushChildren(node);
320 var ns = node.getAttribute('x-ns');
322 var newNamespaces = [];
324 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
327 newNamespaces.push(nsData);
334 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
336 /* retrieve attributes */
337 var attributeIDs = [];
339 for (var i = 0; i < node.attributes.length; i++) {
340 var attr = node.attributes.item(i);
342 m = attr.name.match(XATTR_KNOWN_RE);
352 // check if name starts with "x-attr-name"
353 var m = attr.name.match(XATTR_RE);
355 attributeIDs.push(m[1]);
362 self.result += '<' + tagName;
364 function writeAttr(ns, tag, value) {
366 var nsData = self._assignNamespace(ns);
368 newNamespaces.push(nsData);
374 tag = self._join(nsData.prefix, tag);
377 self.result += ' ' + tag;
378 self.result += '="' + value.replace(/&/g, '&').replace(/"/g, '"') + '"';
381 $.each(attributes, function() {
383 this[0], this[1], this[2]
387 $.each(attributeIDs, function() {
389 node.getAttribute('x-attr-ns-'+this),
390 node.getAttribute('x-attr-name-'+this),
391 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);
412 function html2text(params) {
414 var s = new HTMLSerializer();
415 params.success( s.serialize(params.element, params.stripOuter) );
417 params.error("Nie udało się zserializować tekstu:" + e)