more user friendly toolbar labels
[fnpeditor.git] / modules / documentCanvas / transformations.js
1 define(['libs/jquery-1.9.1.min'], function($) {
2
3     'use strict';
4
5     var transformations = {};
6
7     transformations.fromXML = {
8         getHTMLTree: function(xml) {
9             var inner = $(xml).clone();
10             var toret = $('<div></div>');
11             toret.append(inner);
12             
13             var toBlock = ['div', 'section', 'header'];
14             var toInline = ['aside', 'span'];
15             
16             var transform = function(tags, replacingTagName) {
17                 tags.forEach(function(tagName) {
18                     tagName = tagName.toLowerCase();
19                     console.log('running ' + tagName);
20                     toret.find(tagName).replaceWith(function() {
21                         var currentTag = $(this);
22                         if(currentTag.attr('wlxml-tag'))
23                             return;
24                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
25                         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);}));
26                         for(var i = 0; i < this.attributes.length; i++) {
27                             var attr = this.attributes.item(i);
28                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
29                             toret.attr('wlxml-' + attr.name, value);
30                         }
31                         toret.append(currentTag.contents());
32                         return toret;
33                     });
34                 });
35             };
36             
37             transform(toBlock, 'div');
38             transform(toInline, 'span');
39
40             toret.find(":not(iframe)").addBack().contents().filter(function() {
41                 return this.nodeType == 3;} ).each(function() {
42                     var n = $(this); 
43                     var hasText = /\S/g.test(n.text());
44                     if(!hasText) {
45                         n.remove();
46                         return;
47                     }
48                     var startSpace = /\s/g.test(n.text().substr(0,1));
49                     var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1;
50                     var trimmed = $.trim(n.text());
51                     n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : '');
52             });
53             
54             return toret.children();
55         },
56         getMetaData: function(xml) {
57             var toret = {};
58             $(xml).find('metadata').children().each(function() {
59                 var node = $(this);
60                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
61             });
62             return toret;
63         },
64         getDocumentDescription: function(xml) {
65             return {
66                 HTMLTree: this.getHTMLTree(xml),
67                 metadata: this.getMetaData(xml)
68             };
69         }
70     };
71
72     transformations.toXML = {
73         getXML: function(body) {
74             
75             var inner = body.clone();
76             var toret = $('<div></div>');
77             toret.append(inner);
78             
79             toret.find('div, span').replaceWith(function() {
80                 var div = $(this);
81                 var tagName = div.attr('wlxml-tag');
82                 var toret = $('<'+tagName+'>');
83                                    
84                 for(var i = 0; i < this.attributes.length; i++) {
85                     var attr = this.attributes.item(i);
86                     var split = attr.name.split('-');
87                     console.log(split);
88                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) 
89                         continue;
90                     var wlxmlName = split.splice(1).join('-');
91                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
92                     console.log(name + ': ' + value);
93                     if(value.length && value.length > 0)
94                         toret.attr(wlxmlName, value);
95                 }
96                     
97                 toret.append(div.contents());
98                 return toret;
99             });
100
101             return vkbeautify.xml(toret.html());
102         }
103     };
104
105     return transformations;
106
107 });