Class: Hikithor::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/hikiutils_thor.rb

Constant Summary collapse

HTML_TEMPLATE =
<<EOS
<!DOCTYPE html
    PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<html>
<head>
  <meta http-equiv="Content-Language" content="ja">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title><%= title %></title>
</head>
<body>
  <%= body %>
</body>
</html>
EOS

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hikiutils_thor.rb', line 37

def initialize(*args)
  super
  @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri']
  data_path = File.join(ENV['HOME'], '.hikirc')
  DataFiles.prepare(data_path)

  File.open(DATA_FILE,'r'){|file| @src = YAML.load(file.read)}

  @target = @src[:target]
  @l_dir=@src[:srcs][@target][:local_dir]
  browser = @src[:browser]
  @browser = (browser==nil) ? 'firefox' : browser
  editor_command = @src[:editor_command]
  @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command
  printf("target_no:%i\n",@target)
  printf("editor_command:%s\n",@editor_command)
end

Instance Method Details

#addObject

map “–add” => “add”

option :add


90
91
92
93
94
95
96
97
98
99
# File 'lib/hikiutils_thor.rb', line 90

def add
  cont = {}
  @data_name.each{|name|
    printf("%s ? ", name)
    tmp = STDIN.gets.chomp
    cont[name.to_sym] = tmp
  }
  @src[:srcs] << cont
  show
end

#cd(val) ⇒ Object

map “–target” => “target”



103
104
105
106
107
# File 'lib/hikiutils_thor.rb', line 103

def cd(val)
  @src[:target] = val.to_i
  pwd
  dump_sources
end

#checkdbObject

map “–checkdb” => “checkdb”



183
184
185
186
# File 'lib/hikiutils_thor.rb', line 183

def checkdb
  result= InfoDB.new(@l_dir).show_inconsist
  print (result=='') ? "db agrees with text dir.\n" : result
end

#db(file_name) ⇒ Object

map “–database” => “database”



163
164
165
166
# File 'lib/hikiutils_thor.rb', line 163

def db(file_name)
  info=InfoDB.new(@l_dir)
  p info.show(file_name)
end

#euc(file) ⇒ Object

map “–euc” => “euc”



276
277
278
279
280
# File 'lib/hikiutils_thor.rb', line 276

def euc(file)
  p file_path = File.join(@l_dir,'text',file)
  cont = File.readlines(file_path)
  cont.each{|line| puts line.toeuc }
end

#ls(file) ⇒ Object

map “–list” => “list”



124
125
126
127
128
129
# File 'lib/hikiutils_thor.rb', line 124

def ls(file)
  file ='' if file==nil
  t_file=File.join(@l_dir,'text')
  print "target_dir : "+t_file+"\n"
  print `cd #{t_file} ; ls -lt #{file}*`
end

#mv(files) ⇒ Object

map “–move” => “move”



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
268
269
270
271
272
# File 'lib/hikiutils_thor.rb', line 213

def mv(files)
  begin
    p file1_path = File.join(@l_dir,'text',files[0])
    p file2_path = File.join(@l_dir,'text',files[1])
  rescue => evar
    puts evar.to_s
    puts "error on move_files, check the input format, especially comma separation."
    exit
  end
  return if file1_path==file2_path
  if File.exist?(file2_path) then
    print ("moving target #{files[1]} exists.\n")
    print ("first remove#{files[1]}.\n")
    return
  else
    File.rename(file1_path,file2_path)
  end

  info=InfoDB.new(@l_dir)

  db = info.db

  pp file0=db[files[0]]
  db.delete(files[0])
  db[files[1]]=file0
  db[files[1]][:title]=files[1] if db[files[1]][:title]==files[0]
  pp db[files[1]]

  db.each{|ele|
    ref = ele[1][:references]
    if ref.include?(files[0]) then
      p link_file=ele[0]
      link_path = File.join(@l_dir,'text',link_file)

      cont = File.read(link_path)
      if Kconv.iseuc(cont) then
        print "euc\n"
        utf8_cont=cont.toutf8
        utf8_cont.gsub!(/#{files[0]}/,"#{files[1]}")
        cont = utf8_cont.toeuc
      else
        cont.gsub!(/#{files[0]}/,"#{files[1]}")
      end

      File.write(link_path,cont)

      ref.delete(files[0])
      ref << files[1]

      p cache_path = File.join(@l_dir,'cache/parser',link_file)
      begin
        File.delete(cache_path)
      rescue => evar
        puts evar.to_s
      end
    end
  }

  info.dump
end

#open(file) ⇒ Object

map “–edit” => “edit”



111
112
113
114
115
116
117
118
119
120
# File 'lib/hikiutils_thor.rb', line 111

def open(file)
  t_file=File.join(@l_dir,'text',file)
  unless File.exist?(t_file)
    file=File.open(t_file,'w')
    file.close
    File.chmod(0777,t_file)
  end
  p command="#{@editor_command} #{t_file}"
  system command
end

#pwdObject

map “–show” => “show”



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hikiutils_thor.rb', line 57

def pwd
  @i_size,@n_size,@l_size,@g_size=3,5,30,15 #i,g_size are fixed
  n_l,l_l=0,0
  @src[:srcs].each_with_index{|src,i|
    n_l =(n_l= src[:nick_name].length)>@n_size? n_l:@n_size
    l_l =(l_l= src[:local_dir].length)>@l_size? l_l:@l_size
  }
  @n_size,@l_size=n_l,l_l
  header = display_format('id','name','local directory','global uri',@i_size,@n_size,@l_size,@g_size)

  puts header
  puts '-' * header.size

  @src[:srcs].each_with_index{|src,i|
    target = i==@src[:target] ? '*':' '
    id = target+i.to_s
    name=src[:nick_name]
    local=src[:local_dir]
    global=src[:global_uri]
    puts display_format(id,name,local,global,@i_size,@n_size,@l_size,@g_size)
  }
end

#rm(file_name) ⇒ Object

map “–remove” => “remove”



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/hikiutils_thor.rb', line 190

def rm(file_name)
  p text_path = File.join(@l_dir,'text',file_name)
  p attach_path = File.join(@l_dir,'cache/attach',file_name)
  begin
    File.delete(text_path)
  rescue => evar
    puts evar.to_s
  end
  begin
    Dir.rmdir(attach_path)
  rescue => evar
    puts evar.to_s
  end

  info=InfoDB.new(@l_dir)
  p "delete "
  del_file=info.delete(file_name)
  info.show_link(file_name)
  info.dump
end

#rsyncObject

map “–rsync” => “rsync”

option :rsync


154
155
156
157
158
159
# File 'lib/hikiutils_thor.rb', line 154

def rsync
  p local = @l_dir
  p global = @src[:srcs][@target][:global_dir]
  p command="rsync -auvz -e ssh #{local}/ #{global}"
  system command
end

#show(file) ⇒ Object

map “–display” => “display”



170
171
172
173
174
175
176
177
178
179
# File 'lib/hikiutils_thor.rb', line 170

def show(file)
  body = HikiDoc.to_html(File.read(file))
  source = HTML_TEMPLATE
  title = File.basename(file)
  erb = ERB.new(source)
  t = File.open(file+".html",'w')
  t.puts(erb.result(binding))
  t.close
  system "open #{t.path}"
end

#touch(file0) ⇒ Object

map “–update” => “update”



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hikiutils_thor.rb', line 133

def touch(file0)
  file = (file0==nil) ? 'FrontPage' : file0
  t_file=File.join(@l_dir,'cache/parser',file)
  begin
    FileUtils.rm(t_file,:verbose=>true)
    info=InfoDB.new(@l_dir)
    info.update(file0)
  rescue
    print "some errors on touch, but dont mind...\n"
  end

  l_path = @src[:srcs][@target][:local_uri]
  p command="open -a #{@browser} \'#{l_path}/?#{file}\'"
  system command
  p "If you get open error, try rackup from the src_dir."
  p "If you get 整形式になっていません, try login as a valid user."
end

#versionObject



83
84
85
# File 'lib/hikiutils_thor.rb', line 83

def version
  puts HikiUtils::VERSION
end