# frozen_string_literal: true # lib/todo_marker.rb require 'todo_marker/version' require 'find' module TodoMarker TODO_REGEX = /# !!TODO!!(?: style: (\w+))? "(.*?)"/ STYLE_REGEX = /`style: (\w+?)` (.*?)`/ STYLES = { 'title' => '## %s', 'title1' => '# %s', 'sublist' => ' - %s', 'super-text' => '%s', 'sub-text' => '%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 message = parse_styles(message) todos << { style:, message:, file: path, line: index + 1 } end end create_todo_file(directory, todos) end def self.parse_styles(message) while message.match?(STYLE_REGEX) message.gsub!(STYLE_REGEX) do style, text = Regexp.last_match.captures STYLES[style] % text end end message end def self.create_todo_file(directory, todos) File.open(File.join(directory, 'TODO.md'), 'w') do |file| file.puts "# TODO List\n\n" previous_style = nil 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] != 'sublist' && todo[:style].nil? previous_style = todo[:style] 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