Todo_Marker/lib/todo_marker.rb

76 lines
1.8 KiB
Ruby
Raw Normal View History

2024-06-14 22:59:20 -06:00
# frozen_string_literal: true
# lib/todo_marker.rb
2024-06-14 23:00:56 -06:00
require 'todo_marker/version'
require 'find'
2024-06-14 22:59:20 -06:00
module TodoMarker
TODO_REGEX = /# !!TODO!!(?: style: (\w+))? "(.*?)"/
STYLE_REGEX = /`style: (\w+?)` (.*?)`/
STYLES = {
'title' => '## %s',
'title1' => '# %s',
'sublist' => ' - %s',
'super-text' => '<sup>%s</sup>',
'sub-text' => '<sub>%s</sub>'
}.freeze
2024-06-14 22:59:20 -06:00
def self.generate_todo_md(directory)
todos = []
Find.find(directory) do |path|
2024-06-14 23:00:56 -06:00
next unless path.end_with?('.rb')
2024-06-14 22:59:20 -06:00
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)
2024-06-14 22:59:20 -06:00
todos << {
style:,
message:,
2024-06-14 22:59:20 -06:00
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
2024-06-14 22:59:20 -06:00
def self.create_todo_file(directory, todos)
2024-06-14 23:00:56 -06:00
File.open(File.join(directory, 'TODO.md'), 'w') do |file|
2024-06-14 22:59:20 -06:00
file.puts "# TODO List\n\n"
previous_style = nil
2024-06-14 22:59:20 -06:00
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
2024-06-14 22:59:20 -06:00
end
else
"- #{message}"
2024-06-14 22:59:20 -06:00
end
end
end