Class: Checklister::Parser
- Inherits:
-
Object
- Object
- Checklister::Parser
- Defined in:
- lib/checklister/parser.rb
Overview
Parse a markdown file and return a hash of params.
Instance Method Summary collapse
-
#initialize(file_path, title = nil) ⇒ Parser
constructor
A new instance of Parser.
-
#to_params ⇒ Hash
A hash of params.
Constructor Details
#initialize(file_path, title = nil) ⇒ Parser
Returns a new instance of Parser.
12 13 14 15 |
# File 'lib/checklister/parser.rb', line 12 def initialize(file_path, title = nil) @file_content = File.open(file_path).read @title = title end |
Instance Method Details
#to_params ⇒ Hash
Returns a hash of params.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/checklister/parser.rb', line 18 def to_params checklist = [] @file_content.each_line do |line| # Extract a title, when we find the first <h1> header if @title.nil? && line.start_with?("# ") @title = line.sub("# ", "").sub("\n", "") elsif @title && line.start_with?("# ") next else # Then, keep the text intact until the end of the file checklist << line end end # Default title, if no <H1> header found @title = "Checklist" if @title.nil? # Return the parsed text as an object { title: @title, body: checklist.join } end |