2edf3d071b9bf958a1fc4f6e913255b604ce354b
[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', 'span'];
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                     var toret = $('<div></div>').attr('wlxml-tag', suffix);
25                     var currentTag = $(this);
26                     if(currentTag.attr('class'))
27                         toret.attr('wlxml-class', currentTag.attr('class'));
28                     toret.append(currentTag.contents());
29                     return toret;
30                 });
31             });
32             
33             toInline.forEach(function(tagName) {
34                 tagName = tagName.toLowerCase();
35                 toret.find(tagName).replaceWith(function() {
36                     var currentTag = $(this);
37                     var toret = $('<span></span>').attr('wlxml-tag', tagName);
38                     if(currentTag.attr('class'))
39                         toret.attr('wlxml-class', currentTag.attr('class'));
40                     toret.append(currentTag.contents());
41                     return toret;
42                 });
43             });
44             return toret.children();
45         },
46         getMetaData: function(xml) {
47             var toret = {};
48             $(xml).find('metadata').children().each(function() {
49                 var node = $(this);
50                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
51             })
52             return toret;
53         },
54         getDocumentDescription: function(xml) {
55             return {
56                 HTMLTree: this.getHTMLTree(xml),
57                 metadata: this.getMetaData(xml)
58             }
59         }
60     }
61
62     transformations.toXML = {
63         getXML: function(documentDescription) {
64             
65             var inner = $(documentDescription.HTMLTree);
66             var toret = $('<div></div>');
67             toret.append(inner);
68             
69             toret.find('div, span').replaceWith(function() {
70                 var div = $(this);
71                 var tagName = div.attr('wlxml-tag');
72                 var toret = $('<'+tagName+'>');
73                 if(div.attr('wlxml-class'))
74                     toret.attr('class', div.attr('wlxml-class'))
75                 toret.append(div.contents());
76                 return toret;
77             });
78             
79             var meta = $('<metadata>');
80             _.each(_.keys(documentDescription.metadata), function(key) {
81                 meta.append($('<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>'));
82             });
83             
84             toret.find('document').prepend(meta);
85             
86             return toret.html();
87             
88         }
89     }
90
91
92     if(typeof module !== 'undefined' && module.exports) {
93         module.exports = transformations;
94     } else {
95         rng.modules.visualEditor.transformations = transformations;
96     }
97
98 })($);