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 var transform = function(tags, replacingTagName) {
19 tags.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 = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
27 for(var i = 0; i < this.attributes.length; i++) {
28 var attr = this.attributes.item(i);
29 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
30 toret.attr('wlxml-' + attr.name, value)
32 toret.append(currentTag.contents());
38 transform(toBlock, 'div');
39 transform(toInline, 'span');
41 return toret.children();
43 getMetaData: function(xml) {
45 $(xml).find('metadata').children().each(function() {
47 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
51 getDocumentDescription: function(xml) {
53 HTMLTree: this.getHTMLTree(xml),
54 metadata: this.getMetaData(xml)
59 transformations.toXML = {
60 getXML: function(documentDescription) {
62 var inner = $(documentDescription.HTMLTree);
63 var toret = $('<div></div>');
66 toret.find('div, span').replaceWith(function() {
68 var tagName = div.attr('wlxml-tag');
69 var toret = $('<'+tagName+'>');
71 for(var i = 0; i < this.attributes.length; i++) {
72 var attr = this.attributes.item(i);
73 var split = attr.name.split('-')
75 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
77 var wlxmlName = split.splice(1).join('-');
78 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
79 console.log(name + ': ' + value);
80 toret.attr(wlxmlName, value);
83 toret.append(div.contents());
87 var meta = $('<metadata></metadata>\n');
88 _.each(_.keys(documentDescription.metadata), function(key) {
89 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
93 toret.find('metadata').replaceWith(meta);
101 if(typeof module !== 'undefined' && module.exports) {
102 module.exports = transformations;
104 rng.modules.visualEditor.transformations = transformations;