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 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());
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());
44 return toret.children();
46 getMetaData: function(xml) {
48 $(xml).find('metadata').children().each(function() {
50 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
54 getDocumentDescription: function(xml) {
56 HTMLTree: this.getHTMLTree(xml),
57 metadata: this.getMetaData(xml)
62 transformations.toXML = {
63 getXML: function(documentDescription) {
65 var inner = $(documentDescription.HTMLTree);
66 var toret = $('<div></div>');
69 toret.find('div, span').replaceWith(function() {
71 var tagName = div.attr('wlxml-tag');
72 return $('<'+tagName+'>').append(div.contents());
75 var meta = $('<metadata>');
76 _.each(_.keys(documentDescription.metadata), function(key) {
77 meta.append($('<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>'));
80 toret.find('document').prepend(meta);
88 if(typeof module !== 'undefined' && module.exports) {
89 module.exports = transformations;
91 rng.modules.visualEditor.transformations = transformations;