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