<commit_before>require 'uri'

Pakyow::Console.editor :content do |attribute, value|
  extras = attribute[:extras]
  partial = presenter.store(:console).partial('console/editors', :content).dup

  partial.includes({
    shared: presenter.store(:console).partial('console/editors', :shared).dup,
    actions: presenter.store(:console).partial('console/editors', :actions).dup,
    alignment: presenter.store(:console).partial('console/editors', :alignment).dup
  })

  view = Pakyow::Presenter::ViewContext.new(partial, self)

  if value
    value = value.to_json
    value = URI.escape(value)
    view.scope(:editor).attrs.value = value
  end

  view.scope(:constraints)[0].with do
    attrs.value = URI.escape(extras[:constraints].to_json)
    attrs[:'data-scope'] = nil
  end

  view.scope(:editor).attrs[:name] = "console-datum[#{attribute[:name]}]"
  view.scope(:editor).attrs[:'data-scope'] = nil

  if extras[:restricted]
    view.component(:'content-editor').attrs[:'data-config'] = 'restricted: true'
  end

  view.instance_variable_get(:@view)
end

module Pakyow::Console
  class ContentRenderer
    TYPES = [:default, :image, :embed, :break]

    def self.render(content, view: nil, constraints: nil)
      templates = find_templates(view)

      view.apply(content) do |_, piece|
        type = piece['type'].to_sym
        template = templates[type].dup
        renderer = ContentTypeRegistry.type(type)

        if renderer.nil?
          Pakyow.logger.debug "No content renderer for #{type}"
        else
          type_constraints = constraints.nil? ? nil : constraints[type]

          rendered = renderer.render(piece, template, type_constraints)
          if rendered.is_a?(Pakyow::Presenter::ViewCollection)
            rendered.each do |rendered_view|
              view.append(rendered_view)
            end
          else
            view.append(rendered)
          end
        end
      end
    end

    def self.find_templates(view)
      templates = {}

      TYPES.each do |type|
        tv = view.scope("content-#{type}")[0]
        next if tv.nil?

        if tv.is_a?(Pakyow::Presenter::View)
          templates[type] = tv.dup
        else
          templates[type] = tv.subject.dup
        end

        tv.remove
      end

      templates
    end
  end
end

module Pakyow::Console::Content
  class Default
    def self.render(data, view, constraints)
      content = data['content']

      # NOTE: this is a workaround for trix, which uses <div> rather than <p>
      content.gsub!('<div>', '<p>')
      content.gsub!('</div>', '</p>')

      processor = Pakyow.app.presenter.processor_store[:md]
      view.html = processor.call(content)
      view
    end
  end

  class Image
    def self.render(data, view, constraints)
      alignment = data['align']
      alignment = 'default' if alignment.nil? || alignment.empty?

      images = data['images'].is_a?(String) ? JSON.parse(data['images']) : data['images']
      collection = Pakyow::Presenter::ViewCollection.new

      images.each do |image|
        working = view.dup
        src = Pakyow::Router.instance.group(:file).path(:show, file_id: image['id'])
        file = Pakyow::Console::FileStore.instance.find(image['id'])

        width, height = file.values_at(:width, :height)

        if constraints
          constraints_for_alignment = constraints[alignment.to_sym]

          constraint_mode = Pakyow::Console::FileStore::DEFAULT_MODE
          if constraints_for_alignment
            constraint_width, constraint_height, constraint_mode = constraints_for_alignment.values_at(:width, :height, :mode)
            scale_factor = width.to_i / constraint_width.to_f

            width = constraint_width.to_i<commit_msg>Revert "Process content as markdown"<commit_after>require 'uri'

Pakyow::Console.editor :content do |attribute, value|
  extras = attribute[:extras]
  partial = presenter.store(:console).partial('console/editors', :content).dup

  partial.includes({
    shared: presenter.store(:console).partial('console/editors', :shared).dup,
    actions: presenter.store(:console).partial('console/editors', :actions).dup,
    alignment: presenter.store(:console).partial('console/editors', :alignment).dup
  })

  view = Pakyow::Presenter::ViewContext.new(partial, self)

  if value
    value = value.to_json
    value = URI.escape(value)
    view.scope(:editor).attrs.value = value
  end

  view.scope(:constraints)[0].with do
    attrs.value = URI.escape(extras[:constraints].to_json)
    attrs[:'data-scope'] = nil
  end

  view.scope(:editor).attrs[:name] = "console-datum[#{attribute[:name]}]"
  view.scope(:editor).attrs[:'data-scope'] = nil

  if extras[:restricted]
    view.component(:'content-editor').attrs[:'data-config'] = 'restricted: true'
  end

  view.instance_variable_get(:@view)
end

module Pakyow::Console
  class ContentRenderer
    TYPES = [:default, :image, :embed, :break]

    def self.render(content, view: nil, constraints: nil)
      templates = find_templates(view)

      view.apply(content) do |_, piece|
        type = piece['type'].to_sym
        template = templates[type].dup
        renderer = ContentTypeRegistry.type(type)

        if renderer.nil?
          Pakyow.logger.debug "No content renderer for #{type}"
        else
          type_constraints = constraints.nil? ? nil : constraints[type]

          rendered = renderer.render(piece, template, type_constraints)
          if rendered.is_a?(Pakyow::Presenter::ViewCollection)
            rendered.each do |rendered_view|
              view.append(rendered_view)
            end
          else
            view.append(rendered)
          end
        end
      end
    end

    def self.find_templates(view)
      templates = {}

      TYPES.each do |type|
        tv = view.scope("content-#{type}")[0]
        next if tv.nil?

        if tv.is_a?(Pakyow::Presenter::View)
          templates[type] = tv.dup
        else
          templates[type] = tv.subject.dup
        end

        tv.remove
      end

      templates
    end
  end
end

module Pakyow::Console::Content
  class Default
    def self.render(data, view, constraints)
      content = data['content']

      # NOTE: this is a workaround for trix, which uses <div> rather than <p>
      content.gsub!('<div>', '<p>')
      content.gsub!('</div>', '</p>')

      view.html = content
      view
    end
  end

  class Image
    def self.render(data, view, constraints)
      alignment = data['align']
      alignment = 'default' if alignment.nil? || alignment.empty?

      images = data['images'].is_a?(String) ? JSON.parse(data['images']) : data['images']
      collection = Pakyow::Presenter::ViewCollection.new

      images.each do |image|
        working = view.dup
        src = Pakyow::Router.instance.group(:file).path(:show, file_id: image['id'])
        file = Pakyow::Console::FileStore.instance.find(image['id'])

        width, height = file.values_at(:width, :height)

        if constraints
          constraints_for_alignment = constraints[alignment.to_sym]

          constraint_mode = Pakyow::Console::FileStore::DEFAULT_MODE
          if constraints_for_alignment
            constraint_width, constraint_height, constraint_mode = constraints_for_alignment.values_at(:width, :height, :mode)
            scale_factor = width.to_i / constraint_width.to_f

            width = constraint_width.to_i
{'fraction_non_alphanumeric': 0.130204801302048, 'fraction_numerical': 0.0005425200054252, 'mean_word_length': 2.926517571884984, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4322771', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2181, 'n_words': 443}