transformations.fromXML.getHTMLTree
[fnpeditor.git] / modules / visualEditor.transformations.js
1 if(typeof module !== 'undefined' && module.exports) {
2     var $ = require('jquery');
3 }
4
5 (function($) {
6
7     var transformations = {};
8
9     transformations.fromXML = {
10         getHTMLTree: function(xml) {
11             var inner = $(xml).clone();
12             var toret = $('<div></div>');
13             toret.append(inner);
14             toret.find('metadata').remove();
15             
16             var toBlock = ['div', 'document', 'section', 'header'];
17             var toInline = ['aside'];
18             
19             toBlock.forEach(function(tagName) {
20                 tagName = tagName.toLowerCase();
21                 console.log('running ' + tagName);
22                 toret.find(tagName).replaceWith(function() {
23                     var suffix = tagName !== 'div'  ? tagName : 'block';
24                     return $('<div></div>').addClass('rng-' + suffix).append($(this).contents());
25                 });
26             });
27             
28             toInline.forEach(function(tagName) {
29                 tagName = tagName.toLowerCase();
30                 toret.find(tagName).replaceWith(function() {
31                     var node = this;
32                     return $('<span></span>').addClass('rng-' + tagName).append($(this).contents());
33                 });
34             });
35             return toret.children();
36         },
37         getMetaData: function(xml) {
38             var toret = {};
39             $(xml).find('metadata').children().each(function() {
40                 var node = $(this);
41                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
42             })
43             return toret;
44         },
45         getDocumentDescription: function(xml) {
46             return {
47                 HTMLTree: this.getHTMLTree(xml),
48                 metadata: this.getMetaData(xml)
49             }
50         }
51     }
52
53     transformations.toXML = {
54         getXML: function(documentDescription) {
55             return documentDescription.HTMLTree;
56         }
57     }
58
59
60     if(typeof module !== 'undefined' && module.exports) {
61         module.exports = transformations;
62     } else {
63         rng.modules.visualEditor.transformations = transformations;
64     }
65
66 })($);