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 return toret.children();
\r
42 getMetaData: function(xml) {
\r
44 $(xml).find('metadata').children().each(function() {
\r
46 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
\r
50 getDocumentDescription: function(xml) {
\r
52 HTMLTree: this.getHTMLTree(xml),
\r
53 metadata: this.getMetaData(xml)
\r
58 transformations.toXML = {
\r
59 getXML: function(body) {
\r
61 var inner = $(body);
\r
62 var toret = $('<div></div>');
\r
63 toret.append(inner);
\r
65 toret.find('div, span').replaceWith(function() {
\r
67 var tagName = div.attr('wlxml-tag');
\r
68 var toret = $('<'+tagName+'>');
\r
70 for(var i = 0; i < this.attributes.length; i++) {
\r
71 var attr = this.attributes.item(i);
\r
72 var split = attr.name.split('-')
\r
74 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
\r
76 var wlxmlName = split.splice(1).join('-');
\r
77 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
\r
78 console.log(name + ': ' + value);
\r
79 if(value.length && value.length > 0)
\r
80 toret.attr(wlxmlName, value);
\r
83 toret.append(div.contents());
\r
87 return vkbeautify.xml(toret.html());
\r
91 return transformations;
\r