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, '<_n />$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 $("img", $(doc.childNodes)).each(function() {
70 options.success(doc.childNodes);
72 $.themes.withCanon(function(canonThemes) {
73 if (canonThemes != null) {
74 $('.theme-text-list').addClass('canon').each(function(){
75 var themes = $(this).html().split(',');
77 themes[i] = $.trim(themes[i]);
78 if (canonThemes.indexOf(themes[i]) == -1)
79 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
81 $(this).html(themes.join(', '));
86 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
89 /* USEFULL CONSTANTS */
90 const ELEMENT_NODE = 1;
91 const ATTRIBUTE_NODE = 2;
93 const CDATA_SECTION_NODE = 4;
94 const ENTITY_REFERENCE_NODE = 5;
95 const ENTITY_NODE = 6;
96 const PROCESSING_INSTRUCTION_NODE = 7;
97 const COMMENT_NODE = 8;
98 const DOCUMENT_NODE = 9;
99 const DOCUMENT_TYPE_NODE = 10;
100 const DOCUMENT_FRAGMENT_NODE = 11;
101 const NOTATION_NODE = 12;
102 const XATTR_RE = /^x-attr-name-(.*)$/;
103 const XATTR_KNOWN_RE = /^x-a-([a-z]+)-(.*)$/;
105 const ELEM_START = 1;
110 // namespaces not listed here will be assigned random names
111 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
112 "http://purl.org/dc/elements/1.1/": "dc",
113 "http://www.w3.org/XML/1998/namespace": "xml"
119 for (prefix in NAMESPACES) {
120 NS_PREFIXES[NAMESPACES[prefix]] = prefix
123 class HTMLSerializer {
127 // XML namespace is implicit
128 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
134 _pushElement(element) {
141 _pushChildren(element) {
142 for(var i = element.childNodes.length-1; i >= 0; i--)
143 this._pushElement(element.childNodes.item(i));
146 _pushTagEnd(tagName) {
154 /* true if previous element is a previous verse of a stanza */
155 var parent = node.parentNode;
156 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
159 var prev = node.previousSibling;
161 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
162 prev = prev.previousSibling;
165 return (prev !== null) && prev.hasAttribute('x-verse');
169 return node.getAttribute('x-auto-node') == 'true';
172 _ignoredWithWhitespace(node) {
173 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
174 node = node.childNodes[0];
175 if (node.nodeType == TEXT_NODE)
176 return node.nodeValue.match(/^\s/)
181 serialize(rootElement, stripOuter)
187 self._pushElement(rootElement);
189 self._pushChildren(rootElement);
191 var text_buffer = '';
193 while(self.stack.length > 0) {
194 var token = self.stack.pop();
196 if(token.type === ELEM_END) {
197 self.result += text_buffer;
199 if (token.tagName != '')
200 self.result += "</" + token.tagName + ">";
204 if(token.type === NS_END) {
205 self._unassignNamespace(token.namespace);
209 switch(token.node.nodeType) {
211 if(token.node.hasAttribute('x-pass-thru')
212 || token.node.hasAttribute('data-pass-thru')) {
213 self._pushChildren(token.node);
217 if(!token.node.hasAttribute('x-node'))
220 var xnode = token.node.getAttribute('x-node');
222 if(xnode === 'out-of-flow-text') {
223 self._pushChildren(token.node);
227 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
229 // add whitespace if there's none
230 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
234 self.result += text_buffer;
236 self._serializeElement(token.node);
239 self.result += text_buffer;
240 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
243 self.result += text_buffer;
245 self.result += '<!--' + token.node.nodeValue + '-->';
249 self.result += text_buffer;
255 * TODO: this doesn't support prefix redefinitions
257 _unassignNamespace(nsData) {
258 this.nsMap[nsData.uri] = undefined;
261 _assignNamespace(uri) {
264 return ({"prefix": "", "uri": "", "fresh": false});
267 if(this.nsMap[uri] === undefined) {
268 // this prefix hasn't been defined yet in current context
269 var prefix = NAMESPACES[uri];
271 if (prefix === undefined) { // not predefined
272 prefix = "ns" + this.nsCounter;
276 this.nsMap[uri] = prefix;
284 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
287 _join(prefix, name) {
289 return prefix + ":" + name;
293 _rjoin(prefix, name) {
295 return prefix + ":" + name;
299 _serializeElement(node) {
302 if (self._nodeIgnored(node)) {
303 self._pushTagEnd('');
304 self._pushChildren(node);
307 var ns = node.getAttribute('x-ns');
309 var newNamespaces = [];
311 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
314 newNamespaces.push(nsData);
321 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
323 /* retrieve attributes */
324 var attributeIDs = [];
326 for (var i = 0; i < node.attributes.length; i++) {
327 var attr = node.attributes.item(i);
329 m = attr.name.match(XATTR_KNOWN_RE);
339 // check if name starts with "x-attr-name"
340 var m = attr.name.match(XATTR_RE);
342 attributeIDs.push(m[1]);
349 self.result += '<' + tagName;
351 function writeAttr(ns, tag, value) {
353 var nsData = self._assignNamespace(ns);
355 newNamespaces.push(nsData);
361 tag = self._join(nsData.prefix, tag);
364 self.result += ' ' + tag;
365 self.result += '="' + value.replace(/&/g, '&').replace(/"/g, '"') + '"';
368 $.each(attributes, function() {
370 this[0], this[1], this[2]
374 $.each(attributeIDs, function() {
376 node.getAttribute('x-attr-ns-'+this),
377 node.getAttribute('x-attr-name-'+this),
378 node.getAttribute('x-attr-value-'+this)
382 /* print new namespace declarations */
383 $.each(newNamespaces, function() {
384 self.result += " " + self._rjoin("xmlns", this.prefix);
385 self.result += '="' + this.uri + '"';
388 if (node.childNodes.length > 0) {
390 self._pushTagEnd(tagName);
391 self._pushChildren(node);
400 function html2text(params) {
402 var s = new HTMLSerializer();
403 params.success( s.serialize(params.element, params.stripOuter) );
405 params.error("Nie udało się zserializować tekstu:" + e)