1 if(typeof module !== 'undefined' && module.exports) {
2 var $ = require('jquery');
7 var transformations = {};
9 transformations.fromXML = {
10 getHTMLTree: function(xml) {
11 var inner = $(xml).clone();
12 var toret = $('<div></div>');
14 toret.find('metadata').remove();
16 var toBlock = ['div', 'document', 'section', 'header'];
17 var toInline = ['aside'];
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());
28 toInline.forEach(function(tagName) {
29 tagName = tagName.toLowerCase();
30 toret.find(tagName).replaceWith(function() {
32 return $('<span></span>').addClass('rng-' + tagName).append($(this).contents());
35 return toret.children();
37 getMetaData: function(xml) {
39 $(xml).find('metadata').children().each(function() {
41 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
45 getDocumentDescription: function(xml) {
47 HTMLTree: this.getHTMLTree(xml),
48 metadata: this.getMetaData(xml)
53 transformations.toXML = {
54 getXML: function(documentDescription) {
56 var inner = $(documentDescription.HTMLTree);
57 var toret = $('<div></div>');
60 toret.find('div, span').replaceWith(function() {
62 var tagName = div.attr('class').split('rng-')[1];
63 return $('<'+tagName+'>').append(div.contents());
66 var meta = $('<metadata>');
67 _.each(_.keys(documentDescription.metadata), function(key) {
68 meta.append($('<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>'));
71 toret.find('document').prepend(meta);
79 if(typeof module !== 'undefined' && module.exports) {
80 module.exports = transformations;
82 rng.modules.visualEditor.transformations = transformations;