Class: Madness::Document

Inherits:
Object
  • Object
show all
Includes:
ServerHelper
Defined in:
lib/madness/document.rb

Overview

Handle a single markdown document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ServerHelper

#config, #docroot, #log

Constructor Details

#initialize(path) ⇒ Document

At initialization, we handle three file “types”: :readme - in case the path is a directory, then the only file we

MAY show, is the README.md in it

:file - in case the path is a *.md file :empty - in any other case, we don’t know.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/madness/document.rb', line 14

def initialize(path)
  @path = path

  base = path.empty? ? docroot : "#{docroot}/#{path}"

  if File.directory? base
    @file = "#{base}/README.md"
    @dir  = base
    @type = :readme
  elsif File.exist? "#{base}.md"
    @file = "#{base}.md"
    @dir  = File.dirname file
    @type = :file
  else
    @file = ''
    @dir  = docroot
    @type = :empty
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



7
8
9
# File 'lib/madness/document.rb', line 7

def dir
  @dir
end

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/madness/document.rb', line 7

def file
  @file
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/madness/document.rb', line 7

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/madness/document.rb', line 7

def type
  @type
end

Instance Method Details

#contentObject

Return the HTML for that document



35
36
37
# File 'lib/madness/document.rb', line 35

def content
  @content ||= content!
end

#content!Object

Return the HTML for that document, force re-read.



40
41
42
43
44
45
46
47
# File 'lib/madness/document.rb', line 40

def content!
  if File.exist?(file)
    markdown_to_html
  else
    @type = :empty
    ""
  end
end

#titleObject

Return a reasonable HTML title for the file or directory



50
51
52
53
54
55
56
57
# File 'lib/madness/document.rb', line 50

def title
  if file =~ /README.md/
    result = File.basename File.dirname(file)
  else
    result = File.basename(file,'.md')
  end
  result.tr '-', ' '
end