Module: Format

Included in:
Yac
Defined in:
lib/format.rb

Constant Summary collapse

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"

Instance Method Summary collapse

Instance Method Details

#clean_filename(f) ⇒ Object



50
51
52
# File 'lib/format.rb', line 50

def clean_filename(f)
  return f.sub(/^(.*)?(main|private)\/(.*)/,'\3').sub(/^@/,'')
end

#colorful(stuff, level = "text", line_break = true) ⇒ Object



54
55
56
57
58
# File 'lib/format.rb', line 54

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

#edit_file(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/format.rb', line 34

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

#edit_text(file) ⇒ Object



45
46
47
48
# File 'lib/format.rb', line 45

def edit_text(file)
  prepare_dir(file)
  puts Doc_Error unless system("#{Yac::CONFIG["editor"] || ENV['EDITOR'] ||'vim'} \"#{file}\" 2>/dev/null")
end

#empha(stuff, level = "text", empha_regexp = /(@@@(.*)@@@)/) ⇒ Object



60
61
62
63
64
# File 'lib/format.rb', line 60

def empha(stuff,level="text",empha_regexp=/(@@@(.*)@@@)/)
  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

#format_file(file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/format.rb', line 6

def format_file(file)
  @level = 0
  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["image_command"]||'eog'} \"#{file}\" 2>/dev/null")
  else
    File.new(file).each do |x|
      format_section(x)
    end
  end
rescue
end

#format_section(section, search = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/format.rb', line 22

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||0 ) * 2 ))
  end
end

#prepare_dir(file) ⇒ Object



66
67
68
69
# File 'lib/format.rb', line 66

def prepare_dir(file)
  dirseparator = file.rindex(File::Separator)+1
  FileUtils.mkdir_p(file[0,dirseparator])
end