Class: RailsMarkdownTemplates::Renderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
Redcarpet::Render::SmartyPants
Defined in:
lib/rails_markdown_templates/renderer.rb

Overview

Custom Redcarpet HTML renderer for Markdown with a metadata block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Renderer

Returns a new instance of Renderer.



31
32
33
34
# File 'lib/rails_markdown_templates/renderer.rb', line 31

def initialize(options={})
  super options
  @metadata = {}
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



29
30
31
# File 'lib/rails_markdown_templates/renderer.rb', line 29

def 
  @metadata
end

Instance Method Details

Rendered after all the other elements



54
55
56
# File 'lib/rails_markdown_templates/renderer.rb', line 54

def doc_footer
  nil
end

#doc_headerObject

Render before any other elements



49
50
51
# File 'lib/rails_markdown_templates/renderer.rb', line 49

def doc_header
  nil
end

#metadata_jsonObject

Get JSON for the metadata



44
45
46
# File 'lib/rails_markdown_templates/renderer.rb', line 44

def 
  .to_json.html_safe
end

#metadata_tagsObject

Get HTML tag(s) for the metadata



37
38
39
40
41
# File 'lib/rails_markdown_templates/renderer.rb', line 37

def 
  .map do |k,v|
    "<meta name=\"#{k}\" content=\"#{v}\" />"
  end.join("\n").html_safe
end

#preprocess(full_document) ⇒ Object

Preprocess the whole document before the rendering process



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rails_markdown_templates/renderer.rb', line 59

def preprocess(full_document)
  # Extract and store metadata block from start of document
  #
  # N.B. Implementation "borrowed" from Metadown:
  #
  #  https://github.com/steveklabnik/metadown
  full_document =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
  self. = YAML.load($1) if $1

  # Return the document without the leading metadata block
  $POSTMATCH or full_document
end