<commit_before>
'use strict';


var _     = require('lodash');
var $     = require('nodeca.core/lib/parser/cheequery');


module.exports = function (N) {
  // Fetch attachments metadata and store it into `image_info`
  //
  // Input:
  //
  //  - data (Object) - parser data object
  //  - refs (Array)  - array of [ media_id, size_name ]
  //
  function fetch_attachments_meta(data, refs, callback) {
    var attachments = {};
    var unknown_refs = [];

    refs.forEach(function (ref) {
      var id = ref[0];
      var size = ref[1];
      var attach_params = data.result.image_info['media:' + id + '_' + size];

      if (attach_params && attach_params.type) {
        attachments[id] = _.pick(attach_params, [ 'type', 'file_name' ]);
      } else {
        unknown_refs.push(ref);
      }
    });

    var attach_list = {
      ids: _.uniq(unknown_refs.map(function (r) { return r[0]; }).sort(), true)
    };

    N.wire.emit('internal:common.content.attachments', attach_list, function (err) {
      if (err) {
        callback(err);
        return;
      }

      var result = _.assign(attachments, attach_list.attachments);

      unknown_refs.forEach(function (ref) {
        var id = ref[0];
        var size = ref[1];
        var key = 'media:' + id + '_' + size;

        data.result.image_info[key] = _.assign(_.omit({
          type:      result[id].type,
          file_name: result[id].file_name,
          width:     result[id].image_sizes && result[id].image_sizes[size] && result[id].image_sizes[size].width,
          height:    result[id].image_sizes && result[id].image_sizes[size] && result[id].image_sizes[size].height
        }, _.isUndefined), data.result.image_info[key]);
      });

      callback();
    });
  }

  N.wire.once('init:parser', function attachment_plugin_init() {
    var plugin = require('nodeca.core/lib/parser/plugins/attachment')(N, {
      types: N.models.users.MediaInfo.types
    });

    var sizes = N.config.users.uploads.resize;

    N.parse.addPlugin(
      'attachment',
      function (parser) {
        parser.bus.on('render', function fetch_attachment_data(data, callback) {
          // find all images that point to an attachment and set data-nd-media-id attr
          data.ast.find('.image').each(function () {
            var $attach = $(this);<commit_msg>Fix exception when user links to a non-existent attachment<commit_after>
'use strict';


var _     = require('lodash');
var $     = require('nodeca.core/lib/parser/cheequery');


module.exports = function (N) {
  // Fetch attachments metadata and store it into `image_info`
  //
  // Input:
  //
  //  - data (Object) - parser data object
  //  - refs (Array)  - array of [ media_id, size_name ]
  //
  function fetch_attachments_meta(data, refs, callback) {
    var attachments = {};
    var unknown_refs = [];

    refs.forEach(function (ref) {
      var id = ref[0];
      var size = ref[1];
      var attach_params = data.result.image_info['media:' + id + '_' + size];

      if (attach_params && attach_params.type) {
        attachments[id] = _.pick(attach_params, [ 'type', 'file_name' ]);
      } else {
        unknown_refs.push(ref);
      }
    });

    var attach_list = {
      ids: _.uniq(unknown_refs.map(function (r) { return r[0]; }).sort(), true)
    };

    N.wire.emit('internal:common.content.attachments', attach_list, function (err) {
      if (err) {
        callback(err);
        return;
      }

      var result = _.assign(attachments, attach_list.attachments);

      unknown_refs.forEach(function (ref) {
        var id = ref[0];
        var size = ref[1];
        var key = 'media:' + id + '_' + size;

        if (!result[id]) { return; }

        data.result.image_info[key] = _.assign(_.omit({
          type:      result[id].type,
          file_name: result[id].file_name,
          width:     result[id].image_sizes && result[id].image_sizes[size] && result[id].image_sizes[size].width,
          height:    result[id].image_sizes && result[id].image_sizes[size] && result[id].image_sizes[size].height
        }, _.isUndefined), data.result.image_info[key]);
      });

      callback();
    });
  }

  N.wire.once('init:parser', function attachment_plugin_init() {
    var plugin = require('nodeca.core/lib/parser/plugins/attachment')(N, {
      types: N.models.users.MediaInfo.types
    });

    var sizes = N.config.users.uploads.resize;

    N.parse.addPlugin(
      'attachment',
      function (parser) {
        parser.bus.on('render', function fetch_attachment_data(data, callback) {
          // find all images that point to an attachment and set data-nd-media-id attr
          data.ast.find('.image').each(function () {
            var $attach = $(this);
{'fraction_non_alphanumeric': 0.15392758839720516, 'fraction_numerical': 0.002117298327334321, 'mean_word_length': 2.5148809523809526, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3944403', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1494, 'n_words': 302}