<commit_before>var m = require('mithril');
var chessground = require('chessground');
var classSet = chessground.util.classSet;
var partial = chessground.util.partial;
var util = require('./util');
var defined = util.defined;
var empty = util.empty;
var game = require('game').game;
var renderStatus = require('game').view.status;
var mod = require('game').view.mod;
var router = require('game').router;
var tree = require('./tree/tree');
var treePath = require('./tree/path');
var control = require('./control');
var actionMenu = require('./actionMenu').view;
var renderPromotion = require('./promotion').view;
var pgnExport = require('./pgnExport');
var forecastView = require('./forecast/forecastView');
var cevalView = require('./ceval/cevalView');
var crazyView = require('./crazy/crazyView');
var explorerView = require('./explorer/explorerView');
var raf = require('chessground').util.requestAnimationFrame;

function renderEvalTag(e) {
  return {
    tag: 'eval',
    children: [e]
  };
}

function autoScroll(el) {
  return util.throttle(300, false, function() {
    raf(function() {
      var plyEl = el.querySelector('.active') || el.querySelector('turn:first-child');
      if (plyEl) el.scrollTop = plyEl.offsetTop - el.offsetHeight / 2 + plyEl.offsetHeight / 2;
    });
  });
}

var emptyMove = m('move.empty', '...');
var nullMove = m('move.empty', '');

function renderMove(ctrl, node, path, isMainline) {
  if (!node) return emptyMove;
  var eval = isMainline ? (node.eval || node.ceval || {}) : {};
  var attrs = isMainline ? {} : {
    'data-path': path
  };
  var classes = path === ctrl.vm.path ? ['active'] : [];
  if (path === ctrl.vm.initialPath) classes.push('current');
  if (classes.length) attrs.class = classes.join(' ');
  return {
    tag: 'move',
    attrs: attrs,
    children: [
      defined(eval.cp) ? renderEvalTag(util.renderEval(eval.cp)) : (
        defined(eval.mate) ? renderEvalTag('#' + eval.mate) : null
      ),
      node.san[0] === 'P' ? node.san.slice(1) : node.san
    ]
  };
}

function plyToTurn(ply) {
  return Math.floor((ply - 1) / 2) + 1;
}

function renderVariation(ctrl, node, parent, klass, depth) {
  var path = parent.path + node.id;
  var showMenu = ctrl.vm.variationMenu && ctrl.vm.variationMenu === path;
  var visiting = treePath.contains(path, ctrl.vm.path);
  return m('div', {
    class: klass + ' variation ' + (showMenu ? ' menu' : '') + (visiting ? ' visiting' : '')
  }, [
    m('span', {
      class: 'menu',
      'data-icon': showMenu ? 'L' : '',
      onclick: partial(ctrl.toggleVariationMenu, path)
    }),
    showMenu ? (function() {
      var promotable = util.synthetic(ctrl.data) || !parent.node.fixed;
      return [
        m('a', {
          class: 'delete text',
          'data-icon': 'q',
          onclick: partial(ctrl.deleteVariation, path)
        }, 'Delete variation'),
        promotable ? m('a', {
          class: 'promote text',
          'data-icon': 'E',
          onclick: partial(ctrl.promoteVariation, path)
        }, 'Promote to main line') : null
      ];
    })() :
    renderVariationContent(ctrl, node, path, visiting)
  ]);
}

function renderVariationNested(ctrl, variation, path) {
  return m('span.variation', [
    '(',
    renderVariationContent(ctrl, variation, path, treePath.contains(path, ctrl.vm.path)),
    ')'
  ]);
}

function renderVariationContent(ctrl, node, path, full) {
  var turns = [];
  var line = tree.ops.mainlineNodeList(node);
  if (node.ply % 2 === 0) {
    line = line.slice(0);
    var first = line.shift();
    turns.push({
      turn: plyToTurn(first.ply),
      black: first
    });
  }
  var maxPlies = Math.min(full ? 999 : (path[2] ? 2 : 4), line.length);
  for (i = 0; i < maxPlies; i += 2) turns.push({
    turn: plyToTurn(line[i].ply),
    white: line[i],
    black: line[i + 1]
  });
  return turns.map(function(turn) {
    return renderVariationTurn(ctrl, turn, path);
  });
}

function renderVariationMeta(ctrl, move, path) {
  if (!move || empty(move.variations)) return;
  return move.variations.map(function(variation, i) {
    return renderVariationNested(ctrl, variation, treePath.withVariation(path, i + 1));
  });
}

function renderVariationTurn(ctrl, turn, path) {
  var wPath = turn.white ? treePath.withPly(path, turn.white.ply) : null;
  var wMove = wPath ? renderMove(ctrl, turn.white, wPath) : null;
  var wMeta = renderVariationMeta(ctrl, turn.white, wPath);
  var bPath = turn.black ? treePath.withPly(path, turn.black.ply) : null;
  var bMove = bPath ? renderMove(ctrl, turn.black, bPath) : null;
  var bMeta = renderVariationMeta(ctrl, turn.black, bPath);
  if (wMove) {
    if (wMeta) return [
      renderIndex(turn.turn + '.'),
      wMove,
      wMeta,
      bMove ? [
        bMove,
        bMeta
      ] : null
    ];
    return [renderIndex(turn.turn + '.'), wMove, (bMove ? [' ', bMove, bMeta] : '')];
  }
  return [renderIndex(turn.turn + '...'), bMove, bMeta];
}

function renderCommentOpening(ctrl, opening) {
  return m('div.comment.opening', opening.eco + ' ' + opening.name);
}

function renderMeta(ctrl, node, path) {
  var opening = ctrl.data.game.opening;
  opening = (node && opening && opening.ply === node.ply) ? renderCommentOpening(ctrl, opening) : null;<commit_msg>fix analysis variation display<commit_after>var m = require('mithril');
var chessground = require('chessground');
var classSet = chessground.util.classSet;
var partial = chessground.util.partial;
var util = require('./util');
var defined = util.defined;
var empty = util.empty;
var game = require('game').game;
var renderStatus = require('game').view.status;
var mod = require('game').view.mod;
var router = require('game').router;
var treeOps = require('./tree/ops');
var treePath = require('./tree/path');
var control = require('./control');
var actionMenu = require('./actionMenu').view;
var renderPromotion = require('./promotion').view;
var pgnExport = require('./pgnExport');
var forecastView = require('./forecast/forecastView');
var cevalView = require('./ceval/cevalView');
var crazyView = require('./crazy/crazyView');
var explorerView = require('./explorer/explorerView');
var raf = require('chessground').util.requestAnimationFrame;

function renderEvalTag(e) {
  return {
    tag: 'eval',
    children: [e]
  };
}

function autoScroll(el) {
  return util.throttle(300, false, function() {
    raf(function() {
      var plyEl = el.querySelector('.active') || el.querySelector('turn:first-child');
      if (plyEl) el.scrollTop = plyEl.offsetTop - el.offsetHeight / 2 + plyEl.offsetHeight / 2;
    });
  });
}

var emptyMove = m('move.empty', '...');
var nullMove = m('move.empty', '');

function renderMove(ctrl, node, path, isMainline) {
  if (!node) return emptyMove;
  var eval = isMainline ? (node.eval || node.ceval || {}) : {};
  var attrs = isMainline ? {} : {
    'data-path': path
  };
  var classes = path === ctrl.vm.path ? ['active'] : [];
  if (path === ctrl.vm.initialPath) classes.push('current');
  if (classes.length) attrs.class = classes.join(' ');
  return {
    tag: 'move',
    attrs: attrs,
    children: [
      defined(eval.cp) ? renderEvalTag(util.renderEval(eval.cp)) : (
        defined(eval.mate) ? renderEvalTag('#' + eval.mate) : null
      ),
      node.san[0] === 'P' ? node.san.slice(1) : node.san
    ]
  };
}

function plyToTurn(ply) {
  return Math.floor((ply - 1) / 2) + 1;
}

function renderVariation(ctrl, node, parent, klass, depth) {
  var path = parent.path + node.id;
  var showMenu = ctrl.vm.variationMenu && ctrl.vm.variationMenu === path;
  var visiting = treePath.contains(ctrl.vm.path, path);
  return m('div', {
    class: klass + ' variation ' + (showMenu ? ' menu' : '') + (visiting ? ' visiting' : '')
  }, [
    m('span', {
      class: 'menu',
      'data-icon': showMenu ? 'L' : '',
      onclick: partial(ctrl.toggleVariationMenu, path)
    }),
    showMenu ? (function() {
      var promotable = util.synthetic(ctrl.data) || !parent.node.fixed;
      return [
        m('a', {
          class: 'delete text',
          'data-icon': 'q',
          onclick: partial(ctrl.deleteVariation, path)
        }, 'Delete variation'),
        promotable ? m('a', {
          class: 'promote text',
          'data-icon': 'E',
          onclick: partial(ctrl.promoteVariation, path)
        }, 'Promote to main line') : null
      ];
    })() :
    renderVariationContent(ctrl, node, parent.path, visiting)
  ]);
}

function renderVariationNested(ctrl, variation, path) {
  return m('span.variation', [
    '(',
    renderVariationContent(ctrl, variation, path, treePath.contains(path, ctrl.vm.path)),
    ')'
  ]);
}

function renderVariationContent(ctrl, node, path, full) {
  var turns = [];
  var line = treeOps.mainlineNodeList(node);
  if (node.ply % 2 === 0) {
    line = line.slice(0);
    var first = line.shift();
    turns.push({
      turn: plyToTurn(first.ply),
      black: first
    });
  }
  var maxPlies = Math.min(full ? 999 : 4, line.length);
  for (i = 0; i < maxPlies; i += 2) turns.push({
    turn: plyToTurn(line[i].ply),
    white: line[i],
    black: line[i + 1]
  });
  var dom;
  return turns.map(function(turn) {
    dom = renderVariationTurn(ctrl, turn, path);
    path += (turn.white && turn.white.id || '') + (turn.black && turn.black.id || '');
    return dom;
  });
}

function renderVariationMeta(ctrl, move, path) {
  if (!move || empty(move.variations)) return;
  return move.variations.map(function(variation, i) {
    return renderVariationNested(ctrl, variation, treePath.withVariation(path, i + 1));
  });
}

function renderVariationTurn(ctrl, turn, path) {
  var wPath = turn.white ? path + turn.white.id : null;
  var wMove = wPath ? renderMove(ctrl, turn.white, wPath) : null;
  var wMeta = renderVariationMeta(ctrl, turn.white, wPath);
  var bPath = turn.black ? (wPath || path) + turn.black.id : null;
  var bMove = bPath ? renderMove(ctrl, turn.black, bPath) : null;
  var bMeta = renderVariationMeta(ctrl, turn.black, bPath);
  if (wMove) {
    if (wMeta) return [
      renderIndex(turn.turn + '.'),
      wMove,
      wMeta,
      bMove ? [
        bMove,
        bMeta
      ] : null
    ];
    return [renderIndex(turn.turn + '.'), wMove, (bMove ? [' ', bMove, bMeta] : '')];
  }
  return [renderIndex(turn.turn + '...'), bMove, bMeta];
}

function renderCommentOpening(ctrl, opening) {
  return m('div.comment.opening', opening.eco + ' ' + opening.name);
}

function renderMeta(ctrl, node, path) {
  var opening = ctrl.data.game.opening;
  opening = (node && opening && opening.ply === node.ply) ? renderCommentOpening(ctrl, opening) : null;
{'fraction_non_alphanumeric': 0.17447450278065793, 'fraction_numerical': 0.004147422000188519, 'mean_word_length': 3.337694194603434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5180420', 'n_tokens_mistral': 3577, 'n_tokens_neox': 3534, 'n_words': 796}