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:



25
26
27
# File 'lib/standup_md/parsers/markdown.rb', line 25

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

Instance Attribute Details

#configStandupMD::Config::File (readonly)

Access to file configuration.



19
20
21
# File 'lib/standup_md/parsers/markdown.rb', line 19

def config
  @config
end

Instance Method Details

#read(file_name) ⇒ StandupMD::EntryList

Reads entries from a markdown standup file.

Parameters:

  • file_name (String)

Returns:



35
36
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
# File 'lib/standup_md/parsers/markdown.rb', line 35

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)


94
95
96
# File 'lib/standup_md/parsers/markdown.rb', line 94

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)


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

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