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', 'span'];
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'))
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());
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'))
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());
47 return toret.children();
49 getMetaData: function(xml) {
51 $(xml).find('metadata').children().each(function() {
53 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
57 getDocumentDescription: function(xml) {
59 HTMLTree: this.getHTMLTree(xml),
60 metadata: this.getMetaData(xml)
65 transformations.toXML = {
66 getXML: function(documentDescription) {
68 var inner = $(documentDescription.HTMLTree);
69 var toret = $('<div></div>');
72 toret.find('div, span').replaceWith(function() {
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());
82 var meta = $('<metadata>');
83 _.each(_.keys(documentDescription.metadata), function(key) {
84 meta.append($('<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>'));
87 toret.find('document').prepend(meta);
95 if(typeof module !== 'undefined' && module.exports) {
96 module.exports = transformations;
98 rng.modules.visualEditor.transformations = transformations;