Class: StandupMD::Parsers::Markdown
- Inherits:
-
Object
- Object
- StandupMD::Parsers::Markdown
- Defined in:
- lib/standup_md/parsers/markdown.rb
Overview
Parser and renderer for the markdown standup format.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#config ⇒ StandupMD::Config::File
readonly
Access to file configuration.
Instance Method Summary collapse
-
#initialize(config = StandupMD.config.file) ⇒ Markdown
constructor
Constructs an instance of
StandupMD::Parsers::Markdown. -
#parse(text) ⇒ StandupMD::EntryList
Parses entries from markdown text.
-
#render(entries, start_date:, end_date:) ⇒ String
Renders entries as markdown text.
-
#render_entry(entry) ⇒ String
Renders a single entry as markdown text.
Constructor Details
Instance Attribute Details
#config ⇒ StandupMD::Config::File (readonly)
Access to file configuration.
24 25 26 |
# File 'lib/standup_md/parsers/markdown.rb', line 24 def config @config end |
Instance Method Details
#parse(text) ⇒ StandupMD::EntryList
Parses entries from markdown text.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/standup_md/parsers/markdown.rb', line 40 def parse(text) entry_list = EntryList.new record = nil section = nil text.to_s.each_line do |line| line.chomp! next if line.strip.empty? if header?(line) entry_list << entry(record) if record record = {title: title(line), sections: {}} section = section(:notes) record[:sections][:notes] = section elsif sub_header?(line) section = section(section_type(line)) record[:sections][section.type] = section else section << task(line) end end entry_list << entry(record) if record entry_list.sort rescue => e raise Error, "Markdown malformation: #{e.}" end |
#render(entries, start_date:, end_date:) ⇒ String
Renders entries as markdown text.
76 77 78 79 80 |
# File 'lib/standup_md/parsers/markdown.rb', line 76 def render(entries, start_date:, end_date:) entries.filter(start_date, end_date).sort_reverse.map do |entry| render_entry(entry) end.join end |
#render_entry(entry) ⇒ String
Renders a single entry as markdown text.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/standup_md/parsers/markdown.rb', line 88 def render_entry(entry) lines = [entry_header(entry)] config.sub_header_order.each do |type| section = Section.new(type, entry.public_send("#{type}_tasks")) next if section.empty? lines << section_header(type) section.tasks.each { |task| lines << task_line(task) } end lines << "" lines.join("\n") + "\n" end |