lib/format.rb in yac-1.3.2 vs lib/format.rb in yac-1.4.0

- old
+ new

@@ -1,86 +1,57 @@ -class Symbol - def to_proc - Proc.new { |*args| args.shift.__send__(self, *args) } +module Format + + def Error(x) + colorful "Please Provide A Valid Command To Operate #{x} (~/.yacrc)",'warn' end -end -module Format - 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" - Office_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate Office Document" - Doc_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate Text Document" + def handle_file(file,action = 'show') + colorful(file,"filename") - def format_file(file) - colorful(file,"filename") if file case `file "#{file}" 2>/dev/null` when / PDF document/ - colorful(Pdf_Error,'warn') unless system("#{Yac::CONFIG["pdf_command"]||'evince'} '#{file}' 2>/dev/null") + Error('PDF') unless system("#{Yac::CONFIG["#{action}_pdf"]} '#{file}' ") when /( image)|( bitmap)|(\.svg)/ - colorful(Image_Error,'warn') unless system("#{Yac::CONFIG["image_command"]||'eog'} '#{file}' 2>/dev/null") + Error('Image') unless system("#{Yac::CONFIG["#{action}_image"]} '#{file}'") when /Office Document/ - open_office(file) + Error('Office') unless system("#{Yac::CONFIG["#{action}_office"]} '#{file}'") else - if File.extname(file) =~ /^\.(od[tfspg]|uof)$/ #Support odf uof ods odp... - open_office(file) + if File.extname(file) =~ /^\.(od[tfspg]|uof)$/ # FileType: odf uof ods odp ... + Error('Office') unless system("#{Yac::CONFIG["#{action}_office"]} '#{file}'") else - File.new(file).each do |x| - format_section(x) - end + action =~ /show/ ? File.new(file).each {|x| format_text(x)} : edit_text(file) end end end - def format_section(section,empha_regexp = false) + def edit_text(file) + FileUtils.mkdir_p(File.dirname(file)) # Prepare Directory When Add File + Error('Text') unless system("#{Yac::CONFIG["editor"]||ENV['EDITOR']} '#{file}'") + end + + def format_text(section,empha_regexp = false) if section =~ /^(=+)\s+(.*)/ - level,stuff = $1.size,$2 - # Highlight the keyword when searching - stuff = empha(stuff,"head#{level}",empha_regexp) if empha_regexp - colorful("\s"*(level-1) + stuff,"head#{level}") + level,section = $1.size,$2 + # Highlight keyword when searching + section = empha(section,"head#{level}",empha_regexp) if empha_regexp + colorful("\s"*(level-1) + section,"head#{level}") else - section = empha(section,"text",empha_regexp) if empha_regexp + # command or plain text + level = (section =~ /^\s*\$\s+/) ? 'shell' : 'text' + section.sub!(/^(\s*\$\s+.*)/,"\e[#{@color['shell']}m"+'\1'+"\e[0m") + section = empha(section,level,empha_regexp) if empha_regexp colorful(section) end end - def edit_file(file) - case `file "#{file}" 2>/dev/null` - when / PDF / - colorful(Pdf_Error,'warn') unless system("#{Yac::CONFIG["pdf_edit_command"]||'ooffice'} '#{file}' 2>/dev/null") - when /( image )|(\.svg)/ - colorful(Image_Error,'warn') unless system("#{Yac::CONFIG["image_edit_command"]||'gimp'} '#{file}' 2>/dev/null") - when /Office Document/ - open_office(file) - else - if File.extname(file) =~ /^\.(od[tfspg]|uof)$/ #Support odf uof ods odp... - open_office(file) - else - edit_text(file) - end - end - end - - def open_office(file) - colorful(Office_Error,'warn') unless system("#{Yac::CONFIG["office_command"]||'ooffice'} '#{file}' 2>/dev/null") - end - - def edit_text(file) - prepare_dir(file) - colorful(Doc_Error,'warn') unless system("#{Yac::CONFIG["editor"] || ENV['EDITOR'] ||'vim'} '#{file}' 2>/dev/null") - end - def colorful(stuff,level="text",line_break = true) stuff = empha(stuff,level) - print "\e[%sm%s\e[0m " % [Yac::CONFIG[level],stuff.rstrip] + print "\e[%sm%s\e[0m " % [@color[level],stuff.rstrip] print "\n" if line_break end def empha(stuff,level="text",empha_regexp=/(@(.*)@)/) stuff.to_s.scan(empha_regexp) do |x| - return stuff.gsub(x[0],"\e[0m\e[#{Yac::CONFIG["empha"]}m%s\e[0m\e[%sm" % [x[1],Yac::CONFIG[level]]) + return stuff.gsub(x[0],"\e[0m\e[#{@color["empha"]}m%s\e[0m\e[%sm" % [x[1],@color[level]]) end - end - - def prepare_dir(file) - FileUtils.mkdir_p(File.dirname(file)) end end