<commit_before>/* 
  margin
*/
var phantom = require('phantom'),
  fs = require('fs'),
  handlebars = require('handlebars'),
  marked = require('marked'),
  readOptions = { "encoding": "utf-8" },
  templateFolder = "template";

/*
 * Read in cover template
 * Render cover + data JSON through handlebars
 * Turn pages.md into HTML
 * Add pages.md to end of cover.html
 * Convert HTML into PDF
 */

phantom.create(function(ph){
  ph.createPage(function(page) {
    var templateFilename = __dirname+"/"+templateFolder+"/cover.html";
    fs.readFile(templateFilename, readOptions, function(err, source) {
      var template = handlebars.compile(source);
      
      var dataJSONFileName = __dirname+"/data.json";
      fs.readFile(dataJSONFileName, readOptions, function(err, data) {
    
        var jsonData = JSON.parse(data);
        var result = template(jsonData);
        var filename = __dirname+"/"+templateFolder+"/temp.html";
        
        var pagesFileName = __dirname+"/pages.md";
        
        fs.readFile(pagesFileName, readOptions, function(err, data) {
          var pagesHTML = marked(data);
          var bits = result.split('</body>');
          // wrap pages HTML in a wrapper and insert before the end of the body
          bits.splice(1,0,'<div class="wrapper">',pagesHTML,'</div></body>');
          var htmlForRenderingToPDF = bits.join("");

          fs.writeFile(filename, htmlForRenderingToPDF, function() {
            page.open(filename, function(status) {
              page.set('viewportSize', {width:1280,height:1800}, function(result) {
                page.render('pages.pdf', function(){
                  console.log('Page Rendered');
                  ph.exit();
                  fs.unlink(__dirname+"/"+templateFolder+"/temp.html", function(err) {
                    console.log('Finished');
                  });
                });        
              });
            });
          });
        });
      });
    });
  });
});<commit_msg>Changing margin to be a module. Split out the HTML-rendering part into a separate function<commit_after>/* 
  margin
*/
var phantom = require('phantom'),
  fs = require('fs'),
  path = require('path'),
  handlebars = require('handlebars'),
  marked = require('marked'),
  readOptions = { "encoding": "utf-8" };


var createPdfFromHTML = function(pdfPath, html, callback) {
  var tempFilename = path.join(__dirname,"marginTemporaryHTMLFile.html");
  if(!pdfPath) {
    pdfPath = path.join(folderPath,"pages.pdf");
  }
  if(!html) {
    html = '';
  }
  // write the HTML to a temporary file
  fs.writeFile(tempFilename, html, function() {
    // open the temp file in phantom
    page.open(tempFilename, function(status) {
      page.set('viewportSize', {width:1280,height:1800}, function(result) {
        // create the PDF
        page.render(pdfPath, function(){
          console.log('Page Rendered');
          ph.exit();
          fs.unlink(tempFilename, function(err) {
            console.log('Finished');
            if(callback) {
              callback(err);
            }
          });
        });
      });
    });
  });
});

/*
 * Create a multi-page PDF with a cover template
 *
 * Read in cover template
 * Render cover + data JSON through handlebars
 * Turn pages.md into HTML
 * Add pages.md to end of cover.html
 * Convert HTML into PDF
 */
var createPdfFromFolder = function(folderPath, pdfPath, callback) {
  if(!folderPath) {
    folderPath = __dirname;
  }
  var coverTemplateFilename = path.join(folderPath,"template/cover.html"),
    dataJSONFileName = path.join(folderPath,"data.json"),
    pagesFileName = path.join(folderPath,"pages.md");
    
  phantom.create(function(ph){
    ph.createPage(function(page) {
      // read in the cover template
      fs.readFile(coverTemplateFilename, readOptions, function(err, source) {
        var template = handlebars.compile(source);
        // read in the JSON data for the cover
        fs.readFile(dataJSONFileName, readOptions, function(err, data) {
          var jsonData = JSON.parse(data),
            result = template(jsonData);
          // read in the markdown document
          fs.readFile(pagesFileName, readOptions, function(err, data) {
            // convert markdown to HTML
            var pagesHTML = marked(data),
              bits = result.split('</body>'),
              htmlForRenderingToPDF;
            // wrap pages HTML in a wrapper and insert before the end of the body
            bits.splice(1,0,'<div class="wrapper">',pagesHTML,'</div></body>');
            htmlForRenderingToPDF = bits.join("");
            // create the PDF
            createPdfFromHTML(pdfPath, htmlForRenderingToPDF, callback);
          });
        });
      });
    });
  });  
};

module.exports = {
  createPdfFromHTML: createPdfFromHTML,
  createPdfFromFolder: createPdfFromFolder
};
{'fraction_non_alphanumeric': 0.13466694163745102, 'fraction_numerical': 0.004537017941843679, 'mean_word_length': 2.504335260115607, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4160881', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1351, 'n_words': 319}