/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# This recipe refers: # # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061 import re rx=re.compile(u\"([\\u2e80-\\uffff])\", re.UNICODE) def cjkwrap(text, width, encoding=\"utf8\"): return reduce(lambda line, word, width=width: \'%s%s%s\' % (line, [\' \',\'\\n\', \'\'][(len(line)-line.rfind(\'\\n\')-1 + len(word.split(\'\\n\',1)[0] ) >= width) or line[-1:] == \'\\0\' and 2], word), rx.sub(r\'\\1\\0 \', unicode(text,encoding)).split(\' \') ).replace(\'\\0\', \'\').encode(encoding)