<commit_before>Flow = if exports? then exports else @Flow = {}

marked.setOptions
  smartypants: yes
  highlight: (code, lang) ->
    if window.hljs
      (window.hljs.highlightAuto code, [ lang ]).value
    else
      code

ko.bindingHandlers.cursorPosition =
  init: (element, valueAccessor, allBindings, viewModel, bindingContext) ->
    if arg = ko.unwrap valueAccessor()
      # Bit of a hack. Attaches a method to the bound object that returns the cursor position. Uses dwieeb/jquery-textrange.
      arg.read = -> $(element).textrange 'get', 'position'
    return

Flow.Application = (_) ->
  _view = Flow.Repl _
  Flow.DialogManager _
  
  context: _
  view: _view

Flow.ApplicationContext = (_) ->
  context$
    ready: do edges$

Flow.DialogManager = (_) ->

Flow.HtmlTag = (_, level) ->
  isCode: no
  render: (input, go) ->
    go null,
      text: input.trim() or '(Untitled)'
      template: "flow-#{level}"

Flow.Raw = (_) ->
  isCode: no
  render: (input, go) ->
    go null,
      text: input
      template: 'flow-raw'

Flow.Markdown = (_) ->
  isCode: no
  render: (input, go) ->
    try
      html = marked input.trim() or '(No content)'
      go null,
        html: html
        template: 'flow-md'
    catch error
      go error

Flow.Coffeescript = (_) ->
  render: (input, go) ->

Flow.Repl = (_) ->
  _cells = nodes$ []
  _selectedCell = null
  _selectedCellIndex = -1
  _clipboardCell = null
  _lastDeletedCell = null
  _renderers =
    h1: -> Flow.HtmlTag _, 'h1'
    h2: -> Flow.HtmlTag _, 'h2'
    h3: -> Flow.HtmlTag _, 'h3'
    h4: -> Flow.HtmlTag _, 'h4'
    h5: -> Flow.HtmlTag _, 'h5'
    h6: -> Flow.HtmlTag _, 'h6'
    md: -> Flow.Markdown _
    cs: -> Flow.Coffeescript _
    raw: -> Flow.Raw _

  countLines = (text) ->
    newlineCount = 1
    for character in text when character is '\n'
      newlineCount++
    newlineCount

  checkConsistency = ->
    for cell, i in _cells()
      unless cell
        error "index #{i} is empty"
    return

  selectCell = (target) ->
    return if _selectedCell is target
    _selectedCell.isSelected no if _selectedCell
    _selectedCell = target
    _selectedCell.isSelected yes
    _selectedCellIndex = _cells.indexOf _selectedCell
    checkConsistency()
    return

  createCell = (type='cs', input='') ->
    _type = node$ type
    _renderer = lift$ _type, (type) -> _renderers[type]()
    _isSelected = node$ no
    _isActive = node$ no
    _hasError = node$ no
    _isBusy = node$ no
    _isReady = lift$ _isBusy, (isBusy) -> not isBusy
    _hasInput = node$ yes
    _input = node$ input
    _output = node$ null
    _lineCount = lift$ _input, countLines

    # This is a shim.
    # The ko 'cursorPosition' custom binding attaches a read() method to this.
    _cursorPosition = {}

    # select and display input when activated
    apply$ _isActive, (isActive) ->
      selectCell self if isActive
      _hasInput yes if isActive

    # deactivate when deselected
    apply$ _isSelected, (isSelected) ->
      _isActive no unless isSelected

    select = -> selectCell self

    execute = (go) ->
      renderer = _renderer()
      _isBusy yes
      renderer.render _input(), (error, result) ->
        if error
          _hasError yes<commit_msg>hide html content when html-based cells are being edited (ipython behavior)<commit_after>Flow = if exports? then exports else @Flow = {}

marked.setOptions
  smartypants: yes
  highlight: (code, lang) ->
    if window.hljs
      (window.hljs.highlightAuto code, [ lang ]).value
    else
      code

ko.bindingHandlers.cursorPosition =
  init: (element, valueAccessor, allBindings, viewModel, bindingContext) ->
    if arg = ko.unwrap valueAccessor()
      # Bit of a hack. Attaches a method to the bound object that returns the cursor position. Uses dwieeb/jquery-textrange.
      arg.read = -> $(element).textrange 'get', 'position'
    return

Flow.Application = (_) ->
  _view = Flow.Repl _
  Flow.DialogManager _
  
  context: _
  view: _view

Flow.ApplicationContext = (_) ->
  context$
    ready: do edges$

Flow.DialogManager = (_) ->

Flow.HtmlTag = (_, level) ->
  isCode: no
  render: (input, go) ->
    go null,
      text: input.trim() or '(Untitled)'
      template: "flow-#{level}"

Flow.Raw = (_) ->
  isCode: no
  render: (input, go) ->
    go null,
      text: input
      template: 'flow-raw'

Flow.Markdown = (_) ->
  isCode: no
  render: (input, go) ->
    try
      html = marked input.trim() or '(No content)'
      go null,
        html: html
        template: 'flow-md'
    catch error
      go error

Flow.Coffeescript = (_) ->
  render: (input, go) ->

Flow.Repl = (_) ->
  _cells = nodes$ []
  _selectedCell = null
  _selectedCellIndex = -1
  _clipboardCell = null
  _lastDeletedCell = null
  _renderers =
    h1: -> Flow.HtmlTag _, 'h1'
    h2: -> Flow.HtmlTag _, 'h2'
    h3: -> Flow.HtmlTag _, 'h3'
    h4: -> Flow.HtmlTag _, 'h4'
    h5: -> Flow.HtmlTag _, 'h5'
    h6: -> Flow.HtmlTag _, 'h6'
    md: -> Flow.Markdown _
    cs: -> Flow.Coffeescript _
    raw: -> Flow.Raw _

  countLines = (text) ->
    newlineCount = 1
    for character in text when character is '\n'
      newlineCount++
    newlineCount

  checkConsistency = ->
    for cell, i in _cells()
      unless cell
        error "index #{i} is empty"
    return

  selectCell = (target) ->
    return if _selectedCell is target
    _selectedCell.isSelected no if _selectedCell
    _selectedCell = target
    _selectedCell.isSelected yes
    _selectedCellIndex = _cells.indexOf _selectedCell
    checkConsistency()
    return

  createCell = (type='cs', input='') ->
    _type = node$ type
    _renderer = lift$ _type, (type) -> _renderers[type]()
    _isSelected = node$ no
    _isActive = node$ no
    _hasError = node$ no
    _isBusy = node$ no
    _isReady = lift$ _isBusy, (isBusy) -> not isBusy
    _hasInput = node$ yes
    _input = node$ input
    _output = node$ null
    _lineCount = lift$ _input, countLines

    # This is a shim.
    # The ko 'cursorPosition' custom binding attaches a read() method to this.
    _cursorPosition = {}

    # select and display input when activated
    apply$ _isActive, (isActive) ->
      if isActive
        selectCell self
        _hasInput yes
        _output null if _renderer().isCode is no
      return

    # deactivate when deselected
    apply$ _isSelected, (isSelected) ->
      _isActive no unless isSelected

    select = -> selectCell self

    execute = (go) ->
      renderer = _renderer()
      _isBusy yes
      renderer.render _input(), (error, result) ->
        if error
          _hasError yes
{'fraction_non_alphanumeric': 0.11414693381906496, 'fraction_numerical': 0.004250151791135397, 'mean_word_length': 2.486243386243386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '357909', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2157, 'n_words': 630}