Class: HikiUtils::Command
- Inherits:
-
Object
- Object
- HikiUtils::Command
- Defined in:
- lib/hikiutils.rb
Constant Summary collapse
- HTML_TEMPLATE =
"<!DOCTYPE html\n PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n \"http://www.w3.org/TR/html4/loose.dtd\">\n<html lang=\"ja\">\n<html>\n<head>\n <meta http-equiv=\"Content-Language\" content=\"ja\">\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n <title><%= title %></title>\n</head>\n<body>\n <%= body %>\n</body>\n</html>\n"
Class Method Summary collapse
Instance Method Summary collapse
- #add_sources ⇒ Object
- #check_db ⇒ Object
- #db_file(file_name) ⇒ Object
- #display(file) ⇒ Object
- #display_format(id, name, local, global) ⇒ Object
- #dump_sources ⇒ Object
- #edit_file(file) ⇒ Object
- #euc_file(file) ⇒ Object
- #execute ⇒ Object
- #full_width_count(string) ⇒ Object
-
#initialize(argv = []) ⇒ Command
constructor
A new instance of Command.
- #list_files(file) ⇒ Object
- #move_file(files) ⇒ Object
- #read_sources ⇒ Object
- #remove_file(file_name) ⇒ Object
- #rsync_files ⇒ Object
- #set_target(val) ⇒ Object
- #show_sources ⇒ Object
- #update_file(file0) ⇒ Object
Constructor Details
#initialize(argv = []) ⇒ Command
Returns a new instance of Command.
43 44 45 46 47 48 49 |
# File 'lib/hikiutils.rb', line 43 def initialize(argv=[]) @argv = argv @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri'] data_path = File.join(ENV['HOME'], '.hikirc') DataFiles.prepare(data_path) read_sources end |
Class Method Details
.run(argv = []) ⇒ Object
38 39 40 41 |
# File 'lib/hikiutils.rb', line 38 def self.run(argv=[]) print "hiki says 'Hello world'.\n" new(argv).execute end |
Instance Method Details
#add_sources ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/hikiutils.rb', line 277 def add_sources cont = {} @data_name.each{|name| printf("%s ? ", name) tmp = gets.chomp cont[name.to_sym] = tmp } @src[:srcs] << cont show_sources end |
#check_db ⇒ Object
179 180 181 182 |
# File 'lib/hikiutils.rb', line 179 def check_db result= InfoDB.new(@l_dir).show_inconsist print (result=='') ? "db agrees with text dir.\n" : result end |
#db_file(file_name) ⇒ Object
184 185 186 187 |
# File 'lib/hikiutils.rb', line 184 def db_file(file_name) info=InfoDB.new(@l_dir) p info.show(file_name) end |
#display(file) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/hikiutils.rb', line 81 def display(file) body = HikiDoc.to_html(File.read(file)) source = HTML_TEMPLATE title = '' erb = ERB.new(source) t = File.open(file+".html",'w') t.puts(erb.result(binding)) system "open #{t.path}" end |
#display_format(id, name, local, global) ⇒ Object
267 268 269 270 271 |
# File 'lib/hikiutils.rb', line 267 def display_format(id, name, local, global) name_length = 5-full_width_count(name) local_length = 45-full_width_count(local) [id.to_s.rjust(3), name.ljust(name_length),local.ljust(local_length), global.center(10)].join(' | ') end |
#dump_sources ⇒ Object
237 238 239 240 241 |
# File 'lib/hikiutils.rb', line 237 def dump_sources file = File.open(DATA_FILE,'w') YAML.dump(@src, file) file.close end |
#edit_file(file) ⇒ Object
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/hikiutils.rb', line 226 def edit_file(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(file) ⇒ Object
91 92 93 94 95 |
# File 'lib/hikiutils.rb', line 91 def euc_file(file) p file_path = File.join(@l_dir,'text',file) cont = File.readlines(file_path) cont.each{|line| puts line.toeuc } end |
#execute ⇒ Object
51 52 53 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 79 |
# File 'lib/hikiutils.rb', line 51 def execute @argv << '--help' if @argv.size==0 command_parser = OptionParser.new do |opt| opt.on('-v', '--version','show program Version.') { |v| opt.version = HikiUtils::VERSION puts opt.ver } opt.on('-s', '--show','show sources') {show_sources} opt.on('-a', '--add','add sources info') {add_sources } opt.on('-t', '--target VAL','set target id') {|val| set_target(val) } opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) } opt.on('-l', '--list [FILE]','list files') {|file| list_files(file) } opt.on('-u', '--update FILE','update file') {|file| update_file(file) } opt.on('-r', '--rsync','rsync files') {rsync_files} opt.on('--database FILE','read database file') {|file| db_file(file)} opt.on('--display FILE','display converted hikifile') {|file| display(file)} opt.on('-c', '--checkdb','check database file') {check_db} opt.on('--remove FILE','remove file') {|file| remove_file(file)} opt.on('--move FILES','move file1,file2',Array) {|files| move_file(files)} opt.on('--euc FILE','translate file to euc') {|file| euc_file(file) } end begin command_parser.parse!(@argv) rescue=> eval p eval end dump_sources exit end |
#full_width_count(string) ⇒ Object
273 274 275 |
# File 'lib/hikiutils.rb', line 273 def full_width_count(string) string.each_char.select{|char| !(/[ -~。-゚]/.match(char))}.count end |
#list_files(file) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/hikiutils.rb', line 219 def list_files(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_file(files) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/hikiutils.rb', line 97 def move_file(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 |
#read_sources ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/hikiutils.rb', line 288 def read_sources 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 |
#remove_file(file_name) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/hikiutils.rb', line 158 def remove_file(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 |
#rsync_files ⇒ Object
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/hikiutils.rb', line 189 def rsync_files p local = @l_dir p global = @src[:srcs][@target][:global_dir] #"/Users/bob/Sites/nishitani0/Internal/data" #"bob@dmz0:/Users/bob/nishitani0/Internal/data" # p command="rsync -auvz -e ssh #{local}/ #{global}" p command="rsync -auvz -e ssh #{local}/ #{global}" # system 'rsync -auvz -e ssh ~/Sites/nishitani0 [email protected]:Sites/' system command end |
#set_target(val) ⇒ Object
243 244 245 246 |
# File 'lib/hikiutils.rb', line 243 def set_target(val) @src[:target] = val.to_i show_sources end |
#show_sources ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/hikiutils.rb', line 248 def show_sources() printf("target_no:%i\n",@src[:target]) printf("editor_command:%s\n",@src[:editor_command]) header = display_format('id','name','local directory','global uri') 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) } end |
#update_file(file0) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/hikiutils.rb', line 200 def update_file(file0) file = (file0==nil) ? 'FrontPage' : file0 #rm cache file t_file=File.join(@l_dir,'cache/parser',file) FileUtils.rm(t_file,:verbose=>true) #update info file info=InfoDB.new(@l_dir) info.update(file0) #open file on browser l_path = @src[:srcs][@target][:local_uri] # p command="open -a #{@browser} \'#{l_path}/?c=edit;p=#{file}\'" 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 |