hikiutils 0.2.3.4 → 0.2.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/exe/hiki +1 -3
  3. data/exe/hiki_thor +0 -3
  4. data/exe/hiki_thor~ +8 -0
  5. data/exe/hiki~ +8 -0
  6. data/hikiutils.gemspec +1 -0
  7. data/hikiutils_bob/Rakefile +10 -27
  8. data/hikiutils_bob/figs/hikiutils_bob.004.jpeg +0 -0
  9. data/hikiutils_bob/{private_dir.graffle → hikiutils_bob.graffle} +1 -1
  10. data/hikiutils_bob/hikiutils_bob.key +0 -0
  11. data/hikiutils_bob/hikiutils_bob/hikiutils_bob.004.jpeg +0 -0
  12. data/hikiutils_bob/latex_all.hiki +7 -3
  13. data/hikiutils_bob/latex_dir/hikiutils_bob.log +99 -84
  14. data/hikiutils_bob/latex_dir/hikiutils_bob.pdf +0 -0
  15. data/hikiutils_bob/latex_dir/hikiutils_bob.synctex.gz +0 -0
  16. data/hikiutils_bob/latex_dir/hikiutils_bob.toc +14 -10
  17. data/hikiutils_bob/latex_dir/latex_all.aux +10 -10
  18. data/hikiutils_bob/latex_dir/latex_all.tex +14 -5
  19. data/hikiutils_bob/latex_dir/sync.aux +14 -8
  20. data/hikiutils_bob/latex_dir/sync.tex +70 -17
  21. data/hikiutils_bob/sync.hiki +44 -7
  22. data/hikiutils_yamane/Rakefile +32 -31
  23. data/hikiutils_yamane/abstract.hiki +1 -0
  24. data/hikiutils_yamane/backup/hikiutils%28optparse%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%29 +309 -0
  25. data/hikiutils_yamane/backup/hikiutils%28thor%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%29 +1 -0
  26. data/hikiutils_yamane/backup/hikiutils_yamane +703 -0
  27. data/hikiutils_yamane/backup/hikiutils_yamane.hiki +0 -0
  28. data/hikiutils_yamane/backup/hikiutils_yamane_#hikiutils_yamane +1 -0
  29. data/hikiutils_yamane/backup/hikiutils_yamane_command_table +19 -0
  30. data/hikiutils_yamane/backup/hikiutils_yamane_introduction +7 -0
  31. data/hikiutils_yamane/figs/hikiutils_yamane.001.jpeg +0 -0
  32. data/hikiutils_yamane/figs/hikiutils_yamane.001.jpg +0 -0
  33. data/hikiutils_yamane/figs/hikiutils_yamane.002.jpeg +0 -0
  34. data/hikiutils_yamane/figs/hikiutils_yamane.002.jpg +0 -0
  35. data/hikiutils_yamane/figs/hikiutils_yamane.003.jpeg +0 -0
  36. data/hikiutils_yamane/figs/hikiutils_yamane.003.jpg +0 -0
  37. data/hikiutils_yamane/figs/hikiutils_yamane.004.jpeg +0 -0
  38. data/hikiutils_yamane/figs/hikiutils_yamane.004.jpg +0 -0
  39. data/hikiutils_yamane/figs/hikiutils_yamane.005.jpeg +0 -0
  40. data/hikiutils_yamane/figs/hikiutils_yamane.005.jpg +0 -0
  41. data/hikiutils_yamane/figs/hikiutils_yamane.006.jpeg +0 -0
  42. data/hikiutils_yamane/figs/hikiutils_yamane.007.jpeg +0 -0
  43. data/hikiutils_yamane/hikiutils_yamane.hiki +37 -1
  44. data/hikiutils_yamane/hikiutils_yamane.key +0 -0
  45. data/hikiutils_yamane/hikiutils_yamane.key~11b9dba1272d2104b420497d11c6eda206ace6c2 +0 -0
  46. data/hikiutils_yamane/introduction.hiki +27 -0
  47. data/hikiutils_yamane/latex_dir/abstract.aux +22 -0
  48. data/hikiutils_yamane/latex_dir/abstract.tex +1 -0
  49. data/hikiutils_yamane/latex_dir/command_table.tex +1 -1
  50. data/hikiutils_yamane/latex_dir/head.tex +2 -2
  51. data/hikiutils_yamane/latex_dir/hikiutils_yamane.aux +3 -0
  52. data/hikiutils_yamane/latex_dir/hikiutils_yamane.log +224 -84
  53. data/hikiutils_yamane/latex_dir/hikiutils_yamane.pdf +0 -0
  54. data/hikiutils_yamane/latex_dir/hikiutils_yamane.synctex.gz +0 -0
  55. data/hikiutils_yamane/latex_dir/hikiutils_yamane.tex +16 -2
  56. data/hikiutils_yamane/latex_dir/hikiutils_yamane.toc +20 -0
  57. data/hikiutils_yamane/latex_dir/introduction.aux +26 -0
  58. data/hikiutils_yamane/latex_dir/introduction.tex +33 -0
  59. data/hikiutils_yamane/latex_dir/results.aux +45 -0
  60. data/hikiutils_yamane/latex_dir/results.tex +342 -0
  61. data/hikiutils_yamane/latex_dir/system.tex +23 -0
  62. data/hikiutils_yamane/results.hiki +376 -0
  63. data/hikiutils_yamane/system.hiki +18 -0
  64. data/lib/hikiutils.rb +7 -3
  65. data/lib/hikiutils.rb~ +387 -0
  66. data/lib/hikiutils/version.rb +1 -1
  67. data/lib/hikiutils_thor.rb +36 -20
  68. data/lib/hikiutils_thor.rb~ +324 -0
  69. data/lib/templates/Rakefile_hiki_sync +35 -31
  70. metadata +57 -3
@@ -0,0 +1 @@
1
+ !概要
@@ -0,0 +1,309 @@
1
+ {{toc}}
2
+ !hikiutils.rb
3
+ !!HIkiUtilsモジュール
4
+ !!!self.run(argv=[])メソッド
5
+ <<< ruby
6
+ print "hikiutils: provide utilities for helping hiki editing.\n"
7
+ new(argv).execute
8
+ >>>
9
+ *"hikiutils: provide utilities for helping hiki editing.\n"を表示
10
+ *実行する.
11
+ !!!initialize(argv=[])メソッド
12
+ <<< ruby
13
+ @argv = argv
14
+ @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri']
15
+ data_path = File.join(ENV['HOME'], '.hikirc')
16
+ DataFiles.prepare(data_path)
17
+ read_sources
18
+ >>>
19
+ *@argvにargv配列を代入
20
+ *@data_nameに'nick_name','local_dir','local_uri','global_dir','global_uri'を代入
21
+ *ディレクトリパスENV['HOME'](/Users/yamane)とファイル名.hikircをくっつけ,data_pathに代入する.
22
+ *DaraFilesモジュールでdata_path引数とし実行する..
23
+ *read_sourcesメソッドを実行する.
24
+ !!!executeメソッド
25
+ <<< ruby
26
+ @argv << '--help' if @argv.size==0
27
+ command_parser = OptionParser.new do |opt|
28
+ opt.on('-v', '--version','show program Version.') { |v|
29
+ opt.version = HikiUtils::VERSION
30
+ puts opt.ver
31
+ }
32
+ opt.on('-s', '--show','show sources') {show_sources}
33
+ opt.on('-a', '--add','add sources info') {add_sources }
34
+ opt.on('-t', '--target VAL','set target id') {|val| set_target(val) }
35
+ opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) }
36
+ opt.on('-l', '--list [FILE]','list files') {|file| list_files(file) }
37
+ opt.on('-u', '--update FILE','update file') {|file| update_file(file) }
38
+ opt.on('-r', '--rsync','rsync files') {rsync_files}
39
+ opt.on('--database FILE','read database file') {|file| db_file(file)}
40
+ opt.on('--display FILE','display converted hikifile') {|file| display(f\
41
+ ile)}
42
+ opt.on('-c', '--checkdb','check database file') {check_db}
43
+ opt.on('--remove FILE','remove file') {|file| remove_file(file)}
44
+ opt.on('--move FILES','move file1,file2',Array) {|files| move_file(file\
45
+ s)}
46
+ opt.on('--euc FILE','translate file to euc') {|file| euc_file(file) }
47
+ end
48
+ begin
49
+ command_parser.parse!(@argv)
50
+ rescue=> eval
51
+ p eval
52
+ end
53
+ dump_sources
54
+ exit
55
+ >>>
56
+ !!!display(file)メソッド
57
+ <<< ruby
58
+ body = HikiDoc.to_html(File.read(file))
59
+ source = HTML_TEMPLATE
60
+ title = File.basename(file)
61
+ erb = ERB.new(source)
62
+ t = File.open(file+".html",'w')
63
+ t.puts(erb.result(binding))
64
+ t.close
65
+ system "open #{t.path}"
66
+ >>>
67
+ !!!euc_file(file)メソッド
68
+ <<< ruby
69
+ p file_path = File.join(@l_dir,'text',file)
70
+ cont = File.readlines(file_path)
71
+ cont.each{|line| puts line.toeuc }
72
+ >>>
73
+ !!!move_file(files)メソッド
74
+ begin
75
+ p file1_path = File.join(@l_dir,'text',files[0])
76
+ p file2_path = File.join(@l_dir,'text',files[1])
77
+ rescue => evar
78
+ puts evar.to_s
79
+ puts "error on move_files, check the input format, especially comma separation."
80
+ exit
81
+ end
82
+ return if file1_path==file2_path
83
+ if File.exist?(file2_path) then
84
+ print ("moving target #{files[1]} exists.\n")
85
+ print ("first remove#{files[1]}.\n")
86
+ return
87
+ else
88
+ File.rename(file1_path,file2_path)
89
+ end
90
+ info=InfoDB.new(@l_dir)
91
+ db = info.db
92
+ pp file0=db[files[0]]
93
+ db.delete(files[0])
94
+ db[files[1]]=file0
95
+ db[files[1]][:title]=files[1] if db[files[1]][:title]==files[0]
96
+ pp db[files[1]]
97
+ db.each{|ele|
98
+ ref = ele[1][:references]
99
+ if ref.include?(files[0]) then
100
+ p link_file=ele[0]
101
+ link_path = File.join(@l_dir,'text',link_file)
102
+ cont = File.read(link_path)
103
+ if Kconv.iseuc(cont) then
104
+ print "euc\n"
105
+ utf8_cont=cont.toutf8
106
+ utf8_cont.gsub!(/#{files[0]}/,"#{files[1]}")
107
+ cont = utf8_cont.toeuc
108
+ else
109
+ cont.gsub!(/#{files[0]}/,"#{files[1]}")
110
+ end
111
+ File.write(link_path,cont)
112
+ ref.delete(files[0])
113
+ ref << files[1]
114
+ p cache_path = File.join(@l_dir,'cache/parser',link_file)
115
+ begin
116
+ File.delete(cache_path)
117
+ rescue => evar
118
+ puts evar.to_s
119
+ end
120
+ end
121
+ }
122
+ info.dump
123
+ >>>
124
+ !!!remove_file(file_name)メソッド
125
+ p text_path = File.join(@l_dir,'text',file_name)
126
+ p attach_path = File.join(@l_dir,'cache/attach',file_name)
127
+ begin
128
+ File.delete(text_path)
129
+ rescue => evar
130
+ puts evar.to_s
131
+ end
132
+ begin
133
+ Dir.rmdir(attach_path)
134
+ rescue => evar
135
+ puts evar.to_s
136
+ end
137
+ info=InfoDB.new(@l_dir)
138
+ p "delete "
139
+ del_file=info.delete(file_name)
140
+ info.show_link(file_name)
141
+ info.dump
142
+ >>>
143
+ !!!check_dbメソッド
144
+ <<< ruby
145
+ result= InfoDB.new(@l_dir).show_inconsist
146
+ print (result=='') ? "db agrees with text dir.\n" : result
147
+ >>>
148
+ !!!db_file(file_name)メソッド
149
+ <<< ruby
150
+ info=InfoDB.new(@l_dir)
151
+ p info.show(file_name)
152
+ >>>
153
+ !!!rsync_filesメソッド
154
+ <<< ruby
155
+ p local = @l_dir
156
+ p global = @src[:srcs][@target][:global_dir]
157
+ #"/Users/bob/Sites/nishitani0/Internal/data"
158
+ #"bob@dmz0:/Users/bob/nishitani0/Internal/data"
159
+ # p command="rsync -auvz -e ssh #{local}/ #{global}"
160
+ p command="rsync -auvz -e ssh #{local}/ #{global}"
161
+ # system 'rsync -auvz -e ssh ~/Sites/nishitani0 bob@nishitani0.kwansei.ac.jp:S\
162
+ ites/'
163
+ system command
164
+ >>>
165
+ !!!update_file(file0)メソッド
166
+ <<< ruby
167
+ file = (file0==nil) ? 'FrontPage' : file0
168
+ #rm cache file
169
+ t_file=File.join(@l_dir,'cache/parser',file)
170
+ FileUtils.rm(t_file,:verbose=>true)
171
+
172
+ #update info file
173
+ info=InfoDB.new(@l_dir)
174
+ info.update(file0)
175
+
176
+ #open file on browser
177
+ l_path = @src[:srcs][@target][:local_uri]
178
+ # p command="open -a #{@browser} \'#{l_path}/?c=edit;p=#{file}\'"
179
+ p command="open -a #{@browser} \'#{l_path}/?#{file}\'"
180
+ system command
181
+ p "If you get open error, try rackup from the src_dir."
182
+ p "If you get 整形式になっていません, try login as a valid user."
183
+ >>>
184
+ !!!list_files(file)メソッド
185
+ <<< ruby
186
+ file ='' if file==nil
187
+ t_file=File.join(@l_dir,'text')
188
+ print "target_dir : "+t_file+"\n"
189
+ print `cd #{t_file} ; ls -lt #{file}*`
190
+ >>>
191
+ !!!edit_file(file)メソッド
192
+ <<< ruby
193
+ t_file=File.join(@l_dir,'text',file)
194
+ if !File.exist?(t_file) then
195
+ file=File.open(t_file,'w')
196
+ file.close
197
+ File.chmod(0777,t_file)
198
+ end
199
+ p command="#{@editor_command} #{t_file}"
200
+ system command
201
+ >>>
202
+ !!!dump_sourcesメソッド
203
+ <<< ruby
204
+ file = File.open(DATA_FILE,'w')
205
+ YAML.dump(@src, file)
206
+ file.close
207
+ >>>
208
+ !!!set_target(val)メソッド
209
+ <<< ruby
210
+ @src[:target] = val.to_i
211
+ show_sources
212
+ >>>
213
+ !!!show_sources()メソッド
214
+ <<< ruby
215
+ printf("target_no:%i\n",@src[:target])
216
+ printf("editor_command:%s\n",@src[:editor_command])
217
+ check_display_size()
218
+ header = display_format('id','name','local directory','global uri')
219
+
220
+ puts header
221
+ puts '-' * header.size
222
+
223
+ @src[:srcs].each_with_index{|src,i|
224
+ target = i==@src[:target] ? '*':' '
225
+ id = target+i.to_s
226
+ name=src[:nick_name]
227
+ local=src[:local_dir]
228
+ global=src[:global_uri]
229
+ puts display_format(id,name,local,global)
230
+ }
231
+ >>>
232
+ !!!check_display_sizeメソッド
233
+ <<< ruby
234
+ name_length = @n_size-full_width_count(name)
235
+ local_length = @l_size-full_width_count(local)
236
+ global_string= global.size < @g_size ? global : global[0..@g_size]
237
+ [id.to_s.rjust(@i_size), name.ljust(name_length),
238
+ local.ljust(local_length),
239
+ global_string.ljust(@g_size)].join(' | ')
240
+ >>>
241
+ !!!full_width_count(string)メソッド
242
+ <<< ruby
243
+ string.each_char.select{|char| !(/[ -~。-゚]/.match(char))}.count
244
+ >>>
245
+ !!!add_sourcesメソッド
246
+ <<< ruby
247
+ cont = {}
248
+ @data_name.each{|name|
249
+ printf("%s ? ", name)
250
+ tmp = gets.chomp
251
+ cont[name.to_sym] = tmp
252
+ }
253
+ @src[:srcs] << cont
254
+ show_sources
255
+ >>>
256
+ !!!read_sourcesメソッド
257
+ <<< ruby
258
+ file = File.open(DATA_FILE,'r')
259
+ @src = YAML.load(file.read)
260
+ file.close
261
+ @target = @src[:target]
262
+ @l_dir=@src[:srcs][@target][:local_dir]
263
+ browser = @src[:browser]
264
+ @browser = (browser==nil) ? 'firefox' : browser
265
+ p editor_command = @src[:editor_command]
266
+ @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command
267
+ >>>
268
+ *DATA_FILEを読み込みモードで開き,fileに代入する.
269
+ *fileを読み込みYAML形式でロードし@srcに代入する.
270
+ *ファイルを閉じる.
271
+ *@src配列の:targetの値を@targetに代入する.
272
+ *@l_dirに@src[:srcs][@target][:local_dir]を代入.
273
+ *browserに@src配列の:browserの値を代入する.
274
+ *もしbrowserがnilであれば'firefox'を代入,そうでなければbrowserを@browserに代入する.
275
+ *editor_commandに@src配列の:editor_commandの値を代入し表示する.
276
+ *もしeditor_commandがnilであれば'open -a mi'を,そうでなければeditor_commandを@editor_commandに代入する.
277
+ !!DataFilesモジュール
278
+ !!!self.prepare(data_path)メソッド
279
+ <<< ruby
280
+ create_file_if_not_exists(data_path)
281
+ >>>
282
+ *create_file_if_not_exists(data_path)メソッドを実行する.
283
+ !!!self.create_file_if_not_exists(data_path)メソッド
284
+ <<< ruby
285
+ return if File::exists?(data_path)
286
+ create_data_file(data_path)
287
+ >>>
288
+ *もしdata_pathが存在すればそれを返す.
289
+ *そうでばければcreate_data_file(data_path)を実行する.
290
+ !!!self.create_data_file(data_path)メソッド
291
+ <<< ruby
292
+ print "make #{data_path}\n"
293
+ init_data_file(data_path)
294
+ >>>
295
+ *"make #{data_path}\n"を表示
296
+ *init_data_file(data_path)を実行する.
297
+ !!!self.init_data_file(data_path)メソッド
298
+ <<< ruby
299
+ @src = {:target => 0, :editor_command => 'open -a mi',
300
+ :srcs=>[{:nick_name => 'hoge', :local_dir => 'hogehoge', :local_uri => 'http://localhost/~hoge',
301
+ :global_dir => 'hoge@global_host:/hoge', :global_uri => 'http://hoge'}]}
302
+ file = File.open(data_path,'w')
303
+ YAML.dump(@src,file)
304
+ file.close
305
+ >>>
306
+ *@srcに代入
307
+ *data_pathを書き込みモードで開きfileに代入する.
308
+ *@srcをYAMLフォーマットに書き換え,fileに書き込む.
309
+ *ファイルを閉じる.
@@ -0,0 +1,703 @@
1
+ {{toc}}
2
+ !卒業論文:コマンドラインツール作成ライブラリThorによるhikiutilsの書き換え
3
+ !!関西学院大学理工学部情報科学科 27013554 山根亮太
4
+ !!概要
5
+ [[hikiutils_yamane_introduction]]
6
+ !!既存システム
7
+ [[system]]
8
+ !!hikiutils optparseバージョン
9
+ !!!optparseとは
10
+ optparseモジュールとは,getoptよりも簡便で,柔軟性に富み,かつ強力なコマンドライン解析ライブラリである.
11
+ optparseでは,より宣言的なスタイルのコマンドライン解析手法,すなわちOptionParserのインスタンスでコマンドラインを解析するという手法をとっている.
12
+ これを使うと,GNU/POSIX構文でオプションを指定できるだけでなく,使用法やヘルプメッセージの生成も行える.
13
+ !!!!optparseの基本的な流れ
14
+ ①OptionParserオブジェクトoptを生成する{{br}}
15
+ ②オプションを取り扱うブロックをoptに登録する{{br}}
16
+ ③opt.parse(ARGV)でコマンドラインを実際にparseする{{br}}
17
+ hikiutilsのoptparseバージョンを5つのファイル(hikiutils.rb,version.rb,tmarshal.rb,infodb.rb,hiki)で構成される.
18
+ !!!コードの解説
19
+ !!!!hiki
20
+ <<< ruby
21
+ #!/usr/bin/env ruby
22
+
23
+ require "hikiutils"
24
+
25
+ HikiUtils::Command.run(ARGV)
26
+ >>>
27
+ !!!!hikiutils.rb
28
+ <<< ruby
29
+ # -*- coding: utf-8 -*-
30
+ require 'kconv'
31
+ require 'hikidoc'
32
+ require 'erb'
33
+ require "hikiutils/version"
34
+ require "hikiutils/tmarshal"
35
+ require "hikiutils/infodb"
36
+ require 'systemu'
37
+ require 'optparse'
38
+ require 'fileutils'
39
+ require 'yaml'
40
+ require 'pp'
41
+
42
+ module HikiUtils
43
+ DATA_FILE=File.join(ENV['HOME'],'.hikirc')
44
+ attr_accessor :src, :target, :editor_command, :browser, :data_name, :l_dir
45
+
46
+ class Command
47
+
48
+
49
+ HTML_TEMPLATE = <<EOS
50
+ <!DOCTYPE html
51
+ PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
52
+ "http://www.w3.org/TR/html4/loose.dtd">
53
+ <html lang="ja">
54
+ <html>
55
+ <head>
56
+ <meta http-equiv="Content-Language" content="ja">
57
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
58
+ <title><%= title %></title>
59
+ </head>
60
+ <body>
61
+ <%= body %>
62
+ </body>
63
+ </html>
64
+ EOS
65
+
66
+ def self.run(argv=[])
67
+ print "hikiutils: provide utilities for helping hiki editing.\n"
68
+ new(argv).execute
69
+ end
70
+
71
+ def initialize(argv=[])
72
+ @argv = argv
73
+ @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri']
74
+ data_path = File.join(ENV['HOME'], '.hikirc')
75
+ DataFiles.prepare(data_path)
76
+ read_sources
77
+ end
78
+
79
+ def execute
80
+ @argv << '--help' if @argv.size==0
81
+ command_parser = OptionParser.new do |opt|
82
+ opt.on('-v', '--version','show program Version.') { |v|
83
+ opt.version = HikiUtils::VERSION
84
+ puts opt.ver
85
+ }
86
+ opt.on('-s', '--show','show sources') {show_sources}
87
+ opt.on('-a', '--add','add sources info') {add_sources }
88
+ opt.on('-t', '--target VAL','set target id') {|val| set_target(val)\
89
+ }
90
+ opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) }
91
+ opt.on('-l', '--list [FILE]','list files') {|file| list_files(file)\
92
+ }
93
+ opt.on('-u', '--update FILE','update file') {|file| update_file(fil\
94
+ e) }
95
+ opt.on('-r', '--rsync','rsync files') {rsync_files}
96
+ opt.on('--database FILE','read database file') {|file| db_file(file\
97
+ )}
98
+ opt.on('--display FILE','display converted hikifile') {|file| displ\
99
+ ay(file)}
100
+ opt.on('-c', '--checkdb','check database file') {check_db}
101
+ opt.on('--remove FILE','remove file') {|file| remove_file(file)}
102
+ opt.on('--move FILES','move file1,file2',Array) {|files| move_file(\
103
+ files)}
104
+ opt.on('--euc FILE','translate file to euc') {|file| euc_file(file)\
105
+ }
106
+ opt.on('--initialize','initialize source directory') {dir_init() }
107
+ end
108
+ begin
109
+ command_parser.parse!(@argv)
110
+ rescue=> eval
111
+ p eval
112
+ end
113
+ dump_sources
114
+ exit
115
+ end
116
+ >>>
117
+ {{attach_view(hogehoge.png, optparse_run)}}{{br}}
118
+ ①HikiのHikiUtils::Command.run(ARGV)でhikiutils.rbのrunメソッドを呼ぶ{{br}}
119
+ ②new(argv).executeでexecuteメソッドが実行される{{br}}
120
+ !!!!!seld.runメソッド
121
+ "hikiutils: provide utilities for helping hiki editing.\n"を表示し,executeメソッドを実行する
122
+ !!!!!initializeメソッド
123
+ !!!!!executeメソッド
124
+ OptionParserオブジェクトoptを生成し.optにコマンドを登録する.
125
+ !!!!!displayメソッド〜add_sourcesメソッド
126
+ <<< ruby
127
+ def dir_init()
128
+ begin
129
+ p target_dir = File.readlines('./.hikirc')[0]
130
+ rescue
131
+ p target_dir=@src[:srcs][@target][:local_dir]
132
+ File.open('./.hikirc','w'){|file| file.print "#{target_dir}\n"}
133
+ end
134
+ cp_files=[['Rakefile_hiki_sync','Rakefile'],
135
+ ['hiki_help.yml','hiki_help.yml']]
136
+ cp_files.each{|files|
137
+ p source = File.join(File.expand_path('..', __FILE__),'templates',files[0])
138
+ p target = File.join(Dir.pwd,files[1])
139
+ FileUtils.cp(source,target,:verbose=>true)
140
+ }
141
+ ['figs','data'].each{|dir|
142
+ begin
143
+ Dir.mkdir(dir)
144
+ rescue => e
145
+ print e
146
+ end
147
+ }
148
+ begin
149
+ p cont=File.read('./.gitignore')
150
+ unless cont.include?('.hikirc')
151
+ File.open('./.gitignore','w'){|file| file.print(".hikirc\n")}
152
+ end
153
+ rescue
154
+ File.open('./.gitignore','w'){|file| file.print(".hikirc\n")}
155
+ end
156
+ end
157
+
158
+ def display(file)
159
+ body = HikiDoc.to_html(File.read(file))
160
+ source = HTML_TEMPLATE
161
+ title = File.basename(file)
162
+ erb = ERB.new(source)
163
+ t = File.open(file+".html",'w')
164
+ t.puts(erb.result(binding))
165
+ t.close
166
+ system "open #{t.path}"
167
+ end
168
+
169
+ def euc_file(file)
170
+ p file_path = File.join(@l_dir,'text',file)
171
+ cont = File.readlines(file_path)
172
+ cont.each{|line| puts line.toeuc }
173
+ end
174
+
175
+ def move_file(files)
176
+ begin
177
+ p file1_path = File.join(@l_dir,'text',files[0])
178
+ p file2_path = File.join(@l_dir,'text',files[1])
179
+ rescue => evar
180
+ puts evar.to_s
181
+ puts "error on move_files, check the input format, especially comma separation."
182
+ exit
183
+ end
184
+ return if file1_path==file2_path
185
+ if File.exist?(file2_path) then
186
+ print ("moving target #{files[1]} exists.\n")
187
+ print ("first remove#{files[1]}.\n")
188
+ return
189
+ else
190
+ File.rename(file1_path,file2_path)
191
+ end
192
+
193
+ info=InfoDB.new(@l_dir)
194
+
195
+ db = info.db
196
+
197
+ pp file0=db[files[0]]
198
+ db.delete(files[0])
199
+ db[files[1]]=file0
200
+ db[files[1]][:title]=files[1] if db[files[1]][:title]==files[0]
201
+ pp db[files[1]]
202
+
203
+ db.each{|ele|
204
+ ref = ele[1][:references]
205
+ if ref.include?(files[0]) then
206
+ p link_file=ele[0]
207
+ link_path = File.join(@l_dir,'text',link_file)
208
+
209
+ cont = File.read(link_path)
210
+ if Kconv.iseuc(cont) then
211
+ print "euc\n"
212
+ utf8_cont=cont.toutf8
213
+ utf8_cont.gsub!(/#{files[0]}/,"#{files[1]}")
214
+ cont = utf8_cont.toeuc
215
+ else
216
+ cont.gsub!(/#{files[0]}/,"#{files[1]}")
217
+ end
218
+
219
+ File.write(link_path,cont)
220
+
221
+ ref.delete(files[0])
222
+ ref << files[1]
223
+
224
+ p cache_path = File.join(@l_dir,'cache/parser',link_file)
225
+ begin
226
+ File.delete(cache_path)
227
+ rescue => evar
228
+ puts evar.to_s
229
+ end
230
+ end
231
+ }
232
+
233
+ info.dump
234
+ end
235
+
236
+ def remove_file(file_name)
237
+ p text_path = File.join(@l_dir,'text',file_name)
238
+ p attach_path = File.join(@l_dir,'cache/attach',file_name)
239
+ begin
240
+ File.delete(text_path)
241
+ rescue => evar
242
+ puts evar.to_s
243
+ end
244
+ begin
245
+ Dir.rmdir(attach_path)
246
+ rescue => evar
247
+ puts evar.to_s
248
+ end
249
+
250
+ info=InfoDB.new(@l_dir)
251
+ p "delete "
252
+ del_file=info.delete(file_name)
253
+ info.show_link(file_name)
254
+ info.dump
255
+ end
256
+
257
+ def check_db
258
+ result= InfoDB.new(@l_dir).show_inconsist
259
+ print (result=='') ? "db agrees with text dir.\n" : result
260
+ end
261
+
262
+ def db_file(file_name)
263
+ info=InfoDB.new(@l_dir)
264
+ p info.show(file_name)
265
+ end
266
+
267
+ def rsync_files
268
+ p local = @l_dir
269
+ p global = @src[:srcs][@target][:global_dir]
270
+ #"/Users/bob/Sites/nishitani0/Internal/data"
271
+ #"bob@dmz0:/Users/bob/nishitani0/Internal/data"
272
+ # p command="rsync -auvz -e ssh #{local}/ #{global}"
273
+ p command="rsync -auvz -e ssh #{local}/ #{global}"
274
+ # system 'rsync -auvz -e ssh ~/Sites/nishitani0 bob@nishitani0.kwansei.ac.jp:Sites/'
275
+ system command
276
+ end
277
+
278
+ def update_file(file0)
279
+ file = (file0==nil) ? 'FrontPage' : file0
280
+ #rm cache file
281
+ t_file=File.join(@l_dir,'cache/parser',file)
282
+ begin
283
+ FileUtils.rm(t_file,:verbose=>true)
284
+ #update info file
285
+ info=InfoDB.new(@l_dir)
286
+ info.update(file0)
287
+
288
+ rescue
289
+ print "some errors on touch, but dont mind...\n"
290
+ end
291
+
292
+ #open file on browser
293
+ l_path = @src[:srcs][@target][:local_uri]
294
+ # p command="open -a #{@browser} \'#{l_path}/?c=edit;p=#{file}\'"
295
+ p command="open -a #{@browser} \'#{l_path}/?#{file}\'"
296
+ system command
297
+ p "If you get open error, try rackup from the src_dir."
298
+ p "If you get 整形式になっていません, try login as a valid user."
299
+ end
300
+
301
+ def list_files(file)
302
+ file ='' if file==nil
303
+ t_file=File.join(@l_dir,'text')
304
+ print "target_dir : "+t_file+"\n"
305
+ print `cd #{t_file} ; ls -lt #{file}*`
306
+ end
307
+
308
+ def edit_file(file)
309
+ t_file=File.join(@l_dir,'text',file)
310
+ if !File.exist?(t_file) then
311
+ file=File.open(t_file,'w')
312
+ file.close
313
+ File.chmod(0777,t_file)
314
+ end
315
+ p command="#{@editor_command} #{t_file}"
316
+ system command
317
+ end
318
+
319
+ def dump_sources
320
+ file = File.open(DATA_FILE,'w')
321
+ YAML.dump(@src, file)
322
+ file.close
323
+ end
324
+
325
+ def set_target(val)
326
+ @src[:target] = val.to_i
327
+ show_sources
328
+ end
329
+
330
+ def show_sources()
331
+ printf("target_no:%i\n",@src[:target])
332
+ printf("editor_command:%s\n",@src[:editor_command])
333
+ check_display_size()
334
+ header = display_format('id','name','local directory','global uri')
335
+
336
+ puts header
337
+ puts '-' * header.size
338
+
339
+ @src[:srcs].each_with_index{|src,i|
340
+ target = i==@src[:target] ? '*':' '
341
+ id = target+i.to_s
342
+ name=src[:nick_name]
343
+ local=src[:local_dir]
344
+ global=src[:global_uri]
345
+ puts display_format(id,name,local,global)
346
+ }
347
+
348
+ end
349
+
350
+ def check_display_size
351
+ @i_size,@n_size,@l_size,@g_size=3,5,30,15 #i,g_size are fixed
352
+ n_l,l_l=0,0
353
+ @src[:srcs].each_with_index{|src,i|
354
+ n_l =(n_l= src[:nick_name].length)>@n_size? n_l:@n_size
355
+ l_l =(l_l= src[:local_dir].length)>@l_size? l_l:@l_size
356
+ }
357
+ @n_size,@l_size=n_l,l_l
358
+ end
359
+
360
+ def display_format(id, name, local, global)
361
+ name_length = @n_size-full_width_count(name)
362
+ local_length = @l_size-full_width_count(local)
363
+ global_string= global.size < @g_size ? global : global[0..@g_size]
364
+ [id.to_s.rjust(@i_size), name.ljust(name_length),
365
+ local.ljust(local_length),
366
+ global_string.ljust(@g_size)].join(' | ')
367
+ end
368
+
369
+ def full_width_count(string)
370
+ string.each_char.select{|char| !(/[ -~。-゚]/.match(char))}.count
371
+ end
372
+
373
+ def add_sources
374
+ cont = {}
375
+ @data_name.each{|name|
376
+ printf("%s ? ", name)
377
+ tmp = gets.chomp
378
+ cont[name.to_sym] = tmp
379
+ }
380
+ @src[:srcs] << cont
381
+ show_sources
382
+ end
383
+
384
+ def read_sources
385
+ file = File.open(DATA_FILE,'r')
386
+ @src = YAML.load(file.read)
387
+ file.close
388
+ @target = @src[:target]
389
+ @l_dir=@src[:srcs][@target][:local_dir]
390
+ browser = @src[:browser]
391
+ @browser = (browser==nil) ? 'firefox' : browser
392
+ p editor_command = @src[:editor_command]
393
+ @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command
394
+ end
395
+ end
396
+ end
397
+ >>>
398
+ displayメソッドからadd_sourcesメソッドまではoptで登録されたコマンドの実行コードが書かれている.
399
+ !!hikiutils thorバージョン
400
+ !!!Thorとは
401
+ Thorとは,コマンドラインツールの作成を支援するライブラリのことである. gitやbundlerのようにサブコマンドを含むコマンドラインツールを簡単に作成することができる.
402
+ !!!!Thorの基本的な流れ
403
+ ①Thorを継承したクラスのパブリックメソッドがコマンドになる{{br}}
404
+ ②クラス.start(ARGV)でコマンドラインの処理をスタートする{{br}}
405
+ !!!コードの解説
406
+ !!!!hikiutils_thor.rb
407
+ <<< ruby
408
+ # -*- coding: utf-8 -*-
409
+ require 'thor'
410
+ require 'kconv'
411
+ require 'hikidoc'
412
+ require 'erb'
413
+ require "hikiutils/version"
414
+ require "hikiutils/tmarshal"
415
+ require "hikiutils/infodb"
416
+ require 'systemu'
417
+ require 'fileutils'
418
+ require 'yaml'
419
+ require 'pp'
420
+
421
+ module Hikithor
422
+
423
+ DATA_FILE=File.join(ENV['HOME'],'.hikirc')
424
+ attr_accessor :src, :target, :editor_command, :browser, :data_name, :l_dir
425
+
426
+ class CLI < Thor
427
+ def initialize(*args)
428
+ super
429
+ @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri']
430
+ data_path = File.join(ENV['HOME'], '.hikirc')
431
+ DataFiles.prepare(data_path)
432
+
433
+ file = File.open(DATA_FILE,'r')
434
+ @src = YAML.load(file.read)
435
+ file.close
436
+ @target = @src[:target]
437
+ @l_dir=@src[:srcs][@target][:local_dir]
438
+ browser = @src[:browser]
439
+ @browser = (browser==nil) ? 'firefox' : browser
440
+ p editor_command = @src[:editor_command]
441
+ @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command
442
+ end
443
+ HTML_TEMPLATE = <<EOS
444
+ <!DOCTYPE html
445
+ PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
446
+ "http://www.w3.org/TR/html4/loose.dtd">
447
+ <html lang="ja">
448
+ <html>
449
+ <head>
450
+ <meta http-equiv="Content-Language" content="ja">
451
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
452
+ <title><%= title %></title>
453
+ </head>
454
+ <body>
455
+ <%= body %>
456
+ </body>
457
+ </html>
458
+ EOS
459
+ >>>
460
+ {{attach_view(hogehoge.png, thor_initialize)}}{{br}}
461
+ ①Hikithor::CLI.start(ARGV)が呼ばれる{{br}}
462
+ ②initializeメソッドが呼ばれる{{br}}
463
+ ③これではThorのinitializeメソッドが呼ばれない{{br}}
464
+ ④superを書くことでThorのinitializeメソッドが呼ばれる{{br}}
465
+ !!!!!showメソッド〜eucメソッド
466
+ <<< ruby
467
+ desc 'show,--show', 'show sources'
468
+ map "--show" => "show"
469
+ def show
470
+ printf("target_no:%i\n",@src[:target])
471
+ printf("editor_command:%s\n",@src[:editor_command])
472
+ @i_size,@n_size,@l_size,@g_size=3,5,30,15 #i,g_size are fixed
473
+ n_l,l_l=0,0
474
+ @src[:srcs].each_with_index{|src,i|
475
+ n_l =(n_l= src[:nick_name].length)>@n_size? n_l:@n_size
476
+ l_l =(l_l= src[:local_dir].length)>@l_size? l_l:@l_size
477
+ }
478
+ @n_size,@l_size=n_l,l_l
479
+ command = Command.new
480
+ header = command.display_format('id','name','local directory','global uri\
481
+ ',@i_size,@n_size,@l_size,@g_size)
482
+
483
+ puts header
484
+ puts '-' * header.size
485
+
486
+ @src[:srcs].each_with_index{|src,i|
487
+ target = i==@src[:target] ? '*':' '
488
+ id = target+i.to_s
489
+ name=src[:nick_name]
490
+ local=src[:local_dir]
491
+ global=src[:global_uri]
492
+ puts command.display_format(id,name,local,global,@i_size,@n_size,@l_siz\
493
+ e,@g_size)
494
+ }
495
+ end
496
+
497
+ desc 'version,--version,-v', 'show program version'
498
+ map "--version" => "version"
499
+ map "-v" => "version"
500
+ def version
501
+ puts HikiUtils::VERSION
502
+ end
503
+
504
+ desc 'add,--add', 'add sources info'
505
+ map "--add" => "add"
506
+ option :add
507
+ def add
508
+ cont = {}
509
+ @data_name.each{|name|
510
+ printf("%s ? ", name)
511
+ tmp = STDIN.gets.chomp
512
+ cont[name.to_sym] = tmp
513
+ }
514
+ @src[:srcs] << cont
515
+ show
516
+ end
517
+
518
+ desc 'target VAL,--target VAL', 'set target id'
519
+ map "--target" => "target"
520
+ def target(val)
521
+ @src[:target] = val.to_i
522
+ show
523
+ end
524
+
525
+ desc 'edit FILE,--edit FILE', 'open file'
526
+ map "--edit" => "edit"
527
+ def edit(file)
528
+ t_file=File.join(@l_dir,'text',file)
529
+ if !File.exist?(t_file) then
530
+ file=File.open(t_file,'w')
531
+ file.close
532
+ File.chmod(0777,t_file)
533
+ end
534
+ p command="#{@editor_command} #{t_file}"
535
+ system command
536
+ end
537
+
538
+ desc 'list [FILE],--list [FILE]', 'list files'
539
+ map "--list" => "list"
540
+ def list(file)
541
+ file ='' if file==nil
542
+ t_file=File.join(@l_dir,'text')
543
+ print "target_dir : "+t_file+"\n"
544
+ print `cd #{t_file} ; ls -lt #{file}*`
545
+ end
546
+
547
+ desc 'update FILE,--update FILE', 'update file'
548
+ map "--update" => "update"
549
+ def update(file0)
550
+ file = (file0==nil) ? 'FrontPage' : file0
551
+ t_file=File.join(@l_dir,'cache/parser',file)
552
+ FileUtils.rm(t_file,:verbose=>true)
553
+ info=InfoDB.new(@l_dir)
554
+ info.update(file0)
555
+ l_path = @src[:srcs][@target][:local_uri]
556
+ p command="open -a #{@browser} \'#{l_path}/?#{file}\'"
557
+ system command
558
+ p "If you get open error, try rackup from the src_dir."
559
+ p "If you get 整形式になっていません, try login as a valid user."
560
+ end
561
+
562
+ desc 'rsync,--rsync', 'rsync files'
563
+ map "--rsync" => "rsync"
564
+ option :rsync
565
+ def rsync
566
+ p local = @l_dir
567
+ p global = @src[:srcs][@target][:global_dir]
568
+ p command="rsync -auvz -e ssh #{local}/ #{global}"
569
+ system command
570
+ end
571
+
572
+ desc 'datebase FILE,--database FILE', 'read datebase file'
573
+ map "--database" => "database"
574
+ def database(file_name)
575
+ info=InfoDB.new(@l_dir)
576
+ p info.show(file_name)
577
+ end
578
+
579
+ desc 'display FILE,--display FILE', 'display converted hikifile'
580
+ map "--display" => "display"
581
+ def display(file)
582
+ body = HikiDoc.to_html(File.read(file))
583
+ source = HTML_TEMPLATE
584
+ title = File.basename(file)
585
+ erb = ERB.new(source)
586
+ t = File.open(file+".html",'w')
587
+ t.puts(erb.result(binding))
588
+ t.close
589
+ system "open #{t.path}"
590
+ end
591
+
592
+ desc 'checkdb,--checkdb', 'check database file'
593
+ map "--checkdb" => "checkdb"
594
+ def checkdb
595
+ result= InfoDB.new(@l_dir).show_inconsist
596
+ print (result=='') ? "db agrees with text dir.\n" : result
597
+ end
598
+
599
+ desc 'remove [FILE],--remove [FILE]', 'remove files'
600
+ map "--remove" => "remove"
601
+ def remove(file_name)
602
+ p text_path = File.join(@l_dir,'text',file_name)
603
+ p attach_path = File.join(@l_dir,'cache/attach',file_name)
604
+ begin
605
+ File.delete(text_path)
606
+ rescue => evar
607
+ puts evar.to_s
608
+ end
609
+ begin
610
+ Dir.rmdir(attach_path)
611
+ rescue => evar
612
+ puts evar.to_s
613
+ end
614
+
615
+ info=InfoDB.new(@l_dir)
616
+ p "delete "
617
+ del_file=info.delete(file_name)
618
+ info.show_link(file_name)
619
+ info.dump
620
+ end
621
+
622
+ desc 'move [FILE],--move [FILE]', 'move file'
623
+ map "--move" => "move"
624
+ def move(files)
625
+ begin
626
+ p file1_path = File.join(@l_dir,'text',files[0])
627
+ p file2_path = File.join(@l_dir,'text',files[1])
628
+ rescue => evar
629
+ puts evar.to_s
630
+ puts "error on move_files, check the input format, especially comma sep\
631
+ aration."
632
+ exit
633
+ end
634
+ return if file1_path==file2_path
635
+ if File.exist?(file2_path) then
636
+ print ("moving target #{files[1]} exists.\n")
637
+ print ("first remove#{files[1]}.\n")
638
+ return
639
+ else
640
+ File.rename(file1_path,file2_path)
641
+ end
642
+
643
+ info=InfoDB.new(@l_dir)
644
+
645
+ db = info.db
646
+
647
+ pp file0=db[files[0]]
648
+ db.delete(files[0])
649
+ db[files[1]]=file0
650
+ db[files[1]][:title]=files[1] if db[files[1]][:title]==files[0]
651
+ pp db[files[1]]
652
+
653
+ db.each{|ele|
654
+ ref = ele[1][:references]
655
+ if ref.include?(files[0]) then
656
+ p link_file=ele[0]
657
+ link_path = File.join(@l_dir,'text',link_file)
658
+
659
+ cont = File.read(link_path)
660
+ if Kconv.iseuc(cont) then
661
+ print "euc\n"
662
+ utf8_cont=cont.toutf8
663
+ utf8_cont.gsub!(/#{files[0]}/,"#{files[1]}")
664
+ cont = utf8_cont.toeuc
665
+ else
666
+ cont.gsub!(/#{files[0]}/,"#{files[1]}")
667
+ end
668
+
669
+ File.write(link_path,cont)
670
+
671
+ ref.delete(files[0])
672
+ ref << files[1]
673
+
674
+ p cache_path = File.join(@l_dir,'cache/parser',link_file)
675
+ begin
676
+ File.delete(cache_path)
677
+ rescue => evar
678
+ puts evar.to_s
679
+ end
680
+ end
681
+ }
682
+
683
+ info.dump
684
+ end
685
+
686
+ desc 'euc FILE,--euc FILE', 'translate file to euc'
687
+ map "--euc" => "euc"
688
+ def euc(file)
689
+ p file_path = File.join(@l_dir,'text',file)
690
+ cont = File.readlines(file_path)
691
+ cont.each{|line| puts line.toeuc }
692
+ end
693
+ end
694
+ >>>
695
+ showメソッドからeucメソッドまではコマンドの表示と実行を行う.{{br}}
696
+ {{attach_view(hogehoge.png, thor_show)}}{{br}}
697
+ ①コマンド名,コマンドの説明を一覧に表示させる{{br}}
698
+ ②パブリックメソッドのコマンドを別のコマンド名でも実行できるようにする{{br}}
699
+ ③コマンドの命令の実行コード{{br}}
700
+ !!まとめ
701
+ !!参考文献
702
+
703
+