Module: CommandWrap

Defined in:
lib/command_wrap.rb,
lib/command_wrap/pdf.rb,
lib/command_wrap/xvfb.rb,
lib/command_wrap/image.rb,
lib/command_wrap/config.rb,
lib/command_wrap/open_office.rb

Defined Under Namespace

Modules: Config, Image, OpenOffice, Pdf, Xvfb

Constant Summary collapse

INJECT_SCRIPT =
"
    <script type='text/javascript'>
        function subst() {
            var vars = {};
            var params = window.location.search.substring(1).split('&');
            for (var i = 0; i < params.length; i++) {
                var parts = params[i].split('=');
                vars[parts[0]] = unescape(parts[1]);
            }
            var opts = [ 'frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection' ];
            for (var i in opts) {
                var elems = document.getElementsByClassName(key[i]);
                for (var j = 0; j < elems.length; j++) {
                    elems[j].textContent = vars[key[i]];
                }
            }
        }
        window.onload = subst;
    </script>
"

Class Method Summary collapse

Class Method Details

.capture(url, target) ⇒ Object

Creates a screenshot from the given url Uses CutyCapt (cutycapt.sourceforge.net)



7
8
9
10
# File 'lib/command_wrap.rb', line 7

def self.capture (url, target)
    command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/CutyCapt --min-width=1024 --min-height=768 --url=#{url} --out=#{target}")
    `#{command}`
end

.extension(filename) ⇒ Object



113
114
115
116
# File 'lib/command_wrap.rb', line 113

def self.extension (filename)
    return '' unless filename.include?('.')
    filename.split('.').last
end

.htmltopdf(source, target, options = {}) ⇒ Object



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
# File 'lib/command_wrap.rb', line 33

def self.htmltopdf (source, target, options = {})
    header_html = ''
    footer_html = ''

    params = ''

    options.each do |key, value|
        key = key.to_s
        if value.nil? || value == ''
            params += " --#{key}"
        elsif key == 'header'
            header_html = CommandWrap.temp('html')
            # Inject parameters
            if value.index('<head>')
                value = value.gsub('<head>', '<head>' + INJECT_SCRIPT)
            end

            File.open(header_html, 'w') do |f|
                f.write value
            end

            params += " --header-html #{header_html}"
        elsif key == 'footer'
            footer_html = CommandWrap.temp('html')
            # Inject parameters
            if value.index('<head>')
                value = value.gsub('<head>', '<head>' + INJECT_SCRIPT)
            end

            File.open(footer_html, 'w') do |f|
                f.write value
            end

            params += " --footer-html #{footer_html}"
        else
            params += " --#{key} #{value}"
        end
    end

    command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/wkhtmltopdf --quiet --print-media-type #{source} #{params} #{target}")

    `#{command}`

    File.delete(header_html) if header_html != '' && File.exist?(header_html)
    File.delete(footer_html) if footer_html != '' && File.exist?(footer_html)
end

.index(path) ⇒ Object

Tries to convert content of file to plaintext or html



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/command_wrap.rb', line 166

def self.index (path)
    extension = CommandWrap.extension(path).downcase

    if extension == 'txt'
        return IO.read(path)
    end

    if extension == 'html' || extension == 'htm'
        return IO.read(path)
    end

    tmp = self.temp('html')
    begin
        CommandWrap::OpenOffice.convert(path, tmp)
        return IO.read(tmp) if File.exists?(tmp)
    rescue
    ensure
        File.delete(tmp) if File.exists?(tmp) && File.writable?(tmp)
    end

    ''
end

.preview(source, target, width, height) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/command_wrap.rb', line 118

def self.preview (source, target, width, height)
    extension = self.extension(source).upcase

    # Image ?
    formats = Magick.formats
    if formats.key?(extension) && formats[extension].include?('r')
        begin
            CommandWrap::Image.scale(source, target, width, height)
            return true
        rescue
            return false
        end
    end

    tmppdf = self.temp('pdf')
    tmppng = self.temp('png')
    begin
        # Create a pdf of the document
        CommandWrap::OpenOffice.convert(source, tmppdf)
        # Create a screenshot of first page of the generated pdf
        CommandWrap::Pdf.preview(tmppdf, tmppng)
        # Scale it down to thumb
        CommandWrap::Image.scale(tmppng, target, width, height)
        return true
    rescue

    ensure
        # Cleanup
        File.delete(tmppdf) if File.exists?(tmppdf) && File.writable?(tmppdf)
        File.delete(tmppng) if File.exists?(tmppng) && File.writable?(tmppng)
    end

    nil
end

.temp(extension) ⇒ Object

Generates a temp filepath for the given extension



154
155
156
157
158
159
160
161
162
163
# File 'lib/command_wrap.rb', line 154

def self.temp (extension)
    path = "#{CommandWrap::Config.tmp_dir}/tmp.#{extension}"
    id = 1
    while File.exists?(path)
        path = "#{CommandWrap::Config.tmp_dir}/tmp.#{id}.#{extension}"
        id += 1
    end

    path
end

.zip(target, *sources) ⇒ Object

Sources consists of paths followed by the filename that must be used in the zip



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
# File 'lib/command_wrap.rb', line 81

def self.zip (target, *sources)
    if sources.length == 1 && sources[0].is_a?(Array)
        sources = sources[0]
    end

    if sources.length % 2 == 1
        raise "sources must contain an even number of string (path to file, filename)"
    end

    targetdir = "#{CommandWrap::Config.tmp_dir}/zip"
    id = 1
    while File.exists?(targetdir)
        targetdir = "#{CommandWrap::Config.tmp_dir}/zip#{id}"
        id += 1
    end
    FileUtils.mkdir(targetdir)

    path = ''
    sources.each do |value|
        if path == ''
            path = value
        else
            FileUtils.copy(path, "#{targetdir}/#{value}")
            path = ''
        end
    end

    `#{CommandWrap::Config.zip} -j #{target} #{targetdir}/*`

    FileUtils.rm_rf(targetdir)
end