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



86
87
88
89
90
91
92
93
94
95
# File 'lib/hikiutils_thor.rb', line 86

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



165
166
167
168
# File 'lib/hikiutils_thor.rb', line 165

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

#database(file_name) ⇒ Object



147
148
149
150
# File 'lib/hikiutils_thor.rb', line 147

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

#display(file) ⇒ Object



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

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hikiutils_thor.rb', line 104

def edit(file)
  p @l_dir
  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



255
256
257
258
259
# File 'lib/hikiutils_thor.rb', line 255

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



117
118
119
120
121
122
# File 'lib/hikiutils_thor.rb', line 117

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
# File 'lib/hikiutils_thor.rb', line 193

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hikiutils_thor.rb', line 171

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



139
140
141
142
143
144
# File 'lib/hikiutils_thor.rb', line 139

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



54
55
56
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 54

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



98
99
100
101
# File 'lib/hikiutils_thor.rb', line 98

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

#update(file0) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hikiutils_thor.rb', line 125

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



81
82
83
# File 'lib/hikiutils_thor.rb', line 81

def version
  puts Hikithor::VERSION
end