Class: MarkdownFile

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_usage/markdown.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MarkdownFile

Returns a new instance of MarkdownFile.



4
5
6
7
# File 'lib/dot_usage/markdown.rb', line 4

def initialize(filename)
  @filename = filename
  @contents = File.read filename
end

Instance Method Details

#section(heading) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dot_usage/markdown.rb', line 9

def section(heading)
  after_heading_regexp = /^#{heading}$(?<after>.+)/m
  next_heading_regexp = /(?<section>.+)^#+/m

  after_match_data = after_heading_regexp.match @contents
  return nil unless after_match_data

  after_heading = after_match_data[:after]

  before_match_data = next_heading_regexp.match after_heading
  return nil unless before_match_data

  before_match_data[:section]
end

#snippet(heading) ⇒ Object



24
25
26
27
28
29
# File 'lib/dot_usage/markdown.rb', line 24

def snippet(heading)
  s = section heading
  return nil if s.nil?

  [extract_quoted_snippet(s), extract_indented_snippet(s)].select { |r| ! r.nil? }.first
end