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

De Wikincat
Ir para navegação Ir para pesquisar
imported>Jaideraf
m (uma edição)
imported>Jaideraf
Sem resumo de edição
Linha 1: Linha 1:
function Export2Docx() {
(function () {
"use strict";
"use strict";
let decodeEntities = ( function() {
function Export2Docx() {
let decodeEntities = (function () {
// https://stackoverflow.com/questions/5796718/html-entity-decode
// https://stackoverflow.com/questions/5796718/html-entity-decode
// this prevents any overhead from creating the object each time
// this prevents any overhead from creating the object each time
let element = document.createElement( "div" );
let element = document.createElement("div");


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


return str;
return str;
}
}


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" );
let card = document.getElementById("card");
let p = card.getElementsByTagName( "p" );
let p = card.getElementsByTagName("p");


doc.createParagraph( document.getElementById( "cipParagraph" ).textContent ).center();
doc.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
let 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>" );
let arrayP2 = p[2].innerHTML.split("<br>");
arrayP2.forEach( (item) => {
arrayP2.forEach((item) => {
doc.createParagraph( decodeEntities(item) );
doc.createParagraph(decodeEntities(item));
} );

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

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


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


if (p[4] !== undefined) {
const borderParagraphEndCard = new Paragraph( "" ).createBorder();
let arrayP4 = p[4].innerHTML.split("<br>");
borderParagraphEndCard.Borders.addTopBorder();
arrayP4.forEach((item) => {
doc.addParagraph( borderParagraphEndCard );
doc.createParagraph(decodeEntities(item)).right();
});
}


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


const borderParagraphEndCard = new Paragraph("").createBorder();
// Used to export the file into a .docx file
borderParagraphEndCard.Borders.addTopBorder();
const packer = new Packer();
doc.addParagraph(borderParagraphEndCard);


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


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

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

Edição das 21h19min de 6 de agosto de 2022

(function () {
    "use strict";
    function Export2Docx() {
        let decodeEntities = (function () {
            // https://stackoverflow.com/questions/5796718/html-entity-decode
            // this prevents any overhead from creating the object each time
            let element = document.createElement("div");

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

                return str;
            }

            return decodeHTMLEntities;
        })();

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

        let card = document.getElementById("card");
        let 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("");

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

        doc.createParagraph("");

        let 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) {
            let 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 => {
            console.log(blob);
            saveAs(blob, "Ficha.docx");
            //console.log( "Documento criado com sucesso" );
        });
    }
})();