class Paru::PandocFilter::Metadata

A Metadata object is a Ruby Hash representation of a pandoc metadata node.

Public Class Methods

new(contents = {}) click to toggle source

Create a new Metadata object based on the contents.

@param contents [MetaMap|String|Hash] the initial contents of this

metadata. If contents is a String, it is treated as a YAML string
and converted to a Hash first.

@raise Error when converting contents to a Hash fails

# File lib/paru/filter/metadata.rb, line 36
def initialize(contents = {})
    if not contents.is_a? Hash
        # If not a Hash, it is either a YAML string or can be
        # converted to a YAML string
        if contents.is_a? PandocFilter::MetaMap
            yaml_string = meta2yaml contents
        elsif contents.is_a? String
            yaml_string = contents
        else
            raise FilterError.new("Expected a Hash, MetaMap, or String, got '#{contents}' instead.")
        end

        # Try to convert the YAML string to a Hash
        if yaml_string.empty?
            contents = {}
        else
            contents = YAML.safe_load yaml_string, permitted_classes: [Date]
        end

        if not contents
            # Error parsing YAML
            raise FilterError.new("Unable to convert YAML string '#{yaml_string}' to a Hash.")
        end
    end

    # Merge the contents with this newly created Metadata
    contents.each do |key, value|
        self[key] = value
    end
end

Public Instance Methods

to_meta() click to toggle source

Convert this Metadata to a pandoc AST representation of metadata: {PandocFilter::Meta}

@return [Meta] the pandoc AST representation as a {PandocFilter::Meta} node

# File lib/paru/filter/metadata.rb, line 71
def to_meta()
    if self.empty?
        PandocFilter::Meta.new {}
    else
        begin
            yaml_string = "#{clean_hash.to_yaml}..."
            yaml2json = Paru::Pandoc.new {from "markdown"; to "json"}
            json_string = yaml2json << yaml_string
            meta_doc = PandocFilter::Document.from_JSON json_string
            meta_doc.meta
        rescue
        end
    end
end

Private Instance Methods

clean_hash() click to toggle source

Create a true Hash from this Metadata to prevent the to_yaml method from mixing in the name of this class and confusing pandoc

# File lib/paru/filter/metadata.rb, line 105
def clean_hash
    hash = {}
    each do |key, value|
        hash[key] = value
    end
    hash
end
meta2yaml(meta) click to toggle source

Convert a {PandocFilter::Meta} node to a Metadata

@param meta [Meta|MetaMap] the {PandocFilter::Meta} node to convert to a

MetadataHash
# File lib/paru/filter/metadata.rb, line 92
def meta2yaml(meta)
    begin
        json2yaml = Paru::Pandoc.new {from "json"; to "markdown"; standalone}
        meta = PandocFilter::Meta.from_meta_map(meta) unless meta.is_a? PandocFilter::Meta
        meta_doc = PandocFilter::Document.new(PandocFilter::CURRENT_PANDOC_VERSION, meta.to_ast, [])
        yaml_string = json2yaml << meta_doc.to_JSON
        yaml_string.strip
    rescue
    end
end