Class: Hologram::DocumentBlock
- Inherits:
-
Object
- Object
- Hologram::DocumentBlock
- Defined in:
- lib/hologram/document_block.rb
Constant Summary collapse
- COMMENT_REGEX =
/^\s*---\s(.*?)\s---[\r]?$/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, opts = {}) ⇒ 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
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
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 |