Class: Jekyll::MarkdownOutput::MarkdownPage

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-markdown-output/markdown_page.rb

Overview

Builds the Markdown bytes for a single source document. Not a Jekyll::Page on purpose: we write directly to _site/ in a post_write hook so the converter and layout pipeline cannot touch the output.

Constant Summary collapse

DEFAULT_FRONTMATTER_KEYS =
%w[title date url summary tags category author].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, doc, options = {}) ⇒ MarkdownPage

Returns a new instance of MarkdownPage.



14
15
16
17
18
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 14

def initialize(site, doc, options = {})
  @site = site
  @doc = doc
  @options = options
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



12
13
14
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 12

def doc
  @doc
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 12

def options
  @options
end

#siteObject (readonly)

Returns the value of attribute site.



12
13
14
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 12

def site
  @site
end

Instance Method Details

#destinationObject

Absolute path on disk where this file should be written.



21
22
23
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 21

def destination
  File.join(@site.dest, relative_destination)
end

#relative_destinationObject

Path relative to the site root, e.g. “/foo.md”.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 26

def relative_destination
  ext = @options.fetch("extension", ".md")
  url = @doc.url
  if url.end_with?("/")
    File.join(url, "index#{ext}")
  else
    dir = File.dirname(url)
    base = File.basename(url, ".*")
    File.join(dir, "#{base}#{ext}")
  end
end

#to_sObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll-markdown-output/markdown_page.rb', line 38

def to_s
  parts = []
  fm = build_frontmatter
  parts << "---\n#{fm}---" unless fm.empty?
  if @options.fetch("include_title_heading", true) && @doc.data["title"]
    parts << "# #{@doc.data["title"]}"
  end
  parts << rendered_source.to_s.strip
  "#{parts.join("\n\n")}\n"
end