<commit_before>import * as path from 'path';
import * as fs from 'fs';
import * as ejs from 'ejs';
import converters, { Converters } from '../converters';
import visit = require('unist-util-visit');

export default class LaTeXCompiler {

  public footnotes: any[] = [];
  public definitions: any = {};
  public converters: Converters = converters;

  constructor(
    public file: any,
    public options: any = {},
  ) {
    if (file.extension) {
      file.move({
        extension: 'tex',
      });
    }
    if (file.extname) {
      file.extname = '.tex';
    }

    this.options.templatesDir =
      this.options.templatesDir || path.join(__dirname, '../templates');
  }

  compile(
    node: any,
  ) {
    visit(node, 'definition', (def: any) => {
      const id = def.identifier.toUpperCase();
      this.definitions[id] = def;
    });
    visit(node, 'footnoteDefinition', (def: any) => {
      this.footnotes.push(def);
    });

    return this.visit(node, null) + '\n' + this.generateFootnotes();
  }

  visit(
    node: any,
    parent: any,
    idx: number = 0,
  ) {
    const type: string = (node) ? node.type : '';
    if (!type) {
      this.file.fail(`Expected node \`${node}\``);
    }

    let cloneNode = Object.assign({}, node, {
      index: idx,
      isFirst: (idx === 0),
      isLast: (parent) ? (idx === parent.children.length - 1) : true,
    });<commit_msg>Support template file<commit_after>import * as path from 'path';
import * as fs from 'fs';
import * as ejs from 'ejs';
import converters, { Converters } from '../converters';
import visit = require('unist-util-visit');

export default class LaTeXCompiler {

  public footnotes: any[] = [];
  public definitions: any = {};
  public converters: Converters = converters;

  constructor(
    public file: any,
    public options: any = {},
  ) {
    if (file.extension) {
      file.move({
        extension: 'tex',
      });
    }
    if (file.extname) {
      file.extname = '.tex';
    }

    this.options.templatesDir =
      this.options.templatesDir || path.join(__dirname, '../templates');
  }

  compile(
    node: any,
  ) {
    visit(node, 'definition', (def: any) => {
      const id = def.identifier.toUpperCase();
      this.definitions[id] = def;
    });
    visit(node, 'footnoteDefinition', (def: any) => {
      this.footnotes.push(def);
    });

    const compiled = this.visit(node, null) + '\n' + this.generateFootnotes();
    if (!this.options.baseTemplateFile) {
      return compiled;
    }

    try {
      const template = fs.readFileSync(this.options.baseTemplateFile, 'utf8');
      const data =
        Object.assign({}, this.options.documentInfo || {}, { body: compiled });
      return ejs.render(template, data, <any> { escape: (text: string) => text });
    } catch (_e) {
      console.error(_e.message || _e);
      return compiled;
    }
  }

  visit(
    node: any,
    parent: any,
    idx: number = 0,
  ) {
    const type: string = (node) ? node.type : '';
    if (!type) {
      this.file.fail(`Expected node \`${node}\``);
    }

    let cloneNode = Object.assign({}, node, {
      index: idx,
      isFirst: (idx === 0),
      isLast: (parent) ? (idx === parent.children.length - 1) : true,
    });
{'fraction_non_alphanumeric': 0.168424317617866, 'fraction_numerical': 0.0021712158808933004, 'mean_word_length': 2.5674778761061945, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '6509304', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1034, 'n_words': 256}