<commit_before>module TinyMceHelper
  def parse_tiny_mce_asset_to_token(text, obj)
    ids = []
    html = Nokogiri::HTML(remove_pasted_tokens(text))
    html.search('img').each do |img|
      next unless img['data-token']
      img_id = Base62.decode(img['data-token'])
      ids << img_id
      token = "[~tiny_mce_id:#{img_id}]"
      img.replace(token)
      next unless obj
      tiny_img = TinyMceAsset.find_by_id(img_id)
      tiny_img.reference = obj unless tiny_img.step || tiny_img.result_text
      tiny_img.save
    end
    destroy_removed_tiny_mce_assets(ids, obj) if obj
    html
  end

  # @param pdf_export_ready is needed for wicked_pdf in export report action
  def generate_image_tag_from_token(text, obj, pdf_export_ready = false)
    return unless text
    regex = Constants::TINY_MCE_ASSET_REGEX
    text.gsub(regex) do |el|
      match = el.match(regex)
      img = TinyMceAsset.find_by_id(match[1])
      next unless img && img.team == current_team
      next unless check_image_permissions(obj, img)
      if pdf_export_ready
        report_image_asset_url(img, :tiny_mce_asset, 'tiny-mce-pdf-ready')
      else
        image_tag img.url,
                  class: 'img-responsive',
                  data: { token: Base62.encode(img.id) }
      end
    end
  end

  def link_tiny_mce_assets(text, ref)
    ids = []
    regex = Constants::TINY_MCE_ASSET_REGEX<commit_msg>refactor<commit_after>module TinyMceHelper
  def parse_tiny_mce_asset_to_token(text, obj)
    ids = []
    html = Nokogiri::HTML(remove_pasted_tokens(text))
    html.search('img').each do |img|
      next unless img['data-token']
      img_id = Base62.decode(img['data-token'])
      ids << img_id
      token = "[~tiny_mce_id:#{img_id}]"
      img.replace(token)
      next unless obj
      tiny_img = TinyMceAsset.find_by_id(img_id)
      tiny_img.reference = obj unless tiny_img.step || tiny_img.result_text
      tiny_img.save
    end
    destroy_removed_tiny_mce_assets(ids, obj) if obj
    html
  end

  # @param pdf_export_ready is needed for wicked_pdf in export report action
  def generate_image_tag_from_token(text, obj, pdf_export_ready = false)
    return unless text
    regex = Constants::TINY_MCE_ASSET_REGEX
    text.gsub(regex) do |el|
      match = el.match(regex)
      img = TinyMceAsset.find_by_id(match[1])
      next unless img && img.team == current_team
      next unless check_image_permissions(obj, img)
      if pdf_export_ready
        report_image_asset_url(img, :tiny_mce_asset, 'tiny-mce-pdf-ready')
      else
        image_tag(img.url,
                  class: 'img-responsive',
                  data: { token: Base62.encode(img.id) })
      end
    end
  end

  def link_tiny_mce_assets(text, ref)
    ids = []
    regex = Constants::TINY_MCE_ASSET_REGEX
{'fraction_non_alphanumeric': 0.09691313711414214, 'fraction_numerical': 0.003589375448671931, 'mean_word_length': 2.992836676217765, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6413174', 'n_tokens_mistral': 985, 'n_tokens_neox': 964, 'n_words': 192}