documentToolbar module
[fnpeditor.git] / modules / visualEditor.transformations.js
1 define(['libs/jquery-1.9.1.min'], function($) {
2
3     var transformations = {};
4
5     transformations.fromXML = {
6         getHTMLTree: function(xml) {
7             var inner = $(xml).clone();
8             var toret = $('<div></div>');
9             toret.append(inner);
10             
11             var toBlock = ['div', 'section', 'header'];
12             var toInline = ['aside', 'span'];
13             
14             var transform = function(tags, replacingTagName) {
15                 tags.forEach(function(tagName) {
16                     tagName = tagName.toLowerCase();
17                     console.log('running ' + tagName);
18                     toret.find(tagName).replaceWith(function() {
19                         var currentTag = $(this);
20                         if(currentTag.attr('wlxml-tag'))
21                             return;
22                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
23                         toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
24                         for(var i = 0; i < this.attributes.length; i++) {
25                             var attr = this.attributes.item(i);
26                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
27                             toret.attr('wlxml-' + attr.name, value)
28                         }
29                         toret.append(currentTag.contents());
30                         return toret;
31                     });
32                 });
33             }
34             
35             transform(toBlock, 'div');
36             transform(toInline, 'span');
37
38             return toret.children();
39         },
40         getMetaData: function(xml) {
41             var toret = {};
42             $(xml).find('metadata').children().each(function() {
43                 var node = $(this);
44                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
45             })
46             return toret;
47         },
48         getDocumentDescription: function(xml) {
49             return {
50                 HTMLTree: this.getHTMLTree(xml),
51                 metadata: this.getMetaData(xml)
52             }
53         }
54     }
55
56     transformations.toXML = {
57         getXML: function(documentDescription) {
58             
59             var inner = $(documentDescription.HTMLTree);
60             var toret = $('<div></div>');
61             toret.append(inner);
62             
63             toret.find('div, span').replaceWith(function() {
64                 var div = $(this);
65                 var tagName = div.attr('wlxml-tag');
66                 var toret = $('<'+tagName+'>');
67                                    
68                 for(var i = 0; i < this.attributes.length; i++) {
69                     var attr = this.attributes.item(i);
70                     var split = attr.name.split('-')
71                     console.log(split);
72                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) 
73                         continue;
74                     var wlxmlName = split.splice(1).join('-');
75                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
76                     console.log(name + ': ' + value);
77                     toret.attr(wlxmlName, value);
78                 }
79                     
80                 toret.append(div.contents());
81                 return toret;
82             });
83             
84             var meta = $('<metadata></metadata>\n');
85             _.each(_.keys(documentDescription.metadata), function(key) {
86                 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
87             });
88             meta.append('\n');
89             
90             var metadata = toret.find('metadata');
91             if(metadata.length === 0) {
92                 var section = toret.find('section');
93                 section = section.length ? $(section[0]) : null;
94                 if(section) {
95                     section.prepend(meta)
96                 }
97             } else {
98                 metadata.replaceWith(meta);
99             }
100             
101             
102             return vkbeautify.xml(toret.html());
103         }
104     }
105
106     return transformations;
107
108 });