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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hikiutils_thor.rb', line 20

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 = File.open(DATA_FILE,'r')
   @src = YAML.load(file.read) 
   file.close
   @target = @src[:target]
   @l_dir=@src[:srcs][@target][:local_dir]
   browser = @src[:browser]
   @browser = (browser==nil) ? 'firefox' : browser
   p editor_command = @src[:editor_command]
   @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command
end

Instance Method Details

#addObject



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

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

#checkdbObject



178
179
180
181
# File 'lib/hikiutils_thor.rb', line 178

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

#database(file_name) ⇒ Object



158
159
160
161
# File 'lib/hikiutils_thor.rb', line 158

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

#display(file) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/hikiutils_thor.rb', line 165

def display(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

#edit(file) ⇒ Object



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

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

#euc(file) ⇒ Object



271
272
273
274
275
# File 'lib/hikiutils_thor.rb', line 271

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

#list(file) ⇒ Object



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

def list(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

#move(files) ⇒ Object



208
209
210
211
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/hikiutils_thor.rb', line 208

def move(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

#remove(file_name) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hikiutils_thor.rb', line 185

def remove(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



149
150
151
152
153
154
# File 'lib/hikiutils_thor.rb', line 149

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

#showObject



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
# File 'lib/hikiutils_thor.rb', line 55

def show
  printf("target_no:%i\n",@src[:target])
  printf("editor_command:%s\n",@src[:editor_command])
  @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
  command = Command.new
  header = command.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 command.display_format(id,name,local,global,@i_size,@n_size,@l_size,@g_size)
  }
end

#target(val) ⇒ Object



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

def target(val)
  @src[:target] = val.to_i
  show
end

#update(file0) ⇒ Object



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

def update(file0)
  file = (file0==nil) ? 'FrontPage' : file0
  t_file=File.join(@l_dir,'cache/parser',file)
  FileUtils.rm(t_file,:verbose=>true)
  info=InfoDB.new(@l_dir)
  info.update(file0)
  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



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

def version
  puts HikiUtils::VERSION
end