Top Level Namespace
Defined Under Namespace
Modules: Pdfmdmethods Classes: DOC, Pdfmd, Pdfmdclean, Pdfmdconfig, Pdfmdedit, Pdfmdrename, Pdfmdshow, Pdfmdsort, Pdfmdstat, String
Constant Summary collapse
- VERSION =
'2.1.2'
- NAME =
'pdfmd'
Instance Method Summary collapse
-
#identifyDate(datestring) ⇒ Object
Identify a date Function takes a string and tries to identify a date in there.
-
#init_bashcompletion(name, version, remove = false) ⇒ Object
Initializing or removing the bash_completion file.
-
#queryHiera(keyword, facts = 'UNSET') ⇒ Object
Query Hiera installation I don’t give a sh** about cross platform at this point.
-
#readLongDesc(filename) ⇒ Object
Read the content of the long description from an external file.
-
#readMetadata(pathFile = false) ⇒ Object
Function to read the metadata from a given file hash readMetadata(string).
-
#readUserInput(textstring = 'Enter value: ') ⇒ Object
Read user input.
-
#setKeywordsPreface(metadata, doktype) ⇒ Object
Set Keywords Preface based on title and subject If subject matches a number/character combination and contains no spaces, the preface will be combined with the doktype.
Instance Method Details
#identifyDate(datestring) ⇒ Object
Identify a date Function takes a string and tries to identify a date in there. returns false if no date could be identified otherwise the date is returned in the format as
YYYY:MM:DD HH:mm:ss
For missing time values zero is assumed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/pdfmd/methods.rb', line 178 def identifyDate(datestring) identifiedDate = '' 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 datestring 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 identifiedDate = $1 + ':' + month + ':' + day + ' 00:00:00' else identifiedDate = false end return identifiedDate end |
#init_bashcompletion(name, version, remove = false) ⇒ Object
Initializing or removing the bash_completion file
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 168 169 170 171 172 173 |
# File 'lib/pdfmd/pdfmdmethods.rb', line 133 def init_bashcompletion(name, version, remove = false) # Find the current local path where the original bash completion file might be hiding. paths = [ "#{File.dirname(File.($0))}/../lib", "#{Gem.dir}/gems/#{name}-#{version}/lib", ] bash_completion_destination = '/etc/bash_completion.d/pdfmd.bash' bash_completion_destination_backup = bash_completion_destination + '.backup' paths.each do |value| bash_completion_source = value + '/' + name + '/pdfmd.bash' if File.exists?(bash_completion_source) if !remove # Create a backup file when a file is found if File.exists?(bash_completion_destination) puts 'Existing file found.Taking backup.' `sudo cp #{bash_completion_destination} #{bash_completion_destination_backup}` end puts 'Installing ' + bash_completion_destination `sudo cp #{bash_completion_source} #{bash_completion_destination}` else if File.exists?(bash_completion_destination) puts 'Removing ' + bash_completion_destination `sudo rm #{bash_completion_destination}` if $?.exitstatus == 0 puts 'File successfully removed.' end else puts bash_completion_destination + ' not found.' end end end end end |
#queryHiera(keyword, facts = 'UNSET') ⇒ Object
Query Hiera installation I don’t give a sh** about cross platform at this point.
Return the hash of the hiera values or false (if no hiera is found)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pdfmd/methods.rb', line 11 def queryHiera(keyword,facts = 'UNSET') # Set default facts facts == 'UNSET' ? facts = "fqdn=#{`hostname`}" : '' # If hiera isn't found, return false # otherwise return the hash if !system('which hiera > /dev/null 2>&1') puts 'Cannot find "hiera" command in $path.' return eval('{}') else commandreturn = '' commandreturn = `hiera #{keyword} #{facts} 2>/dev/null` if $?.exitstatus == 1 return eval('{}') else return eval(commandreturn) end end end |
#readLongDesc(filename) ⇒ Object
Read the content of the long description from an external file
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/run.rb', line 12 def readLongDesc(filename) paths = [ "#{File.dirname(File.($0))}../lib", "#{Gem.dir}/gems/#{NAME}-#{VERSION}/lib", ] longDescContent = '' paths.each do |value| if File.exists?(value + '/' + filename) File.open(value + '/' + filename, 'r') do |infile| while (line = infile.gets) longDescContent = longDescContent + line end end end end longDescContent end |
#readMetadata(pathFile = false) ⇒ Object
Function to read the metadata from a given file hash readMetadata(string)
Besides the fields from the exif-fields two additional fields can be set:
error: is being set with a string in case exiftools returns a warning field password: is being set when a password has been necessary to access the
protected fields.
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/pdfmd/methods.rb', line 70 def readMetadata(pathFile = false) = Hash.new ['keywords'] = '' ['subject'] = '' ['title'] = '' ['author'] = '' ['creator'] = '' ['createdate'] = '' ['password'] = '' ['error'] = '' if not File.file?(pathFile) puts "Cannot access file #{pathFile}. Abort" abort end # Fetch the Metada with the help of exiftools (unless something better is # found = '^Creator\s+\:|^Author|Create Date|Subject|Keywords|Title|^Warning' = `exiftool '#{pathFile}' | egrep -i '#{}'` # Create an array of all data entries = .split("\n") # If this matches, the file is password protected. # Grep the password from hiera or from the user if entries.index{ |x| x.match(/Document is password protected/) } # Grep data from hiera hieraDefaults = queryHiera('pdfmd::config') # Use Hiera default PW if possible if not hieraDefaults['default'].nil? and not hieraDefaults['default']['password'].nil? and not hieraDefaults['default']['password'] == '' documentPassword = hieraDefaults['default']['password'] # Ask the user for a password else documentPassword = readUserInput('Please provide user password: ') end # Try to get the metadata again, this time with the password = `exiftool -password '#{documentPassword}' '#{pathFile}' | egrep -i '#{}'` # Add the password to the metadata and make it available to the other procedures ['password'] = documentPassword # Create an array of all entries entries = .split("\n") end entries.each do |entry| values = entry.split(" : ") values[0].match(/Creator/) and ['creator'] == '' ? ['creator'] = values[1]: ['creator'] = '' values[0].match(/Author/) and ['author'] == '' ? ['author'] = values[1]: ['author'] = '' values[0].match(/Create Date/) and ['createdate'] == '' ? ['createdate'] = values[1]: ['createdate'] = '' values[0].match(/Subject/) and ['subject'] == '' ? ['subject'] = values[1]: ['subject'] = '' values[0].match(/Keywords/) and ['keywords'] == '' ? ['keywords'] = values[1]: ['keywords'] ='' values[0].match(/Title/) and ['title'] == '' ? ['title'] = values[1]: ['title'] ='' if values[0].match(/Warning/) and values[1].match(/Document is password protected/) puts 'Document is protected' end # Password is not correct. Abort if values[0].match(/Warning/) and values[1].match(/Incorrect password/) abort values[1] + '. Abort!' end end return end |
#readUserInput(textstring = 'Enter value: ') ⇒ Object
Read user input
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/pdfmd/methods.rb', line 150 def readUserInput(textstring = 'Enter value: ') # if there is a password mentioned, hide the input if textstring.match(/password/i) print textstring userinput = STDIN.noecho(&:gets).chomp puts '' return userinput else return ask textstring end end |
#setKeywordsPreface(metadata, doktype) ⇒ Object
Set Keywords Preface based on title and subject If subject matches a number/character combination and contains no spaces, the preface will be combined with the doktype. If not: preface will contain the whole subject with dots and spaces being replaced with underscores
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pdfmd/methods.rb', line 42 def setKeywordsPreface(, doktype) if ['subject'].match(/^\d+[^+s]+.*/) and doktype != 'dok' return doktype + ['subject'] else subject = ['subject'] # Take care of special characters I18n.enforce_available_locales = false subject = I18n.transliterate(['subject']) # Replace everything else subject = subject.gsub(/[^a-zA-Z0-9]+/,'_') return subject end end |