Browse Source

Stop stripping periods from multi-sentence titles

If a topic title has multiple sentences in it, it looks strange to strip
the trailing period off it, so we only want to do that automatically
when it's a single sentence.
merge-requests/126/merge
Deimos 4 years ago
parent
commit
e4a187eb56
  1. 9
      tildes/tests/test_title.py
  2. 10
      tildes/tildes/schemas/topic.py

9
tildes/tests/test_title.py

@ -46,12 +46,19 @@ def test_whitespace_trimmed(title_schema):
def test_trailing_periods_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." title = "This is an interesting story."
result = title_schema.load({"title": title}) result = title_schema.load({"title": title})
assert not result["title"].endswith(".") 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): def test_consecutive_whitespace_removed(title_schema):
"""Ensure consecutive whitespace in a title is compressed.""" """Ensure consecutive whitespace in a title is compressed."""
title = "sure are \n a lot of spaces" title = "sure are \n a lot of spaces"

10
tildes/tildes/schemas/topic.py

@ -45,8 +45,14 @@ class TopicSchema(Schema):
new_data = data.copy() 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 return new_data

Loading…
Cancel
Save