MediaWiki:CardToDocx.js: mudanças entre as edições

De Wikincat
Ir para navegação Ir para pesquisar
m (uma edição)
Sem resumo de edição
 
(4 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
/* jshint esversion: 10 */
function Export2Docx() {
function Export2Docx() {
const decodeEntities = (function () {
"use strict";
// https://stackoverflow.com/questions/5796718/
let decodeEntities = ( function() {
// this prevents any overhead from creating the object each time
// https://stackoverflow.com/questions/5796718/html-entity-decode
const element = document.createElement('div');
// this prevents any overhead from creating the object each time
let element = document.createElement( "div" );


function decodeHTMLEntities (str) {
function decodeHTMLEntities(str) {
if(str && typeof str === "string") {
let string = str;
// strip script/html tags
if (str && typeof str === 'string') {
str = str.replace( /<script[^>]*>([\S\s]*?)<\/script>/gmi, "" );
// strip script/html tags
str = str.replace( /<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, "" );
string = string.replace(/<script[^>]*>([\S\s]*?)<\/script>/gim, '');
string = string.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gim, '');
element.innerHTML = str;
str = element.textContent;
// add by jaideraf:
string = str.replace(/&nbsp;/g, String.fromCharCode(32));
element.textContent = "";
}
element.innerHTML = str;
string = element.textContent;
element.textContent = '';
}


return str;
return string;
}
}


return decodeHTMLEntities;
return decodeHTMLEntities;
} )();
})();


// Create document
// Create document
const doc = new Document( {
const doc = new Document({
creator: "Wikincat",
creator: 'Wikincat',
title: "Ficha catalográfica",
title: 'Ficha catalográfica',
description: "Ficha catalográfica gerada pelo Wikincat"
description: 'Ficha catalográfica gerada pelo Wikincat',
} );
});


let card = document.getElementById( "card" );
const card = document.getElementById('card');
let p = card.getElementsByTagName( "p" );
const p = card.getElementsByTagName('p');


doc
doc.createParagraph( document.getElementById( "cipParagraph" ).textContent ).center();
.createParagraph(document.getElementById('cipParagraph').textContent)
.center();


const borderParagraphStartCard = new Paragraph( "" ).createBorder();
const borderParagraphStartCard = new Paragraph('').createBorder();
borderParagraphStartCard.Borders.addBottomBorder();
borderParagraphStartCard.Borders.addBottomBorder();
doc.addParagraph( borderParagraphStartCard );
doc.addParagraph(borderParagraphStartCard);


doc.createParagraph( "" );
doc.createParagraph('');


doc.createParagraph( decodeEntities(p[0].textContent) ); // 1XX
doc.createParagraph(decodeEntities(p[0].textContent)); // 1XX
doc.createParagraph( "" );
doc.createParagraph('');


let arrayP1 = p[1].innerHTML.split( "<br>" ); // 24X
const arrayP1 = p[1].innerHTML.split('<br>'); // 24X
arrayP1.forEach( (item) => {
arrayP1.forEach((item) => {
doc.createParagraph( decodeEntities(item) );
doc.createParagraph(decodeEntities(item));
} );
});


doc.createParagraph( "" );
doc.createParagraph('');


let arrayP2 = p[2].innerHTML.split( "<br>" );
const arrayP2 = p[2].innerHTML.split('<br>');
arrayP2.forEach( (item) => {
arrayP2.forEach((item) => {
doc.createParagraph( decodeEntities(item) );
doc.createParagraph(decodeEntities(item));
} );
});


doc.createParagraph( "" );
doc.createParagraph('');
doc.createParagraph( decodeEntities( p[3].textContent ) ); // Track
doc.createParagraph(decodeEntities(p[3].textContent)); // Track
doc.createParagraph( "" );
doc.createParagraph('');


if ( p[4] !== undefined ) {
if (p[4] !== undefined) {
let arrayP4 = p[4].innerHTML.split("<br>");
const arrayP4 = p[4].innerHTML.split('<br>');
arrayP4.forEach( (item) => {
arrayP4.forEach((item) => {
doc.createParagraph( decodeEntities( item ) ).right();
doc.createParagraph(decodeEntities(item)).right();
});
});
}
}


doc.createParagraph( "" );
doc.createParagraph('');


const borderParagraphEndCard = new Paragraph( "" ).createBorder();
const borderParagraphEndCard = new Paragraph('').createBorder();
borderParagraphEndCard.Borders.addTopBorder();
borderParagraphEndCard.Borders.addTopBorder();
doc.addParagraph( borderParagraphEndCard );
doc.addParagraph(borderParagraphEndCard);


doc.createParagraph( document.getElementById( "credit" ).textContent ).center();
doc.createParagraph(document.getElementById('credit').textContent).center();


// Used to export the file into a .docx file
// Used to export the file into a .docx file
const packer = new Packer();
const packer = new Packer();

packer.toBlob(doc).then(blob => {
console.log(blob);
saveAs(blob, "Ficha.docx");
console.log( "Documento criado com sucesso" );
});


packer.toBlob(doc).then((blob) => {
saveAs(blob, 'Ficha.docx');
// console.log( "Documento criado com sucesso" );
});
}
}

Edição atual tal como às 13h40min de 21 de abril de 2024

/* jshint esversion: 10 */
function Export2Docx() {
  const decodeEntities = (function () {
    // https://stackoverflow.com/questions/5796718/
    // this prevents any overhead from creating the object each time
    const element = document.createElement('div');

    function decodeHTMLEntities(str) {
      let string = str;
      if (str && typeof str === 'string') {
        // strip script/html tags
        string = string.replace(/<script[^>]*>([\S\s]*?)<\/script>/gim, '');
        string = string.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gim, '');
        // add by jaideraf:
        string = str.replace(/&nbsp;/g, String.fromCharCode(32));
        element.innerHTML = str;
        string = element.textContent;
        element.textContent = '';
      }

      return string;
    }

    return decodeHTMLEntities;
  })();

  // Create document
  const doc = new Document({
    creator: 'Wikincat',
    title: 'Ficha catalográfica',
    description: 'Ficha catalográfica gerada pelo Wikincat',
  });

  const card = document.getElementById('card');
  const p = card.getElementsByTagName('p');

  doc
    .createParagraph(document.getElementById('cipParagraph').textContent)
    .center();

  const borderParagraphStartCard = new Paragraph('').createBorder();
  borderParagraphStartCard.Borders.addBottomBorder();
  doc.addParagraph(borderParagraphStartCard);

  doc.createParagraph('');

  doc.createParagraph(decodeEntities(p[0].textContent)); // 1XX
  doc.createParagraph('');

  const arrayP1 = p[1].innerHTML.split('<br>'); // 24X
  arrayP1.forEach((item) => {
    doc.createParagraph(decodeEntities(item));
  });

  doc.createParagraph('');

  const arrayP2 = p[2].innerHTML.split('<br>');
  arrayP2.forEach((item) => {
    doc.createParagraph(decodeEntities(item));
  });

  doc.createParagraph('');
  doc.createParagraph(decodeEntities(p[3].textContent)); // Track
  doc.createParagraph('');

  if (p[4] !== undefined) {
    const arrayP4 = p[4].innerHTML.split('<br>');
    arrayP4.forEach((item) => {
      doc.createParagraph(decodeEntities(item)).right();
    });
  }

  doc.createParagraph('');

  const borderParagraphEndCard = new Paragraph('').createBorder();
  borderParagraphEndCard.Borders.addTopBorder();
  doc.addParagraph(borderParagraphEndCard);

  doc.createParagraph(document.getElementById('credit').textContent).center();

  // Used to export the file into a .docx file
  const packer = new Packer();

  packer.toBlob(doc).then((blob) => {
    saveAs(blob, 'Ficha.docx');
    // console.log( "Documento criado com sucesso" );
  });
}