Skip to content

Instantly share code, notes, and snippets.

@huseyin
Last active January 9, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save huseyin/448eec0ea42064ec7875e80ab9698aae to your computer and use it in GitHub Desktop.
Save huseyin/448eec0ea42064ec7875e80ab9698aae to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'colorize'
module ParseUtils
TASK_ELEMENT_REGEX = /(?m:^- \[( |X|x)\] (.*)$)/
BOLD_REGEX = /[*][*]([^*]+)[*][*]/
ITALIC_REGEX = /\*([^\*]+)\*/
FENCED_REGEX = /`([\S]+)`/
module_function
def parse(src)
m = src.match TASK_ELEMENT_REGEX
text = src
checked = false
if m
text = m[2]
checked = %w(x X).include?(m[1]) ? true : false
end
yield text, checked if block_given?
end
def render(src)
src.gsub!(BOLD_REGEX, %(\\1).bold) if src =~ BOLD_REGEX
src.gsub!(FENCED_REGEX, %(\\1).light_black) if src =~ FENCED_REGEX
src.gsub!(ITALIC_REGEX, %(\\1).italic) if src =~ ITALIC_REGEX
src
end
end
# require 'test/unit'
#
# class TestParseUtils < Test::Unit::TestCase
# @@checklist_items = {
# '-Foo bar baz hede' => ['-Foo bar baz hede', false],
# '- Foo bar baz hodo' => ['- Foo bar baz hodo', false],
# '-[] Foo bar baz' => ['-[] Foo bar baz', false],
# '-[ ] Foo bar baz' => ['-[ ] Foo bar baz', false],
# '- [] Foo bar baz' => ['- [] Foo bar baz', false],
# '- [ ] Foo bar baz' => ['Foo bar baz', false],
# '- [_] Foo bar baz' => ['- [_] Foo bar baz', false],
# '- [x] Foo bar baz' => ['Foo bar baz', true],
# '- [X] Foo bar baz' => ['Foo bar baz', true],
# }
# @@formatting_items = {
# '- [ ] `Foo`' => "#{'Foo'.light_black}",
# '- [ ] ``Foo``' => "#{'`Foo`'.light_black}",
# '- [ ] *Foo*' => "#{'Foo'.italic}",
# '- [ ] **Foo**' => "#{'Foo'.bold}",
# }
#
# def test_parse
# @@checklist_items.each do |item, args|
# ParseUtils.parse(item) do |text, checked|
# assert_equal text, args.first
# assert_equal checked, args.last
# end
# end
# end
#
# def test_render
# @@formatting_items.each do |item, formatted|
# ParseUtils.parse(item) do |text|
# assert_equal ParseUtils.render(text), formatted
# end
# end
# end
# end
def process_line(line)
ParseUtils.parse(line) do |text, checked|
warn '✓ '.green + ParseUtils.render(text) if checked
warn '✕ '.red + ParseUtils.render(text) unless checked
end if line =~ ParseUtils::TASK_ELEMENT_REGEX
end
def main
fp = File.join File.expand_path('.'), 'TODO.md'
abort 'Bu dizinde `TODO.md` dosyası yok!' unless File.exist?(fp)
File.readlines(fp).each { |line| process_line(line) }
end
main if __FILE__ == $PROGRAM_NAME
  • a task list item
  • list syntax required
  • normal formatting, @mentions, #1234 refs
  • incomplete
  • completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment