1 define(['libs/jquery-1.9.1.min'], function($) {
5 var transformations = {};
7 transformations.fromXML = {
8 getHTMLTree: function(xml) {
9 var inner = $(xml).clone();
10 var toret = $('<div></div>');
13 var toBlock = ['div', 'section', 'header'];
14 var toInline = ['aside', 'span'];
16 var transform = function(tags, replacingTagName) {
17 tags.forEach(function(tagName) {
18 tagName = tagName.toLowerCase();
19 console.log('running ' + tagName);
20 toret.find(tagName).replaceWith(function() {
21 var currentTag = $(this);
22 if(currentTag.attr('wlxml-tag'))
24 var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
25 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);}));
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());
37 transform(toBlock, 'div');
38 transform(toInline, 'span');
40 toret.find(":not(iframe)").addBack().contents().filter(function() {
41 return this.nodeType == 3;} ).each(function() {
43 var hasText = /\S/g.test(n.text());
48 var startSpace = /\s/g.test(n.text().substr(0,1));
49 var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1;
50 var trimmed = $.trim(n.text());
51 n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : '');
54 return toret.children();
56 getMetaData: function(xml) {
58 $(xml).find('metadata').children().each(function() {
60 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
64 getDocumentDescription: function(xml) {
66 HTMLTree: this.getHTMLTree(xml),
67 metadata: this.getMetaData(xml)
72 transformations.toXML = {
73 getXML: function(body) {
75 var inner = body.clone();
76 var toret = $('<div></div>');
79 toret.find('div, span').replaceWith(function() {
81 var tagName = div.attr('wlxml-tag');
82 var toret = $('<'+tagName+'>');
84 for(var i = 0; i < this.attributes.length; i++) {
85 var attr = this.attributes.item(i);
86 var split = attr.name.split('-');
88 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
90 var wlxmlName = split.splice(1).join('-');
91 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
92 console.log(name + ': ' + value);
93 if(value.length && value.length > 0)
94 toret.attr(wlxmlName, value);
97 toret.append(div.contents());
101 return vkbeautify.xml(toret.html());
105 return transformations;