Class: Hologram::DocumentBlock

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

Constant Summary collapse

COMMENT_REGEX =
/^\s*---\s(.*?)\s---$/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



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

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

#is_valid?Boolean

Returns:

  • (Boolean)


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

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) ⇒ Object

sets the header tag based on how deep your nesting is



53
54
55
# File 'lib/hologram/document_block.rb', line 53

def markdown_with_heading(heading = 1)
  "\n\n<h#{heading.to_s} id=\"#{@name}\">#{@title}</h#{heading.to_s}>" + @markdown
end

#set_members(config, markdown) ⇒ Object



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

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

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