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', '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 toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
28 for(var i = 0; i < this.attributes.length; i++) {
29 var attr = this.attributes.item(i);
30 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
31 toret.attr('wlxml-' + attr.name, value)
33 toret.append(currentTag.contents());
39 transform(toBlock, 'div');
40 transform(toInline, 'span');
42 return toret.children();
44 getMetaData: function(xml) {
46 $(xml).find('metadata').children().each(function() {
48 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
52 getDocumentDescription: function(xml) {
54 HTMLTree: this.getHTMLTree(xml),
55 metadata: this.getMetaData(xml)
60 transformations.toXML = {
61 getXML: function(documentDescription) {
63 var inner = $(documentDescription.HTMLTree);
64 var toret = $('<div></div>');
67 toret.find('div, span').replaceWith(function() {
69 var tagName = div.attr('wlxml-tag');
70 var toret = $('<'+tagName+'>');
72 for(var i = 0; i < this.attributes.length; i++) {
73 var attr = this.attributes.item(i);
74 var split = attr.name.split('-')
76 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
78 var wlxmlName = split.splice(1).join('-');
79 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
80 console.log(name + ': ' + value);
81 toret.attr(wlxmlName, value);
84 toret.append(div.contents());
88 var meta = $('<metadata></metadata>\n');
89 _.each(_.keys(documentDescription.metadata), function(key) {
90 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
94 var metadata = toret.find('metadata');
95 if(metadata.length === 0) {
96 var section = toret.find('section');
97 section = section.length ? $(section[0]) : null;
102 metadata.replaceWith(meta);
106 return vkbeautify.xml(toret.html());
111 if(typeof module !== 'undefined' && module.exports) {
112 module.exports = transformations;
114 rng.modules.visualEditor.transformations = transformations;