Skip to content

Commit

Permalink
Suporta HTML e texto puro no parser de articulação.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliomelo committed Sep 12, 2017
1 parent d0a40a6 commit 7229ac8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ Todas as opções de validação são habilitadas (valor true) por padrão.
Para interpretar um texto puro, transformando em um texto estruturado utilizando LexML, utilize a função interpretar (veja [código-fonte](src/interpretadorArticulacao.js)), com a seguinte sintaxe:

```javascript
interpretar(texto, formato);
interpretar(texto, formatoDestino, formatoOrigem);
```

onde ``texto`` é uma `string` e ``formato`` é uma das opções "json", "lexml" (padrão) ou "lexmlString".
onde ``texto`` é uma `string`, ``formatoDestino`` é uma das opções "json", "lexml" (padrão) ou "lexmlString" e ``formatoOrigem`` é "texto" (padrão) ou "html".

Contribuições desejadas
-----------------------
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
],
"author": "Assembleia Legislativa de Minas Gerais (ALMG)",
"license": "LGPL",
"repository": {
"type": "git",
"url": "https://github.com/silegis-mg/editor-articulacao.git"
},
"dependencies": {
},
"devDependencies": {
Expand Down
47 changes: 36 additions & 11 deletions src/interpretadorArticulacao.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,22 +549,30 @@ function transformarEmLexML(json) {
* Interpreta conteúdo de articulação.
*
* @param {String} texto Texto a ser interpretado
* @param {String} formato Formato a ser retornado: 'json', 'lexml' (padrão) ou "lexmlString".
* @param {String} formatoDestino Formato a ser retornado: 'json', 'lexml' (padrão) ou "lexmlString".
* @param {String} formatoOrigem Formatao a ser processado: 'texto' (padrão), 'html'.
* @returns {Object|DocumentFragment}
*/
function interpretarArticulacao(texto, formato) {
function interpretarArticulacao(texto, formatoDestino, formatoOrigem) {
var json;

try {
if (typeof texto === 'string') {
var div = document.createElement('div');
div.innerHTML = texto;
json = parseTexto(div.innerHTML.replace(/<P>(.+?)<\/P>/gi, '$1\n'));
} else {
throw 'Formato não suportado.';
switch ((formatoOrigem || 'texto').toLowerCase()) {
case 'texto':
json = parseTexto(texto);
break;

case 'html':
let div = document.createElement('div');
div.innerHTML = texto;
json = parseTexto(removerEntidadeHtml(div.innerHTML.replace(/<P>(.+?)<\/P>/gi, '$1\n').trim()));
break;

default:
throw 'Formato não suportado.';
}

switch ((formato || 'lexml').toLowerCase()) {
switch ((formatoDestino || 'lexml').toLowerCase()) {
case 'json':
return json;

Expand All @@ -576,18 +584,35 @@ function interpretarArticulacao(texto, formato) {
return transformarEmLexML(json);

default:
throw 'Formato não suportado: ' + formato;
throw 'Formato não suportado: ' + formatoDestino;
}
} catch (e) {
throw {
mensagem: 'Erro interpretando articulação.',
item: texto,
formato: formato,
formato: formatoDestino,
erroOriginal: e
};
}
}

function removerEntidadeHtml(html) {
var safeXmlEntities = ["&lt;", "&gt;", "&quot;", "&amp;", "&apos;"];

return html.replace(/&.+?;/g, function(entidade) {
if(safeXmlEntities.indexOf(entidade)>=0) {
return entidade;
} else {
/* A entidade não é uma das predefinidas no xml e é suportada só no HTML. Por exemplo: &nbsp; ou &copy;.
* Nesse caso, converte para texto e no replace abaixo substitui pela notação unicode.
*/
var span = document.createElement('span');
span.innerHTML = entidade;
return span.textContent;
}
});
}

function transformarEmLexMLFragmento(json) {
let lexml = transformarEmLexML(json);
let container = document.createElement('div');
Expand Down
7 changes: 7 additions & 0 deletions test/karma/interpretarArticulacao.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ describe('Parser de articulação', function () {
});
});

it('Deve escapar entidades html', function() {
var texto = '<P>Art. 1&#176; &#8211; Fica declarado de utilidade p&#250;blica o treste &#160;asdf asd f &#160; &#160;asd, com sede no Munic&#237;pio de Abadia dos Dourados.</P><P>Art. 2&#176; &#8211; Esta lei entra em vigor na data de sua publica&#231;&#227;o.</P>';
var lexml = parser.interpretar(texto, 'lexml-string', 'html');

expect(lexml).toEqual('<Articulacao xmlns="http://www.lexml.gov.br/1.0"><Artigo id="art1"><Rotulo>Art. 1º –</Rotulo><Caput id="art1_cpt"><p>Fica declarado de utilidade pública o treste  asdf asd f    asd, com sede no Município de Abadia dos Dourados.</p></Caput></Artigo><Artigo id="art2"><Rotulo>Art. 2º –</Rotulo><Caput id="art2_cpt"><p>Esta lei entra em vigor na data de sua publicação.</p></Caput></Artigo></Articulacao>');
});

describe('transfomarQuebrasDeLinhaEmP', function () {
var transformarQuebrasDeLinhaEmP = window.interpretadorArticulacao.transformarQuebrasDeLinhaEmP;

Expand Down

0 comments on commit 7229ac8

Please sign in to comment.