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>');
15 var toBlock = ['div', 'document', 'section', 'header'];
16 var toInline = ['aside', 'span'];
18 toBlock.forEach(function(tagName) {
19 tagName = tagName.toLowerCase();
20 console.log('running ' + tagName);
21 toret.find(tagName).replaceWith(function() {
22 var currentTag = $(this);
23 if(currentTag.attr('wlxml-tag'))
25 var toret = $('<div></div>').attr('wlxml-tag', tagName);
26 if(currentTag.attr('class'))
27 toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
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 if(currentTag.attr('wlxml-tag'))
39 var toret = $('<span></span>').attr('wlxml-tag', tagName);
40 if(currentTag.attr('class'))
41 toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
42 toret.append(currentTag.contents());
46 return toret.children();
48 getMetaData: function(xml) {
50 $(xml).find('metadata').children().each(function() {
52 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
56 getDocumentDescription: function(xml) {
58 HTMLTree: this.getHTMLTree(xml),
59 metadata: this.getMetaData(xml)
64 transformations.toXML = {
65 getXML: function(documentDescription) {
67 var inner = $(documentDescription.HTMLTree);
68 var toret = $('<div></div>');
71 toret.find('div, span').replaceWith(function() {
73 var tagName = div.attr('wlxml-tag');
74 var toret = $('<'+tagName+'>');
75 if(div.attr('wlxml-class'))
76 toret.attr('class', div.attr('wlxml-class').replace(/-/g, '.'))
77 toret.append(div.contents());
81 var meta = $('<metadata></metadata>\n');
82 _.each(_.keys(documentDescription.metadata), function(key) {
83 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
87 toret.find('metadata').replaceWith(meta);
95 if(typeof module !== 'undefined' && module.exports) {
96 module.exports = transformations;
98 rng.modules.visualEditor.transformations = transformations;