jupyter_to_scrapbox 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/jupyter_to_scrapbox +30 -0
- data/jupyter_to_scrapbox.gemspec +28 -0
- data/lib/jupyter_to_scrapbox.rb +164 -0
- data/lib/jupyter_to_scrapbox/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 948f3717903d4013141c5be1b2775e25c61283f5
|
4
|
+
data.tar.gz: b38321b0604369962ae00b5774db3d3d26f944d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 116639cb1609d2183b8fa756cb0a1d2097edc01a50b6e52a2dd7563d615c105c552691df6ae852a591c2fb52127a17a5743dd4b8a26b5bfb46aa90bd6fd7d283
|
7
|
+
data.tar.gz: 24f3aba9628ddddcd216cebdc344608535758ac894a1485693e9eca47167ce1000729e991d6f0d24f6998cb9ba32a1bb6a3912e31ff237330d650dccb535eab3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Thomas Park
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# JupyterToScrapbox
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jupyter_to_scrapbox`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
Warning : do not believe the instruction below.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'jupyter_to_scrapbox'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install jupyter_to_scrapbox
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
|
28
|
+
$ bundle exec bin/jupyter_to_scrapbox FILE > scrapbox.json
|
29
|
+
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hsugawa8651/jupyter_to_scrapbox_gem.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jupyter_to_scrapbox"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require "jupyter_to_scrapbox"
|
5
|
+
require 'thor'
|
6
|
+
|
7
|
+
module JupyterToScrapbox
|
8
|
+
class CLI < Thor
|
9
|
+
class_option :help, type: :boolean, aliases: '-h', desc: 'help message.'
|
10
|
+
class_option :version, type: :boolean, desc: 'version'
|
11
|
+
default_task :help
|
12
|
+
|
13
|
+
option :file, type: :string, aliases: '-f', desc: 'specify input jupyter-notebook file (.ipynb)'
|
14
|
+
method_option :verbose, type: :boolean, aliases: '-v', desc: 'verbose message'
|
15
|
+
desc 'convert FILE [options]', 'Convert jupyter-notebook file to scrapbox-json'
|
16
|
+
|
17
|
+
def convert(path)
|
18
|
+
converter=JupyterToScrapbox::Converter.new(path)
|
19
|
+
converter.set_verbose() if options[:verbose]
|
20
|
+
converter.start()
|
21
|
+
# puts "Hello "+path
|
22
|
+
end
|
23
|
+
desc 'version', 'version'
|
24
|
+
def version
|
25
|
+
p JupyterToScrapbox::VERSION
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
JupyterToScrapbox::CLI.start(ARGV)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "jupyter_to_scrapbox/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jupyter_to_scrapbox"
|
8
|
+
spec.version = JupyterToScrapbox::VERSION
|
9
|
+
spec.authors = ["Hiroharu Sugawara"]
|
10
|
+
spec.email = ["hsugawa@tmu.ac.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{convert jupyter-notebook to scrapbox-ready-json}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = %q{http://github.com/hsugawa8651/juoyter_to_scrapbox_gem}
|
15
|
+
spec.licenses = [ "MIT" ]
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_dependency "thor"
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require "jupyter_to_scrapbox/version"
|
2
|
+
require "JSON"
|
3
|
+
|
4
|
+
module JupyterToScrapbox
|
5
|
+
# Your code goes here...
|
6
|
+
|
7
|
+
class Converter
|
8
|
+
def initialize(path)
|
9
|
+
@ipynb_path=File.expand_path(path)
|
10
|
+
@sb_json=[]
|
11
|
+
@verbose=false
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_verbose()
|
15
|
+
@verbose=true
|
16
|
+
end
|
17
|
+
|
18
|
+
def vp(s)
|
19
|
+
p(s) if @verbose
|
20
|
+
end
|
21
|
+
|
22
|
+
def vprint(s)
|
23
|
+
print(s) if @verbose
|
24
|
+
end
|
25
|
+
|
26
|
+
def vputs(s)
|
27
|
+
puts(s) if @verbose
|
28
|
+
end
|
29
|
+
|
30
|
+
def push_text(s)
|
31
|
+
# @sb_json << s
|
32
|
+
s.split("\n").each do |s1|
|
33
|
+
@sb_json << s1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def push_empty_text()
|
38
|
+
@sb_json << ""
|
39
|
+
end
|
40
|
+
|
41
|
+
def push_code(s)
|
42
|
+
# @sb_json << "\t"+s
|
43
|
+
s.split("\n").each do |s1|
|
44
|
+
@sb_json << "\t"+s1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def push_texts(ww)
|
50
|
+
ww.each do |s|
|
51
|
+
push_text(s)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def push_codes(ww)
|
56
|
+
ww.each do |s|
|
57
|
+
push_code(s)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_cell_markdown(md)
|
62
|
+
vputs "-- source"
|
63
|
+
vputs md["source"]
|
64
|
+
push_texts(md["source"])
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_cell_code(code)
|
68
|
+
vputs "-- source"
|
69
|
+
vputs code["source"]
|
70
|
+
push_text("code:source.jl")
|
71
|
+
if @display_input_number
|
72
|
+
count=code["execution_count"]
|
73
|
+
push_code("#{@prefix_comment} In[#{count}]")
|
74
|
+
end
|
75
|
+
push_codes(code["source"])
|
76
|
+
push_empty_text()
|
77
|
+
|
78
|
+
# p w
|
79
|
+
vputs "-- outputs"
|
80
|
+
if code["outputs"]
|
81
|
+
(code["outputs"]).each_with_index do |oo,oo_c|
|
82
|
+
vprint "-- outputs #{oo_c} "
|
83
|
+
vp oo.keys
|
84
|
+
vprint "-- outputs #{oo_c} output_type="+oo["output_type"]+"\n"
|
85
|
+
if oo["evalue"]
|
86
|
+
push_text("code:error.txt")
|
87
|
+
push_code(oo["evalue"])
|
88
|
+
push_empty_text()
|
89
|
+
elsif oo["data"]
|
90
|
+
out_data_c=1
|
91
|
+
oo["data"].each_pair do |out_data_k,out_data_v|
|
92
|
+
vputs "-- outputs #{out_data_c} inner data => #{out_data_k}"
|
93
|
+
case out_data_k
|
94
|
+
when "image/png"
|
95
|
+
vputs out_data_v
|
96
|
+
push_text("code:output.txt")
|
97
|
+
push_code("image/png")
|
98
|
+
push_empty_text()
|
99
|
+
when "text/plain"
|
100
|
+
vputs out_data_v
|
101
|
+
push_text("code:output.txt")
|
102
|
+
push_codes(out_data_v)
|
103
|
+
push_empty_text()
|
104
|
+
else
|
105
|
+
raise "data unknown"
|
106
|
+
end
|
107
|
+
out_data_c += 1
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def parse_cells(cells)
|
115
|
+
cells.each_with_index do |cell,cell_count|
|
116
|
+
vprint "cell #{cell_count} "
|
117
|
+
vprint "-- cell_type="+cell["cell_type"]+"\n"
|
118
|
+
case cell["cell_type"]
|
119
|
+
when "markdown"
|
120
|
+
parse_cell_markdown(cell)
|
121
|
+
when "code"
|
122
|
+
parse_cell_code(cell)
|
123
|
+
else
|
124
|
+
raise "Unknown cell_type"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def parse_ipynb()
|
130
|
+
texts=File.read(@ipynb_path)
|
131
|
+
# vputs texts
|
132
|
+
js=JSON.parse(texts)
|
133
|
+
vp js.length
|
134
|
+
@file_extension=js["metadata"]["language_info"]["file_extension"]
|
135
|
+
vp @file_extension
|
136
|
+
|
137
|
+
if %r!\.(jl|py|rb)!i =~ @file_extension
|
138
|
+
@display_input_number=true
|
139
|
+
@prefix_comment="#"
|
140
|
+
end
|
141
|
+
|
142
|
+
vputs "----- -----"
|
143
|
+
# p js
|
144
|
+
parse_cells(js["cells"])
|
145
|
+
end
|
146
|
+
|
147
|
+
def start()
|
148
|
+
vputs "this is a pen"
|
149
|
+
vputs "Hello "+@ipynb_path
|
150
|
+
parse_ipynb()
|
151
|
+
|
152
|
+
my_title=File.basename(@ipynb_path,".ipynb")
|
153
|
+
result={
|
154
|
+
"pages": [ {
|
155
|
+
"title": my_title,
|
156
|
+
"lines": @sb_json.unshift(my_title)
|
157
|
+
}]
|
158
|
+
}
|
159
|
+
|
160
|
+
puts result.to_json
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jupyter_to_scrapbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hiroharu Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- hsugawa@tmu.ac.jp
|
58
|
+
executables:
|
59
|
+
- ".DS_Store"
|
60
|
+
- jupyter_to_scrapbox
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- exe/.DS_Store
|
72
|
+
- exe/jupyter_to_scrapbox
|
73
|
+
- jupyter_to_scrapbox.gemspec
|
74
|
+
- lib/.DS_Store
|
75
|
+
- lib/jupyter_to_scrapbox.rb
|
76
|
+
- lib/jupyter_to_scrapbox/version.rb
|
77
|
+
homepage: http://github.com/hsugawa8651/juoyter_to_scrapbox_gem
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.12
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: convert jupyter-notebook to scrapbox-ready-json
|
101
|
+
test_files: []
|