class Pandocomatic::PandocMetadata
PandocMetadata
represents the metadata with pandoc options set in templates and input files.
Public Class Methods
Create an empty metadata object with only the source format set.
@param src_format [String] the source format @return [PandocMetadata[ empty metadata with only pandoc’s source format set.
# File lib/pandocomatic/pandoc_metadata.rb, line 54 def self.empty(src_format) PandocMetadata.new({ 'pandocomatic_' => { 'pandoc' => { 'from' => src_format } } }) end
Extract the metadata from an input file
@param input [String] the string to extract metadata from @return [Array] The return value is an array with the following two values:
-
The extracted metadata as a YAML string
-
The number of times the pandocomatic properties did occur in the input.
If more than one pandocomatic property is contained in the input, all but the first are discarded and are not present in the extracted metadata YAML string.
@raise [PandocomaticError] when pandoc metadata cannot be extracted.
# File lib/pandocomatic/pandoc_metadata.rb, line 207 def self.extract_metadata(input, path = nil) metadata_blocks = MetadataBlockList.new input, path ["#{YAML.dump(metadata_blocks.full)}...", metadata_blocks.count_pandocomatic_blocks] end
Collect the metadata embedded in the src file and create a new PandocMetadata
instance
@param input [String] the string to load the metadata from @param path [String|Nil] the path to the source of the input, if any @return [PandocMetadata] the metadata in the source file, or an empty
one if no such metadata is contained in the source file.
@raise [PandocomaticError] when the pandoc metadata cannot be extracted.
# File lib/pandocomatic/pandoc_metadata.rb, line 78 def self.load(input, path = nil) yaml, pandocomatic_blocks = extract_metadata(input, path) if yaml.empty? PandocMetadata.new else PandocMetadata.new PandocomaticYAML.load(yaml, path), unique: pandocomatic_blocks <= 1 end end
Collect the metadata embedded in the src file and create a new PandocMetadata
instance
@param src [String] the path to the file to load metadata from @return [PandocMetadata] the metadata in the source file, or an empty
one if no such metadata is contained in the source file.
# File lib/pandocomatic/pandoc_metadata.rb, line 64 def self.load_file(src) self.load File.read(src), src end
Creat e new PandocMetadata
object based on the properties contained in a Hash
@param hash [Hash] initial properties for this new PandocMetadata
object
@param unique [Boolean = true] the pandocomatic property did occur at most once. @return [PandocMetadata]
# File lib/pandocomatic/pandoc_metadata.rb, line 96 def initialize(hash = {}, unique: true) super() merge! hash @unique = unique end
Extract the YAML metadata from an input string
@param input [String] the input string @return [String] the YAML data embedded in the input string
# File lib/pandocomatic/pandoc_metadata.rb, line 45 def self.pandoc2yaml(input) extract_metadata(input).first end
Public Instance Methods
Get the pandoc options for this PandocMetadata
object
@return [Hash] the pandoc options if there are any, an empty Hash
otherwise.
# File lib/pandocomatic/pandoc_metadata.rb, line 166 def pandoc_options if pandoc_options? pandocomatic['pandoc'] else {} end end
Does this PandocMetadata
object has a pandoc options property?
@return [Boolean] True if there is a pandoc options property in this
PandocMetadata object. False otherwise.
# File lib/pandocomatic/pandoc_metadata.rb, line 188 def pandoc_options? pandocomatic? and pandocomatic.key? 'pandoc' and !pandocomatic['pandoc'].nil? end
Get the pandocomatic property of this PandocMetadata
object
@return [Hash] the pandocomatic property as a Hash, if any, an empty
Hash otherwise.
# File lib/pandocomatic/pandoc_metadata.rb, line 178 def pandocomatic return self['pandocomatic'] if key? 'pandocomatic' self['pandocomatic_'] if key? 'pandocomatic_' end
Does this PandocMetadata
object have a pandocomatic property?
@return [Boolean] True if this PandocMetadata
object has a Hash property named “pandocomatic_”. False otherwise.
Note. For backward compatibility with older versions of pandocomatic, properties named “pandocomatic” (without the trailing underscore) are also accepted.
# File lib/pandocomatic/pandoc_metadata.rb, line 153 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
Does this PandocMetadata
object use a template?
@return [Boolean] true if it has a key ‘use-template’ among the
pandocomatic template properties.
# File lib/pandocomatic/pandoc_metadata.rb, line 114 def template? pandocomatic? and pandocomatic.key? 'use-template' and !pandocomatic['use-template'].empty? end
Get the used template’s name
@return [String] the name of the template used, if any, “” otherwise.
# File lib/pandocomatic/pandoc_metadata.rb, line 137 def template_name if template? pandocomatic['use-template'] else '' end end
Get all the templates of this PandocMetadata
oject
@return [Array] an array of templates used in this PandocMetadata
object
# File lib/pandocomatic/pandoc_metadata.rb, line 122 def templates if template? if pandocomatic['use-template'].is_a? Array pandocomatic['use-template'] else [pandocomatic['use-template']] end else [''] end end
Did the metadata contain multiple pandocomatic blocks?
@return [Boolean] True if at most one pandocomatic block was present in the metadata
# File lib/pandocomatic/pandoc_metadata.rb, line 106 def unique? @unique end