Thanks to visit codestin.com
Credit goes to gist.github.com

Skip to content

Instantly share code, notes, and snippets.

@jeffbowen
Last active September 11, 2023 18:08
Show Gist options
  • Select an option

  • Save jeffbowen/b98be3709d7725ffe74b1acb2b8d346a to your computer and use it in GitHub Desktop.

Select an option

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"
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