Todo_Marker/lib/todo_marker.rb
2024-06-22 11:01:46 -06:00

60 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# lib/todo_marker.rb
require 'todo_marker/version'
require 'find'
module TodoMarker
TODO_REGEX = /# !!TODO!!(?: style: (\w+))? "(.*?)"/
STYLES = {
'title' => '## %s',
'title1' => '# %s',
'sublist' => ' - %s'
}.freeze
def self.generate_todo_md(directory)
todos = []
Find.find(directory) do |path|
next unless path.end_with?('.rb')
File.readlines(path).each_with_index do |line, index|
next unless line.match(TODO_REGEX)
style, message = line.match(TODO_REGEX).captures
todos << {
style:,
message:,
file: path,
line: index + 1
}
end
end
create_todo_file(directory, todos)
end
def self.create_todo_file(directory, todos)
File.open(File.join(directory, 'TODO.md'), 'w') do |file|
file.puts "# TODO List\n\n"
todos.each do |todo|
formatted_message = format_message(todo[:style], todo[:message])
file.puts formatted_message
file.puts " (#{todo[:file]}:#{todo[:line]})" if todo[:style].nil?
end
end
end
def self.format_message(style, message)
if style
if style == 'sublist'
" - #{message}"
else
STYLES[style] % message
end
else
"- #{message}"
end
end
end