Module: NbUtil

Defined in:
lib/cli.rb,
lib/nb_util.rb,
lib/nb_util/iputs.rb,
lib/nb_util/combine.rb,
lib/nb_util/getcode.rb,
lib/nb_util/version.rb,
lib/nb_util/ipynb2tex.rb,
lib/nb_util/yaml2ipynb.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.5.1"

Class Method Summary collapse

Class Method Details

.combine(argv0, argv1, argv2) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nb_util/combine.rb', line 10

def combine(argv0, argv1, argv2)
  ipynb0 = JSON.load(File.read(ARGV[1]))
  ipynb1 = JSON.load(File.read(ARGV[2]))

  output_filename = ARGV[3] 
  p output_filename

  ipynb0["cells"].each do |cell|
    pp cell
    ipynb1["cells"] << cell
  end

  File.open(output_filename + ".ipynb", 'w') do |target|
    JSON.dump(ipynb1,target)
  end
end

.delete_folder(target) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/nb_util/ipynb2tex.rb', line 158

def delete_folder(target)
  loop do
    print "Are you sure?: "
    input = STDIN.gets.to_s.chomp
    if input == 'Y' || input == 'y'

      mk_latex = File.join(File.absolute_path("../", target), '/mk_latex')
      old = File.join(File.absolute_path("../", target), '/old')
      if Dir.exist?(mk_latex) && Dir.exist?(old)
        print "\e[31mdelete: \e[0m"
        FileUtils.rm_r(mk_latex)
        puts "\e[31m#{mk_latex}\e[0m"
        print "\e[31mdelete: \e[0m"
        FileUtils.rm_r(old)
        puts "\e[31m#{old}\e[0m"
        break
      elsif Dir.exist?(mk_latex)
        print "\e[31mdelete: \e[0m"
        FileUtils.rm_r(mk_latex)
        puts "\e[31m#{mk_latex}\e[0m"
        break
      elsif Dir.exist?(old)
        print "\e[31mdelete: \e[0m"
        FileUtils.rm_r(old)
        puts "\e[31m#{old}\e[0m"
        break
      else
        puts "\e[31mNo such file or directory\e[0m"
        break
      end
    elsif input == 'N' || input == 'n'
      puts
      break
    end
  end
end

.flatten_hash_from(hash) ⇒ Object

nest hash to single hash



173
174
175
176
177
178
179
180
# File 'lib/nb_util/yaml2ipynb.rb', line 173

def flatten_hash_from hash
  hash.each_with_object({}) do |(key, value), memo|
    next flatten_hash_from(value).each do |k, v|
      memo["#{key}.#{k}".intern] = v
    end if value.is_a? Hash
    memo[key] = value
  end
end

.get_name(str) ⇒ Object



13
14
15
# File 'lib/nb_util.rb', line 13

def get_name(str)
  str.delete(' ').split(/[\/]/)
end

.getcode(argv0) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nb_util/getcode.rb', line 8

def getcode(argv0)
  input_filename = ARGV[1]
  ipynb = JSON.parse(File.read(input_filename))
  ipynb_filename = ARGV[2] || input_filename.gsub(/(.ipynb)$/, '')
  hash = {}
  i = 0
  ipynb["cells"].each do |k, v|
    hash[i.to_s] = k
    i += 1
  end
  for j in 0..i-1 do
    var="@hash#{j}"
    eval("#{var}={}")
    hash[j.to_s].each do |k, v|
      eval("#{var}[k] = v")
    end
  end

  flag = 0
  source_count = 0
  @getcode = ""
  for i in 0..j - 1 do
    eval("if @hash#{i}[\"cell_type\"] != \"code\" then flag = 1 end")
    if flag == 0 then
      eval("puts @getcode = @hash#{i}[\"source\"]")
      source_count = source_count + 1
      output_filename = ipynb_filename + source_count.to_s + ipynb["metadata"]["language_info"]["file_extension"]
      File.open(output_filename, 'w+') do |f|
        f.puts(@getcode)
      end
    end
    flag = 0
  end
end

.iputs(argv0) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nb_util/iputs.rb', line 7

def iputs(argv0)
  ipynb = JSON.load(File.read(ARGV[1]))
  ipynb.each do |cells|
    next unless cells.include?("cells")
    cells[1].each do |cell|
      cell["source"].each do |line|
        print line
      end
    end
  end
end

.ipynb2tex_handout(target) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/nb_util/ipynb2tex.rb', line 85

def ipynb2tex_handout(target)
 loop do
   target_parent = File.dirname(target)
   exist_info = File.join(target_parent, 'mk_latex/split_files/informations/informations.tex')
   if File.exist?(exist_info)
     FileUtils.mkdir_p(target_parent + '/split_files/informations')
     FileUtils.cp(exist_info, target_parent)
   else
     your_informations(ARGV[1], "handout")
   end
   print "Are you ok with it?: "
   input = STDIN.gets.to_s.chomp
   if input == 'Y' || input == 'y'
     location = Open3.capture3("gem environment gemdir")
     versions = Open3.capture3("gem list nb_util")
     latest_version = versions[0].split(",")
     cp_lib_data_handout_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/handout")
     cp_lib_data_handout_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/handout_pieces")
     cp_lib_data_handout_bundle = File.join(Dir.pwd, '/lib/data/handout')
     cp_lib_data_handout_pieces_bundle = File.join(Dir.pwd, '/lib/data/handout_pieces')
     re_fig = /(.+\.jpg)|(.+\.jpeg)|(.+\.png)/

     print "\e[32minputfile: \e[0m"
     target = File.expand_path(ARGV[1])
     print "\e[32m#{target}\n\e[0m"
     print "\e[32moutputfile: \e[0m"
     tex_src = target.sub('.ipynb', '.tex')
     print "\e[32m#{tex_src}\n\e[0m"
     target_parent = File.dirname(target)
     target_basename = File.basename(tex_src)
     Open3.capture3("jupyter nbconvert --to latex #{target}")
     lines = File.readlines(tex_src)
     lines.each_with_index do |line, i|
       line.sub!("\documentclass[11pt]{article}",
         "\documentclass[11pt,dvipdfmx]{jsarticle}")
       print "\e[32m#{line}\n\e[0m" if line =~ re_fig  #redにする"\e[31m\e[0m"
       line.sub!(line, '%' + line) if line.include?('.svg')
     end
     File.open(tex_src, 'w') { |file| file.print lines.join }

     FileUtils.mkdir_p(target_parent + '/latex')
     FileUtils.mv(tex_src, target_parent + '/latex')
     replace_figs(File.join(target_parent + '/latex', target_basename), "handout")
     revise_lines(File.join(target_parent + '/latex', target_basename))
     split_files(File.join(target_parent + '/latex', target_basename), target, "handout")
     FileUtils.mv(target_parent + '/tmp.tex', target_parent + '/split_files/tmp')
     FileUtils.mv(target_parent + '/informations.tex', target_parent + '/split_files/informations')
     mk_thesis_location(target, "handout")
     FileUtils.mv(target_parent + '/.splits_location.tex', target_parent + '/handout')

     mk_xbb(target, re_fig)
     if Dir.exist?(cp_lib_data_handout_pieces_bundle.to_s) && Dir.exist?(cp_lib_data_handout_bundle.to_s)
       FileUtils.cp_r(cp_lib_data_handout_pieces_bundle, target_parent)
       FileUtils.cp_r(cp_lib_data_handout_bundle, target_parent)
     else
       FileUtils.cp_r(cp_lib_data_handout_pieces_gem, target_parent)
       FileUtils.cp_r(cp_lib_data_handout_gem, target_parent)
     end

     mk_latex_and_mv_to_latex(target, target_parent, "handout")
     Open3.capture3("open #{target_parent}")
     Open3.capture3("open #{target_parent}/mk_latex/handout/handout.tex/")

     exit
      break
    elsif input == 'N' || input == 'n'
      target_parent = File.dirname(target)
      FileUtils.rm_r(File.join(target_parent.to_s, '/informations.tex'))
      break
    end
  end
end

.ipynb2tex_thesis(target) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nb_util/ipynb2tex.rb', line 11

def ipynb2tex_thesis(target)
  loop do
    target_parent = File.dirname(target)
    exist_info = File.join(target_parent, 'mk_latex/split_files/informations/informations.tex')
    if File.exist?(exist_info)
      FileUtils.mkdir_p(target_parent + '/split_files/informations')
      FileUtils.cp(exist_info, target_parent)
    else
      your_informations(ARGV[1], "thesis")
    end
    print "Are you ok with it?: "
    input = STDIN.gets.to_s.chomp
    if input == 'Y' || input == 'y'
      location = Open3.capture3("gem environment gemdir")
      versions = Open3.capture3("gem list nb_util")
      latest_version = versions[0].split(",")
      cp_lib_data_thesis_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis")
      cp_lib_data_thesis_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis_pieces")
      cp_lib_data_thesis_bundle = File.join(Dir.pwd, '/lib/data/thesis')
      cp_lib_data_thesis_pieces_bundle = File.join(Dir.pwd, '/lib/data/thesis_pieces')
      re_fig = /(.+\.jpg)|(.+\.jpeg)|(.+\.png)/

      print "\e[32minputfile: \e[0m"
      target = File.expand_path(ARGV[1])
      print "\e[32m#{target}\n\e[0m"
      print "\e[32moutputfile: \e[0m"
      tex_src = target.sub('.ipynb', '.tex')
      print "\e[32m#{tex_src}\n\e[0m"
      target_parent = File.dirname(target)
      target_basename = File.basename(tex_src)
      Open3.capture3("jupyter nbconvert --to latex #{target}")
      lines = File.readlines(tex_src)
      lines.each_with_index do |line, i|
        line.sub!("\documentclass[11pt]{article}",
          "\documentclass[11pt,dvipdfmx]{jsarticle}")
        print "\e[32m#{line}\n\e[0m" if line =~ re_fig  #redにする"\e[31m\e[0m"
        line.sub!(line, '%' + line) if line.include?('.svg')
      end
      File.open(tex_src, 'w') { |file| file.print lines.join }

      FileUtils.mkdir_p(target_parent + '/latex')
      FileUtils.mv(tex_src, target_parent + '/latex')
      replace_figs(File.join(target_parent + '/latex', target_basename), "thesis")
      revise_lines(File.join(target_parent + '/latex', target_basename))
      split_files(File.join(target_parent + '/latex', target_basename), target, "thesis")
      FileUtils.mv(target_parent + '/tmp.tex', target_parent + '/split_files/tmp')
      FileUtils.mv(target_parent + '/informations.tex', target_parent + '/split_files/informations')
      mk_thesis_location(target, "thesis")
      FileUtils.mv(target_parent + '/.splits_location.tex', target_parent + '/thesis')
      mk_xbb(target, re_fig)

      if Dir.exist?(cp_lib_data_thesis_pieces_bundle.to_s) && Dir.exist?(cp_lib_data_thesis_bundle.to_s)
        FileUtils.cp_r(cp_lib_data_thesis_pieces_bundle, target_parent)
        FileUtils.cp_r(cp_lib_data_thesis_bundle, target_parent)
      else
        FileUtils.cp_r(cp_lib_data_thesis_pieces_gem, target_parent)
        FileUtils.cp_r(cp_lib_data_thesis_gem, target_parent)
      end

      mk_latex_and_mv_to_latex(target, target_parent, "thesis")
      Open3.capture3("open #{target_parent}")
#        p File.join(target_parent,"mk_latex/thesis")
      Open3.capture3("open #{target_parent}/mk_latex/thesis/thesis.tex/")

      exit
      break
    elsif input == 'N' || input == 'n'
      target_parent = File.dirname(target)
      FileUtils.rm_r(File.join(target_parent.to_s, '/informations.tex'))
      break
    end
  end
end

.mk_latex_and_mv_to_latex(target, target_parent, thesis_or_handout) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/nb_util/ipynb2tex.rb', line 366

def mk_latex_and_mv_to_latex(target, target_parent, thesis_or_handout)
  mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex'))
  d = DateTime.now
  if thesis_or_handout == "thesis"
    if Dir.exist?(File.join(mk_latex[0].to_s, '/thesis'))
      old_file = File.join(File.dirname(target),"/old/thesis/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
      FileUtils.mkdir_p(old_file)
      FileUtils.cp_r(mk_latex[0], old_file)
      FileUtils.rm_r(mk_latex[0])
    elsif Dir.exist?(File.join(mk_latex[0].to_s, '/handout'))
      old_file = File.join(File.dirname(target),"/old/handout/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
      FileUtils.mkdir_p(old_file)
      FileUtils.cp_r(mk_latex[0], old_file)
      FileUtils.rm_r(mk_latex[0])
    end
    mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target), '/mk_latex'))
    split_files = File.join(target_parent, '/split_files')
    thesis_pieces = File.join(target_parent, '/thesis_pieces')
    FileUtils.mv(thesis_pieces, mk_latex[0])
    thesis = File.join(target_parent, '/thesis')
    latex = File.join(target_parent, '/latex')
    FileUtils.mv(split_files, File.join(mk_latex[0], "/split_files"))
    FileUtils.mv(latex, mk_latex[0])
    FileUtils.mv(thesis, mk_latex[0])
  end

  if thesis_or_handout == "handout"
    if Dir.exist?(File.join(mk_latex[0].to_s, '/thesis'))
      old_file = File.join(File.dirname(target),"/old/thesis/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
      FileUtils.mkdir_p(old_file)
      FileUtils.cp_r(mk_latex[0], old_file)
      FileUtils.rm_r(mk_latex[0])
    elsif Dir.exist?(File.join(mk_latex[0].to_s, '/handout'))
      old_file = File.join(File.dirname(target),"/old/handout/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
      FileUtils.mkdir_p(old_file)
      FileUtils.cp_r(mk_latex[0], old_file)
      FileUtils.rm_r(mk_latex[0])
    end
    mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target), '/mk_latex'))
    split_files = File.join(target_parent, '/split_files')
    handout_pieces = File.join(target_parent, '/handout_pieces')
    FileUtils.mv(handout_pieces, mk_latex[0])
    handout = File.join(target_parent, '/handout')
    latex = File.join(target_parent, '/latex')
    FileUtils.mv(split_files, File.join(mk_latex[0], "/split_files"))
    FileUtils.mv(latex, mk_latex[0])
    FileUtils.mv(handout, mk_latex[0])
  end
end

.mk_thesis_location(input_ipynb, thesis_or_handout) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/nb_util/ipynb2tex.rb', line 416

def mk_thesis_location(input_ipynb, thesis_or_handout)
  target_parent = File.dirname(input_ipynb)
  ipynb = JSON.parse(File.read(input_ipynb))
  pickup_ipynb = ipynb["cells"].to_s.split(",")
  chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '') }
  chapter_size = chapter.size

  if thesis_or_handout == "thesis"
    FileUtils.mkdir_p(target_parent + '/thesis')
  end
  if thesis_or_handout == "handout"
    FileUtils.mkdir_p(target_parent + '/handout')
  end
  File.open(target_parent + '/.splits_location.tex', "w") do |f|
    for num in 0..chapter_size-1 do
      f.print("\\input{../split_files/chapter#{num+1}/chapter#{num+1}}\n")
    end
  end
end

.mk_xbb(target, re_fig) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/nb_util/ipynb2tex.rb', line 314

def mk_xbb(target, re_fig)
  target_parent = File.absolute_path("../..", target)
  FileUtils.mkdir_p(target_parent + '/figs')
  FileUtils.cd(target_parent + '/figs')
  Dir.entries('.').each do |file|
    next unless file =~ re_fig
    m = file.split('.')[0..-2]
    next if File.exist?(m.join('.') + '.xbb')
    command = "extractbb #{file}"
    p command
    system command
  end
end

.replace_figs(target, thesis_or_handout) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/nb_util/ipynb2tex.rb', line 269

def replace_figs(target, thesis_or_handout)
  lines = File.readlines(target)
  counter = -1
  # settings of each
  data = [["This", 150, -4, 0]]
  lines.each_with_index do |line, i|
    lines[i] = "   \\usepackage{wrapfig}\n"+line if line.include?("\\usepackage{graphicx}")
    lines[i] = '%' + line if line.include?("\\renewcommand{\\includegraphics}")
    lines[i] = '%' + line if line.include?("\\DeclareCaptionLabelFormat")
    lines[i] = '%' + line if line.include?("\\captionsetup{labelformat=nolabel}")
    if m = line.match(/\\includegraphics\{(.+)\}/)
      counter += 1
      file_name, label, size, top, bottom = [m[1], data[counter]].flatten
      caption = lines[i + 1]
      label_name = file_name.to_s.gsub('figs', '').gsub('.png', '').gsub('/', '')
      if thesis_or_handout == "thesis"
        thesis_wrap_figs = <<"EOS"
\\begin{center}
\\includegraphics[width=150mm]{../../#{file_name}}
\\end{center}
#{caption}
\\label{fig:#{label}}
EOS
      end
      if thesis_or_handout == "handout"
        thesis_wrap_figs = <<"EOS"
\\begin{center}
\\includegraphics[width=80mm]{../../#{file_name}}
\\end{center}
#{caption}
\\label{fig:#{label}}
EOS
      end
      lines[i]
      lines[i] = thesis_wrap_figs
      lines.delete_at(i + 1) # if no caption, comment out here
    end
  end
  File.open(target, 'w') do |f|
    lines.each do |line|
      f.print line.to_s.gsub(/begin{figure}/, "begin{figure}[H]")
    end
  end
end

.revise_lines(target) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/nb_util/ipynb2tex.rb', line 195

def revise_lines(target)
  bugs = [['\end{quote}',:chomp]]
  lines = File.readlines(target)
  lines.each do |line|
    bugs.each do |bug|
      if line.include?(bug[0])
        p line
        line.chomp!
      end
    end
  end

  File.open(target,'w') do |f|
    lines.each{|line| f.print line}
  end
end

.split_files(target, input_ipynb, thesis_or_handout) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/nb_util/ipynb2tex.rb', line 212

def split_files(target, input_ipynb, thesis_or_handout)
  target_parent = File.absolute_path("../..", target)
  ipynb = JSON.parse(File.read(input_ipynb))
  pickup_ipynb = ipynb["cells"].to_s.split(",")
  chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '').gsub(/\\n/, '') }
  if thesis_or_handout == "thesis"
    chapter_size = chapter.size
    p chapter
    for num in 0..chapter_size-1 do
      splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num+1}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num+1}")],
        ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
      cont = File.read(target)
      splitters.reverse.each do |splitter|
        split = cont.split(splitter[0])
        split[1].to_s.gsub!(/subsection/, 'section')
        split[1].to_s.gsub!(/subsubsection/, 'subsection')
        split[1].to_s.gsub!(/paragraph/, 'subsubsection')
        cont = split[0]

        File.open(splitter[1], 'w') do |f|
          f.print splitter[0].sub(/section/, 'chapter').gsub(/_/,'\_')
          if num+1 != chapter_size
            f.print split[1].to_s.sub(/\\section{#{chapter[num+1]}}\\label.*/m, '')
          end
          if num+1 == chapter_size
            f.print split[1].to_s
          end
        end
      end
      FileUtils.mv(target_parent + "/chapter#{num+1}.tex", target_parent + "/split_files/chapter#{num+1}")
    end
  end

  if thesis_or_handout == "handout"
    section_size = chapter.size
    for num in 0..section_size-1 do
      splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num+1}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num+1}")],
        ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
      cont = File.read(target)
      splitters.reverse.each do |splitter|
        split = cont.split(splitter[0])
        cont = split[0]
        File.open(splitter[1], 'w') do |f|
          f.print splitter[0]
          if num+1 != section_size
            f.print split[1].to_s.sub!(/ \\section{#{chapter[num+1]}}\\label.*/m, '')
          end
          if num+1 == section_size
            f.print split[1].to_s
          end
        end
      end
      FileUtils.mv(target_parent + "/chapter#{num+1}.tex", target_parent + "/split_files/chapter#{num+1}")
    end
  end
end

.yaml2ipynb(argv0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/nb_util/yaml2ipynb.rb', line 10

def yaml2ipynb(argv0)
  input_filename = ARGV[1]
  output_filename = ARGV[2] || input_filename.gsub(/(yml|yaml)$/, 'ipynb')
  cont = YAML.load(File.read(ARGV[1]))
  contsinglehash = flatten_hash_from cont
  #pp contsinglehash

  contsinglehashneed = contsinglehash.select{|k, v| k.match(/title/) || k.match(/cont/) }
  pp contsinglehashneed

  contdiffstring = contsinglehashneed.to_s

  contdiffstring.gsub!(/=>/,'')
    str = contsinglehashneed.keys
    contnum=str.count
    for i in 0..contnum do
      ignore = str[i].to_s
      p ignore
      contdiffstring.sub!(/#{ignore}/,'')
  end
  contdiffstring.gsub!(/:\"\"\"/,'# ')
  contdiffstring.gsub!(/\[/,'')
  contdiffstring.gsub!(/\]/,'')
  contdiffstring.gsub!(/{/,'')
  contdiffstring.gsub!(/}/,'')
  contdiffstring.gsub!(/\", :\"\"\"/,'\n')
  contdiffstring.gsub!(/\"/,'')
  contdiffstring.gsub!(/,/,'\n')
  pp contdiffstring

  #cellのデータ
  cellsData = <<EOS
{
 "cell_type": "markdown",
 "metadata": {},
 "source": [
  "#{contdiffstring}"
 ]
}
EOS

  meta = <<-EOS
{
 "cells": [
{
 "cell_type": "markdown",
 "metadata": {},
 "source": [
  "test code"
 ]
},
#{cellsData}
 ],
 "metadata": {
"kernelspec": {
 "display_name": "Python 3",
 "language": "python",
 "name": "python3"
},
"language_info": {
 "codemirror_mode": {
  "name": "ipython",
  "version": 3
 },
 "file_extension": ".py",
 "mimetype": "text/x-python",
 "name": "python",
 "nbconvert_exporter": "python",
 "pygments_lexer": "ipython3",
 "version": "3.6.2"
},
"latex_envs": {
 "LaTeX_envs_menu_present": true,
 "autocomplete": true,
 "bibliofile": "biblio.bib",
 "cite_by": "apalike",
 "current_citInitial": 1,
 "eqLabelWithNumbers": true,
 "eqNumInitial": 1,
 "hotkeys": {
  "equation": "Ctrl-E",
  "itemize": "Ctrl-I"
 },
 "labels_anchors": false,
 "latex_user_defs": false,
 "report_style_numbering": false,
 "user_envs_cfg": false
},
"nbTranslate": {
 "displayLangs": [
  "*"
 ],
 "hotkey": "alt-t",
 "langInMainMenu": true,
 "sourceLang": "en",
 "targetLang": "fr",
 "useGoogleTranslate": true
},
"toc": {
 "colors": {
  "hover_highlight": "#DAA520",
  "navigate_num": "#000000",
  "navigate_text": "#333333",
  "running_highlight": "#FF0000",
  "selected_highlight": "#FFD700",
  "sidebar_border": "#EEEEEE",
  "wrapper_background": "#FFFFFF"
 },
 "moveMenuLeft": true,
 "nav_menu": {
  "height": "12px",
  "width": "252px"
 },
 "navigate_menu": true,
 "number_sections": true,
 "sideBar": true,
 "threshold": 4,
 "toc_cell": false,
 "toc_section_display": "block",
 "toc_window_display": true,
 "widenNotebook": false
},
"varInspector": {
 "cols": {
  "lenName": 16,
  "lenType": 16,
  "lenVar": 40
 },
 "kernels_config": {
  "python": {
   "delete_cmd_postfix": "",
   "delete_cmd_prefix": "del ",
   "library": "var_list.py",
   "varRefreshCmd": "print(var_dic_list())"
  },
  "r": {
   "delete_cmd_postfix": ") ",
   "delete_cmd_prefix": "rm(",
   "library": "var_list.r",
   "varRefreshCmd": "cat(var_dic_list()) "
  }
 },
 "types_to_exclude": [
  "module",
  "function",
  "builtin_function_or_method",
  "instance",
  "_Feature"
 ],
 "window_display": false
}
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
EOS

  File.open(output_filename, 'w+') do |f|
    f.print(meta)
  end
end

.your_informations(target, thesis_or_handout) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/nb_util/ipynb2tex.rb', line 328

def your_informations(target, thesis_or_handout)
  info = Array.new(3)

  print "thesis title: "
  info[0] = STDIN.gets.to_s.chomp.gsub(/_/,'\_')
  print "student number(eight-digit): "
  info[1] = STDIN.gets.to_s.chomp.gsub(/_/,'\_')
  print "your name: "
  info[2] = STDIN.gets.to_s.chomp.gsub(/_/,'\_')

  target_parent = File.dirname(target)
  d = Date.today
  thesis_infomations = <<"EOS"
\\title{卒業論文\\\\#{info[0]}}
\\author{関西学院大学理工学部\\\\情報科学科 西谷研究室\\\\#{info[1]} #{info[2]}}
\\date{#{d.year}年3月}
\\begin{document}
\\maketitle
\\newpage
EOS
  handout_infomations = <<"EOS"
\\title{{\\large#{d.year}年度 卒業論文審査}\\\\#{info[0]}}
\\author{関西学院大学理工学部\\\\情報科学科 西谷研究室 #{info[1]} #{info[2]}}
\\date{}
\\begin{document}
\\maketitle
EOS
  FileUtils.mkdir_p(target_parent + '/split_files/informations')
  File.open(target_parent + '/informations.tex', "w") do |f|
    if thesis_or_handout == "thesis"
      f.print(thesis_infomations)
    end
    if thesis_or_handout == "handout"
      f.print(handout_infomations)
    end
  end
end