Class: MdnQuery::Section
- Inherits:
-
Object
- Object
- MdnQuery::Section
- Defined in:
- lib/mdn_query/section.rb
Overview
A section of an entry of the Mozilla Developer Network documentation.
Instance Attribute Summary collapse
-
#children ⇒ Array<MdnQuery::Section>
readonly
The list of child sections.
-
#level ⇒ Fixnum
readonly
The level of the section.
-
#name ⇒ String
readonly
The name and title of the section.
-
#parent ⇒ MdnQuery::Section
readonly
The parent section.
-
#text ⇒ Array<String>
readonly
The text segments of the section.
Instance Method Summary collapse
-
#append_code(snippet, language: '') ⇒ void
Appends a code segment to the section.
-
#append_text(text) ⇒ void
Appends a text segment to the section.
-
#create_child(name) ⇒ MdnQuery::Section
Creates a new child section.
-
#initialize(name, level: 1, parent: nil) ⇒ MdnQuery::Section
constructor
Creates a new section.
-
#to_s ⇒ String
Returns the string representation of the section.
Constructor Details
#initialize(name, level: 1, parent: nil) ⇒ MdnQuery::Section
Creates a new section.
25 26 27 28 29 30 31 |
# File 'lib/mdn_query/section.rb', line 25 def initialize(name, level: 1, parent: nil) @name = (name) @level = level @parent = parent @text = [] @children = [] end |
Instance Attribute Details
#children ⇒ Array<MdnQuery::Section> (readonly)
Returns the list of child sections.
5 6 7 |
# File 'lib/mdn_query/section.rb', line 5 def children @children end |
#level ⇒ Fixnum (readonly)
Returns the level of the section.
11 12 13 |
# File 'lib/mdn_query/section.rb', line 11 def level @level end |
#name ⇒ String (readonly)
Returns the name and title of the section.
8 9 10 |
# File 'lib/mdn_query/section.rb', line 8 def name @name end |
#parent ⇒ MdnQuery::Section (readonly)
Returns the parent section.
14 15 16 |
# File 'lib/mdn_query/section.rb', line 14 def parent @parent end |
#text ⇒ Array<String> (readonly)
Returns the text segments of the section.
17 18 19 |
# File 'lib/mdn_query/section.rb', line 17 def text @text end |
Instance Method Details
#append_code(snippet, language: '') ⇒ void
This method returns an undefined value.
Appends a code segment to the section.
If the code segment is empty (i.e. consists of just whitespaces), it is not appended. The given snippet is embedded in a Markdown code block.
71 72 73 |
# File 'lib/mdn_query/section.rb', line 71 def append_code(snippet, language: '') @text << "\n```#{language}\n#{snippet}\n```\n" unless text_empty?(snippet) end |
#append_text(text) ⇒ void
This method returns an undefined value.
Appends a text segment to the section.
Spaces before and after newlines are removed. If the text segment is empty (i.e. consists of just whitespaces), it is not appended.
50 51 52 53 54 |
# File 'lib/mdn_query/section.rb', line 50 def append_text(text) trimmed = text.gsub(/\n[[:blank:]]+|[[:blank:]]+\n/, "\n") escaped = (trimmed) @text << escaped unless text_empty?(escaped) end |
#create_child(name) ⇒ MdnQuery::Section
Creates a new child section.
37 38 39 40 41 |
# File 'lib/mdn_query/section.rb', line 37 def create_child(name) child = MdnQuery::Section.new(name, parent: self, level: @level + 1) @children << child child end |
#to_s ⇒ String
Returns the string representation of the section.
78 79 80 81 82 83 |
# File 'lib/mdn_query/section.rb', line 78 def to_s str = "#{'#' * level} #{name}\n\n#{join_text}\n\n#{join_children}\n" str.gsub!(/\n+[[:blank:]]+\n+|\n{3,}/, "\n\n") str.strip! str end |