Class: Hologram::DocumentBlock
- Inherits:
-
Object
- Object
- Hologram::DocumentBlock
- Defined in:
- lib/hologram/document_block.rb
Constant Summary collapse
- COMMENT_REGEX =
/^\s*---\s(.*?)\s---$/m
Instance Attribute Summary collapse
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#children ⇒ Object
Returns the value of attribute children.
-
#config ⇒ Object
Returns the value of attribute config.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#heading ⇒ Object
Returns the value of attribute heading.
-
#markdown ⇒ Object
Returns the value of attribute markdown.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #get_hash ⇒ Object
-
#initialize(config = nil, markdown = nil) ⇒ DocumentBlock
constructor
A new instance of DocumentBlock.
- #is_valid? ⇒ Boolean
-
#markdown_with_heading(heading = 1) ⇒ Object
sets the header tag based on how deep your nesting is.
- #set_members(config, markdown) ⇒ Object
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
#categories ⇒ Object
Returns the value of attribute categories.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def categories @categories end |
#children ⇒ Object
Returns the value of attribute children.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def children @children end |
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def config @config end |
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def errors @errors end |
#heading ⇒ Object
Returns the value of attribute heading.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def heading @heading end |
#markdown ⇒ Object
Returns the value of attribute markdown.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def markdown @markdown end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/hologram/document_block.rb', line 5 def parent @parent end |
#title ⇒ Object
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_hash ⇒ Object
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
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 |