diff --git a/lib/gimporter.js b/lib/gimporter.js index 0a5d271..1a36876 100644 --- a/lib/gimporter.js +++ b/lib/gimporter.js @@ -22,6 +22,32 @@ var connection = mysql.createConnection({ connection.connect(); +function unwrap(childrens) { + var escapedChildrens = []; + childrens.forEach(function(el){ + if (el.type === 'tag' && el.name === 'a') { + if (el.children) { + var els = unwrap(el.children); + Array.prototype.push.apply(escapedChildrens, els); + } else { + if (el.type === 'tag' && el.name === 'a') { + el.children.forEach(function(_el){ + escapedChildrens.push(el); + }) + } else { + escapedChildrens.push(el); + } + } + } else { + escapedChildrens.push(el); + if (el.children) { + el.children = unwrap(el.children); + } + } + }); + return escapedChildrens; +} + gimporter.insertFile = function(data, tableName) { var deferred = Q.defer(); var baseData = { @@ -66,7 +92,16 @@ gimporter.getLegislacionData = function($file, file) { $file('version').not(function(i, el){ return cmsConfigExcludes.indexOf(el.attribs.cms_config) > -1; }).each(function (k, v) { - var $el = $file(v); + + var $el = $file(v), + childrens = []; + + $el.contents().each(function(i, el) { + childrens.push(el); + }); + + $el[0].children = unwrap(childrens); + contentBuffer.push($el.html()); contentBufferText.push($el.text()); }); @@ -110,7 +145,15 @@ gimporter.getJurisprudenciaData = function($file, file) { $file('version').not(function(i, el){ return cmsConfigExcludes.indexOf(el.attribs.cms_config) > -1; }).each(function (k, v) { - var $el = $file(v); + var $el = $file(v), + childrens = []; + + $el.contents().each(function(i, el) { + childrens.push(el); + }); + + $el[0].children = unwrap(childrens); + contentBuffer.push($el.html()); contentBufferText.push($el.text()); });