From 03f55753639087734ed97daa560c3cf1f7bc1aaf Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Fri, 22 Jan 2016 22:09:20 -0200 Subject: [PATCH 01/11] Add syntax highlight for overdue dates --- syntax/todo.vim | 62 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/syntax/todo.vim b/syntax/todo.vim index eabcd9f..eed08c8 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -9,33 +9,34 @@ if exists("b:current_syntax") finish endif -syntax match TodoDone '^[xX]\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityA '^([aA])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityB '^([bB])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityC '^([cC])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityD '^([dD])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityE '^([eE])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityF '^([fF])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityG '^([gG])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityH '^([hH])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityI '^([iI])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityJ '^([jJ])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityK '^([kK])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityL '^([lL])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityM '^([mM])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityN '^([nN])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityO '^([oO])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityP '^([pP])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityQ '^([qQ])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityR '^([rR])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityS '^([sS])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityT '^([tT])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityU '^([uU])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityV '^([vV])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityW '^([wW])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityX '^([xX])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityY '^([yY])\s.\+$' contains=TodoDate,TodoProject,TodoContext -syntax match TodoPriorityZ '^([zZ])\s.\+$' contains=TodoDate,TodoProject,TodoContext +syntax match TodoDone '^[xX]\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityA '^([aA])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityB '^([bB])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityC '^([cC])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityD '^([dD])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityE '^([eE])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityF '^([fF])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityG '^([gG])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityH '^([hH])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityI '^([iI])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityJ '^([jJ])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityK '^([kK])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityL '^([lL])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityM '^([mM])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityN '^([nN])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityO '^([oO])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityP '^([pP])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityQ '^([qQ])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityR '^([rR])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityS '^([sS])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityT '^([tT])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityU '^([uU])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityV '^([vV])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityW '^([wW])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityX '^([xX])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +syntax match TodoPriorityY '^([yY])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate +let due_date = "/^([zZ])\s.\+$/" +execute "syntax match TodoPriorityZ " due_date " contains=TodoDate,TodoProject,TodoContext" syntax match TodoDate '\d\{2,4\}-\d\{2\}-\d\{2\}' contains=NONE syntax match TodoProject '\(^\|\W\)+[^[:blank:]]\+' contains=NONE syntax match TodoContext '\(^\|\W\)@[^[:blank:]]\+' contains=NONE @@ -49,4 +50,11 @@ highlight default link TodoDate PreProc highlight default link TodoProject Special highlight default link TodoContext Special +if has('python') + let b:curdir = expand(':p:h') + execute "pyfile " . b:curdir . "/../python/todo.py" +else + echom "Your version of vim has no python support. Overdue dates won't be highlighted" +endif + let b:current_syntax = "todo" From 6e1bcc0e1a6ced10ae7732a5bae5ceca2d5afa98 Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Sun, 24 Jan 2016 16:57:01 -0200 Subject: [PATCH 02/11] Overdue date syntax highlight --- .gitmodules | 3 +++ python/dateregex | 1 + python/todo.py | 25 +++++++++++++++++++++++++ syntax/todo.vim | 1 + 4 files changed, 30 insertions(+) create mode 100644 .gitmodules create mode 160000 python/dateregex create mode 100644 python/todo.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..448d7bf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "python/dateregex"] + path = python/dateregex + url = git@github.com:victal/dateregex.git diff --git a/python/dateregex b/python/dateregex new file mode 160000 index 0000000..7db2e6c --- /dev/null +++ b/python/dateregex @@ -0,0 +1 @@ +Subproject commit 7db2e6ca018b579e5964566d0aa6dad4f97cea9d diff --git a/python/todo.py b/python/todo.py new file mode 100644 index 0000000..c6251b2 --- /dev/null +++ b/python/todo.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +import vim +import os +import sys +from datetime import date + +dateregex_dir = os.path.join(vim.eval('s:script_dir'), 'dateregex') +if os.path.isdir(dateregex_dir): + sys.path.insert(0, dateregex_dir) + +def add_due_date_syntax_highlight(): + try: + from dateregex import regex_date_before + except ImportError: + print("dateregex module not found. Overdue dates won't be highlighted") + return + + regex = regex_date_before(date.today()) + regex = r'(^|<)due:%s(>|$)' % regex + + vim.command("syntax match OverDueDate '\\v%s'" % regex) + vim.command("highlight default link OverDueDate Error") + +add_due_date_syntax_highlight() diff --git a/syntax/todo.vim b/syntax/todo.vim index eed08c8..4b48bc2 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -52,6 +52,7 @@ highlight default link TodoContext Special if has('python') let b:curdir = expand(':p:h') + let s:script_dir = b:curdir . "/../python/" execute "pyfile " . b:curdir . "/../python/todo.py" else echom "Your version of vim has no python support. Overdue dates won't be highlighted" From 7232106f45ba0442e5d44877f8125f96415a00c4 Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Sun, 24 Jan 2016 17:22:24 -0200 Subject: [PATCH 03/11] Move python scripts to syntax/ --- .gitmodules | 4 ++-- python/dateregex | 1 - python/todo.py | 25 ------------------------- syntax/todo.vim | 4 ++-- 4 files changed, 4 insertions(+), 30 deletions(-) delete mode 160000 python/dateregex delete mode 100644 python/todo.py diff --git a/.gitmodules b/.gitmodules index 448d7bf..f8d1960 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "python/dateregex"] - path = python/dateregex - url = git@github.com:victal/dateregex.git + path = syntax/python/dateregex + url = https://github.com/victal/dateregex.git diff --git a/python/dateregex b/python/dateregex deleted file mode 160000 index 7db2e6c..0000000 --- a/python/dateregex +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7db2e6ca018b579e5964566d0aa6dad4f97cea9d diff --git a/python/todo.py b/python/todo.py deleted file mode 100644 index c6251b2..0000000 --- a/python/todo.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env python2 -# -*- coding: utf-8 -*- -import vim -import os -import sys -from datetime import date - -dateregex_dir = os.path.join(vim.eval('s:script_dir'), 'dateregex') -if os.path.isdir(dateregex_dir): - sys.path.insert(0, dateregex_dir) - -def add_due_date_syntax_highlight(): - try: - from dateregex import regex_date_before - except ImportError: - print("dateregex module not found. Overdue dates won't be highlighted") - return - - regex = regex_date_before(date.today()) - regex = r'(^|<)due:%s(>|$)' % regex - - vim.command("syntax match OverDueDate '\\v%s'" % regex) - vim.command("highlight default link OverDueDate Error") - -add_due_date_syntax_highlight() diff --git a/syntax/todo.vim b/syntax/todo.vim index 4b48bc2..2d9e6b8 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -52,8 +52,8 @@ highlight default link TodoContext Special if has('python') let b:curdir = expand(':p:h') - let s:script_dir = b:curdir . "/../python/" - execute "pyfile " . b:curdir . "/../python/todo.py" + let s:script_dir = b:curdir . "/python/" + execute "pyfile " . s:script_dir. "todo.py" else echom "Your version of vim has no python support. Overdue dates won't be highlighted" endif From b87c7dd98deebecbadc304c4e72570773a66e8ac Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Sun, 24 Jan 2016 17:23:50 -0200 Subject: [PATCH 04/11] Correct .gitmodules file --- .gitmodules | 2 +- syntax/python/dateregex | 1 + syntax/python/todo.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 160000 syntax/python/dateregex create mode 100644 syntax/python/todo.py diff --git a/.gitmodules b/.gitmodules index f8d1960..4ba8be8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "python/dateregex"] +[submodule "syntax/python/dateregex"] path = syntax/python/dateregex url = https://github.com/victal/dateregex.git diff --git a/syntax/python/dateregex b/syntax/python/dateregex new file mode 160000 index 0000000..7db2e6c --- /dev/null +++ b/syntax/python/dateregex @@ -0,0 +1 @@ +Subproject commit 7db2e6ca018b579e5964566d0aa6dad4f97cea9d diff --git a/syntax/python/todo.py b/syntax/python/todo.py new file mode 100644 index 0000000..c6251b2 --- /dev/null +++ b/syntax/python/todo.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +import vim +import os +import sys +from datetime import date + +dateregex_dir = os.path.join(vim.eval('s:script_dir'), 'dateregex') +if os.path.isdir(dateregex_dir): + sys.path.insert(0, dateregex_dir) + +def add_due_date_syntax_highlight(): + try: + from dateregex import regex_date_before + except ImportError: + print("dateregex module not found. Overdue dates won't be highlighted") + return + + regex = regex_date_before(date.today()) + regex = r'(^|<)due:%s(>|$)' % regex + + vim.command("syntax match OverDueDate '\\v%s'" % regex) + vim.command("highlight default link OverDueDate Error") + +add_due_date_syntax_highlight() From fc0b4c32728d6d89f4081feb3f93d348b0805e46 Mon Sep 17 00:00:00 2001 From: victal Date: Mon, 25 Jan 2016 10:02:02 -0200 Subject: [PATCH 05/11] Update README with +python warning --- README.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.markdown b/README.markdown index 7220c9f..c924ce4 100644 --- a/README.markdown +++ b/README.markdown @@ -2,11 +2,17 @@ As it was suggested on issue [#28](https://github.com/freitass/todo.txt-vim/issues/28) (and as recommended by vim's documentation), all mappings were changed to use `` instead of ``. If you don't have `maplocalleader` set on your environment then yours is probably `\`. For more information on that regard, please take a look at `:h `. +### (Jan 2016) Note: Overdue date highlight and Python Optional dependency + +A new feature was added to highlight dates in overdue tasks as an Error (as suggested on issue [#44][https://github.com/freitass/todo.txt-vim/issues/44]). It depends on a Python library, however, and as such will only be able to work if your version of Vim was compiled with the `+python` option (as most common versions do). + +If your Vim installation does **not** have Python support, this plugin **will work just fine** but this feature will be disabled ### Quick install git clone git://github.com/freitass/todo.txt-vim.git cd todo.txt-vim + git submodule update --init cp -R * ~/.vim @@ -17,6 +23,7 @@ Sorting tasks: `s+` Sort the file on +Projects `s@` Sort the file on @Contexts `sd` Sort the file on dates +`sdd` Sort the file on due dates Edit priority: `j` Decrease the priority of the current line From 03501dfe425c9e71189c882c80dc53284fc8afcb Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Tue, 26 Jan 2016 09:32:10 -0200 Subject: [PATCH 06/11] Remove test on syntax file --- syntax/todo.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax/todo.vim b/syntax/todo.vim index 2d9e6b8..e413c16 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -35,8 +35,8 @@ syntax match TodoPriorityV '^([vV])\s.\+$' contains=TodoDate,Todo syntax match TodoPriorityW '^([wW])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate syntax match TodoPriorityX '^([xX])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate syntax match TodoPriorityY '^([yY])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate -let due_date = "/^([zZ])\s.\+$/" -execute "syntax match TodoPriorityZ " due_date " contains=TodoDate,TodoProject,TodoContext" +syntax match TodoPriorityZ '^([zZ])\s.\+$' contains=TodoDate,TodoProject,TodoContext + syntax match TodoDate '\d\{2,4\}-\d\{2\}-\d\{2\}' contains=NONE syntax match TodoProject '\(^\|\W\)+[^[:blank:]]\+' contains=NONE syntax match TodoContext '\(^\|\W\)@[^[:blank:]]\+' contains=NONE From 29a6ea4b1d5c6731b470a0d61f5460bdfceb0327 Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Tue, 26 Jan 2016 10:29:37 -0200 Subject: [PATCH 07/11] Add description and license info --- syntax/python/todo.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/syntax/python/todo.py b/syntax/python/todo.py index c6251b2..36d0d9d 100644 --- a/syntax/python/todo.py +++ b/syntax/python/todo.py @@ -1,5 +1,11 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +# File: todo.py +# Description: Todo.txt overdue date syntax script +# License: Vim license +# Website: http://github.com/freitass/todo.txt-vim +# Version: 0.1 + import vim import os import sys From 87a6c371dd7e9e31af0464ac9b65180ebeb4277d Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Tue, 26 Jan 2016 20:56:48 -0200 Subject: [PATCH 08/11] Add Overdue Date group to (Z) tasks syntax match --- syntax/todo.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntax/todo.vim b/syntax/todo.vim index e413c16..f7c5105 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -35,7 +35,7 @@ syntax match TodoPriorityV '^([vV])\s.\+$' contains=TodoDate,Todo syntax match TodoPriorityW '^([wW])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate syntax match TodoPriorityX '^([xX])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate syntax match TodoPriorityY '^([yY])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate -syntax match TodoPriorityZ '^([zZ])\s.\+$' contains=TodoDate,TodoProject,TodoContext +syntax match TodoPriorityZ '^([zZ])\s.\+$' contains=TodoDate,TodoProject,TodoContext,OverDueDate syntax match TodoDate '\d\{2,4\}-\d\{2\}-\d\{2\}' contains=NONE syntax match TodoProject '\(^\|\W\)+[^[:blank:]]\+' contains=NONE From eb7f7cb8f4e98da12f4442ad8d2a48536cee20a0 Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Tue, 26 Jan 2016 21:02:22 -0200 Subject: [PATCH 09/11] Add plugin manager notice --- README.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index c924ce4..c6b4e82 100644 --- a/README.markdown +++ b/README.markdown @@ -6,7 +6,9 @@ As it was suggested on issue [#28](https://github.com/freitass/todo.txt-vim/issu A new feature was added to highlight dates in overdue tasks as an Error (as suggested on issue [#44][https://github.com/freitass/todo.txt-vim/issues/44]). It depends on a Python library, however, and as such will only be able to work if your version of Vim was compiled with the `+python` option (as most common versions do). -If your Vim installation does **not** have Python support, this plugin **will work just fine** but this feature will be disabled +If your Vim installation does **not** have Python support, this plugin **will work just fine** but this feature will be disabled. + +If you use [Vundle][https://github.com/VundleVim/Vundle.vim], [Pathogen][https://github.com/tpope/vim-pathogen] or another plugin manager, you might need to run `git submodule update --init` manualy inside the plugin directory after installation for this feature to work. ### Quick install From 2535cf2ece6929b9dc858b18829e6dddd6b8781e Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Wed, 27 Jan 2016 10:53:13 -0200 Subject: [PATCH 10/11] Remove dateregex submodule --- .gitmodules | 3 --- syntax/python/dateregex | 1 - 2 files changed, 4 deletions(-) delete mode 160000 syntax/python/dateregex diff --git a/.gitmodules b/.gitmodules index 4ba8be8..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "syntax/python/dateregex"] - path = syntax/python/dateregex - url = https://github.com/victal/dateregex.git diff --git a/syntax/python/dateregex b/syntax/python/dateregex deleted file mode 160000 index 7db2e6c..0000000 --- a/syntax/python/dateregex +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7db2e6ca018b579e5964566d0aa6dad4f97cea9d From d769f1271013895bc144caa47a3fe0b18ee4e5bb Mon Sep 17 00:00:00 2001 From: Guilherme Victal Date: Wed, 27 Jan 2016 11:01:57 -0200 Subject: [PATCH 11/11] Embed dateregex instead of using a submodule --- README.markdown | 3 - syntax/python/dateregex/dateregex/__init__.py | 11 +++ syntax/python/dateregex/dateregex/after.py | 95 +++++++++++++++++++ syntax/python/dateregex/dateregex/before.py | 79 +++++++++++++++ 4 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 syntax/python/dateregex/dateregex/__init__.py create mode 100644 syntax/python/dateregex/dateregex/after.py create mode 100644 syntax/python/dateregex/dateregex/before.py diff --git a/README.markdown b/README.markdown index c6b4e82..d677ce5 100644 --- a/README.markdown +++ b/README.markdown @@ -8,13 +8,10 @@ A new feature was added to highlight dates in overdue tasks as an Error (as sugg If your Vim installation does **not** have Python support, this plugin **will work just fine** but this feature will be disabled. -If you use [Vundle][https://github.com/VundleVim/Vundle.vim], [Pathogen][https://github.com/tpope/vim-pathogen] or another plugin manager, you might need to run `git submodule update --init` manualy inside the plugin directory after installation for this feature to work. - ### Quick install git clone git://github.com/freitass/todo.txt-vim.git cd todo.txt-vim - git submodule update --init cp -R * ~/.vim diff --git a/syntax/python/dateregex/dateregex/__init__.py b/syntax/python/dateregex/dateregex/__init__.py new file mode 100644 index 0000000..cfbc141 --- /dev/null +++ b/syntax/python/dateregex/dateregex/__init__.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# File: __init__.py +# Author: Guilherme Victal +# Description: Dateregex library entry point +# License: Vim license +# Website: http://github.com/freitass/todo.txt-vim +# Version: 0.1 + +from after import regex_date_after +from before import regex_date_before diff --git a/syntax/python/dateregex/dateregex/after.py b/syntax/python/dateregex/dateregex/after.py new file mode 100644 index 0000000..8a13574 --- /dev/null +++ b/syntax/python/dateregex/dateregex/after.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# File: after.py +# Author: Guilherme Victal +# Description: Generates regexes after a certain date +# License: Vim license +# Website: http://github.com/freitass/todo.txt-vim +# Version: 0.1 + +from datetime import date, timedelta, MAXYEAR + + +def _year_regex_after(year): + if int(year) > MAXYEAR: + return None + + year_regex = r'(\d+\d{%s}' % len(year) + for idx, digit in enumerate(year): + if digit != '9': + regex = '|' + year[0:idx] + regex += '9' if digit == '8' else '[%s-9]' % str(int(digit) + 1) + if idx < len(year) - 1: + regex += '\d{%s}' % (len(year) - (idx + 1)) + year_regex += regex + + year_regex += ')' + return '-'.join((year_regex, r'\d{2}', r'\d{2}')) + + +def _month_regex_after(year, month): + if month == '12': + return None + + digit1, digit2 = month + if digit1 == '1': + month_regex = r'12' if month == '11' else r'1[12]' + else: + month_regex = r'1[0-2]' + if digit2 != '9': + if digit2 == '8': + month_regex = r'(' + month_regex + r'|09)' + else: + month_regex = r'(' + month_regex + r'|0[%s-9])' + month_regex = month_regex % str(int(digit2) + 1) + return '-'.join((year, month_regex, r'\d{2}')) + +def _day_regex_after(year, month, day): + last_month_day = str((date(int(year), (int(month) + 1) % 12, 1) + - date.resolution).day) + if day == last_month_day: + return None + day_regex = r'(' + digit1, digit2 = day + last_digit1, last_digit2 = last_month_day + if digit1 == last_digit1: + day_regex = last_month_day if int(digit2) == int(last_digit2) - 1 else last_digit1 + r'[%s-%s]' % (str(int(digit2) + 1), last_digit2) + else: + day_regex = r'(' + day_regex += last_digit1 if int(digit1) == int(last_digit1) - 1 else r'[%s-%s]' % (str(int(digit1) + 1), last_digit1) + day_regex +=r'\d' + if digit2 < '9': + day_regex += '|' + digit1 + day_regex += '9' if digit2 == '8' else r'[%s-9]' % str(int(digit2) + 1) + + day_regex += ')' + return '-'.join((year, month, day_regex)) + + +def regex_date_after(given_date): + year, month, day = given_date.isoformat().split('-') + + year_regex = _year_regex_after(year) + month_regex = _month_regex_after(year, month) + day_regex = _day_regex_after(year, month, day) + + date_regex = '(' + year_regex if year_regex else '(' + date_regex += ('|' + month_regex) if month_regex else '' + date_regex += ('|' + day_regex) if day_regex else '' + date_regex += ')' + return date_regex + + +def __main(): + import re + date_regex = regex_date_after(date(1999,12,31)) + print(date_regex) + pattern = re.compile(date_regex) + + + d = date.today() + date.resolution + assert pattern.match(date.strftime(d, '%Y-%m-%d')) is not None + print(date.strftime(d, '%Y-%m-%d') + ' is okay') + d += date.resolution + +if __name__ == '__main__': + __main() diff --git a/syntax/python/dateregex/dateregex/before.py b/syntax/python/dateregex/dateregex/before.py new file mode 100644 index 0000000..8a3402e --- /dev/null +++ b/syntax/python/dateregex/dateregex/before.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# File: before.py +# Author: Guilherme Victal +# Description: Generates regexes before a certain date +# License: Vim license +# Website: http://github.com/freitass/todo.txt-vim +# Version: 0.1 + +from datetime import date, timedelta, MINYEAR + +def _year_regex_before(year): + if int(year) <= MINYEAR: + return None + year_regex = r'(' + year_regex += r'\d{1,%s}' % str(len(year) - 1) if len(year) > 1 else '' + for idx, digit in enumerate(year): + if digit != '0': + regex = '|' + year[0:idx] + regex += '0' if digit == '1' else '[0-%s]' % str(int(digit) - 1) + if idx < len(year) - 1: + regex += '\d{%s}' % (len(year) - (idx + 1)) + year_regex += regex + + year_regex += ')' + return '-'.join((year_regex, r'\d{2}', r'\d{2}')) + +def _month_regex_before(year, month): + if month == '01': + return None + + digit1, digit2 = month + if digit1 == '0': + month_regex = '01' if month == '02' else r'0[1-%s]' % str(int(digit2) - 1) + elif month == '10': + month_regex = r'0\d' + elif month == '11': + month_regex = r'(0\d|10)' + else: + month_regex = r'(0\d|1[01])' + + return '-'.join((year, month_regex, r'\d{2}')) + +def _day_regex_before(year, month, day): + if day == '01': + return None + last_month_day = str((date(int(year), (int(month) + 1) % 12, 1) + - date.resolution).day) + last_digit1, last_digit2 = last_month_day + + digit1, digit2 = day + if digit1 == '0': + day_regex = '01' if day == '02' else r'0[1-%s]' % str(int(digit2) - 1) + else: + day_regex = r'(' + day_regex += '0' if digit1 == '1' else r'[0-%s]' % str(int(digit1) - 1) + day_regex += r'\d' + if digit2 != '0': + day_regex += '|' + day_regex += digit1 + day_regex += '0' if digit2 == '1' else r'[0-%s]' % str(int(digit2) - 1) + day_regex += ')' + + return '-'.join((year, month, day_regex)) + + + + +def regex_date_before(given_date): + year, month, day = given_date.isoformat().split('-') + + year_regex = _year_regex_before(year) + month_regex = _month_regex_before(year, month) + day_regex = _day_regex_before(year, month, day) + + date_regex = '(' + year_regex if year_regex else '(' + date_regex += ('|' + month_regex) if month_regex else '' + date_regex += ('|' + day_regex) if day_regex else '' + date_regex += ')' + return date_regex