nb_util 0.3.4

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.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "nb_util"
4
+ require "nb_util/yaml2ipynb"
5
+ require "nb_util/combine"
6
+ require "nb_util/iputs"
7
+ require "nb_util/ipynb2tex"
8
+ require "nb_util/getcode"
9
+
10
+ NbUtil::CLI.start(ARGV)
Binary file
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+ require 'pp'
3
+ require 'yaml'
4
+ require 'json'
5
+ require 'thor'
6
+
7
+
8
+ module NbUtil
9
+ class CLI < Thor
10
+
11
+ desc "red WORD", "red words print." # コマンドの概要(サンプル)
12
+ def red(word) # コマンドはメソッドとして定義する
13
+ say(word, :red)
14
+ end
15
+
16
+ desc "yaml2ipynb [input filename]", "convert yaml to ipynb" # コマンドの使用例と、概要
17
+ def yaml2ipynb(argv0) # コマンドはメソッドとして定義する
18
+ NbUtil.yaml2ipynb(ARGV[1])
19
+ end
20
+
21
+ desc "combine [input file1] [input file2] [output filename]", "combine file1 and file2" # コマンドの使用例と、概要
22
+ def combine(argv0, argv1, argv2) # コマンドはメソッドとして定義する
23
+ NbUtil.combine(ARGV[1], ARGV[2], ARGV[3])
24
+ end
25
+
26
+ desc "iputs [filename]", "display ipynb file contents" # コマンドの使用例と、概要
27
+ def iputs(argv0) # コマンドはメソッドとして定義する
28
+ NbUtil.iputs(ARGV[1])
29
+ end
30
+
31
+ desc "getcode [filename]", "save in ruby format" # コマンドの使用例と、概要
32
+ def getcode(argv0) # コマンドはメソッドとして定義する
33
+ NbUtil.getcode(ARGV[1])
34
+ end
35
+
36
+ desc "ipynb2tex [filename]", "convert ipynb to tex" # コマンドの使用例と、概要
37
+ def ipynb2tex(argv0) # コマンドはメソッドとして定義する
38
+ NbUtil.ipynb2tex(ARGV[1])
39
+ NbUtil.revise_lines(ARGV[1])
40
+ NbUtil.split_files(ARGV[1])
41
+ NbUtil.replace_figs(ARGV[1])
42
+ NbUtil.your_informations(ARGV[1])
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ %スタイル,パッケージの設定
2
+ \usepackage[dvipdfmx]{graphicx}%図の挿入のためのパッケージ
3
+ \usepackage{amsmath}
4
+ \usepackage{setspace}
5
+ \usepackage{amssymb}
6
+ \usepackage{ascmac}
7
+ \usepackage{framed}
8
+ \usepackage{wrapfig}
9
+ \usepackage{graphicx}
10
+ \usepackage{lineno}
11
+
12
+ %余白の設定
13
+ \setlength{\textheight}{\paperheight}
14
+ \setlength{\topmargin}{4.6truemm}
15
+ \addtolength{\topmargin}{-\headheight}
16
+ \addtolength{\topmargin}{-\headsep}
17
+ \addtolength{\textheight}{-60truemm}
18
+ \setlength{\textwidth}{\paperwidth}
19
+ \setlength{\oddsidemargin}{-0.4truemm}
20
+ \setlength{\evensidemargin}{-0.4truemm}
21
+ \addtolength{\textwidth}{-50truemm}
22
+
23
+ %行間
24
+ \setstretch{1.4}
25
+
@@ -0,0 +1,2 @@
1
+ %\tightlistの定義(箇条書きの際に必要)
2
+ \def\tightlist{\itemsep1pt\parskip0pt\parsep0pt}
@@ -0,0 +1,14 @@
1
+ %スタイル,パッケージの設定
2
+ \usepackage[dvipdfmx]{graphicx}%図の挿入のためのパッケージ
3
+ \usepackage{amsmath}
4
+ \usepackage{setspace}
5
+ \usepackage{amssymb}
6
+ \usepackage{ascmac}
7
+ \usepackage{framed}
8
+ \usepackage{wrapfig}
9
+ \usepackage{graphicx}
10
+ \usepackage[subrefformat=parens]{subcaption}
11
+ \usepackage{amsmath,amssymb}
12
+ \usepackage{comment}
13
+ \usepackage{lineno}
14
+
@@ -0,0 +1,26 @@
1
+ \documentclass[12pt,a4]{jreport}%chapterが使えるスタイル
2
+ %フォーマットの設定,パッケージの呼び出し
3
+ \input{../pieces/usepackage}% pieces
4
+ \input{../pieces/form00_style}% pieces
5
+ \input{../pieces/tightlist_setting}% pieces
6
+ %参考文献の設定===========================================================
7
+ \renewcommand{\bibname}{参考文献}
8
+
9
+ %表紙======================================================================
10
+ \input{../split_files/informations/informations}
11
+
12
+ %概要======================================================================
13
+ %\input{../abstract/abstract}
14
+
15
+ %目次======================================================================
16
+ \tableofcontents
17
+
18
+ %本文======================================================================
19
+ \input{./.splits_location.tex}
20
+
21
+ %参考文献===================================================================
22
+ \begin{thebibliography}{9}
23
+ \bibitem{}
24
+ \end{thebibliography}
25
+
26
+ \end{document}
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'pp'
3
+ require 'yaml'
4
+ require 'json'
5
+ require "nb_util/version"
6
+ require 'cli'
7
+
8
+ module NbUtil
9
+ module_function
10
+
11
+ directry = "#{Dir.home}"
12
+ def get_name(str)
13
+ str.delete(' ').split(/[\/]/)
14
+ end
15
+ name = get_name(directry)
16
+ puts "nb_util says hello, #{name[2]} !!"
17
+ end
@@ -0,0 +1 @@
1
+ lib/nb_util/EAGLE@MAC.local.2953
Binary file
@@ -0,0 +1,26 @@
1
+ # join_ipynb.rb
2
+ # join ipynbs
3
+ require 'nb_util/version'
4
+ require 'cli'
5
+ require 'pp'
6
+ require 'json'
7
+
8
+ module NbUtil
9
+ module_function
10
+ def combine(argv0, argv1, argv2)
11
+ ipynb0 = JSON.load(File.read(ARGV[1]))
12
+ ipynb1 = JSON.load(File.read(ARGV[2]))
13
+
14
+ output_filename = ARGV[3]
15
+ p output_filename
16
+
17
+ ipynb0["cells"].each do |cell|
18
+ pp cell
19
+ ipynb1["cells"] << cell
20
+ end
21
+
22
+ File.open(output_filename + ".ipynb", 'w') do |target|
23
+ JSON.dump(ipynb1,target)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'nb_util/version'
2
+ require 'cli'
3
+ require 'json'
4
+ require 'pp'
5
+
6
+ module NbUtil
7
+ module_function
8
+ def getcode(argv0)
9
+ input_filename = ARGV[1]
10
+ ipynb = JSON.parse(File.read(input_filename))
11
+ ipynb_filename = ARGV[2] || input_filename.gsub(/(.ipynb)$/, '')
12
+ hash = {}
13
+ i = 0
14
+ ipynb["cells"].each do |k, v|
15
+ hash[i.to_s] = k
16
+ i += 1
17
+ end
18
+ for j in 0..i-1 do
19
+ var="@hash#{j}"
20
+ eval("#{var}={}")
21
+ hash[j.to_s].each do |k, v|
22
+ eval("#{var}[k] = v")
23
+ end
24
+ end
25
+
26
+ flag = 0
27
+ source_count = 0
28
+ @getcode = ""
29
+ for i in 0..j - 1 do
30
+ eval("if @hash#{i}[\"cell_type\"] != \"code\" then flag = 1 end")
31
+ if flag == 0 then
32
+ eval("puts @getcode = @hash#{i}[\"source\"]")
33
+ source_count = source_count + 1
34
+ output_filename = ipynb_filename + source_count.to_s + ipynb["metadata"]["language_info"]["file_extension"]
35
+ File.open(output_filename, 'w+') do |f|
36
+ f.puts(@getcode)
37
+ end
38
+ end
39
+ flag = 0
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ require 'nb_util/version'
2
+ require 'cli'
3
+ require 'json'
4
+
5
+ module NbUtil
6
+ module_function
7
+ def iputs(argv0)
8
+ ipynb = JSON.load(File.read(ARGV[1]))
9
+ ipynb.each do |cells|
10
+ next unless cells.include?("cells")
11
+ cells[1].each do |cell|
12
+ cell["source"].each do |line|
13
+ print line
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,242 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'nb_util/version'
3
+ require 'cli'
4
+ require 'json'
5
+ require 'fileutils'
6
+ require "date"
7
+ require 'open3'
8
+
9
+ module NbUtil
10
+ module_function
11
+ def ipynb2tex(target)
12
+ loop do
13
+ your_informations(ARGV[1])
14
+
15
+ puts ">上記の情報で実行する場合は「Y」、終了する場合は「N」を入力して下さい。"
16
+ input = STDIN.gets.to_s.chomp
17
+ if input == 'Y' || input == 'y'
18
+ location = Open3.capture3("gem environment gemdir")
19
+ versions = Open3.capture3("gem list nb_util")
20
+ latest_version = versions[0].split(",")
21
+ p cp_lib_data_thesis_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/thesis")
22
+ p cp_lib_data_pieces_gem = File.join(location[0].chomp, "/gems/#{latest_version[0].chomp.gsub(' (','-').gsub(')','')}/lib/data/pieces")
23
+ cp_lib_data_thesis_bundle = File.join(Dir.pwd, '/lib/data/thesis')
24
+ cp_lib_data_pieces_bundle = File.join(Dir.pwd, '/lib/data/pieces')
25
+ re_fig = /(.+\.jpg)|(.+\.jpeg)|(.+\.png)/
26
+
27
+ print "\e[32minputfile: \e[0m"
28
+ target = ARGV[1]
29
+ print "\e[32m#{target}\n\e[0m"
30
+ print "\e[32moutputfile: \e[0m"
31
+ tex_src = target.sub('.ipynb', '.tex')
32
+ print "\e[32m#{tex_src}\n\e[0m"
33
+ target_parent = File.dirname(target)
34
+ target_basename = File.basename(tex_src)
35
+ Open3.capture3("jupyter nbconvert --to latex #{target}")
36
+ lines = File.readlines(tex_src)
37
+ lines.each_with_index do |line, i|
38
+ line.sub!("\documentclass[11pt]{article}",
39
+ "\documentclass[11pt,dvipdfmx]{jsarticle}")
40
+ print "\e[32m#{line}\n\e[0m" if line =~ re_fig #redにする"\e[31m\e[0m"
41
+ line.sub!(line, '%' + line) if line.include?('.svg')
42
+ end
43
+ File.open(tex_src, 'w') { |file| file.print lines.join }
44
+
45
+ FileUtils.mkdir_p(target_parent + '/latex')
46
+ FileUtils.mv(tex_src, target_parent + '/latex')
47
+ replace_figs(File.join(target_parent + '/latex', target_basename))
48
+ revise_lines(File.join(target_parent + '/latex', target_basename))
49
+ split_files(File.join(target_parent + '/latex', target_basename), target)
50
+ FileUtils.mv(target_parent + '/tmp.tex', target_parent + '/split_files/tmp')
51
+ FileUtils.mv(target_parent + '/informations.tex', target_parent + '/split_files/informations')
52
+ mk_thesis_location(target)
53
+ FileUtils.mv(target_parent + '/.splits_location.tex', target_parent + '/thesis')
54
+
55
+ mk_xbb(target, re_fig)
56
+
57
+ if File.exist?(cp_lib_data_pieces_bundle) then
58
+ FileUtils.cp_r(cp_lib_data_pieces_bundle, target_parent)
59
+ FileUtils.cp_r(cp_lib_data_thesis_bundle, target_parent)
60
+ else
61
+ FileUtils.cp_r(cp_lib_data_pieces_gem, target_parent)
62
+ FileUtils.cp_r(cp_lib_data_thesis_gem, target_parent)
63
+ end
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)
74
+ Open3.capture3("open #{target_parent}")
75
+ Open3.capture3("open #{target_parent}/mk_latex/thesis/thesis.tex/")
76
+
77
+ exit
78
+ break
79
+ elsif input == 'N' || input == 'n'
80
+ p '作業を中断します'
81
+ break
82
+ else
83
+ p "「Y」又は「N」を入力して下さい"
84
+ end
85
+ end
86
+ end
87
+
88
+ def revise_lines(target)
89
+ bugs = [['\end{quote}',:chomp]]
90
+ lines = File.readlines(target)
91
+ lines.each do |line|
92
+ bugs.each do |bug|
93
+ if line.include?(bug[0])
94
+ p line
95
+ line.chomp!
96
+ end
97
+ end
98
+ end
99
+
100
+ File.open(target,'w') do |f|
101
+ lines.each{|line| f.print line}
102
+ end
103
+ end
104
+
105
+ def split_files(target, input_ipynb)
106
+ target_parent = File.absolute_path("../..", target)
107
+ ipynb = JSON.parse(File.read(input_ipynb))
108
+ pickup_ipynb = ipynb["cells"].to_s.split(",")
109
+ chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '') }
110
+ chapter_size = chapter.size
111
+
112
+ for num in 0..chapter_size-1 do
113
+ splitters = [ ["\\section{#{chapter[num]}}", target_parent + "/chapter#{num}.tex", FileUtils.mkdir_p(target_parent + "/split_files/chapter#{num}")],
114
+ ["\\begin{Verbatim}", target_parent + '/tmp.tex', FileUtils.mkdir_p(target_parent + '/split_files/tmp')]]
115
+ cont = File.read(target)
116
+ splitters.reverse.each do |splitter|
117
+ split = cont.split(splitter[0])
118
+ split[1].to_s.gsub!(/subsection/, 'section')
119
+ split[1].to_s.gsub!(/subsubsection/, 'subsection')
120
+ split[1].to_s.gsub!(/paragraph/, 'subsubsection')
121
+ cont = split[0]
122
+ puts split[1]
123
+ File.open(splitter[1], 'w') do |f|
124
+ f.print splitter[0].gsub!(/section/, 'chapter')
125
+ f.print split[1]
126
+
127
+
128
+ end
129
+ end
130
+ FileUtils.mv(target_parent + "/chapter#{num}.tex", target_parent + "/split_files/chapter#{num}")
131
+ end
132
+ end
133
+
134
+ def replace_figs(target)
135
+ lines = File.readlines(target)
136
+ counter = -1
137
+ # settings of each
138
+ data = [["This", 150, -4, 0]]
139
+ lines.each_with_index do |line, i|
140
+ lines[i] = " \\usepackage{wrapfig}\n"+line if line.include?("\\usepackage{graphicx}")
141
+ lines[i] = '%' + line if line.include?("\\renewcommand{\\includegraphics}")
142
+ lines[i] = '%' + line if line.include?("\\DeclareCaptionLabelFormat")
143
+ lines[i] = '%' + line if line.include?("\\captionsetup{labelformat=nolabel}")
144
+ if m = line.match(/\\includegraphics\{(.+)\}/)
145
+ counter += 1
146
+ file_name, label, size, top, bottom = [m[1], data[counter]].flatten
147
+ caption = lines[i + 1]
148
+ label_name = file_name.to_s.gsub('figs', '').gsub('.png', '').gsub('/', '')
149
+ wrap_figs = <<"EOS"
150
+ \\begin{wrapfigure}{r}{#{size}mm}
151
+ \\begin{center}
152
+ \\includegraphics[bb= 0 0 1024 768, width=#{size}mm]{../../#{file_name}}
153
+ #{caption}
154
+ \\label{fig:#{label_name}}
155
+ \\end{center}
156
+ \\end{wrapfigure}
157
+ EOS
158
+ # \\vspace{#{top}\\baselineskip}
159
+ # \\vspace{#{bottom}\\baselineskip}
160
+
161
+ lines[i] = wrap_figs
162
+ lines.delete_at(i + 1) # if no caption, comment out here
163
+ end
164
+ end
165
+
166
+ File.open(target, 'w') do |f|
167
+ lines.each{|line| f.print line}
168
+ end
169
+ end
170
+
171
+ def mk_xbb(target, re_fig)
172
+ target_parent = File.absolute_path("../..", target)
173
+ FileUtils.mkdir_p(target_parent + '/figs')
174
+ FileUtils.cd(target_parent + '/figs')
175
+ Dir.entries('.').each do |file|
176
+ next unless file =~ re_fig
177
+ m = file.split('.')[0..-2]
178
+ next if File.exist?(m.join('.') + '.xbb')
179
+ command = "extractbb #{file}"
180
+ p command
181
+ system command
182
+ end
183
+ end
184
+
185
+ def your_informations(target)
186
+ info = Array.new(3)
187
+
188
+ print "卒論の題目: "
189
+ info[0] = STDIN.gets.to_s.chomp
190
+ print "学籍番号(7桁): "
191
+ info[1] = STDIN.gets.to_s.chomp
192
+ print "あなたの名前: "
193
+ info[2] = STDIN.gets.to_s.chomp
194
+
195
+ target_parent = File.dirname(target)
196
+ d = Date.today
197
+ infomations = <<"EOS"
198
+ \\title{卒業論文\\\\#{info[0]}}
199
+ \\author{関西学院大学理工学部\\\\情報科学科 西谷研究室\\\\#{info[1]} #{info[2]}}
200
+ \\date{#{d.year}年3月}
201
+ \\begin{document}
202
+ \\maketitle
203
+ \\newpage
204
+ EOS
205
+ FileUtils.mkdir_p(target_parent + '/split_files/informations')
206
+ File.open(target_parent + '/informations.tex', "w") do |f|
207
+ f.print(infomations)
208
+ end
209
+ end
210
+
211
+ def mk_latex_and_mv_to_latex(target, target_parent)
212
+ mk_latex = File.join(File.dirname(target),'/mk_latex')
213
+ mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex'))
214
+ FileUtils.rm_r(mk_latex[0])
215
+ mk_latex = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex'))
216
+ #p split_files = FileUtils.mkdir_p(File.join(File.dirname(target),'/mk_latex/split_files'))
217
+ split_files = File.join(target_parent, '/split_files')
218
+ pieces = File.join(target_parent, '/pieces')
219
+ thesis = File.join(target_parent, '/thesis')
220
+ latex = File.join(target_parent, '/latex')
221
+
222
+ FileUtils.mv(split_files, mk_latex[0]+'/split_files')
223
+ FileUtils.mv(pieces, mk_latex[0])
224
+ FileUtils.mv(thesis, mk_latex[0])
225
+ FileUtils.mv(latex, mk_latex[0])
226
+ end
227
+
228
+ def mk_thesis_location(input_ipynb)
229
+ target_parent = File.dirname(input_ipynb)
230
+ ipynb = JSON.parse(File.read(input_ipynb))
231
+ pickup_ipynb = ipynb["cells"].to_s.split(",")
232
+ chapter = pickup_ipynb.grep(/"# /).map{ |i| i.gsub(/.*# /, '').gsub(/".*/, '') }
233
+ chapter_size = chapter.size
234
+
235
+ FileUtils.mkdir_p(target_parent + '/thesis')
236
+ File.open(target_parent + '/.splits_location.tex', "w") do |f|
237
+ for num in 0..chapter_size-1 do
238
+ f.print("\\input{../split_files/chapter#{num}/chapter#{num}}\n")
239
+ end
240
+ end
241
+ end
242
+ end