module Bergamasco::Markdown

Constants

YAML_FRONT_MATTER_REGEXP

split file into YAML frontmatter and content

Public Class Methods

extract_references(html) click to toggle source

expects a references list generated by pandoc

# File lib/bergamasco/markdown.rb, line 64
def self.extract_references(html)
  doc = Nokogiri::HTML(html)
  doc.xpath('//div[@id="refs"]/div/@id').map { |ref| ref.value[4..-1] }
end
join_yaml_frontmatter(metadata, content) click to toggle source
# File lib/bergamasco/markdown.rb, line 18
def self.join_yaml_frontmatter(metadata, content)
  metadata.to_yaml + "---\n" + content
end
read_yaml(filepath) click to toggle source
# File lib/bergamasco/markdown.rb, line 31
def self.read_yaml(filepath)
  unless File.exist?(filepath)
    parentdir = Pathname.new(filepath).parent
    FileUtils.mkdir_p parentdir
    FileUtils.touch filepath
  end

  file = IO.read(filepath)
  SafeYAML.load(file)
end
read_yaml_for_doi_metadata(filepath, options={}) click to toggle source
# File lib/bergamasco/markdown.rb, line 42
def self.read_yaml_for_doi_metadata(filepath, options={})
  return nil unless File.exist?(filepath)

  file = IO.read(filepath)
  yaml = SafeYAML.load(file)
  return nil unless yaml.present?

  keys = options[:keys] || ["title", "author", "date", "tags", "summary", "accession_number", "doi", "type", "version", "references", "published"]
  metadata = yaml.extract!(*keys).compact

  content = YAML_FRONT_MATTER_REGEXP.match(file).post_match
  html = Bergamasco::Pandoc.convert(content, options)
  metadata["summary"] = Bergamasco::Summarize.summary_from_html(html, options)
  metadata["references"] = extract_references(html)
  metadata
end
split_yaml_frontmatter(file) click to toggle source
# File lib/bergamasco/markdown.rb, line 6
def self.split_yaml_frontmatter(file)
  return file unless match = YAML_FRONT_MATTER_REGEXP.match(file)

  metadata = SafeYAML.load(file)
  content = match.post_match
  [metadata, content]
end
update_file(filepath, new_metadata) click to toggle source
# File lib/bergamasco/markdown.rb, line 22
def self.update_file(filepath, new_metadata)
  file = IO.read(filepath)
  metadata, content = split_yaml_frontmatter(file)
  metadata = update_yaml_frontmatter(metadata, new_metadata)
  new_file = join_yaml_frontmatter(metadata, content)
  IO.write(filepath, new_file)
  metadata
end
update_yaml_frontmatter(metadata, new_metadata) click to toggle source
# File lib/bergamasco/markdown.rb, line 14
def self.update_yaml_frontmatter(metadata, new_metadata)
  metadata.merge(new_metadata).compact
end
write_yaml(filepath, content) click to toggle source
# File lib/bergamasco/markdown.rb, line 59
def self.write_yaml(filepath, content)
  IO.write(filepath, content.to_yaml)
end