Class: IpynbDiff::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/transformer.rb

Overview

Returns a markdown version of the Jupyter Notebook

Class Method Summary collapse

Class Method Details

.format_traceback(traceback) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/transformer.rb', line 33

def self.format_traceback(traceback)
  traceback.map do |t|
    t.split("\n").map do |line|
      line.gsub(/\[[0-9][0-9;]*m/, '').sub("\u001B", '    ').gsub(/\u001B/, '').rstrip
    end
  end.join("\n")
end

.transform(notebook, include_metadata: TRUE) ⇒ Object



9
10
11
12
13
14
# File 'lib/transformer.rb', line 9

def self.transform(notebook, include_metadata: TRUE)
  notebook_json = JSON.parse(notebook)
  transformed_blocks = notebook_json['cells'].map { |cell| transform_cell(cell, notebook_json) }
  transformed_blocks.prepend((notebook_json)) if 
  transformed_blocks.join("\n")
end

.transform_cell(cell, notebook) ⇒ Object



16
17
18
# File 'lib/transformer.rb', line 16

def self.transform_cell(cell, notebook)
  cell['cell_type'] == 'code' ? transform_code_cell(cell, notebook) : transform_text_cell(cell)
end

.transform_code_cell(cell, notebook) ⇒ Object



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

def self.transform_code_cell(cell, notebook)
  tags = cell['metadata'].fetch('tags', []).join(' ')

  [
    %(<div class="cell code" data-id="#{cell['id']}" data-tags="#{tags}">\n\n),
    %(``` #{notebook['metadata']['kernelspec']['language']}\n),
    *cell['source'],
    "\n```\n",
    *cell['outputs'].map { |output| transform_output(output) },
    "\n</div>\n"
  ].join('')
end

.transform_error_result(output) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/transformer.rb', line 59

def self.transform_error_result(output)
  [
    %(\n<div class="output error">\n\n),
    format_traceback(output['traceback']),
    "\n\n</div>\n"
  ].join('')
end

.transform_execute_result(output) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/transformer.rb', line 41

def self.transform_execute_result(output)
  [
    %(\n<div class="output execute_result">\n\n),
    *output['data']['text/plain'].map { |line| "    #{line}" },
    "\n\n</div>\n"
  ].join('')
end

.transform_image_result(output) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/transformer.rb', line 49

def self.transform_image_result(output)
  if output['data'].key?('image/png')
    [
      %(\n<div class="output display_data">\n\n),
      "![](data:image/png;base64,#{output['data']['image/png'].gsub("\n", '')})",
      "\n\n</div>\n"
    ].join('')
  end
end

.transform_metadata(notebook_json) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/transformer.rb', line 90

def self.(notebook_json)
  {
    'jupyter' => {
      'kernelspec' => notebook_json['metadata']['kernelspec'],
      'language_info' => notebook_json['metadata']['language_info'],
      'nbformat' => notebook_json['nbformat'],
      'nbformat_minor' => notebook_json['nbformat_minor']
    }
  }.to_yaml + "---\n"
end

.transform_output(output) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/transformer.rb', line 67

def self.transform_output(output)
  case output['output_type']
  when 'execute_result'
    transform_execute_result(output)
  when 'display_data'
    transform_image_result(output)
  when 'error'
    transform_error_result(output)
  end
end

.transform_text_cell(cell) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/transformer.rb', line 78

def self.transform_text_cell(cell)
  tags = cell['metadata'].fetch('tags', []).join(' ')
  id = cell['id']
  cell_type = cell['cell_type']

  [
    %(<div class="cell #{cell_type}" data-id="#{id}" data-tags="#{tags}">\n\n),
    *cell['source'],
    "\n\n</div>\n"
  ].join('')
end