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 toret = $('<div></div>').attr('wlxml-tag', tagName);
24 var currentTag = $(this);
25 if(currentTag.attr('class'))
26 toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
27 toret.append(currentTag.contents());
32 toInline.forEach(function(tagName) {
33 tagName = tagName.toLowerCase();
34 toret.find(tagName).replaceWith(function() {
35 var currentTag = $(this);
36 var toret = $('<span></span>').attr('wlxml-tag', tagName);
37 if(currentTag.attr('class'))
38 toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
39 toret.append(currentTag.contents());
43 return toret.children();
45 getMetaData: function(xml) {
47 $(xml).find('metadata').children().each(function() {
49 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
53 getDocumentDescription: function(xml) {
55 HTMLTree: this.getHTMLTree(xml),
56 metadata: this.getMetaData(xml)
61 transformations.toXML = {
62 getXML: function(documentDescription) {
64 var inner = $(documentDescription.HTMLTree);
65 var toret = $('<div></div>');
68 toret.find('div, span').replaceWith(function() {
70 var tagName = div.attr('wlxml-tag');
71 var toret = $('<'+tagName+'>');
72 if(div.attr('wlxml-class'))
73 toret.attr('class', div.attr('wlxml-class').replace(/-/g, '.'))
74 toret.append(div.contents());
78 var meta = $('<metadata>');
79 _.each(_.keys(documentDescription.metadata), function(key) {
80 meta.append($('<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>'));
83 toret.find('document').prepend(meta);
91 if(typeof module !== 'undefined' && module.exports) {
92 module.exports = transformations;
94 rng.modules.visualEditor.transformations = transformations;