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