MediaWiki:CardToDocx.js

De Wikincat
Revisão de 15h19min de 26 de outubro de 2020 por Jaider.ferreira (discussão | contribs) (uma edição)
Ir para navegação Ir para pesquisar

Nota: Após publicar, você pode ter que limpar o "cache" do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer/Edge: PressioneCtrl enquanto clica Recarregar, ou Pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
function Export2Docx() {
"use strict";
    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" );
    });

}