Skip to content
Create a gist now

Instantly share code, notes, and snippets.

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
part.rstrip('-') for part in
(hyphenator.wrap(word, max_length) or (word, ))
) 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
Something went wrong with that request. Please try again.