1 define(['libs/jquery-1.9.1.min'], function($) {
3 var transformations = {};
5 transformations.fromXML = {
6 getHTMLTree: function(xml) {
7 var inner = $(xml).clone();
8 var toret = $('<div></div>');
11 var toBlock = ['div', 'section', 'header'];
12 var toInline = ['aside', 'span'];
14 var transform = function(tags, replacingTagName) {
15 tags.forEach(function(tagName) {
16 tagName = tagName.toLowerCase();
17 console.log('running ' + tagName);
18 toret.find(tagName).replaceWith(function() {
19 var currentTag = $(this);
20 if(currentTag.attr('wlxml-tag'))
22 var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);
23 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);}));
24 for(var i = 0; i < this.attributes.length; i++) {
25 var attr = this.attributes.item(i);
26 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
27 toret.attr('wlxml-' + attr.name, value)
29 toret.append(currentTag.contents());
35 transform(toBlock, 'div');
36 transform(toInline, 'span');
38 return toret.children();
40 getMetaData: function(xml) {
42 $(xml).find('metadata').children().each(function() {
44 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
48 getDocumentDescription: function(xml) {
50 HTMLTree: this.getHTMLTree(xml),
51 metadata: this.getMetaData(xml)
56 transformations.toXML = {
57 getXML: function(documentDescription) {
59 var inner = $(documentDescription.HTMLTree);
60 var toret = $('<div></div>');
63 toret.find('div, span').replaceWith(function() {
65 var tagName = div.attr('wlxml-tag');
66 var toret = $('<'+tagName+'>');
68 for(var i = 0; i < this.attributes.length; i++) {
69 var attr = this.attributes.item(i);
70 var split = attr.name.split('-')
72 if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag'))
74 var wlxmlName = split.splice(1).join('-');
75 var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;
76 console.log(name + ': ' + value);
77 toret.attr(wlxmlName, value);
80 toret.append(div.contents());
84 var meta = $('<metadata></metadata>\n');
85 _.each(_.keys(documentDescription.metadata), function(key) {
86 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
90 var metadata = toret.find('metadata');
91 if(metadata.length === 0) {
92 var section = toret.find('section');
93 section = section.length ? $(section[0]) : null;
98 metadata.replaceWith(meta);
102 return vkbeautify.xml(toret.html());
106 return transformations;