Skip to content

Instantly share code, notes, and snippets.

@madebyjazz
Forked from malthe/hyphenation.py
Created September 23, 2011 17:24
Show Gist options
  • Save madebyjazz/1237938 to your computer and use it in GitHub Desktop.
Save madebyjazz/1237938 to your computer and use it in GitHub Desktop.
Dictionary-based HTML hyphenation using soft hyphen
import os
import re
from htmlentitydefs import name2codepoint
# Must install the `hyphenator` library from PyPi!
from hyphenator import Hyphenator
# Firefox comes with an English hyphenation dictionary
path = os.popen('locate hyph_en_US.dic').readlines()[0].strip()
hyphenator = Hyphenator(path)
spaces = re.compile(r'\s+')
shy = unichr(name2codepoint['shy'])
def hyphenate(string, max_length=20):
return " ".join(
len(word) > max_length and
shy.join(hyphenator.wrap(word, max_length) or (word, )).\
replace('-', '')
or word for word in spaces.split(string.strip())
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment