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