diff --git a/bin/mv-to-docs b/bin/mv-to-docs index afeb666..52f441a 100755 --- a/bin/mv-to-docs +++ b/bin/mv-to-docs @@ -11,20 +11,25 @@ import shutil import sys from datetime import date +from unicodedata import normalize -_slugify_strip_re = re.compile(r'[^\w\s-]') -_slugify_hyphenate_re = re.compile(r'[-\s]+') -def slugify(value): - ''' - Normalizes string, converts to lowercase, removes non-alpha characters, - and converts spaces to hyphens. +_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') - From Django's "django/template/defaultfilters.py". - ''' - import unicodedata - value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') - value = unicode(_slugify_strip_re.sub('', value).strip().lower()) - return _slugify_hyphenate_re.sub('-', value) +def slugify(text, delim='-'): + """ + Generate an ASCII-only slug. + """ + + result = [] + for word in _punctuation_re.split(text.lower()): + word = normalize('NFKD', word) \ + .encode('ascii', 'ignore') \ + .decode('utf-8') + + if word: + result.append(word) + + return delim.join(result) def usage(): return 'Usage: mv-to-docs [SRC_DOC] [CATEGORY] [MESSAGE]' @@ -43,18 +48,19 @@ def main(file_name='', dir_name='', message = ''): print('could not find path → creating directory: {}'.format(doc_path)) os.mkdir(doc_path) - path_with_year = "%s/%s".format((doc_path, file_date.strftime("%Y"))) + year = file_date.strftime("%Y") + path_with_year = '{}/{}'.format(doc_path, year) if not os.path.exists(path_with_year): os.mkdir(path_with_year) pass - new_file_path = '%s/%s_%s.%s'.format(path_with_year, - file_date.strftime("%Y-%m-%d"), + new_file_path = '{}/{}_{}.{}'.format(path_with_year, + file_date, message, file_type) if not os.path.isfile(new_file_path): - print("Moving file '%s' to '%s'" % (file_name, new_file_path)) + print("Moving file '{}' to '{}'".format(file_name, new_file_path)) shutil.copyfile(file_name, new_file_path) os.unlink(file_name) else: