Skip to main content

Python Port of John Gruber's titlecase.pl

Project description

https://travis-ci.org/ppannuto/python-titlecase.svg?branch=master https://coveralls.io/repos/github/ppannuto/python-titlecase/badge.svg?branch=master

This filter changes a given text to Title Caps, and attempts to be clever about SMALL words like a/an/the in the input. The list of “SMALL words” which are not capped comes from the New York Times Manual of Style, plus some others like ‘vs’ and ‘v’.

The filter employs some heuristics to guess abbreviations that don’t need conversion.

Original

Conversion

this is a test

This Is a Test

THIS IS A TEST

This Is a Test

this is a TEST

This Is a TEST

More examples and expected behavior for corner cases are available in the package test suite.

This library is a resurrection of Stuart Colville’s titlecase.py, which was in turn a port of John Gruber’s titlecase.pl.

Issues, updates, pull requests, etc should be directed to github.

Installation

The easiest method is to simply use pip:

(sudo) pip install titlecase

Usage

Titlecase provides only one function, simply:

>>> from titlecase import titlecase
>>> titlecase('a thing')
'A Thing'

A callback function may also be supplied, which will be called for every word:

>>> def abbreviations(word, **kwargs):
...   if word.upper() in ('TCP', 'UDP'):
...     return word.upper()
...
>>> titlecase.titlecase('a simple tcp and udp wrapper', callback=abbreviations)
'A Simple TCP and UDP Wrapper'

The callback function is supplied with an all_caps keyword argument, indicating whether the entire line of text was entirely capitalized. Returning None from the callback function will allow titlecase to process the word as normal.

Command Line Usage

Titlecase also provides a command line utility titlecase:

$ titlecase make me a title
Make Me a Title
$ echo "Can pipe and/or whatever else" | titlecase
Can Pipe and/or Whatever Else
# Or read/write files:
$ titlecase -f infile -o outfile

In addition, commonly used acronyms can be kept in a local file at ~/.titlecase.txt. This file contains one acronym per line. The acronym will be maintained in the title as it is provided. Once there is e.g. one line saying TCP, then it will be automatically used when used from the command line.

$ titlecase I LOVE TCP
I Love TCP

Limitations

This is a best-effort library that uses regexes to try to do intelligent things, but will have limitations. For example, it does not have the contextual awareness to distinguish acronyms from words: us (we) versus US (United States).

The regexes and titlecasing rules were written for American English. While there is basic support for Unicode characters, such that something like “El Niño” will work, it is likely that accents or non-English phrases will not be handled correctly.

If anyone has concrete solutions to improve these or other shortcomings of the library, pull requests are very welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

titlecase-1.1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

titlecase-1.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file titlecase-1.1.1.tar.gz.

File metadata

  • Download URL: titlecase-1.1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for titlecase-1.1.1.tar.gz
Algorithm Hash digest
SHA256 16e279edf085293bc9c44a68ce959c7d6cd5c653e6b5669a3a3640015cb63eb6
MD5 304a425308b2f2f1ba5388835a1298c5
BLAKE2b-256 2f74da46ac04bc4ad3f214c17de9bc25bdb7c06cada62fa07b11878107f602b4

See more details on using hashes here.

File details

Details for the file titlecase-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: titlecase-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for titlecase-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e590c0a67503d5f69c1f8f006f45b802506fb17397faaf17186f0b9292b486c
MD5 19a8c00e7c8a2bc662bb2c7b436bfedd
BLAKE2b-256 adfbb7996f035728b265055392961f9abe5e16103916570498dcb24d0d29f40d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page