Class: Hologram::DocumentBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/hologram/document_block.rb

Constant Summary collapse

COMMENT_REGEX =
/^\s*---\s(.*?)\s---[\r]?$/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, markdown = nil) ⇒ DocumentBlock

Returns a new instance of DocumentBlock.



7
8
9
10
11
# File 'lib/hologram/document_block.rb', line 7

def initialize(config = nil, markdown = nil)
  @children = {}
  @errors = []
  set_members(config, markdown) if config and markdown
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def categories
  @categories
end

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def children
  @children
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def config
  @config
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def errors
  @errors
end

#headingObject

Returns the value of attribute heading.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def heading
  @heading
end

#markdownObject

Returns the value of attribute markdown.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def markdown
  @markdown
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def name
  @name
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def parent
  @parent
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/hologram/document_block.rb', line 5

def title
  @title
end

Class Method Details

.from_comment(comment) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hologram/document_block.rb', line 13

def self.from_comment(comment)
  comment_match = COMMENT_REGEX.match(comment)
  return if !comment_match

  markdown = comment.sub(comment_match[0], '')
  config = YAML::load(comment_match[1])

  self.new(config, markdown)
rescue ArgumentError, Psych::SyntaxError
  raise CommentLoadError, "Could not parse comment:\n#{comment}"
end

Instance Method Details

#get_hashObject



38
39
40
41
42
43
44
# File 'lib/hologram/document_block.rb', line 38

def get_hash
  {:name => @name,
   :parent => @parent,
   :categories => @categories,
   :title => @title
  }
end

#is_valid?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/hologram/document_block.rb', line 46

def is_valid?
  errors << 'Missing required category and/or parent config value' if !categories and !parent
  errors << 'Missing name or title config value' if !name and !title
  errors << 'Missing required markdown' if !markdown
  errors.empty?
end

#markdown_with_heading(heading = 1, opts = {}) ⇒ Object

sets the header tag based on how deep your nesting is



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hologram/document_block.rb', line 55

def markdown_with_heading(heading = 1, opts={})
  include_sub_nav = opts[:include_sub_nav] || false

  output = "\n\n<h#{heading.to_s} id=\"#{@name}\" class=\"#{css_class_name}\">#{@title}</h#{heading.to_s}>"
  if include_sub_nav && !children.empty?
    output += "\n<ul class=\"section-nav\">"
    children.values.each do |child|
      output += "\n  <li><a href=\"\##{child.name}\">#{child.title}</a></li>"
    end
    output += "\n</ul>\n"
  end
  output += @markdown
  output
end

#set_members(config, markdown) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hologram/document_block.rb', line 25

def set_members(config, markdown)
  @config   = config
  @name     = config['name']
  @categories = Array(config['category'] || config['categories'])
  @title    = h(config['title'])
  @parent   = config['parent']
  @markdown = markdown

  if @name.nil?
    @name = @title.gsub(' ', '_')
  end
end