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 class HTMLSerializer {
134 // XML namespace is implicit
135 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
141 _pushElement(element) {
148 _pushChildren(element) {
149 for(var i = element.childNodes.length-1; i >= 0; i--)
150 this._pushElement(element.childNodes.item(i));
153 _pushTagEnd(tagName) {
161 /* true if previous element is a previous verse of a stanza */
162 var parent = node.parentNode;
163 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
166 var prev = node.previousSibling;
168 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
169 prev = prev.previousSibling;
172 return (prev !== null) && prev.hasAttribute('x-verse');
176 return node.getAttribute('x-auto-node') == 'true';
179 _ignoredWithWhitespace(node) {
180 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
181 node = node.childNodes[0];
182 if (node.nodeType == TEXT_NODE)
183 return node.nodeValue.match(/^\s/)
188 serialize(rootElement, stripOuter)
194 self._pushElement(rootElement);
196 self._pushChildren(rootElement);
198 var text_buffer = '';
200 while(self.stack.length > 0) {
201 var token = self.stack.pop();
203 if(token.type === ELEM_END) {
204 self.result += text_buffer;
206 if (token.tagName != '')
207 self.result += "</" + token.tagName + ">";
211 if(token.type === NS_END) {
212 self._unassignNamespace(token.namespace);
216 switch(token.node.nodeType) {
218 if(token.node.hasAttribute('x-pass-thru')
219 || token.node.hasAttribute('data-pass-thru')) {
220 self._pushChildren(token.node);
224 if(!token.node.hasAttribute('x-node'))
227 var xnode = token.node.getAttribute('x-node');
229 if(xnode === 'out-of-flow-text') {
230 self._pushChildren(token.node);
234 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
236 // add whitespace if there's none
237 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
241 self.result += text_buffer;
243 self._serializeElement(token.node);
246 self.result += text_buffer;
247 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
250 self.result += text_buffer;
252 self.result += '<!--' + token.node.nodeValue + '-->';
256 self.result += text_buffer;
262 * TODO: this doesn't support prefix redefinitions
264 _unassignNamespace(nsData) {
265 this.nsMap[nsData.uri] = undefined;
268 _assignNamespace(uri) {
271 return ({"prefix": "", "uri": "", "fresh": false});
274 if(this.nsMap[uri] === undefined) {
275 // this prefix hasn't been defined yet in current context
276 var prefix = NAMESPACES[uri];
278 if (prefix === undefined) { // not predefined
279 prefix = "ns" + this.nsCounter;
283 this.nsMap[uri] = prefix;
291 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
294 _join(prefix, name) {
296 return prefix + ":" + name;
300 _rjoin(prefix, name) {
302 return prefix + ":" + name;
306 _serializeElement(node) {
309 if (self._nodeIgnored(node)) {
310 self._pushTagEnd('');
311 self._pushChildren(node);
314 var ns = node.getAttribute('x-ns');
316 var newNamespaces = [];
318 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
321 newNamespaces.push(nsData);
328 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
330 /* retrieve attributes */
331 var attributeIDs = [];
333 for (var i = 0; i < node.attributes.length; i++) {
334 var attr = node.attributes.item(i);
336 m = attr.name.match(XATTR_KNOWN_RE);
346 // check if name starts with "x-attr-name"
347 var m = attr.name.match(XATTR_RE);
349 attributeIDs.push(m[1]);
356 self.result += '<' + tagName;
358 function writeAttr(ns, tag, value) {
360 var nsData = self._assignNamespace(ns);
362 newNamespaces.push(nsData);
368 tag = self._join(nsData.prefix, tag);
371 self.result += ' ' + tag;
372 self.result += '="' + value.replace(/&/g, '&').replace(/"/g, '"') + '"';
375 $.each(attributes, function() {
377 this[0], this[1], this[2]
381 $.each(attributeIDs, function() {
383 node.getAttribute('x-attr-ns-'+this),
384 node.getAttribute('x-attr-name-'+this),
385 node.getAttribute('x-attr-value-'+this)
389 /* print new namespace declarations */
390 $.each(newNamespaces, function() {
391 self.result += " " + self._rjoin("xmlns", this.prefix);
392 self.result += '="' + this.uri + '"';
395 if (node.childNodes.length > 0) {
397 self._pushTagEnd(tagName);
398 self._pushChildren(node);
407 function html2text(params) {
409 var s = new HTMLSerializer();
410 params.success( s.serialize(params.element, params.stripOuter) );
412 params.error("Nie udało się zserializować tekstu:" + e)