From 8fde981b1d827f1bd0bcb9d5605506ff7fc42f68 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 19 Dec 2011 19:41:09 -0800 Subject: [PATCH] add asciify function --- beets/library.py | 6 ++++++ docs/reference/pathformat.rst | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/beets/library.py b/beets/library.py index b75870e09..dd0d83b94 100644 --- a/beets/library.py +++ b/beets/library.py @@ -17,6 +17,7 @@ import os import re import sys import logging +from unidecode import unidecode from beets.mediafile import MediaFile from beets import plugins from beets import util @@ -1301,6 +1302,10 @@ def _tmpl_if(condition, trueval, falseval=u''): return trueval else: return falseval +def _tmpl_asciify(s): + """Translate non-ASCII characters to their ASCII equivalents. + """ + return unidecode(s) TEMPLATE_FUNCTIONS = { 'lower': _tmpl_lower, @@ -1309,4 +1314,5 @@ TEMPLATE_FUNCTIONS = { 'left': _tmpl_left, 'right': _tmpl_right, 'if': _tmpl_if, + 'asciify': _tmpl_asciify, } diff --git a/docs/reference/pathformat.rst b/docs/reference/pathformat.rst index 0fd05f127..062411c12 100644 --- a/docs/reference/pathformat.rst +++ b/docs/reference/pathformat.rst @@ -65,6 +65,11 @@ These functions are built in to beets: ``condition`` is nonempty (or nonzero, if it's a number), then returns the second argument. Otherwise, returns the third argument if specified (or nothing if ``falsetext`` is left off). +* ``%asciify{text}``: Convert non-ASCII characters to their ASCII equivalents. + For example, "café" becomes "cafe". Uses the mapping provided by the + `unidecode module`_. + +.. _unidecode module: http://pypi.python.org/pypi/Unidecode Plugins can extend beets with more template functions (see :ref:`writing-plugins`).