-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringkit.py
67 lines (52 loc) · 1.65 KB
/
stringkit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2012, Rui Carmo
Description: Utility functions for retrieving process information
License: MIT (see LICENSE.md for details)
"""
import sys
import re
import logging
import htmlentitydefs
import unicodedata
log = logging.getLogger()
def rsplit(s, sep=None, maxsplit=-1):
"""Equivalent to str.split, except splitting from the right"""
if sys.version_info < (2, 4, 0):
if sep is not None:
sep = sep[::-1]
L = s[::-1].split(sep, maxsplit)
L.reverse()
return [s[::-1] for s in L]
else:
return s.rsplit(sep, maxsplit)
def shrink(line, bound=50, rep='[...]'):
"""Shrinks a string, adding an ellipsis to the middle"""
l = len(line)
if l < bound:
return line
if bound <= len(rep):
return rep
k = bound - len(rep)
return line[0:k / 2] + rep + line[-k / 2:]
def convert_entity(m):
"""Converts entities to codepoints where applicable"""
if m.group(1) == '#':
try:
return unichr(int(m.group(2)))
except ValueError:
return '&#%s;' % m.group(2)
try:
return unichr(htmlentitydefs.name2codepoint[m.group(2)])
except KeyError:
return '&%s;' % m.group(2)
def convert_html(buffer):
"""Replaces all entities with codepoints"""
return re.sub(r'&(#?)(.+?);', convertentity, buffer)
def munge_string(buffer):
"""Builds anchor IDs"""
return re.sub("[\W+]", "-", buffer.lower())
def remove_diacritics(buffer):
"""Remove diactritical marks in Latin characters"""
unicodedata.normalize('NFKD', unicode(buffer)).encode('ASCII', 'ignore')