class JupyterToScrapbox::Converter

Your code goes here…

Public Class Methods

add(path) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 65
def Converter.add(path)
  @@converters.push Converter.new(path)
end
new(path) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 83
def initialize(path)
  @page_title=""
  @last_modified=nil
  @sb_json=[]
  @display_input_numbers=false
  @prefix_comment="#"

  @offline=true
  @ipynb_path=path.chomp
  case @ipynb_path
  when %r!^https?://!i, %r!^ftp://!i
    @offline=false
  else
    @ipynb_path=File.expand_path(@ipynb_path)
  end
end
perform() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 69
def Converter.perform()
  pages=@@converters.collect do |c|
    c.start()
    c.page()
  end

  result={
    "pages": pages
  }

  puts result.to_json

end
prepareGyazoclient() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 46
def Converter.prepareGyazoclient()
end
set_num_asterisks(v) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 61
def Converter.set_num_asterisks(v)
  @@num_asterisk_for_h1=v
end
set_parse_markdown(v) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 57
def Converter.set_parse_markdown(v)
  @@parse_markdown_notations=v
end
set_register_images(v) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 53
def Converter.set_register_images(v)
  @@register_images=v
end
set_verbose(v) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 49
def Converter.set_verbose(v)
bose=v
end

Public Instance Methods

page() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 276
def page()
  @page_title=@last_modified.to_s
  @sb_json.unshift("")
  @sb_json.unshift(@ipynb_path)
  @sb_json.unshift(@page_title)

  page= {
    "title": @page_title,
    "lines": @sb_json
  }
  return page
end
parse_cell_code(code) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 182
def parse_cell_code(code)
  vputs "-- source"
  vputs code["source"]
  push_text("code:source.jl")
  if @display_input_numbers
    count=code["execution_count"]
    push_code("#{@prefix_comment} In[#{count}]")
  end
  push_codes(code["source"])
  push_empty_text()

  # p w
  vputs "-- outputs"
  if code["outputs"]
    (code["outputs"]).each_with_index do |oo,oo_c|
      vprint "-- outputs #{oo_c} "
      vp oo.keys
      vprint "-- outputs #{oo_c} output_type="+oo["output_type"]+"\n"
      if    oo["evalue"]
        push_text("code:error.txt")
        push_code(oo["evalue"])
        push_empty_text()
      elsif oo["data"]
        out_data_c=1
        oo["data"].each_pair do |out_data_k,out_data_v|
          vputs "-- outputs #{out_data_c} inner data => #{out_data_k}"
          case out_data_k
          when "image/png"
            parse_image_png(out_data_v)
          when "text/plain"
            vputs out_data_v
            push_text("code:output.txt")
            push_codes(out_data_v)
            push_empty_text()
          else
            raise "data unknown"
          end
          out_data_c += 1
        end
      end
    end
  end
end
parse_cell_markdown(md) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 151
def parse_cell_markdown(md)
  vputs "-- source"
  vputs md["source"]
  push_markdown_texts(md["source"])
end
parse_cells(cells) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 226
def parse_cells(cells)
  cells.each_with_index do |cell,cell_count|
    vprint "cell #{cell_count} "
    vprint "-- cell_type="+cell["cell_type"]+"\n"
    case cell["cell_type"]
    when "markdown"
      parse_cell_markdown(cell)
    when "code"
      parse_cell_code(cell)
    else
      raise "Unknown cell_type"
    end
  end
end
parse_image_png(v) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 169
def parse_image_png(v)
  vputs(v)

  push_text("code:output.txt")
  push_code("image/png")
  if @@register_images
    r=register_image(v)
    url=r["url"]
    push_text( "["+url+"]"  )
  end
  push_empty_text()
end
parse_ipynb() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 241
def parse_ipynb()
  @texts=""
  open(@ipynb_path) do |f|
    if @offline
      @last_modified=f.mtime
    else
      @last_modified=f.last_modified
      @last_modified=Time.now unless @last_modified
    end
    @texts=f.read
  end
  # vputs texts
  js=JSON.parse(@texts)
  vp js.length

  begin
    @file_extension=js["metadata"]["language_info"]["file_extension"]
  rescue
  end
  vp @file_extension

  if %r!\.(jl|py|rb)!i =~ @file_extension
    @display_input_numbers=true
    @prefix_comment="#"
  end

  vputs "----- -----"
  # p js
  parse_cells(js["cells"])
end
push_code(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 157
def push_code(s)
  s.split("\n").each do |s1|
    @sb_json << "\t"+s1
  end
end
push_codes(ww) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 163
def push_codes(ww)
  ww.each do |s|
    push_code(s)
  end
end
push_empty_text() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 112
def push_empty_text()
  @sb_json << ""
end
push_markdown_text(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 128
def push_markdown_text(s)
  s.split("\n").each do |s1|
    if @@parse_markdown_notations

             # Atx style header
      if     s1.sub!(%r!^(#+)(.*)$!) { "["+"*"*([@@num_asterisk_for_h1+1-($1.length),1].max) + $2 + "]" } 
      
             # inline math
      elsif  s1.sub!(%r!^\$\$(.+)\$\$!, '[$\1 ]' ) 
      else
             s1.gsub!(%r!\$(.+)\$!, '[$\1 ]' ) 
      end 
    end
    @sb_json << s1
  end
end
push_markdown_texts(ww) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 145
def push_markdown_texts(ww)
  ww.each do |s|
    push_markdown_text(s)
  end
end
push_text(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 116
def push_text(s)
  s.split("\n").each do |s1|
    @sb_json << s1
  end
end
push_texts(ww) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 122
def push_texts(ww)
  ww.each do |s|
    push_text(s)
  end
end
start() click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 272
def start()
  parse_ipynb()
end
vp(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 100
def vp(s)
  p(s) if @@verbose
end
vprint(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 104
def vprint(s)
  print(s) if @@verbose
end
vputs(s) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 108
def vputs(s)
  puts(s) if @@verbose
end