class Madness::Document

Handle a single document path.

Attributes

base[R]
dir[R]
file[R]
path[R]
title[R]
type[R]

Public Class Methods

new(path) click to toggle source
# File lib/madness/document.rb, line 9
def initialize(path)
  @path = path
  @base = path.empty? ? docroot : "#{docroot}/#{path}"
  @base.chomp! '/'
  set_base_attributes
end

Public Instance Methods

content() click to toggle source

Return the HTML for that document

# File lib/madness/document.rb, line 17
def content
  @content ||= %i[empty missing].include?(type) ? "<h1>#{title}</h1>" : markdown.to_html
end
href() click to toggle source
# File lib/madness/document.rb, line 25
def href
  relative_file.to_href
end
relative_file() click to toggle source
# File lib/madness/document.rb, line 21
def relative_file
  file[%r{^#{docroot}/(.*)}, 1]
end

Private Instance Methods

cover_page() click to toggle source
# File lib/madness/document.rb, line 81
def cover_page
  @cover_page ||= cover_page!
end
cover_page!() click to toggle source
# File lib/madness/document.rb, line 85
def cover_page!
  cover_page_candidates.each do |candidate|
    return candidate if File.exist? candidate
  end

  nil
end
cover_page_candidates() click to toggle source
# File lib/madness/document.rb, line 93
def cover_page_candidates
  @cover_page_candidates ||= [
    File.expand_path("../#{File.basename(base)}.md", base),
    File.expand_path('index.md', base),
    File.expand_path('README.md', base),
  ]
end
markdown() click to toggle source
# File lib/madness/document.rb, line 65
def markdown
  @markdown ||= MarkdownDocument.new(markdown_text, title: title)
end
markdown_text() click to toggle source
# File lib/madness/document.rb, line 69
def markdown_text
  @markdown_text ||= File.read file
end
md_file?() click to toggle source
# File lib/madness/document.rb, line 73
def md_file?
  File.exist?("#{base}.md") || (File.exist?(base) && File.extname(base) == '.md')
end
md_filename() click to toggle source
# File lib/madness/document.rb, line 77
def md_filename
  File.extname(base) == '.md' ? base : "#{base}.md"
end
set_base_attributes() click to toggle source

Identify file, dir and type. :readme - in case the path is a directory, and it contains index.md

or README.md

:file - in case the path is a *.md file :empty - in case it is a folder without README.md or index.md :missing - in any other case, we don’t know (will trigger 404)

# File lib/madness/document.rb, line 37
def set_base_attributes
  @dir  = docroot
  @type = :missing
  @file = ''
  @title = 'Index'

  if File.directory? base
    @title = File.basename(path).to_label unless path.empty?
    set_base_attributes_for_directory
  elsif md_file?
    @file = md_filename
    @title = File.basename(base).to_label
    @dir  = File.dirname file
    @type = :file
  end
end
set_base_attributes_for_directory() click to toggle source
# File lib/madness/document.rb, line 54
def set_base_attributes_for_directory
  @dir  = base
  @type = :readme

  if cover_page
    @file = cover_page
  else
    @type = :empty
  end
end