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