Class: Pdfmdedit

Inherits:
Pdfmd show all
Defined in:
lib/pdfmd/pdfmdedit.rb

Overview

Class: pdfmdedit

Edit Metadata of PDF documentsc

Constant Summary collapse

@@edit_tags =
Hash.new

Instance Attribute Summary collapse

Attributes inherited from Pdfmd

#logfile, #logstatus

Instance Method Summary collapse

Methods inherited from Pdfmd

#check_metatags, #metadata, #readUserInput, #read_metatags

Methods included from Pdfmdmethods

#determineValidSetting, #log, #queryHiera

Constructor Details

#initialize(filename) ⇒ Pdfmdedit

Returns a new instance of Pdfmdedit.



11
12
13
14
# File 'lib/pdfmd/pdfmdedit.rb', line 11

def initialize(filename)
  super(filename)
  self.set_tags(@@default_tags)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/pdfmd/pdfmdedit.rb', line 7

def filename
  @filename
end

#opendocObject

Returns the value of attribute opendoc.



7
8
9
# File 'lib/pdfmd/pdfmdedit.rb', line 7

def opendoc
  @opendoc
end

#pdfviewerObject

Returns the value of attribute pdfviewer.



7
8
9
# File 'lib/pdfmd/pdfmdedit.rb', line 7

def pdfviewer
  @pdfviewer
end

Instance Method Details

#set_tags(tags = Array.new) ⇒ Object

Setting the tags to edit



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
# File 'lib/pdfmd/pdfmdedit.rb', line 36

def set_tags(tags = Array.new)

  if tags.is_a?(String) and tags.downcase == 'all'
    @@default_tags.each do |value|
      @@edit_tags[value] = ''
    end
  elsif tags.is_a?(Array)
    tags.each do |value|
      @@edit_tags[value] = ''
    end
  elsif tags.is_a?(Hash)
    # NOTE: might need some adjustment here
    # Not sure this is used at all
    @@edit_tags = tags
  else


    # Try to match tags
    if tags.is_a?(String)

      @@edit_tags = {}
      tagsForEditing = tags.split(',')
      tagsForEditing.each do |value|

        # Matchin for seperator
        separator = @@edit_separator
        if value.match(/#{separator}/)

          self.log('debug', 'Found tag value assignment.')
          tagmatching = value.split(separator)

          # Check date for validity
          if tagmatching[0] == 'createdate'
            validatedDate = validateDate(tagmatching[1])
            if !validatedDate
              self.log('error',"Date not recognized: '#{tagmatching[1]}'.")
              abort 'Date format not recognized. Abort.'
            else
              self.log('debug',"Identified date: #{validatedDate} ")
              @@edit_tags[tagmatching[0]] = validatedDate
            end
          else
            self.log('debug', "Identified key #{tagmatching[0]} with value '#{tagmatching[1]}'.")
            @@edit_tags[tagmatching[0]] = tagmatching[1]
          end
        else
          @@edit_tags[value] = ''
        end

      end

    end

  end


end

#start_viewer(filename = '', viewer = '') ⇒ Object

Start a viewer



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pdfmd/pdfmdedit.rb', line 18

def start_viewer(filename = '', viewer = '')
  if File.exists?(filename) and !viewer.empty?

    pid = IO.popen("#{viewer} '#{filename}' 2>&1")
    self.log('debug', "Application '#{viewer}' with PID #{pid.pid} started to show file '#{filename}'.")
    pid.pid

  elsif viewer.empty?
    self.log('error', 'No viewer specified. Aborting document view.')
  else
    self.log('error', "Could not find file '#{filename}' for viewing.")
  end

end

#update_tagsObject

Update the tags

Reads @@edit_tags and asks for updates from the user if no value in
@@edit_tags is provided


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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pdfmd/pdfmdedit.rb', line 99

def update_tags()

  # Empty String for possible viewer Process PID
  viewerPID = ''

  # Iterate through all tags and request information from user
  #   if necessary
  @@edit_tags.each do |key,value|
    if value.empty?

      # At this poing:
      # 1. If @opendoc
      # 2. viewerPID.empty? (no viewer stated)
      # => Start the viewer
      if @opendoc and viewerPID.to_s.empty?
        viewerPID = start_viewer(@filename, @pdfviewer)
        self.log('debug', "Started external viewer '#{@pdfviewer}' with file '#{@filename}' and PID: #{viewerPID}")
      end

      puts 'Changing ' + key.capitalize + ', current value: ' + @@metadata[key].to_s

      # Save the current value
      current_value = @@metadata[key]

      # Validate Check for date input
      if key.downcase == 'createdate'

        # Repeat asking for a valid date
        validatedDate = false
        while !validatedDate
          userInput = readUserInput('New date value: ')

          if userInput.empty? and !current_value.empty?
            @@metadata[key] = current_value
            self.log('debug', "User decided to take over old value for #{key}.")
            puts 'Date is needed. Setting old value: ' + current_value
            break
          end

          # Update loop condition variable
          validatedDate = validateDate(userInput)

          # Update Metadata
          @@metadata[key] = validatedDate
        end

        # Input of all other values
      else

        @@metadata[key] = readUserInput('New value: ')

      end

    else

      # Setting the new metadata
      @@metadata[key] = value

    end
  end

  # Close the external PDF viewer if a PID has been set.
  if !viewerPID.to_s.empty?
    `kill #{viewerPID}`
    `pkill -f "#{@pdfviewer} #{@filename}"` # Double kill
    self.log('debug', "Viewer process with PID #{viewerPID} killed.")
  end

end

#validateDate(date) ⇒ Object

Function to validate and interprete date information



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/pdfmd/pdfmdedit.rb', line 171

def validateDate(date)

  year    = '[1-2][90][0-9][0-9]'
  month   = '0[1-9]|10|11|12'
  day     = '[1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]'
  hour    = '[0-1][0-9]|2[0-3]|[1-9]'
  minute  = '[0-5][0-9]'
  second  = '[0-5][0-9]'
  case date
  when /^(#{year})(#{month})(#{day})$/
    identifiedDate =  $1 + ':' + $2 + ':' + $3 + ' 00:00:00'
  when /^(#{year})(#{month})(#{day})(#{hour})(#{minute})(#{second})$/
    identifiedDate =  $1 + ':' + $2 + ':' + $3 + ' ' + $4 + ':' + $5 + ':' + $6
  when /^(#{year})[\:|\.|\-](#{month})[\:|\.|\-](#{day})\s(#{hour})[\:](#{minute})[\:](#{second})$/
    identifiedDate =  $1 + ':' + $2 + ':' + $3 + ' ' + $4 + ':' + $5 + ':' + $6
  when /^(#{year})[\:|\.|\-](#{month})[\:|\.|\-](#{day})$/
    day   = "%02d" % $3
    month = "%02d" % $2

    # Return the identified string
    $1 + ':' + month + ':' + day + ' 00:00:00'

  else

    # This wasn't a date we recognize
    false

  end
end

#write_tags(filename) ⇒ Object

Write tags from the @@metadata back into the file



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/pdfmd/pdfmdedit.rb', line 203

def write_tags(filename)

  filename.empty? ? filename = @filename : ''

  commandparameter = '-overwrite_original'
  @@metadata.each do |key,value|
    commandparameter = commandparameter + " -#{key}=\"#{value}\""
  end

  if !@@documentPassword.to_s.empty?
    commandparameter = commandparameter + " -password '#{@@documentPassword}'"
  end

  command = "exiftool #{commandparameter} '#{filename}'"
  `#{command}`
  self.log('info',"Updating '#{filename}' with " + commandparameter.gsub(/\s\-password\s\'.*\'/,'').gsub(/\-overwrite\_original\s/,'').gsub(/\'\s\-/,"', ").gsub(/\-/,' ') )

end