Editorial Workflows

Title Case

unlisted workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Convert selected text to title case. Uses [John Gruber's more intelligent title-casing rules](http://daringfireball.net/2008/05/title_case), as converted to Python by [Stuart Colville](http://muffinresearch.co.uk/archives/2008/05/27/titlecasepy-titlecase-in-python/).

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Selected Text ?
Entire Line(s)
OFF
Empty Selection Output
  • No Output
  • All Text
  • Closest Word
Folded Text
  • Include
  • Replace with:
Run Python Script ?
Source Code
#coding: utf-8
import workflow
import unittest
import sys
import re
import clipboard

# Get the custom parameters as a dictionary (titles are keys):
params = workflow.get_parameters()
# Get the action's input (a string):
action_in = workflow.get_input()

# -*- coding: utf-8 -*-
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
titlecase.py v0.2
Original Perl version by: John Gruber http://daringfireball.net/ 10 May 2008
Python version by Stuart Colville http://muffinresearch.co.uk
License: http://www.opensource.org/licenses/mit-license.php
"""


SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
PUNCT = "[!\"#$%&'‘()*+,-./:;?@[\\\\\\]_`{|}~]"

SMALL_WORDS = re.compile(r'^(%s)$' % SMALL, re.I)
INLINE_PERIOD = re.compile(r'[a-zA-Z][.][a-zA-Z]')
UC_ELSEWHERE = re.compile(r'%s*?[a-zA-Z]+[A-Z]+?' % PUNCT)
CAPFIRST = re.compile(r"^%s*?([A-Za-z])" % PUNCT)
SMALL_FIRST = re.compile(r'^(%s*)(%s)\b' % (PUNCT, SMALL), re.I)
SMALL_LAST = re.compile(r'\b(%s)%s?$' % (SMALL, PUNCT), re.I)
SUBPHRASE = re.compile(r'([:.;?!][ ])(%s)' % SMALL)

"""
    Titlecases input text

    This filter changes all words to Title Caps, and attempts to be clever
    about *un*capitalizing 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 'vs' and 'v'.

"""

words = re.split('\s', action_in)
line = []
for word in words:
    if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):
        line.append(word)
        continue
    if SMALL_WORDS.match(word):
        line.append(word.lower())
        continue
    line.append(CAPFIRST.sub(lambda m: m.group(0).upper(), word))

line = " ".join(line)

line = SMALL_FIRST.sub(lambda m: '%s%s' % (
    m.group(1),
    m.group(2).capitalize()
), line)

line = SMALL_LAST.sub(lambda m: m.group(0).capitalize(), line)

line = SUBPHRASE.sub(lambda m: '%s%s' % (
    m.group(1),
    m.group(2).capitalize()
), line)

workflow.set_output(line)
Replace Selected Text ?
Replacement Text
Input