<commit_before>- unless plugin_loaded(plugin.url)
  = content_for :external_scripts do
    %script{src:plugin.url}

- if plugin.version < 2
  -# Version 1
  .plugin-output{id: "output-#{plugin.id}"}
  :javascript
    // Begin script for #{plugin.name}
    $(document).ready( function() {
      env = {
        name: '#{plugin.name}',
        scriptLabel: '#{plugin.label}',
        scriptUrl: '#{plugin.url}',
        pluginId: '#{plugin.id}',
        config: '#{ escape_javascript(plugin.author_data) }',
        div: $('##{runtime_div}')
      }
      console.log("Adding script #{plugin.label} with V1 (ExternalScript) API");
      ExternalScripts.init('#{plugin.label}', env);

    });
- else
  -# Version 2
  - learner_state = PluginLearnerState.find_or_create(plugin, @run).state
  - if wrapped_embeddable
    -# Plugin is wrapping some other embeddable. It's injected into its div and gets question content.
    - runtime_div_selector = "#question-#{wrapped_embeddable.embeddable_id}"
    - wrapped_selector = "#question-#{wrapped_embeddable.embeddable_id} .question-container"
  - else
    -# Typical case. Plugin renders its own output.
    .plugin-output{id: "output-#{plugin.id}"}
    - runtime_div_selector = "#output-#{plugin.id}"
    - wrapped_selector = nil

  :javascript
    // Begin script for #{plugin.name}
    var savePath = `#{escape_javascript(api_v1_update_plugin_learner_state_path(plugin.id, @run.id))}`
    var loadPath = `#{escape_javascript(api_v1_show_plugin_learner_state_path(plugin.id, @run.id))}`
    var pluginStatePaths = {
      savePath: savePath,
      loadPath: loadPath
    };
    var learner_state  = '#{ escape_javascript(learner_state) }'
    $(document).ready( function() {
      env = {
        name: '#{plugin.name}',
        url: '#{plugin.url}',
        pluginId: '#{plugin.id}',
        authoredState: '#{ escape_javascript(plugin.author_data) }',
        learnerState: learner_state,
        div: $('#{runtime_div_selector}')[0],
        wrappedEmbeddableDiv: $('#{wrapped_selector}')[0],
        wrappedEmbeddableContext: #{wrapped_embeddable ? (LaraSerializationHelper.new).wrap_export(wrapped_embeddable).to_json : 'null'}
      }
      console.log("Adding script #{plugin.label} with V2 LARA Plugin API")
      Plugins.initPlugin('#{plugin.label}', env, pluginStatePaths);<commit_msg>Fix runtime div selector when V1 of Plugin API is used<commit_after>- unless plugin_loaded(plugin.url)
  = content_for :external_scripts do
    %script{src:plugin.url}

- if plugin.version < 2
  -# Version 1
  - output_id = "output-#{plugin.id}"
  .plugin-output{id: output_id}
  - runtime_div_selector = "##{output_id}"
  :javascript
    // Begin script for #{plugin.name}
    $(document).ready( function() {
      env = {
        name: '#{plugin.name}',
        scriptLabel: '#{plugin.label}',
        scriptUrl: '#{plugin.url}',
        pluginId: '#{plugin.id}',
        config: '#{ escape_javascript(plugin.author_data) }',
        div: $('#{runtime_div_selector}')[0]
      }
      console.log("Adding script #{plugin.label} with V1 (ExternalScript) API");
      ExternalScripts.init('#{plugin.label}', env);
    });
- else
  -# Version 2
  - learner_state = PluginLearnerState.find_or_create(plugin, @run).state
  - if wrapped_embeddable
    -# Plugin is wrapping some other embeddable. It's injected into its div and gets question content.
    - runtime_div_selector = "#question-#{wrapped_embeddable.embeddable_id}"
    - wrapped_selector = "#question-#{wrapped_embeddable.embeddable_id} .question-container"
  - else
    -# Typical case. Plugin renders its own output.
    - output_id = "output-#{plugin.id}"
    .plugin-output{id: output_id}
    - runtime_div_selector = "##{output_id}"
    - wrapped_selector = nil

  :javascript
    // Begin script for #{plugin.name}
    var savePath = `#{escape_javascript(api_v1_update_plugin_learner_state_path(plugin.id, @run.id))}`
    var loadPath = `#{escape_javascript(api_v1_show_plugin_learner_state_path(plugin.id, @run.id))}`
    var pluginStatePaths = {
      savePath: savePath,
      loadPath: loadPath
    };
    var learner_state  = '#{ escape_javascript(learner_state) }'
    $(document).ready( function() {
      env = {
        name: '#{plugin.name}',
        url: '#{plugin.url}',
        pluginId: '#{plugin.id}',
        authoredState: '#{ escape_javascript(plugin.author_data) }',
        learnerState: learner_state,
        div: $('#{runtime_div_selector}')[0],
        wrappedEmbeddableDiv: $('#{wrapped_selector}')[0],
        wrappedEmbeddableContext: #{wrapped_embeddable ? (LaraSerializationHelper.new).wrap_export(wrapped_embeddable).to_json : 'null'}
      }
      console.log("Adding script #{plugin.label} with V2 LARA Plugin API")
      Plugins.initPlugin('#{plugin.label}', env, pluginStatePaths);
{'fraction_non_alphanumeric': 0.1483710313343017, 'fraction_numerical': 0.004150238638721726, 'mean_word_length': 3.50046685340803, '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': '214580', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1563, 'n_words': 293}