Class: IpynbDiff::OutputTransformer

Inherits:
Object
  • Object
show all
Includes:
SymbolizedMarkdownHelper
Defined in:
lib/output_transformer.rb

Overview

Transforms Jupyter output data into markdown

Constant Summary collapse

HIDDEN_IMAGE_OUTPUT =
'    [Hidden Image Output]'
ORDERED_KEYS =
{
  'execute_result' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex text/plain],
  'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex],
  'stream' => %w[text]
}.freeze

Instance Method Summary collapse

Methods included from SymbolizedMarkdownHelper

#_, #array_if_not_array, #symbolize_array

Constructor Details

#initialize(hide_images: false) ⇒ OutputTransformer

Returns a new instance of OutputTransformer.



17
18
19
# File 'lib/output_transformer.rb', line 17

def initialize(hide_images: false)
  @hide_images = hide_images
end

Instance Method Details

#decorate_output(output_rows, output, symbol) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/output_transformer.rb', line 34

def decorate_output(output_rows, output, symbol)
  [
    _,
    _(symbol, %(%%%% Output: #{output['output_type']})),
    _,
    *output_rows
  ]
end

#transform(output, symbol) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/output_transformer.rb', line 21

def transform(output, symbol)
  transformed = case (output_type = output['output_type'])
                when 'error'
                  transform_error(output['traceback'], symbol / 'traceback')
                when 'execute_result', 'display_data'
                  transform_non_error(ORDERED_KEYS[output_type], output['data'], symbol / 'data')
                when 'stream'
                  transform_element('text', output['text'], symbol)
                end

  transformed ? decorate_output(transformed, output, symbol) : []
end

#transform_element(output_type, output_element, symbol_prefix) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/output_transformer.rb', line 57

def transform_element(output_type, output_element, symbol_prefix)
  new_symbol = symbol_prefix / output_type
  case output_type
  when 'image/png', 'image/jpeg'
    transform_image(output_type + ';base64', output_element, new_symbol)
  when 'image/svg+xml'
    transform_image(output_type + ';utf8', output_element, new_symbol)
  when 'text/markdown', 'text/latex', 'text/plain', 'text'
    transform_text(output_element, new_symbol)
  end
end

#transform_error(traceback, symbol) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/output_transformer.rb', line 43

def transform_error(traceback, symbol)
  traceback.map.with_index do |t, idx|
    t.split("\n").map do |l|
      _(symbol / idx, l.gsub(/\[[0-9][0-9;]*m/, '').sub("\u001B", '    ').gsub(/\u001B/, '').rstrip)
    end
  end
end

#transform_image(image_type, image_content, symbol) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/output_transformer.rb', line 69

def transform_image(image_type, image_content, symbol)
  return _(nil, HIDDEN_IMAGE_OUTPUT) if @hide_images

  lines = image_content.is_a?(Array) ? image_content : [image_content]

  single_line = lines.map(&:strip).join.gsub(/\s+/, ' ')

  _(symbol, "    ![](data:#{image_type},#{single_line})")
end

#transform_non_error(accepted_keys, elements, symbol) ⇒ Object



51
52
53
54
55
# File 'lib/output_transformer.rb', line 51

def transform_non_error(accepted_keys, elements, symbol)
  accepted_keys.filter { |key| elements.key?(key) }.map do |key|
    transform_element(key, elements[key], symbol)
  end
end

#transform_text(text_content, symbol) ⇒ Object



79
80
81
# File 'lib/output_transformer.rb', line 79

def transform_text(text_content, symbol)
  symbolize_array(symbol, text_content) { |l| "    #{l.rstrip}" }
end