Class: Pdfmdshow

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

Overview

Class: pdfmd.show

Constant Summary collapse

@@show_filename =
false
@@outputformat =
''
@@default_tags =
['createdate', 'author', 'title', 'subject', 'keywords']

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) ⇒ Pdfmdshow

Returns a new instance of Pdfmdshow.



10
11
12
13
# File 'lib/pdfmd/pdfmdshow.rb', line 10

def initialize(filename)
  super(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/pdfmd/pdfmdshow.rb', line 4

def filename
  @filename
end

Instance Method Details

#set_outputformat(format = 'yaml') ⇒ Object

Define the output format for showing the metadata



23
24
25
26
27
# File 'lib/pdfmd/pdfmdshow.rb', line 23

def set_outputformat( format = 'yaml' )
  format.nil? ? format = 'yaml' : ''
  self.log('debug',"Output format set to '#{format}'.")
  @@outputformat = format
end

#set_tags(tags = @@default_tags) ⇒ Object

Overvwrite the tags



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pdfmd/pdfmdshow.rb', line 30

def set_tags( tags = @@default_tags)

  if tags

    # Tags can be specified as array or string
    #
    case tags.class.to_s
    when /array/i
      @@default_tags = tags
    when /string/i
      @@default_tags = tags.split(/,\s+/)
    end

  end

end

#show_filename(enable = nil) ⇒ Object

Define if the filename should be visible in the output



17
18
19
# File 'lib/pdfmd/pdfmdshow.rb', line 17

def show_filename( enable = nil)
  @@show_filename = enable ? true : false
end

#show_metatags(tags = @@default_tags, format = @@outputformat, show_filename = @@show_filename) ⇒ Object

Return the provided metatags



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

def show_metatags( tags = @@default_tags, format = @@outputformat, show_filename = @@show_filename )

  # Build the output hash from the tags matching the values in @@default_tags
  metadataOutputHash = Hash.new
  tags.each do |tagname|
    if @@metadata.has_key?(tagname)
      metadataOutputHash[tagname] = @@metadata[tagname]
    elsif tagname.downcase == 'all' # Exception when for keyword 'all'
      metadataOutputHash = @@metadata
    end
  end

  if show_filename
    metadataOutputHash['filename'] = @filename
  end

  # Return output well formatted
  case format
  when /hash/i
    self.log('info',"Showing metatags for '#{@filename}' in format 'hash'.")
    metadataOutputHash
  when /csv/i
    csvData = Hash.new
    metadataOutputHash.keys.each do |tagname|
      csvData[tagname] = '"' + metadataOutputHash[tagname.downcase].to_s.gsub(/"/,'""') + '"'
    end
    self.log('info',"Showing metatags for '#{@filename}' in format 'csv'.")
    csvData.values.join(',')
  when /json/i
    require 'json'
    self.log('info',"Showing metatags for '#{@filename}' in format 'json'.")
    metadataOutputHash.to_json
  else 
    require 'yaml'
    self.log('info',"Showing metatags for '#{@filename}' in format 'yaml'.")
    metadataOutputHash.to_yaml
  end

end