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 for(var i = 0; i < this.attributes.length; i++) {
27 var attr = this.attributes.item(i);
28 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
29 toret.attr('wlxml-' + attr.name, value)
31 toret.append(currentTag.contents());
36 toInline.forEach(function(tagName) {
37 tagName = tagName.toLowerCase();
38 toret.find(tagName).replaceWith(function() {
39 var currentTag = $(this);
40 if(currentTag.attr('wlxml-tag'))
42 var toret = $('<span></span>').attr('wlxml-tag', tagName);
43 for(var i = 0; i < this.attributes.length; i++) {
44 var attr = this.attributes.item(i);
45 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
46 toret.attr('wlxml-' + attr.name, value)
48 toret.append(currentTag.contents());
52 return toret.children();
54 getMetaData: function(xml) {
56 $(xml).find('metadata').children().each(function() {
58 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
62 getDocumentDescription: function(xml) {
64 HTMLTree: this.getHTMLTree(xml),
65 metadata: this.getMetaData(xml)
70 transformations.toXML = {
71 getXML: function(documentDescription) {
73 var inner = $(documentDescription.HTMLTree);
74 var toret = $('<div></div>');
77 toret.find('div, span').replaceWith(function() {
79 var tagName = div.attr('wlxml-tag');
80 var toret = $('<'+tagName+'>');
82 for(var i = 0; i < this.attributes.length; i++) {
83 var attr = this.attributes.item(i);
84 var split = attr.name.split('-')
86 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
88 var wlxmlName = split.splice(1).join('-');
89 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
90 console.log(name + ': ' + value);
91 toret.attr(wlxmlName, value);
94 toret.append(div.contents());
98 var meta = $('<metadata></metadata>\n');
99 _.each(_.keys(documentDescription.metadata), function(key) {
100 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
104 toret.find('metadata').replaceWith(meta);
112 if(typeof module !== 'undefined' && module.exports) {
113 module.exports = transformations;
115 rng.modules.visualEditor.transformations = transformations;