lib/format.rb in yac-0.1.6 vs lib/format.rb in yac-1.0.0

- old
+ new

@@ -1,56 +1,70 @@ module Format - Pdf_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Open PDF Document" - Image_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Open Image Document" + Pdf_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate PDF Document" + Image_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate Image Document" + Doc_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate Text Document" def format_file(file) @level = 0 - case `file #{file}` - when / PDF / - puts Pdf_Error unless system("#{Yac::CONFIG["pdf_command"]||'evince'} #{file}") + colorful(file,"filename") if file + case `file "#{file}" 2>/dev/null` + when / PDF document/ + puts Pdf_Error unless system("#{Yac::CONFIG["pdf_command"]||'evince'} \"#{file}\" 2>/dev/null") when /( image )|(\.svg)/ - puts Image_Error unless system("#{Yac::CONFIG["pic_command"]||'eog'} #{file}") + puts Image_Error unless system("#{Yac::CONFIG["image_command"]||'eog'} \"#{file}\" 2>/dev/null") else File.new(file).each do |x| format_section(x) end end + rescue end def format_section(section,search = false) case section when /^(=+)\s+(.*)/ @level = search ? 1 : $1.size colorful("\s"*2*(@level-1) + $2,"head#{@level}") when /^(\s*)#/ + colorful(section.sub(/^\s*/,'')) if search else - colorful(section.sub(/^\#/,"#").sub(/^\s*/, "\s" * @level * 2 )) + colorful(section.sub(/^\#/,"#").sub(/^\s*/, "\s" * ( @level||0 ) * 2 )) end end - def editor - Yac::CONFIG["editor"] || ENV['EDITOR'] || "vim" + def edit_file(file) + case `file "#{file}" 2>/dev/null` + when / PDF / + puts Pdf_Error unless system("#{Yac::CONFIG["pdf_edit_command"]||'ooffice'} \"#{file}\" 2>/dev/null") + when /( image )|(\.svg)/ + puts Image_Error unless system("#{Yac::CONFIG["image_edit_command"]||'gimp'} \"#{file}\" 2>/dev/null") + else + edit_text(file) + end end - def title_of_file(f) - f[0..((f.rindex('.')||0) - 1)] + def edit_text(file) + prepare_dir(file) + puts Doc_Error unless system("#{Yac::CONFIG["editor"] || ENV['EDITOR'] ||'vim'} \"#{file}\" 2>/dev/null") end + def clean_filename(f) + return f.sub(/^(.*)?(main|private)\/(.*)/,'\3').sub(/^@/,'') + end + def colorful(stuff,level="text",line_break = true) stuff = empha(stuff,level) print "\033[%sm%s\033[0m" % [Yac::CONFIG[level],stuff.rstrip] print "\n" if line_break end def empha(stuff,level="text",empha_regexp=/(@@@(.*)@@@)/) - stuff.scan(empha_regexp) do |x| + stuff.to_s.scan(empha_regexp) do |x| return stuff.gsub(x[0],"\033[0m\033[#{Yac::CONFIG["empha"].to_s}m%s\033[0m\033[%sm" % [x[1],Yac::CONFIG[level]]) end end - def show_possible_result - unless @all_result.to_s.empty? - colorful("ALL POSSIBLE RESULT:","possible_result_title") - colorful(@all_result.join("\s"*2),"possible_result_content") - end + def prepare_dir(file) + dirseparator = file.rindex(File::Separator)+1 + FileUtils.mkdir_p(file[0,dirseparator]) end end