Class: SheepAChangelog::Node
- Inherits:
-
Object
- Object
- SheepAChangelog::Node
- Defined in:
- lib/sheep-a-changelog/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#anchors ⇒ Object
readonly
Returns the value of attribute anchors.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_lines ⇒ Object
Get lines from 1.
- #all_lines_wo_heading ⇒ Object
-
#build_nodes(lines, next_level) ⇒ Object
Contruct nodes for current node from all lines.
-
#build_tree ⇒ Object
Serialize tree to the hashes.
-
#format_heading ⇒ Object
Format your node’s title.
- #h_regexp(level) ⇒ Object
- #hashes(n = @level) ⇒ Object
-
#initialize(lines, title, level) ⇒ Node
constructor
Create node hierarchy from keep-a-changeloh markdown lines.
-
#to_s ⇒ Object
Print the markdown output.
Constructor Details
#initialize(lines, title, level) ⇒ Node
Create node hierarchy from keep-a-changeloh markdown lines
54 55 56 57 58 59 60 |
# File 'lib/sheep-a-changelog/node.rb', line 54 def initialize(lines, title, level) content_lines, anchors = Node.pick_lines(lines) @title = title @anchors = anchors @nodes = build_nodes(content_lines, level + 1) @level = level end |
Instance Attribute Details
#anchors ⇒ Object (readonly)
Returns the value of attribute anchors.
3 4 5 |
# File 'lib/sheep-a-changelog/node.rb', line 3 def anchors @anchors end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
3 4 5 |
# File 'lib/sheep-a-changelog/node.rb', line 3 def lines @lines end |
#nodes ⇒ Object
Returns the value of attribute nodes.
4 5 6 |
# File 'lib/sheep-a-changelog/node.rb', line 4 def nodes @nodes end |
#title ⇒ Object
Returns the value of attribute title.
4 5 6 |
# File 'lib/sheep-a-changelog/node.rb', line 4 def title @title end |
Class Method Details
.parse(string) ⇒ Object
5 6 7 |
# File 'lib/sheep-a-changelog/node.rb', line 5 def self.parse(string) new(string.split("\n")) end |
.pick_lines(lines) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sheep-a-changelog/node.rb', line 37 def self.pick_lines(lines) # add matches for links groups_lines = lines.inject([]) do |acc, line| groups = line.match(/^\[(.*)\]\s*:\s*(\S+)\s*$/).to_a acc + [[groups, line]] end # if no matches, it is content lines content_lines = groups_lines .select { |x| x.first.empty? }.map { |_, l| l } # if matches, it is link anchors = groups_lines .reject { |x| x.first.empty? } .map { |groups, _| { v: groups[1], url: groups[2] } } [content_lines, anchors] end |
Instance Method Details
#all_lines ⇒ Object
Get lines from
-
your title
-
your immidiate lines
-
recursively from your direct nodes
71 72 73 74 75 76 |
# File 'lib/sheep-a-changelog/node.rb', line 71 def all_lines res = [] res << format_heading if @title != :empty res += all_lines_wo_heading res end |
#all_lines_wo_heading ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/sheep-a-changelog/node.rb', line 78 def all_lines_wo_heading res = [] res += @lines res += @nodes.map(&:all_lines) res += @anchors.map { |a| "[#{a[:v]}]: #{a[:url]}" } res end |
#build_nodes(lines, next_level) ⇒ Object
Contruct nodes for current node from all lines
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sheep-a-changelog/node.rb', line 18 def build_nodes(lines, next_level) # rubocop:disable Metrics/MethodLength last = nil line_buff = [] (lines + [:last]).each_with_object([]) do |line, nodes| heading = line[h_regexp(next_level), 1] if line.is_a? String if heading || line == :last if last.nil? @lines = line_buff else nodes << Node.new(line_buff, last, next_level) end last = heading line_buff = [] else line_buff << line end end end |
#build_tree ⇒ Object
Serialize tree to the hashes
92 93 94 95 96 97 |
# File 'lib/sheep-a-changelog/node.rb', line 92 def build_tree { title: @title, lines: @lines, anchors: @anchors, nodes: @nodes.map(&:build_tree) } end |
#format_heading ⇒ Object
Format your node’s title
63 64 65 |
# File 'lib/sheep-a-changelog/node.rb', line 63 def format_heading "#{hashes} #{@title}" end |
#h_regexp(level) ⇒ Object
13 14 15 |
# File 'lib/sheep-a-changelog/node.rb', line 13 def h_regexp(level) Regexp.new('^' + hashes(level) + '\s+([\S\s]+)$') end |
#hashes(n = @level) ⇒ Object
9 10 11 |
# File 'lib/sheep-a-changelog/node.rb', line 9 def hashes(n = @level) '#' * n end |
#to_s ⇒ Object
Print the markdown output
87 88 89 |
# File 'lib/sheep-a-changelog/node.rb', line 87 def to_s all_lines.join("\n") end |