nb_util 0.3.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,19 +8,19 @@ require 'open3'
8
8
 
9
9
  module NbUtil
10
10
  module_function
11
- def ipynb2tex(target)
11
+ def ipynb2tex_thesis(target)
12
12
  loop do
13
- your_informations(ARGV[1])
13
+ your_informations(ARGV[1], "thesis")
14
14
  print "Are you ok with it?: "
15
15
  input = STDIN.gets.to_s.chomp
16
16
  if input == 'Y' || input == 'y'
17
17
  location = Open3.capture3("gem environment gemdir")
18
18
  versions = Open3.capture3("gem list nb_util")
19
19
  latest_version = versions[0].split(",")
20
- p cp_lib_data_thesis_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis")
21
- p cp_lib_data_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/pieces")
20
+ cp_lib_data_thesis_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis")
21
+ cp_lib_data_thesis_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis_pieces")
22
22
  cp_lib_data_thesis_bundle = File.join(Dir.pwd, '/lib/data/thesis')
23
- cp_lib_data_pieces_bundle = File.join(Dir.pwd, '/lib/data/pieces')
23
+ cp_lib_data_thesis_pieces_bundle = File.join(Dir.pwd, '/lib/data/thesis_pieces')
24
24
  re_fig = /(.+\.jpg)|(.+\.jpeg)|(.+\.png)/
25
25
 
26
26
  print "\e[32minputfile: \e[0m"
@@ -43,34 +43,25 @@ module NbUtil
43
43
 
44
44
  FileUtils.mkdir_p(target_parent + '/latex')
45
45
  FileUtils.mv(tex_src, target_parent + '/latex')
46
- replace_figs(File.join(target_parent + '/latex', target_basename))
46
+ replace_figs(File.join(target_parent + '/latex', target_basename), "thesis")
47
47
  revise_lines(File.join(target_parent + '/latex', target_basename))
48
- split_files(File.join(target_parent + '/latex', target_basename), target)
48
+ split_files(File.join(target_parent + '/latex', target_basename), target, "thesis")
49
49
  FileUtils.mv(target_parent + '/tmp.tex', target_parent + '/split_files/tmp')
50
50
  FileUtils.mv(target_parent + '/informations.tex', target_parent + '/split_files/informations')
51
- mk_thesis_location(target)
51
+ mk_thesis_location(target, "thesis")
52
52
  FileUtils.mv(target_parent + '/.splits_location.tex', target_parent + '/thesis')
53
53
 
54
54
  mk_xbb(target, re_fig)
55
55
 
56
- if Dir.exist?(cp_lib_data_pieces_bundle.to_s)
57
- FileUtils.cp_r(cp_lib_data_pieces_bundle, target_parent)
56
+ if Dir.exist?(cp_lib_data_thesis_pieces_bundle.to_s) && Dir.exist?(cp_lib_data_thesis_bundle.to_s)
57
+ FileUtils.cp_r(cp_lib_data_thesis_pieces_bundle, target_parent)
58
58
  FileUtils.cp_r(cp_lib_data_thesis_bundle, target_parent)
59
59
  else
60
- FileUtils.cp_r(cp_lib_data_pieces_gem, target_parent)
60
+ FileUtils.cp_r(cp_lib_data_thesis_pieces_gem, target_parent)
61
61
  FileUtils.cp_r(cp_lib_data_thesis_gem, target_parent)
62
62
  end
63
63
 
64
- =begin
65
- if (Open3.capture3("bundle exec exe/nb_util ipynb2tex #{target}")) then
66
- FileUtils.cp_r(cp_lib_data_pieces_bundle, target_parent)
67
- FileUtils.cp_r(cp_lib_data_thesis_bundle, target_parent)
68
- else
69
- FileUtils.cp_r(cp_lib_data_pieces_gem, target_parent)
70
- FileUtils.cp_r(cp_lib_data_thesis_gem, target_parent)
71
- end
72
- =end
73
- mk_latex_and_mv_to_latex(target, target_parent)
64
+ mk_latex_and_mv_to_latex(target, target_parent, "thesis")
74
65
  Open3.capture3("open #{target_parent}")
75
66
  Open3.capture3("open #{target_parent}/mk_latex/thesis/thesis.tex/")
76
67
 
@@ -84,6 +75,72 @@ module NbUtil
84
75
  end
85
76
  end
86
77
 
78
+ def ipynb2tex_handout(target)
79
+ loop do
80
+ your_informations(ARGV[1], "handout")
81
+ print "Are you ok with it?: "
82
+ input = STDIN.gets.to_s.chomp
83
+ if input == 'Y' || input == 'y'
84
+ location = Open3.capture3("gem environment gemdir")
85
+ versions = Open3.capture3("gem list nb_util")
86
+ latest_version = versions[0].split(",")
87
+ cp_lib_data_handout_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/handout")
88
+ cp_lib_data_handout_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/handout_pieces")
89
+ cp_lib_data_handout_bundle = File.join(Dir.pwd, '/lib/data/handout')
90
+ cp_lib_data_handout_pieces_bundle = File.join(Dir.pwd, '/lib/data/handout_pieces')
91
+ re_fig = /(.+\.jpg)|(.+\.jpeg)|(.+\.png)/
92
+
93
+ print "\e[32minputfile: \e[0m"
94
+ target = File.expand_path(ARGV[1])
95
+ print "\e[32m#{target}\n\e[0m"
96
+ print "\e[32moutputfile: \e[0m"
97
+ tex_src = target.sub('.ipynb', '.tex')
98
+ print "\e[32m#{tex_src}\n\e[0m"
99
+ target_parent = File.dirname(target)
100
+ target_basename = File.basename(tex_src)
101
+ Open3.capture3("jupyter nbconvert --to latex #{target}")
102
+ lines = File.readlines(tex_src)
103
+ lines.each_with_index do |line, i|
104
+ line.sub!("\documentclass[11pt]{article}",
105
+ "\documentclass[11pt,dvipdfmx]{jsarticle}")
106
+ print "\e[32m#{line}\n\e[0m" if line =~ re_fig #redにする"\e[31m\e[0m"
107
+ line.sub!(line, '%' + line) if line.include?('.svg')
108
+ end
109
+ File.open(tex_src, 'w') { |file| file.print lines.join }
110
+
111
+ FileUtils.mkdir_p(target_parent + '/latex')
112
+ FileUtils.mv(tex_src, target_parent + '/latex')
113
+ replace_figs(File.join(target_parent + '/latex', target_basename), "handout")
114
+ revise_lines(File.join(target_parent + '/latex', target_basename))
115
+ split_files(File.join(target_parent + '/latex', target_basename), target, "handout")
116
+ FileUtils.mv(target_parent + '/tmp.tex', target_parent + '/split_files/tmp')
117
+ FileUtils.mv(target_parent + '/informations.tex', target_parent + '/split_files/informations')
118
+ mk_thesis_location(target, "handout")
119
+ FileUtils.mv(target_parent + '/.splits_location.tex', target_parent + '/handout')
120
+
121
+ mk_xbb(target, re_fig)
122
+ if Dir.exist?(cp_lib_data_handout_pieces_bundle.to_s) && Dir.exist?(cp_lib_data_handout_bundle.to_s)
123
+ FileUtils.cp_r(cp_lib_data_handout_pieces_bundle, target_parent)
124
+ FileUtils.cp_r(cp_lib_data_handout_bundle, target_parent)
125
+ else
126
+ FileUtils.cp_r(cp_lib_data_handout_pieces_gem, target_parent)
127
+ FileUtils.cp_r(cp_lib_data_handout_gem, target_parent)
128
+ end
129
+
130
+ mk_latex_and_mv_to_latex(target, target_parent, "handout")
131
+ Open3.capture3("open #{target_parent}")
132
+ Open3.capture3("open #{target_parent}/mk_latex/handout/handout.tex/")
133
+
134
+ exit
135
+ break
136
+ elsif input == 'N' || input == 'n'
137
+ target_parent = File.dirname(target)
138
+ FileUtils.rm_r(File.join(target_parent.to_s, '/informations.tex'))
139
+ break
140
+ end
141
+ end
142
+ end
143
+
87
144
  def revise_lines(target)
88
145
  bugs = [['\end{quote}',:chomp]]
89
146
  lines = File.readlines(target)
@@ -101,38 +158,63 @@ module NbUtil
101
158
  end
102
159
  end
103
160
 
104
- def split_files(target, input_ipynb)
161
+ def split_files(target, input_ipynb, thesis_or_handout)
105
162
  target_parent = File.absolute_path("../..", target)
106
163
  ipynb = JSON.parse(File.read(input_ipynb))
107
164
  pickup_ipynb = ipynb["cells"].to_s.split(",")
108
165
  chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '') }
109
- chapter_size = chapter.size
110
- for num in 0..chapter_size-1 do
111
- splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num}")],
112
- ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
113
- cont = File.read(target)
114
- splitters.reverse.each do |splitter|
115
- split = cont.split(splitter[0])
116
- split[1].to_s.gsub!(/subsection/, 'section')
117
- split[1].to_s.gsub!(/subsubsection/, 'subsection')
118
- split[1].to_s.gsub!(/paragraph/, 'subsubsection')
119
- cont = split[0]
120
-
121
- File.open(splitter[1], 'w') do |f|
122
- f.print splitter[0].gsub!(/section/, 'chapter')
123
- if num+1 != chapter_size
124
- f.print split[1].sub!(/ \\section{#{chapter[num+1]}}\\label.*/m, '')
166
+ if thesis_or_handout == "thesis"
167
+ chapter_size = chapter.size
168
+ for num in 0..chapter_size-1 do
169
+ splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num}")],
170
+ ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
171
+ cont = File.read(target)
172
+ splitters.reverse.each do |splitter|
173
+ split = cont.split(splitter[0])
174
+ split[1].to_s.gsub!(/subsection/, 'section')
175
+ split[1].to_s.gsub!(/subsubsection/, 'subsection')
176
+ split[1].to_s.gsub!(/paragraph/, 'subsubsection')
177
+ cont = split[0]
178
+
179
+ File.open(splitter[1], 'w') do |f|
180
+ f.print splitter[0].gsub!(/section/, 'chapter')
181
+ if num+1 != chapter_size
182
+ f.print split[1].sub!(/ \\section{#{chapter[num+1]}}\\label.*/m, '')
183
+ end
184
+ if num+1 == chapter_size
185
+ f.print split[1]
186
+ end
125
187
  end
126
- if num+1 == chapter_size
127
- f.print split[1]
188
+ end
189
+ FileUtils.mv(target_parent + "/chapter#{num}.tex", target_parent + "/split_files/chapter#{num}")
190
+ end
191
+ end
192
+
193
+ if thesis_or_handout == "handout"
194
+ section_size = chapter.size
195
+ for num in 0..section_size-1 do
196
+ splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num}")],
197
+ ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
198
+ cont = File.read(target)
199
+ splitters.reverse.each do |splitter|
200
+ split = cont.split(splitter[0])
201
+ cont = split[0]
202
+ File.open(splitter[1], 'w') do |f|
203
+ f.print splitter[0]
204
+ if num+1 != section_size
205
+ f.print split[1].sub!(/ \\section{#{chapter[num+1]}}\\label.*/m, '')
206
+ end
207
+ if num+1 == section_size
208
+ f.print split[1]
209
+ end
128
210
  end
129
211
  end
212
+ FileUtils.mv(target_parent + "/chapter#{num}.tex", target_parent + "/split_files/chapter#{num}")
130
213
  end
131
- FileUtils.mv(target_parent + "/chapter#{num}.tex", target_parent + "/split_files/chapter#{num}")
132
214
  end
133
215
  end
134
216
 
135
- def replace_figs(target)
217
+ def replace_figs(target, thesis_or_handout)
136
218
  lines = File.readlines(target)
137
219
  counter = -1
138
220
  # settings of each
@@ -147,26 +229,34 @@ module NbUtil
147
229
  file_name, label, size, top, bottom = [m[1], data[counter]].flatten
148
230
  caption = lines[i + 1]
149
231
  label_name = file_name.to_s.gsub('figs', '').gsub('.png', '').gsub('/', '')
150
- wrap_figs = <<"EOS"
232
+ if thesis_or_handout == "thesis"
233
+ thesis_wrap_figs = <<"EOS"
151
234
  \\begin{center}
152
235
  \\includegraphics[width=#{size}mm]{../../#{file_name}}
153
236
  \\end{center}
154
237
  #{caption}
155
238
  \\label{fig:#{label}}
156
239
  EOS
157
- # \\vspace{#{top}\\baselineskip}
158
- # \\vspace{#{bottom}\\baselineskip}
159
-
160
- lines[i] = wrap_figs
240
+ end
241
+ if thesis_or_handout == "handout"
242
+ thesis_wrap_figs = <<"EOS"
243
+ \\begin{center}
244
+ \\includegraphics[width=80mm]{../../#{file_name}}
245
+ \\end{center}
246
+ #{caption}
247
+ \\label{fig:#{label}}
248
+ EOS
249
+ end
250
+ lines[i] = thesis_wrap_figs
161
251
  lines.delete_at(i + 1) # if no caption, comment out here
162
252
  end
163
253
  end
164
-
165
254
  File.open(target, 'w') do |f|
166
255
  lines.each{|line| f.print line}
167
256
  end
168
257
  end
169
258
 
259
+
170
260
  def mk_xbb(target, re_fig)
171
261
  target_parent = File.absolute_path("../..", target)
172
262
  FileUtils.mkdir_p(target_parent + '/figs')
@@ -181,7 +271,7 @@ EOS
181
271
  end
182
272
  end
183
273
 
184
- def your_informations(target)
274
+ def your_informations(target, thesis_or_handout)
185
275
  info = Array.new(3)
186
276
 
187
277
  print "thesis title: "
@@ -193,51 +283,95 @@ EOS
193
283
 
194
284
  target_parent = File.dirname(target)
195
285
  d = Date.today
196
- infomations = <<"EOS"
286
+ thesis_infomations = <<"EOS"
197
287
  \\title{卒業論文\\\\#{info[0]}}
198
288
  \\author{関西学院大学理工学部\\\\情報科学科 西谷研究室\\\\#{info[1]} #{info[2]}}
199
289
  \\date{#{d.year}年3月}
200
290
  \\begin{document}
201
291
  \\maketitle
202
292
  \\newpage
293
+ EOS
294
+ handout_infomations = <<"EOS"
295
+ \\title{{\\large#{d.year}年度 卒業論文審査}\\\\#{info[0]}}
296
+ \\author{関西学院大学理工学部\\\\情報科学科 西谷研究室 #{info[1]} #{info[2]}}
297
+ \\date{}
298
+ \\begin{document}
299
+ \\maketitle
203
300
  EOS
204
301
  FileUtils.mkdir_p(target_parent + '/split_files/informations')
205
302
  File.open(target_parent + '/informations.tex', "w") do |f|
206
- f.print(infomations)
303
+ if thesis_or_handout == "thesis"
304
+ f.print(thesis_infomations)
305
+ end
306
+ if thesis_or_handout == "handout"
307
+ f.print(handout_infomations)
308
+ end
207
309
  end
208
310
  end
209
311
 
210
- def mk_latex_and_mv_to_latex(target, target_parent)
312
+ def mk_latex_and_mv_to_latex(target, target_parent, thesis_or_handout)
211
313
  mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex'))
212
- if Dir.exist?(File.join(mk_latex[0].to_s, '/pieces'))
213
- d = Date.today
214
- old_file = File.join(File.dirname(target),"/old/#{d.year}#{d.month}#{d.day}")
215
- FileUtils.mkdir_p(old_file)
216
- FileUtils.cp_r(mk_latex[0], old_file)
217
- FileUtils.rm_r(mk_latex[0])
314
+ d = DateTime.now
315
+ if thesis_or_handout == "thesis"
316
+ if Dir.exist?(File.join(mk_latex[0].to_s, '/thesis'))
317
+ old_file = File.join(File.dirname(target),"/old/thesis/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
318
+ FileUtils.mkdir_p(old_file)
319
+ FileUtils.cp_r(mk_latex[0], old_file)
320
+ FileUtils.rm_r(mk_latex[0])
321
+ elsif Dir.exist?(File.join(mk_latex[0].to_s, '/handout'))
322
+ old_file = File.join(File.dirname(target),"/old/handout/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
323
+ FileUtils.mkdir_p(old_file)
324
+ FileUtils.cp_r(mk_latex[0], old_file)
325
+ FileUtils.rm_r(mk_latex[0])
326
+ end
327
+ mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target), '/mk_latex'))
328
+ split_files = File.join(target_parent, '/split_files')
329
+ thesis_pieces = File.join(target_parent, '/thesis_pieces')
330
+ FileUtils.mv(thesis_pieces, mk_latex[0])
331
+ thesis = File.join(target_parent, '/thesis')
332
+ latex = File.join(target_parent, '/latex')
333
+ FileUtils.mv(split_files, File.join(mk_latex[0], "/split_files"))
334
+ FileUtils.mv(latex, mk_latex[0])
335
+ FileUtils.mv(thesis, mk_latex[0])
218
336
  end
219
- mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex'))
220
-
221
- #p split_files = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex/split_files'))
222
- split_files = File.join(target_parent, '/split_files')
223
- pieces = File.join(target_parent, '/pieces')
224
- thesis = File.join(target_parent, '/thesis')
225
- latex = File.join(target_parent, '/latex')
226
337
 
227
- FileUtils.mv(split_files, File.join(mk_latex[0], "/split_files"))
228
- FileUtils.mv(pieces, mk_latex[0])
229
- FileUtils.mv(thesis, mk_latex[0])
230
- FileUtils.mv(latex, mk_latex[0])
338
+ if thesis_or_handout == "handout"
339
+ if Dir.exist?(File.join(mk_latex[0].to_s, '/thesis'))
340
+ old_file = File.join(File.dirname(target),"/old/thesis/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
341
+ FileUtils.mkdir_p(old_file)
342
+ FileUtils.cp_r(mk_latex[0], old_file)
343
+ FileUtils.rm_r(mk_latex[0])
344
+ elsif Dir.exist?(File.join(mk_latex[0].to_s, '/handout'))
345
+ old_file = File.join(File.dirname(target),"/old/handout/#{d.year}#{d.month}#{d.day}#{d.hour}#{d.min}")
346
+ FileUtils.mkdir_p(old_file)
347
+ FileUtils.cp_r(mk_latex[0], old_file)
348
+ FileUtils.rm_r(mk_latex[0])
349
+ end
350
+ mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target), '/mk_latex'))
351
+ split_files = File.join(target_parent, '/split_files')
352
+ handout_pieces = File.join(target_parent, '/handout_pieces')
353
+ FileUtils.mv(handout_pieces, mk_latex[0])
354
+ handout = File.join(target_parent, '/handout')
355
+ latex = File.join(target_parent, '/latex')
356
+ FileUtils.mv(split_files, File.join(mk_latex[0], "/split_files"))
357
+ FileUtils.mv(latex, mk_latex[0])
358
+ FileUtils.mv(handout, mk_latex[0])
359
+ end
231
360
  end
232
361
 
233
- def mk_thesis_location(input_ipynb)
362
+ def mk_thesis_location(input_ipynb, thesis_or_handout)
234
363
  target_parent = File.dirname(input_ipynb)
235
364
  ipynb = JSON.parse(File.read(input_ipynb))
236
365
  pickup_ipynb = ipynb["cells"].to_s.split(",")
237
366
  chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '') }
238
367
  chapter_size = chapter.size
239
368
 
240
- FileUtils.mkdir_p(target_parent + '/thesis')
369
+ if thesis_or_handout == "thesis"
370
+ FileUtils.mkdir_p(target_parent + '/thesis')
371
+ end
372
+ if thesis_or_handout == "handout"
373
+ FileUtils.mkdir_p(target_parent + '/handout')
374
+ end
241
375
  File.open(target_parent + '/.splits_location.tex', "w") do |f|
242
376
  for num in 0..chapter_size-1 do
243
377
  f.print("\\input{../split_files/chapter#{num}/chapter#{num}}\n")
@@ -1,3 +1,3 @@
1
1
  module NbUtil
2
- VERSION = "0.3.8"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nb_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masatoshi Kowaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-21 00:00:00.000000000 Z
11
+ date: 2017-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,14 +88,18 @@ files:
88
88
  - bin/setup
89
89
  - docs/.ipynb_checkpoints/abst-checkpoint.ipynb
90
90
  - docs/CopyAppend_ipynb.ipynb
91
- - docs/how_to_set_up_mac.ipynb
92
91
  - exe/nb_util
93
92
  - lib/.DS_Store
94
93
  - lib/cli.rb
95
- - lib/data/pieces/form00_style.tex
96
- - lib/data/pieces/tightlist_setting.tex
97
- - lib/data/pieces/usepackage.tex
94
+ - lib/data/handout/handout.tex
95
+ - lib/data/handout_pieces/form00_style.tex
96
+ - lib/data/handout_pieces/tightlist_setting.tex
97
+ - lib/data/handout_pieces/usepackage.tex
98
+ - lib/data/thesis/thesis.log
98
99
  - lib/data/thesis/thesis.tex
100
+ - lib/data/thesis_pieces/form00_style.tex
101
+ - lib/data/thesis_pieces/tightlist_setting.tex
102
+ - lib/data/thesis_pieces/usepackage.tex
99
103
  - lib/nb_util.rb
100
104
  - lib/nb_util/.DS_Store
101
105
  - lib/nb_util/combine.rb