Class: StandupMD::Parsers::Markdown

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

Overview

Parser and renderer for the markdown standup format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = StandupMD.config.file) ⇒ Markdown

Constructs an instance of StandupMD::Parsers::Markdown.

Parameters:



27
28
29
# File 'lib/standup_md/parsers/markdown.rb', line 27

def initialize(config = StandupMD.config.file)
  @config = config
end

Instance Attribute Details

#configStandupMD::Config::File (readonly)

Access to file configuration.



21
22
23
# File 'lib/standup_md/parsers/markdown.rb', line 21

def config
  @config
end

Instance Method Details

#read(file_name) ⇒ StandupMD::EntryList

Reads entries from a markdown standup file.

Parameters:

  • file_name (String)

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/standup_md/parsers/markdown.rb', line 37

def read(file_name)
  entry_list = EntryList.new
  record = nil
  section = nil

  ::File.foreach(file_name) 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 "File malformation: #{e}"
end

#task_line(task) ⇒ String

Renders a task as a markdown list item.

Parameters:

Returns:

  • (String)


96
97
98
# File 'lib/standup_md/parsers/markdown.rb', line 96

def task_line(task)
  build_task(task).to_markdown
end

#write(file_name, entries, start_date:, end_date:) ⇒ Boolean

Writes entries to a markdown standup file.

Parameters:

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/standup_md/parsers/markdown.rb', line 74

def write(file_name, entries, start_date:, end_date:)
  ::File.open(file_name, "w") do |f|
    entries.filter(start_date, end_date).sort_reverse.each do |entry|
      f.puts Title.new(entry.date).to_markdown
      config.sub_header_order.each do |attr|
        section = Section.new(attr, entry.public_send("#{attr}_tasks"))
        next if section.empty?

        f.puts section.to_markdown
      end
      f.puts
    end
  end
  true
end