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-(.*)$/;
111 const ELEM_START = 1;
116 // namespaces not listed here will be assigned random names
117 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
118 "http://purl.org/dc/elements/1.1/": "dc",
119 "http://www.w3.org/XML/1998/namespace": "xml"
122 function HTMLSerializer() {
128 HTMLSerializer.prototype._prepare = function() {
131 // XML namespace is implicit
132 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
138 HTMLSerializer.prototype._pushElement = function(element) {
145 HTMLSerializer.prototype._pushChildren = function(element) {
146 for(var i = element.childNodes.length-1; i >= 0; i--)
147 this._pushElement(element.childNodes.item(i));
150 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
157 HTMLSerializer.prototype._verseBefore = function(node) {
158 /* true if previous element is a previous verse of a stanza */
159 var parent = node.parentNode;
160 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
163 var prev = node.previousSibling;
165 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
166 prev = prev.previousSibling;
169 return (prev !== null) && prev.hasAttribute('x-verse');
172 HTMLSerializer.prototype._nodeIgnored = function(node) {
173 return node.getAttribute('x-auto-node') == 'true';
176 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
177 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
178 node = node.childNodes[0];
179 if (node.nodeType == TEXT_NODE)
180 return node.nodeValue.match(/^\s/)
185 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
191 self._pushElement(rootElement);
193 self._pushChildren(rootElement);
195 var text_buffer = '';
197 while(self.stack.length > 0) {
198 var token = self.stack.pop();
200 if(token.type === ELEM_END) {
201 self.result += text_buffer;
203 if (token.tagName != '')
204 self.result += "</" + token.tagName + ">";
208 if(token.type === NS_END) {
209 self._unassignNamespace(token.namespace);
214 switch(token.node.nodeType) {
216 if(token.node.hasAttribute('x-pass-thru')
217 || token.node.hasAttribute('data-pass-thru')) {
218 self._pushChildren(token.node);
222 if(!token.node.hasAttribute('x-node'))
225 var xnode = token.node.getAttribute('x-node');
227 if(xnode === 'out-of-flow-text') {
228 self._pushChildren(token.node);
232 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
234 // add whitespace if there's none
235 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
239 self.result += text_buffer;
241 self._serializeElement(token.node);
244 self.result += text_buffer;
245 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
248 self.result += text_buffer;
250 self.result += '<!--' + token.node.nodeValue + '-->';
254 self.result += text_buffer;
260 * TODO: this doesn't support prefix redefinitions
262 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
263 this.nsMap[nsData.uri] = undefined;
266 HTMLSerializer.prototype._assignNamespace = function(uri) {
269 return ({"prefix": "", "uri": "", "fresh": false});
272 if(this.nsMap[uri] === undefined) {
273 // this prefix hasn't been defined yet in current context
274 var prefix = NAMESPACES[uri];
276 if (prefix === undefined) { // not predefined
277 prefix = "ns" + this.nsCounter;
281 this.nsMap[uri] = prefix;
289 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
292 HTMLSerializer.prototype._join = function(prefix, name) {
294 return prefix + ":" + name;
298 HTMLSerializer.prototype._rjoin = function(prefix, name) {
300 return prefix + ":" + name;
304 HTMLSerializer.prototype._serializeElement = function(node) {
307 if (self._nodeIgnored(node)) {
308 self._pushTagEnd('');
309 self._pushChildren(node);
312 var ns = node.getAttribute('x-ns');
314 var newNamespaces = [];
316 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
319 newNamespaces.push(nsData);
326 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
328 /* retrieve attributes */
329 var attributeIDs = [];
330 for (var i = 0; i < node.attributes.length; i++) {
331 var attr = node.attributes.item(i);
333 // check if name starts with "x-attr-name"
334 var m = attr.name.match(XATTR_RE);
336 attributeIDs.push(m[1]);
341 self.result += '<' + tagName;
343 $.each(attributeIDs, function() {
344 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
347 newNamespaces.push(nsData);
354 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
355 self.result += '="' + node.getAttribute('x-attr-value-'+this).replace(/&/g, '&').replace(/"/g, '"') + '"';
358 /* print new namespace declarations */
359 $.each(newNamespaces, function() {
360 self.result += " " + self._rjoin("xmlns", this.prefix);
361 self.result += '="' + this.uri + '"';
364 if (node.childNodes.length > 0) {
366 self._pushTagEnd(tagName);
367 self._pushChildren(node);
375 function html2text(params) {
377 var s = new HTMLSerializer();
378 params.success( s.serialize(params.element, params.stripOuter) );
380 params.error("Nie udało się zserializować tekstu:" + e)