<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';

import {
  ICellModel, IRawCellModel, IMarkdownCellModel, ICodeCellModel,
  isRawCellModel, isCodeCellModel, isMarkdownCellModel
} from '../cells';

import {
  INotebookContent, ICell, MAJOR_VERSION, MINOR_VERSION,
  IRawCell, ICodeCell, IMarkdownCell, isRawCell, isMarkdownCell,
  isCodeCell
} from './nbformat';

import {
  INotebookModel
} from './model';


/**
 * Serialize a notebook model.
 */
export 
function serialize(nb: INotebookModel): INotebookContent {
  let cells: ICell[] = [];
  for (let i = 0; i < nb.cells.length; i++) {
    let cell = nb.cells.get(i);
    cells.push(serializeCell(cell));
  }
  return {
    cells: cells,
    metadata: nb.metadata, 
    nbformat: MAJOR_VERSION, 
    nbformat_minor: MINOR_VERSION 
  };
}

/**
 * Deserialize notebook content into a model.
 */
export
function deserialize(data: INotebookContent, model: INotebookModel): void {
  model.cells.clear();
  let cell: ICellModel;

  // Iterate through the cell data, creating cell models.
  data.cells.forEach(c => {
    if (isMarkdownCell(c)) {
      cell = model.createMarkdownCell();
    } else if (isCodeCell(c)) {
      cell = model.createCodeCell();
    } else if (isRawCell(c)) {
      cell = model.createRawCell();
    }
    deserializeCell(c, cell);
    model.cells.add(cell);
  });
  
  if (!model.cells.length) {
    cell = model.createCodeCell();
    model.cells.add(cell);
  }
  model.activeCellIndex = 0;
  model.metadata = data.metadata;
}


/**
 * Serialize a cell model.
 */
function
serializeCell(cell: ICellModel): ICell {
  let output: ICell = {
    source: cell.input.textEditor.text,
    cell_type: cell.type,
    metadata: { trusted: cell.trusted }
  }
  if (cell.tags) {
    output.metadata.tags = cell.tags;
  }
  if (cell.name) {
    output.metadata.name = cell.name;
  }
  if (isRawCellModel(cell)) {
    (output as IRawCell).metadata.format = (cell as IRawCellModel).format;
  } else if (isCodeCellModel(cell)) {
    let out = output as ICodeCell;
    out.metadata.scrolled = cell.scrolled;
    out.metadata.collapsed = cell.collapsed;
    out.outputs = [];
    for (let i = 0; i < cell.output.outputs.length; i++) {
       out.outputs.push(cell.output.outputs.get(i));
    }
    out.execution_count = cell.executionCount;
  }
  return output;
}


/**
 * Deserialize cell data.
 */
function deserializeCell(data: ICell, model: ICellModel): void {
  let source = data.source as string;
  if (Array.isArray(data.source)) {
    source = (data.source as string[]).join('\n');
  }
  model.input.textEditor.text = source;
  model.tags = data.metadata.tags;
  model.name = data.metadata.name;
  model.trusted = data.metadata.trusted;

  if (isCodeCellModel(model)) {<commit_msg>Export the cell serialization functions<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';

import {
  ICellModel, IRawCellModel, IMarkdownCellModel, ICodeCellModel,
  isRawCellModel, isCodeCellModel, isMarkdownCellModel
} from '../cells';

import {
  INotebookContent, ICell, MAJOR_VERSION, MINOR_VERSION,
  IRawCell, ICodeCell, IMarkdownCell, isRawCell, isMarkdownCell,
  isCodeCell
} from './nbformat';

import {
  INotebookModel
} from './model';


/**
 * Serialize a notebook model.
 */
export 
function serialize(nb: INotebookModel): INotebookContent {
  let cells: ICell[] = [];
  for (let i = 0; i < nb.cells.length; i++) {
    let cell = nb.cells.get(i);
    cells.push(serializeCell(cell));
  }
  return {
    cells: cells,
    metadata: nb.metadata, 
    nbformat: MAJOR_VERSION, 
    nbformat_minor: MINOR_VERSION 
  };
}

/**
 * Deserialize notebook content into a model.
 */
export
function deserialize(data: INotebookContent, model: INotebookModel): void {
  model.cells.clear();
  let cell: ICellModel;

  // Iterate through the cell data, creating cell models.
  data.cells.forEach(c => {
    if (isMarkdownCell(c)) {
      cell = model.createMarkdownCell();
    } else if (isCodeCell(c)) {
      cell = model.createCodeCell();
    } else if (isRawCell(c)) {
      cell = model.createRawCell();
    }
    deserializeCell(c, cell);
    model.cells.add(cell);
  });
  
  if (!model.cells.length) {
    cell = model.createCodeCell();
    model.cells.add(cell);
  }
  model.activeCellIndex = 0;
  model.metadata = data.metadata;
}


/**
 * Serialize a cell model.
 */
export
function serializeCell(cell: ICellModel): ICell {
  let output: ICell = {
    source: cell.input.textEditor.text,
    cell_type: cell.type,
    metadata: { trusted: cell.trusted }
  }
  if (cell.tags) {
    output.metadata.tags = cell.tags;
  }
  if (cell.name) {
    output.metadata.name = cell.name;
  }
  if (isRawCellModel(cell)) {
    (output as IRawCell).metadata.format = (cell as IRawCellModel).format;
  } else if (isCodeCellModel(cell)) {
    let out = output as ICodeCell;
    out.metadata.scrolled = cell.scrolled;
    out.metadata.collapsed = cell.collapsed;
    out.outputs = [];
    for (let i = 0; i < cell.output.outputs.length; i++) {
       out.outputs.push(cell.output.outputs.get(i));
    }
    out.execution_count = cell.executionCount;
  }
  return output;
}


/**
 * Deserialize cell data.
 */
export 
function deserializeCell(data: ICell, model: ICellModel): void {
  let source = data.source as string;
  if (Array.isArray(data.source)) {
    source = (data.source as string[]).join('\n');
  }
  model.input.textEditor.text = source;
  model.tags = data.metadata.tags;
  model.name = data.metadata.name;
  model.trusted = data.metadata.trusted;

  if (isCodeCellModel(model)) {
{'fraction_non_alphanumeric': 0.13546236181786278, 'fraction_numerical': 0.001052816283558519, 'mean_word_length': 3.3846153846153846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '485081', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1924, 'n_words': 440}