smartTruncate_1.py 674 B

12345678910111213141516171819202122232425262728
  1. def smartcut(value, arg):
  2. """
  3. usage: description|smartcut:"20:30"
  4. 'this is a string text that will become smart truncated'|smartcut:'20:30' will become to
  5. 'this is a string text that...'
  6. normal truncate with 20 chars will cut the word in half
  7. 'this is a string tex...'
  8. """
  9. els = map(int, arg.split(':'))
  10. start = els[0]
  11. end = els[1]
  12. if len(value) <= end:
  13. return value
  14. ocurrence = value[start:end].find(" ")
  15. if ocurrence:
  16. for pos in range(start, len(value)):
  17. if value[pos] == " " and pos < end:
  18. ocurrence = pos
  19. value = value[0:ocurrence] + "..."
  20. return value