forked from DasIch/brownie
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# coding: utf-8 | ||
""" | ||
brownie.tests.text | ||
~~~~~~~~~~~~~~~~~~ | ||
Tests for :mod:`brownie.tests`. | ||
:copyright: 2010-2011 by Daniel Neuhäuser | ||
:license: BSD, see LICENSE.rst for details | ||
""" | ||
try: | ||
import translitcodec | ||
except ImportError: | ||
translitcodec = None | ||
from attest import Tests, Assert | ||
|
||
from brownie.text import transliterate | ||
|
||
|
||
tests = Tests() | ||
|
||
|
||
@tests.test | ||
def test_transliterate(): | ||
Assert(transliterate(u'äöü', 'one')) == u'aou' | ||
|
||
tests = zip( | ||
[ | ||
(u'©', 'long'), | ||
(u'©', 'short'), | ||
(u'☺', 'one'), | ||
], | ||
[u''] * 3 if translitcodec is None else [u'(c)', u'c', u'?'] | ||
) | ||
for args, result in tests: | ||
Assert(transliterate(*args)) == result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# coding: utf-8 | ||
""" | ||
brownie.text | ||
~~~~~~~~~~~~ | ||
Utilities to deal with text. | ||
.. versionadded:: 0.6 | ||
:copyright: 2010-2011 by Daniel Neuhäuser | ||
:license: BSD, see LICENSE.rst for details | ||
""" | ||
import unicodedata | ||
|
||
try: | ||
import translitcodec | ||
except ImportError: # pragma: no cover | ||
translitcodec = None | ||
|
||
|
||
def transliterate(string, length='long'): | ||
""" | ||
Returns a transliterated version of the given unicode `string`. | ||
By specifying `length` you can specify how many characters are used for a | ||
replacement: | ||
`long` | ||
Use as many characters as needed to make a natural replacement. | ||
`short` | ||
Use as few characters as possible to make a replacement. | ||
`one` | ||
Use only one character to make a replacement. If a character cannot | ||
be transliterated with a single character replace it with `'?'`. | ||
If available translitcodec_ is used, which provides more natural results. | ||
.. _translitcodec: http://pypi.python.org/pypi/translitcodec | ||
""" | ||
if length not in ('long', 'short', 'one'): | ||
raise ValueError('unknown length: %r' % length) | ||
if translitcodec is None: | ||
return unicodedata.normalize('NFKD', string) \ | ||
.encode('ascii', 'ignore') \ | ||
.decode('ascii') | ||
else: | ||
if length == 'one': | ||
return string.encode('translit/one/ascii', 'replace').decode('ascii') | ||
return string.encode('translit/%s' % length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.. module:: brownie.text | ||
|
||
Text | ||
==== | ||
|
||
This module provides utilities to deal with text. | ||
|
||
|
||
.. autofunction:: transliterate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters