Class: Paru::Pandoc2Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/paru/pandoc2yaml.rb

Overview

Utility class to extract YAML metadata form a markdown file in pandoc’s own markdown format.

Constant Summary collapse

PANDOC_2_JSON =

Converter from pandoc’s markdown to pandoc’s AST JSON

Paru::Pandoc.new do
  from 'markdown'
  to 'json'
end
JSON_2_PANDOC =

Converter from pandoc’s AST JSON back to pandoc. Note the ‘standalone’ property, which is needed to output the metadata as well.

Paru::Pandoc.new do
  from 'json'
  to 'markdown'
  standalone
end
VERSION =

Pandoc-type API version key

'pandoc-api-version'
META =

Meta block key

'meta'
BLOCKS =

Content’s blocks key

'blocks'

Class Method Summary collapse

Class Method Details

.extract_metadata(input_document) ⇒ String

Extract the YAML metadata from input document

Parameters:

  • input_document (String)

    path to input document

Returns:

  • (String)

    YAML metadata from input document on STDOUT



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/paru/pandoc2yaml.rb', line 61

def self.(input_document)
  json = JSON.parse(PANDOC_2_JSON << File.read(input_document))
  yaml = ''

  version,  = json.values_at(VERSION, META)

  unless .empty?
     = {
      VERSION => version,
      META => ,
      BLOCKS => []
    }

    yaml = JSON_2_PANDOC << JSON.generate()
  end

  yaml
end