<commit_before>$ = require 'jquery'
_ = require 'underscore'
{View, $$, $$$} = require 'space-pen'

coffee = require 'coffee-script'
beautifyHtml = require('js-beautify').html

Editor = require 'editor'

OverlaySelectListView = require './overlay-select-list-view'

_.extend View,
  exampleCode: (spacePenCoffee) ->
    # I need to pass the proper context to the spacepen functions, so I return
    # a fn, which I will apply after eval'ing. Magic!
    wrappedCode = """
      evaluator = ->
      #{('  '+line for line in spacePenCoffee.split('\n')).join('\n')}
      evaluator
    """

    # FIXME: Is this the best way to grab the html from spacepen?
    html = $$$ ->
      coffee.eval(wrappedCode).apply(this)

    @div class: 'example', =>
      @div class: 'example-rendered', =>
        @raw html

      @div class: 'example-code show-example-space-pen', =>
        @div class: 'btn-group btn-group-xs btn-toggle', =>
          @button class: 'btn selected', 'data-display-class': 'show-example-space-pen', 'space-pen'
          @button class: 'btn', 'data-display-class': 'show-example-html', 'html'
        @colorizedCodeBlock 'example-space-pen', 'source.coffee', spacePenCoffee
        @colorizedCodeBlock 'example-html', 'text.xml', beautifyHtml(html)

  colorizedCodeBlock: (cssClass, grammarScopeName, code) ->
    editorBlock = $$ ->
      @pre class: cssClass+' editor-colors editor', ''

    refreshHtml = (grammar) ->
      editorBlock.empty()
      for tokens in grammar.tokenizeLines(code)
        editorBlock.append(Editor.buildLineHtml({tokens, text: code}))

    if grammar = syntax.grammarForScopeName(grammarScopeName)
      refreshHtml(grammar)
    else
      syntax.on 'grammar-added grammar-updated', (grammar) ->
        return unless grammar.scopeName == grammarScopeName
        refreshHtml(grammar)

    @subview '__', editorBlock

    # TODO: maybe take this out. It might not add that much to the docs?
  exampleOverlaySelectList: (array) ->
    selectList = new OverlaySelectListView array, (item) ->
      $$ ->
        @li =>
          @raw item

    html = $('<div/>').append(selectList).html()

    coffeeScript = """<commit_msg>:lipstick:<commit_after>$ = require 'jquery'
_ = require 'underscore'
{View, $$, $$$} = require 'space-pen'

coffee = require 'coffee-script'
beautifyHtml = require('js-beautify').html

Editor = require 'editor'

OverlaySelectListView = require './overlay-select-list-view'

_.extend View,
  exampleCode: (spacePenCoffee) ->
    # I need to pass the proper context to the spacepen functions, so I return
    # a fn, which I will apply after eval'ing. Magic!
    wrappedCode = """
      evaluator = ->
      #{('  '+line for line in spacePenCoffee.split('\n')).join('\n')}
      evaluator
    """

    # FIXME: Is this the best way to grab the html from spacepen?
    html = $$$ ->
      coffee.eval(wrappedCode).apply(this)

    @div class: 'example', =>
      @div class: 'example-rendered', =>
        @raw html

      @div class: 'example-code show-example-space-pen', =>
        @div class: 'btn-group btn-group-xs btn-toggle', =>
          @button class: 'btn selected', 'data-display-class': 'show-example-space-pen', 'space-pen'
          @button class: 'btn', 'data-display-class': 'show-example-html', 'html'
        @colorizedCodeBlock 'example-space-pen', 'source.coffee', spacePenCoffee
        @colorizedCodeBlock 'example-html', 'text.xml', beautifyHtml(html)

  colorizedCodeBlock: (cssClass, grammarScopeName, code) ->
    editorBlock = $$ ->
      @pre class: cssClass+' editor-colors editor', ''

    refreshHtml = (grammar) ->
      editorBlock.empty()
      for tokens in grammar.tokenizeLines(code)
        editorBlock.append(Editor.buildLineHtml({tokens, text: code}))

    if grammar = syntax.grammarForScopeName(grammarScopeName)
      refreshHtml(grammar)
    else
      syntax.on 'grammar-added grammar-updated', (grammar) ->
        return unless grammar.scopeName == grammarScopeName
        refreshHtml(grammar)

    @subview '__', editorBlock

  # TODO: maybe take this out. It might not add that much to the docs?
  exampleOverlaySelectList: (array) ->
    selectList = new OverlaySelectListView array, (item) ->
      $$ ->
        @li =>
          @raw item

    html = $('<div/>').append(selectList).html()

    coffeeScript = """
{'fraction_non_alphanumeric': 0.14262635587352873, 'fraction_numerical': 0.0, 'mean_word_length': 3.077140169332079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '498037', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1320, 'n_words': 338}