Module: Mireru::Widget

Defined in:
lib/mireru/widget.rb,
lib/mireru/widget/pdf.rb,
lib/mireru/widget/svg.rb,
lib/mireru/widget/text.rb,
lib/mireru/widget/image.rb,
lib/mireru/widget/video.rb,
lib/mireru/widget/binary.rb,
lib/mireru/widget/extracted_text.rb

Defined Under Namespace

Classes: Binary, ExtractedText, Image, PDF, SVG, Text, Video

Class Method Summary collapse

Class Method Details

.binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
# File 'lib/mireru/widget.rb', line 102

def binary?(file)
  if /\.(la|lo|o|so|a|dll|exe|msi|tar|gz|zip|7z|lzh|rar|iso)\z/i =~ file
    true
  else
    bytes = File.read(file, 512)
    return false if bytes.nil?
    return false if utf16?(bytes)
    bytes.count("\x00-\x07\x0b\x0e-\x1a\x1c-\x1f") > (bytes.size / 10)
  end
end

.checkObject

TODO: improve



65
66
67
68
69
70
71
# File 'lib/mireru/widget.rb', line 65

def check
  begin
    yield
  rescue LoadError
    $stderr.puts("Warning: #{$!.message}")
  end
end

.create(file, width, height, chupa = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mireru/widget.rb', line 24

def create(file, width, height, chupa=false)
  if chupa
    check do
      require "mireru/widget/extracted_text"
      return Widget::ExtractedText.new(file)
    end
  end

  if image?(file)
    return Widget::Image.new(file, width, height)
  end

  if video?(file) or music?(file)
    check do
      require "mireru/widget/video"
      return Widget::Video.new(file)
    end
  end

  if pdf?(file)
    check do
      require "mireru/widget/pdf"
      return Widget::PDF.new(file)
    end
  end

  if svg?(file)
    check do
      require "mireru/widget/svg"
      return Widget::SVG.new(file)
    end
  end

  if text?(file)
    return Widget::Text.new(file)
  end

  return Widget::Binary.new(file)
end

.document?(file) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mireru/widget.rb', line 93

def document?(file)
  /\.(odt|ods|odp|doc|xls|ppt|docx|xlsx|pptx)\z/i =~ file
end

.image?(file) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/mireru/widget.rb', line 73

def image?(file)
  /\.(png|jpe?g|gif|ico|ani|bmp|pnm|ras|tga|tiff|xbm|xpm)\z/i =~ file
end

.music?(file) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mireru/widget.rb', line 77

def music?(file)
  /\.(og[ag]|wav|acc|mp3|m4a|wma|flac|tta|aiff|ape|tak)\z/i =~ file
end

.pdf?(file) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mireru/widget.rb', line 85

def pdf?(file)
  /\.pdf\z/i =~ file
end

.svg?(file) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mireru/widget.rb', line 89

def svg?(file)
  /\.svg\z/i =~ file
end

.text?(file) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/mireru/widget.rb', line 97

def text?(file)
  return false if binary?(file)
  true
end

.utf16?(bytes) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
# File 'lib/mireru/widget.rb', line 113

def utf16?(bytes)
  # TODO: simplify
  (bytes.start_with?("\xff\xfe".force_encoding("ASCII-8BIT")) or
     bytes.start_with?("\xfe\xff".force_encoding("ASCII-8BIT"))) and
    bytes.count("\x01-\x07\x0b\x0e-\x1a\x1c-\x1f") < (bytes.size / 20)
end

.video?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

def video?(file)
  /\.(ogm|mp4|m4v|flv|mpe?g2?|ts|mov|avi|divx|mkv|wmv|asf|wmx)\z/i =~ file
end