Class: Pandocomatic::PandocMetadata

Inherits:
Hash
  • Object
show all
Defined in:
lib/pandocomatic/pandoc_metadata.rb

Overview

PandocMetadata represents the metadata with pandoc options set in templates and input files.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, unique: true) ⇒ PandocMetadata

Creat e new PandocMetadata object based on the properties contained in a Hash

at most once.

Parameters:

  • hash (Hash) (defaults to: {})

    initial properties for this new PandocMetadata object

  • unique (Boolean = true) (defaults to: true)

    the pandocomatic property did occur



109
110
111
112
113
# File 'lib/pandocomatic/pandoc_metadata.rb', line 109

def initialize(hash = {}, unique: true)
  super()
  merge! hash
  @unique = unique
end

Class Method Details

.empty(src_format, ignore_pandocomatic: false) ⇒ PandocMetadata[ empty metadata with only pandoc's source format

Create an empty metadata object with only the source format set.

configuration in YAML metadata blocks set.

Parameters:

  • src_format (String)

    the source format

  • ignore_pandocomatic (Boolean) (defaults to: false)

    when true, ignore pandocomatic

Returns:

  • (PandocMetadata[ empty metadata with only pandoc's source format)

    PandocMetadata[ empty metadata with only pandoc’s source format



56
57
58
59
60
# File 'lib/pandocomatic/pandoc_metadata.rb', line 56

def self.empty(src_format, ignore_pandocomatic: false)
   = PandocMetadata.new
  ['pandocomatic_'] = { 'pandoc' => { 'from' => src_format } } unless ignore_pandocomatic
  
end

.load(input, path: nil, ignore_pandocomatic: false) ⇒ PandocMetadata

Collect the metadata embedded in the src file and create a new PandocMetadata instance

configuration in YAML metadata blocks extracted.

Parameters:

  • input (String)

    the string to load the metadata from

  • path (String|Nil) (defaults to: nil)

    the path to the source of the input, if any

  • ignore_pandocomatic (Boolean) (defaults to: false)

    when true, ignore pandocomatic

Returns:

  • (PandocMetadata)

    the metadata in the source file, or an empty one if no such metadata is contained in the source file.

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pandocomatic/pandoc_metadata.rb', line 84

def self.load(input, path: nil, ignore_pandocomatic: false)
  yaml, pandocomatic_blocks = (input, path)

  if yaml.empty?
    PandocMetadata.new
  else
     = PandocMetadata.new PandocomaticYAML.load(yaml, path), unique: pandocomatic_blocks <= 1

    if ignore_pandocomatic
      .delete('pandocomatic')
      .delete('pandocomatic_')
    end

    
  end
end

.load_file(src, ignore_pandocomatic: false) ⇒ PandocMetadata

Collect the metadata embedded in the src file and create a new PandocMetadata instance

Parameters:

  • src (String)

    the path to the file to load metadata from

Returns:

  • (PandocMetadata)

    the metadata in the source file, or an empty one if no such metadata is contained in the source file.



68
69
70
# File 'lib/pandocomatic/pandoc_metadata.rb', line 68

def self.load_file(src, ignore_pandocomatic: false)
  self.load File.read(src), path: src, ignore_pandocomatic:
end

.pandoc2yaml(input) ⇒ String

Extract the YAML metadata from an input string

Parameters:

  • input (String)

    the input string

Returns:

  • (String)

    the YAML data embedded in the input string



45
46
47
# File 'lib/pandocomatic/pandoc_metadata.rb', line 45

def self.pandoc2yaml(input)
  (input).first
end

Instance Method Details

#pandoc_optionsHash

Get the pandoc options for this PandocMetadata object

Returns:

  • (Hash)

    the pandoc options if there are any, an empty Hash otherwise.



179
180
181
182
183
184
185
# File 'lib/pandocomatic/pandoc_metadata.rb', line 179

def pandoc_options
  if pandoc_options?
    pandocomatic['pandoc']
  else
    {}
  end
end

#pandoc_options?Boolean

Does this PandocMetadata object has a pandoc options property?

Returns:

  • (Boolean)

    True if there is a pandoc options property in this PandocMetadata object. False otherwise.



201
202
203
# File 'lib/pandocomatic/pandoc_metadata.rb', line 201

def pandoc_options?
  pandocomatic? and pandocomatic.key? 'pandoc' and !pandocomatic['pandoc'].nil?
end

#pandocomaticHash

Get the pandocomatic property of this PandocMetadata object

Returns:

  • (Hash)

    the pandocomatic property as a Hash, if any, an empty Hash otherwise.



191
192
193
194
195
# File 'lib/pandocomatic/pandoc_metadata.rb', line 191

def pandocomatic
  return self['pandocomatic'] if key? 'pandocomatic'

  self['pandocomatic_'] if key? 'pandocomatic_'
end

#pandocomatic?Boolean

Does this PandocMetadata object have a pandocomatic property?

property named “pandocomatic_”. False otherwise.

Note. For backward compatibility with older versions of pandocomatic, properties named “pandocomatic” (without the trailing underscore) are also accepted.

Returns:

  • (Boolean)

    True if this PandocMetadata object has a Hash



166
167
168
169
170
171
172
173
# File 'lib/pandocomatic/pandoc_metadata.rb', line 166

def pandocomatic?
  config = nil
  if key?('pandocomatic') || key?('pandocomatic_')
    config = self['pandocomatic'] if key? 'pandocomatic'
    config = self['pandocomatic_'] if key? 'pandocomatic_'
  end
  config.is_a? Hash
end

#template?Boolean

Does this PandocMetadata object use a template?

Returns:

  • (Boolean)

    true if it has a key ‘use-template’ among the pandocomatic template properties.



127
128
129
# File 'lib/pandocomatic/pandoc_metadata.rb', line 127

def template?
  pandocomatic? and pandocomatic.key? 'use-template' and !pandocomatic['use-template'].empty?
end

#template_nameString

Get the used template’s name

Returns:

  • (String)

    the name of the template used, if any, “” otherwise.



150
151
152
153
154
155
156
# File 'lib/pandocomatic/pandoc_metadata.rb', line 150

def template_name
  if template?
    pandocomatic['use-template']
  else
    ''
  end
end

#templatesArray

Get all the templates of this PandocMetadata oject

Returns:

  • (Array)

    an array of templates used in this PandocMetadata object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pandocomatic/pandoc_metadata.rb', line 135

def templates
  if template?
    if pandocomatic['use-template'].is_a? Array
      pandocomatic['use-template']
    else
      [pandocomatic['use-template']]
    end
  else
    ['']
  end
end

#unique?Boolean

Did the metadata contain multiple pandocomatic blocks?

in the metadata

Returns:

  • (Boolean)

    True if at most one pandocomatic block was present



119
120
121
# File 'lib/pandocomatic/pandoc_metadata.rb', line 119

def unique?
  @unique
end