File: console.rb

package info (click to toggle)
origami-pdf 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,484 kB
  • sloc: ruby: 17,883; makefile: 8
file content (125 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
=begin

    This file is part of Origami, PDF manipulation framework for Ruby
    Copyright (C) 2016	Guillaume Delugré.

    Origami is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Origami is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Origami.  If not, see <http://www.gnu.org/licenses/>.

=end

require 'tempfile'
require 'hexdump'
require 'colorize'

String.disable_colorization(false)

module Origami
    module Object
        def inspect
            to_s
        end
    end

    class Stream
        def edit(editor = ENV['EDITOR'])
            Tempfile.open("origami") do |tmpfile|
                tmpfile.write(self.data)
                tmpfile.flush

                Process.wait Kernel.spawn "#{editor} #{tmpfile.path}"

                self.data = File.read(tmpfile.path)
                tmpfile.unlink
            end

            true
        end

        def inspect
            self.data.hexdump
        end
    end

    class Page < Dictionary
        def edit
            each_content_stream do |stream|
                stream.edit
            end
        end
    end

    class PDF
        if defined?(PDF::JavaScript::Engine)
            class JavaScript::Engine
                def shell
                    while (print 'js> '.magenta; line = gets)
                        begin
                            puts exec(line)
                        rescue V8::JSError => e
                            puts "Error: #{e.message}"
                        end
                    end
                end
            end
        end

        class Revision
            def to_s
                puts "----------  Body  ----------".white.bold
                @body.each_value do |obj|
                    print "#{obj.reference.to_s.rjust(8,' ')}".ljust(10).magenta
                    puts "#{obj.type}".yellow
                end

                puts "---------- Trailer ---------".white.bold
                if not @trailer.dictionary
                    puts "  [x] No trailer found.".blue
                else
                    @trailer.dictionary.each_pair do |entry, value|
                        print "  [*] ".magenta
                        print "#{entry}: ".yellow
                        puts "#{value}".red
                    end

                    print "  [+] ".magenta
                    print "startxref: ".yellow
                    puts "#{@trailer.startxref}".red
                end
            end

            def inspect
                to_s
            end
        end

        def to_s
            puts

            puts "---------- Header ----------".white.bold
            print "  [+] ".magenta
            print "Version: ".yellow
            puts "#{@header.major_version}.#{@header.minor_version}".red

            @revisions.each do |revision|
                revision.to_s
            end
            puts
        end

        def inspect
            to_s
        end
    end

end