<commit_before>'use strict';

const fs = require('fs');
const path = require('path');
const SVGO = require('svgo');
const dot = require('dot');
const measureTextWidth = require('./measure-text.js');

// cache templates.
const templates = {};
const templateFiles = fs.readdirSync(path.join(__dirname, '..', 'templates'));
dot.templateSettings.strip = false;  // Do not strip whitespace.
templateFiles.forEach(function(filename) {
  if (filename[0] === '.') { return; }
  const templateData = fs.readFileSync(
    path.join(__dirname, '..', 'templates', filename)).toString();
  const extension = path.extname(filename).slice(1);
  const style = filename.slice(0, -(('-template.' + extension).length));
  // Compile the template. Necessary to always have a working template.
  templates[style + '-' + extension] = dot.template(templateData);
  if (extension === 'svg') {
    // Substitute dot code.
    const mapping = new Map();
    let mappingIndex = 1;
    const untemplatedSvg = templateData.replace(/{{.*?}}/g, function(match) {
      // Weird substitution that currently works for all templates.
      const mapKey = '99999990' + mappingIndex + '.1';
      mappingIndex++;
      mapping.set(mapKey, match);
      return mapKey;
    });
    compressSvg(untemplatedSvg, function(object) {
      if (object.error !== undefined) {
        console.error('Template ' + filename + ': ' + object.error + '\n' +
          '  Generated untemplated SVG:\n' +
          '---\n' + untemplatedSvg + '---\n');
        return;
      }
      // Substitute dot code back.
      let svg = object.data;
      const unmappedKeys = [];
      mapping.forEach(function(value, key) {
        let keySubstituted = false;
        svg = svg.replace(RegExp(key, 'g'), function() {
          keySubstituted = true;
          return value;
        });
        if (!keySubstituted) {
          unmappedKeys.push(key);
        }
      });
      if (unmappedKeys.length > 0) {
        console.error('Template ' + filename + ' has unmapped keys ' +
          unmappedKeys.join(', ') + '.\n' +
          '  Generated untemplated SVG:\n' +
          '---\n' + untemplatedSvg + '\n---\n' +
          '  Generated template:\n' +
          '---\n' + svg + '\n---\n');
        return;
      }
      templates[style + '-' + extension] = dot.template(svg);
    });
  }
});

function escapeXml(s) {
  return s.replace(/&/g, '&amp;')
          .replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/"/g, '&quot;')
          .replace(/'/g, '&apos;');
}
function capitalize(s) {
  return s[0].toUpperCase() + s.slice(1);
}
function addEscapers(data) {
  data.escapeXml = escapeXml;<commit_msg>Use template strings in make-badge (#1296)<commit_after>'use strict';

const fs = require('fs');
const path = require('path');
const SVGO = require('svgo');
const dot = require('dot');
const measureTextWidth = require('./measure-text.js');

// cache templates.
const templates = {};
const templateFiles = fs.readdirSync(path.join(__dirname, '..', 'templates'));
dot.templateSettings.strip = false;  // Do not strip whitespace.
templateFiles.forEach(function(filename) {
  if (filename[0] === '.') { return; }
  const templateData = fs.readFileSync(
    path.join(__dirname, '..', 'templates', filename)).toString();
  const extension = path.extname(filename).slice(1);
  const style = filename.slice(0, -(('-template.' + extension).length));
  // Compile the template. Necessary to always have a working template.
  templates[style + '-' + extension] = dot.template(templateData);
  if (extension === 'svg') {
    // Substitute dot code.
    const mapping = new Map();
    let mappingIndex = 1;
    const untemplatedSvg = templateData.replace(/{{.*?}}/g, function(match) {
      // Weird substitution that currently works for all templates.
      const mapKey = '99999990' + mappingIndex + '.1';
      mappingIndex++;
      mapping.set(mapKey, match);
      return mapKey;
    });
    compressSvg(untemplatedSvg, function(object) {
      if (object.error !== undefined) {
        console.error(`Template ${filename}: ${object.error}\n` +
          '  Generated untemplated SVG:\n' +
          `---\n${untemplatedSvg}---\n`);
        return;
      }
      // Substitute dot code back.
      let svg = object.data;
      const unmappedKeys = [];
      mapping.forEach(function(value, key) {
        let keySubstituted = false;
        svg = svg.replace(RegExp(key, 'g'), function() {
          keySubstituted = true;
          return value;
        });
        if (!keySubstituted) {
          unmappedKeys.push(key);
        }
      });
      if (unmappedKeys.length > 0) {
        console.error(`Template ${filename} has unmapped keys ` +
          `${unmappedKeys.join(', ')}.\n` +
          '  Generated untemplated SVG:\n' +
          `---\n${untemplatedSvg}\n---\n` +
          '  Generated template:\n' +
          `---\n${svg}\n---\n`);
        return;
      }
      templates[style + '-' + extension] = dot.template(svg);
    });
  }
});

function escapeXml(s) {
  return s.replace(/&/g, '&amp;')
          .replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/"/g, '&quot;')
          .replace(/'/g, '&apos;');
}
function capitalize(s) {
  return s[0].toUpperCase() + s.slice(1);
}
function addEscapers(data) {
  data.escapeXml = escapeXml;
{'fraction_non_alphanumeric': 0.17786931286275978, 'fraction_numerical': 0.006740310803220371, 'mean_word_length': 2.54479097544791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2593991', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1675, 'n_words': 367}