diff --git a/tildes/tests/test_title.py b/tildes/tests/test_title.py index f4cccd2..4b8268b 100644 --- a/tildes/tests/test_title.py +++ b/tildes/tests/test_title.py @@ -46,12 +46,19 @@ def test_whitespace_trimmed(title_schema): def test_trailing_periods_trimmed(title_schema): - """Ensure trailing periods on a title are removed.""" + """Ensure trailing periods on a single-sentence title are removed.""" title = "This is an interesting story." result = title_schema.load({"title": title}) assert not result["title"].endswith(".") +def test_multisentence_trailing_period_kept(title_schema): + """Ensure a trailing period is kept if the title has multiple sentences.""" + title = "I came. I saw. I conquered." + result = title_schema.load({"title": title}) + assert result["title"].endswith(".") + + def test_consecutive_whitespace_removed(title_schema): """Ensure consecutive whitespace in a title is compressed.""" title = "sure are \n a lot of spaces" diff --git a/tildes/tildes/schemas/topic.py b/tildes/tildes/schemas/topic.py index dc76272..af8ef88 100644 --- a/tildes/tildes/schemas/topic.py +++ b/tildes/tildes/schemas/topic.py @@ -45,8 +45,14 @@ class TopicSchema(Schema): new_data = data.copy() - # strip any trailing periods - new_data["title"] = new_data["title"].rstrip(".") + split_title = re.split("[.?!]+", new_data["title"]) + + # the last string in the list will be empty if it ended with punctuation + num_sentences = len([piece for piece in split_title if piece]) + + # strip trailing periods off single-sentence titles + if num_sentences == 1: + new_data["title"] = new_data["title"].rstrip(".") return new_data