Class: PDF::Reader::Explore

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/explore.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, xref) ⇒ Explore

Returns a new instance of Explore.



36
37
38
39
# File 'lib/pdf/reader/explore.rb', line 36

def initialize (receiver, xref)
  @xref = xref
  @pwd  = '/'
end

Class Method Details

.file(name) ⇒ Object



32
33
34
# File 'lib/pdf/reader/explore.rb', line 32

def self.file (name)
  PDF::Reader.new.parse(File.open(name), self)
end

Instance Method Details

#cd(path) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/pdf/reader/explore.rb', line 72

def cd (path)
  path = path.to_s

  if path[0,1] == "/"
    @pwd = path
  else
    @pwd = Pathname.new(@pwd + '/' + path).cleanpath.to_s
  end
end

#document(root) ⇒ Object



41
42
43
44
# File 'lib/pdf/reader/explore.rb', line 41

def document (root)
  @root = root
  self
end

#ls(entry = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pdf/reader/explore.rb', line 86

def ls (entry = nil)
  parts = @pwd.split('/')
  obj   = @root

  parts.shift if parts[0] == ""
  parts.push(entry) if entry

  parts.each do |p|
    case obj
    when Hash
      unless obj.has_key?(p)
        puts "invalid path at #{p}"
        return
      end
      obj = obj[p]

    when Array
      obj = obj[p.to_i]
    end

    obj = @xref.object(obj)
  end

  output_parent(obj)
  "#{@pwd}: #{obj.class}"
end

#output_child(obj) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/pdf/reader/explore.rb', line 61

def output_child (obj)
  print ": #{obj.class}"

  case obj
  when Float
    print ": #{obj}"
  when String
    print ": #{obj[0, 20].sub(/\n/, ' ')}"
  end
end

#output_parent(obj) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdf/reader/explore.rb', line 46

def output_parent (obj)
  case obj
  when Hash
    obj.each do |k,v| 
      print "#{k}"; output_child(v); print "\n"
      Explore::const_set(k, k) if !Explore.const_defined?(k)
    end
  when Array
    obj.each_with_index {|o, i| print "#{i}: "; output_child(o); print "\n"}
  else
    output_child(obj)
    print "\n"
  end
end

#pwdObject



82
83
84
# File 'lib/pdf/reader/explore.rb', line 82

def pwd
  @pwd
end