8a32831753e4fe649d1f888aa8dff43b357e770d
[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             
15             var toBlock = ['div', 'section', 'header'];
16             var toInline = ['aside', 'span'];
17             
18             var transform = function(tags, replacingTagName) {
19                 tags.forEach(function(tagName) {
20                     tagName = tagName.toLowerCase();
21                     console.log('running ' + tagName);
22                     toret.find(tagName).replaceWith(function() {
23                         var currentTag = $(this);
24                         if(currentTag.attr('wlxml-tag'))
25                             return;
26                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
27                         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);}));
28                         for(var i = 0; i < this.attributes.length; i++) {
29                             var attr = this.attributes.item(i);
30                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
31                             toret.attr('wlxml-' + attr.name, value)
32                         }
33                         toret.append(currentTag.contents());
34                         return toret;
35                     });
36                 });
37             }
38             
39             transform(toBlock, 'div');
40             transform(toInline, 'span');
41
42             return toret.children();
43         },
44         getMetaData: function(xml) {
45             var toret = {};
46             $(xml).find('metadata').children().each(function() {
47                 var node = $(this);
48                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
49             })
50             return toret;
51         },
52         getDocumentDescription: function(xml) {
53             return {
54                 HTMLTree: this.getHTMLTree(xml),
55                 metadata: this.getMetaData(xml)
56             }
57         }
58     }
59
60     transformations.toXML = {
61         getXML: function(documentDescription) {
62             
63             var inner = $(documentDescription.HTMLTree);
64             var toret = $('<div></div>');
65             toret.append(inner);
66             
67             toret.find('div, span').replaceWith(function() {
68                 var div = $(this);
69                 var tagName = div.attr('wlxml-tag');
70                 var toret = $('<'+tagName+'>');
71                                    
72                 for(var i = 0; i < this.attributes.length; i++) {
73                     var attr = this.attributes.item(i);
74                     var split = attr.name.split('-')
75                     console.log(split);
76                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) 
77                         continue;
78                     var wlxmlName = split.splice(1).join('-');
79                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
80                     console.log(name + ': ' + value);
81                     toret.attr(wlxmlName, value);
82                 }
83                     
84                 toret.append(div.contents());
85                 return toret;
86             });
87             
88             var meta = $('<metadata></metadata>\n');
89             _.each(_.keys(documentDescription.metadata), function(key) {
90                 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
91             });
92             meta.append('\n');
93             
94             var metadata = toret.find('metadata');
95             if(metadata.length === 0) {
96                 var section = toret.find('section');
97                 section = section.length ? $(section[0]) : null;
98                 if(section) {
99                     section.prepend(meta)
100                 }
101             } else {
102                 metadata.replaceWith(meta);
103             }
104             
105             
106             return vkbeautify.xml(toret.html());
107         }
108     }
109
110
111     if(typeof module !== 'undefined' && module.exports) {
112         module.exports = transformations;
113     } else {
114         rng.modules.visualEditor.transformations = transformations;
115     }
116
117 })($);