code: def smart_truncate(value, max_length=0, word_boundaries=False, separator=' '):
      
  
      value = value.strip(separator)
  
      if not max_length:
          return value
  
      if len(value) < max_length:
          return value
  
      if not word_boundaries:
          return value[:max_length].strip(separator)
  
      if separator not in value:
          return value[:max_length]
  
      truncated = ''
      for word in value.split(separator):
          if word:
              next_len = len(truncated) + len(word) + len(separator)
              if next_len <= max_length:
                  truncated += '{0}{1}'.format(word, separator)
      if not truncated:
          truncated = value[:max_length]
      return truncated.strip(separator)
docs: Truncate a string