Last active
September 11, 2023 18:08
-
-
Save jeffbowen/b98be3709d7725ffe74b1acb2b8d346a to your computer and use it in GitHub Desktop.
possessive — A Django template filter for adding appropriate punctuation to possessive proper nouns per "The Associated Press Stylebook"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import template | |
| from django.template.defaultfilters import stringfilter | |
| register = template.Library() | |
| @register.filter | |
| @stringfilter | |
| def possessive(string): | |
| if string[-2:] == "'s" or string[-2:] == "’s": | |
| return string | |
| elif string[-1:] == "s": | |
| return f"{string}’" | |
| else: | |
| return f"{string}’s" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment