def _truncate_in_space(lab_text, max_len_lab):
    """
    This truncates a string to a given length but tries to cut
    at a space position instead of splitting a word.
    """
    if len(lab_text) > max_len_lab:
        idx = lab_text.find(" ", max_len_lab)
        if idx < 0:
            idx = max_len_lab

            # BEWARE: This must not fall in the middle of an html entity "&amp;", etc... ...
            idx_semi_colon = lab_text.find(";", idx)
            if idx_semi_colon < 0:
                idx = max_len_lab
            else:
                idx = idx_semi_colon + 1 # Just after the semi-colon.

        return lab_text[:idx]
    else:
        return lab_text