#lang scribble/manual @(require scribble/eval (for-label txexpr racket hyphenate xml)) @(define my-eval (make-base-eval)) @(my-eval `(require txexpr hyphenate xml)) @title{Hyphenate} @author[(author+email "Matthew Butterick" "mb@mbtype.com")] @defmodule[#:multi (hyphenate (submod hyphenate safe) typed/hyphenate)] A simple hyphenation engine that uses the Knuth–Liang hyphenation algorithm originally developed for TeX. I have added little to their work. Accordingly, I take little credit. I originally put together this module to handle hyphenation for my web-based book @link["http://practicaltypography.com"]{Butterick's Practical Typography} (which I made with Racket & Scribble). Though support for CSS-based hyphenation in web browsers is @link["http://caniuse.com/#search=hyphen"]{still iffy}, soft hyphens work reliably well. But putting them into the text manually is a drag. Thus a module was born. @section{Installation} At the command line: @verbatim{raco pkg install hyphenate} After that, you can update the package like so: @verbatim{raco pkg update hyphenate} @section{Importing the module} The module can be invoked three ways: fast, safe, and typed. Fast mode is the default, which you get by importing the module in the usual way: @code{(require hyphenate)}. Safe mode enables the function contracts documented below. Use safe mode by importing the module as @code{(require (submod hyphenate safe))}. The typed version is invoked as @code{(require typed/hyphenate)}. The typed version is implemented ``natively'' in the sense that it is compiled separately with type annotations. It is not a @racket[require/typed] wrapper around the untyped code. This avoids the contract barrier that is otherwise automatically imposed between typed and untyped code. @section{Interface} @defproc[ (hyphenate [xexpr xexpr/c] [joiner (or/c char? string?) (integer->char #x00AD)] [#:exceptions exceptions (listof string?) empty] [#:min-length length (or/c integer? false?) 5] [#:min-left-length left-length (or/c (and/c integer? positive?) #f) 2] [#:min-right-length right-length (or/c (and/c integer? positive?) #f) 2] [#:omit-word word-test (string? . -> . any/c) (λ(x) #f)] [#:omit-string string-test (string? . -> . any/c) (λ(x) #f)] [#:omit-txexpr txexpr-test (txexpr? . -> . any/c) (λ(x) #f)]) xexpr/c] Hyphenate @racket[_xexpr] by calculating hyphenation points and inserting @racket[_joiner] at those points. By default, @racket[_joiner] is the soft hyphen (Unicode 00AD = decimal 173). Words shorter than @racket[#:min-length] @racket[_length] will not be hyphenated. To hyphenate words of any length, use @racket[#:min-length] @racket[#f]. @margin-note{The REPL displays a soft hyphen as @code{\u00AD}. But in ordinary use, you'll only see a soft hyphen when it appears at the end of a line or page as part of a hyphenated word. Otherwise it's not displayed. In most of the examples here, I use a standard hyphen for clarity (by adding @code{#\-} as an argument).} @examples[#:eval my-eval (hyphenate "ergo polymorphism") (hyphenate "ergo polymorphism" #\-) (hyphenate "ergo polymorphism" #:min-length 13) (hyphenate "ergo polymorphism" #:min-length #f) ] The @racket[#:min-left-length] and @racket[#:min-right-length] keyword arguments set the minimum distance between a potential hyphen and the left or right ends of the word. The default is 2 characters. Larger values will reduce hyphens, but also prevent small words from breaking. These values will override a smaller @racket[#:min-length] value. @examples[#:eval my-eval (hyphenate "ergo polymorphism" #\-) (hyphenate "ergo polymorphism" #\- #:min-left-length #f) (hyphenate "ergo polymorphism" #\- #:min-length 2 #:min-left-length 5) (hyphenate "ergo polymorphism" #\- #:min-right-length 6) (code:comment @#,t{Next words won't be hyphenated becase of large #:min-left-length}) (hyphenate "ergo polymorphism" #\- #:min-length #f #:min-left-length 15) ] Because the hyphenation is based on an algorithm rather than a dictionary, it makes good guesses with unusual words: @examples[#:eval my-eval (hyphenate "scraunched strengths" #\-) (hyphenate "RacketCon" #\-) (hyphenate "supercalifragilisticexpialidocious" #\-) ] Using the @racket[#:exceptions] keyword, you can pass hyphenation exceptions as a list of words with hyphenation points marked with regular hyphens (@racket["-"]). If an exception word contains no hyphens, that word will never be hyphenated. @examples[#:eval my-eval (hyphenate "polymorphism" #\-) (hyphenate "polymorphism" #\- #:exceptions '("polymo-rphism")) (hyphenate "polymorphism" #\- #:exceptions '("polymorphism")) ] Knuth & Liang were sufficiently confident about their algorithm that they originally released it with only 14 exceptions: @italic{associate[s], declination, obligatory, philanthropic, present[s], project[s], reciprocity, recognizance, reformation, retribution}, and @italic{table}. Admirable bravado, but it's not hard to discover others that need adjustment. @examples[#:eval my-eval (hyphenate "wrong: columns signage lawyers" #\-) (hyphenate "right: columns signage lawyers" #\- #:exceptions '("col-umns" "sign-age" "law-yers")) ] The Knuth–Liang algorithm is designed to omit legitimate hyphenation points (i.e., generate false negatives) more often than it creates erroneous hyphenation points (i.e., false positives). This is good policy. Perfect hyphenation — that is, hyphenation that represents an exact linguistic syllabification of each word — is superfluous for typesetting. Hyphenation simply seeks to mark possible line-break and page-break locations for whatever layout engine is drawing the text. The ultimate goal is to permit more even text flow. Like horseshoes and hand grenades, close is good enough. And a word wrongly hyphenated is more likely to be noticed by a reader than a word inefficiently hyphenated. For this reason, certain words can't be hyphenated algorithmically, because the correct hyphenation depends on meaning, not merely on spelling. For instance: @examples[#:eval my-eval (hyphenate "adder") ] This is the right result. If you used @italic{adder} to mean the machine, it would be hyphenated @italic{add-er}; if you meant the snake, it would be @italic{ad-der}. Better to avoid hyphenation than to hyphenate incorrectly. You can send HTML-style X-expressions through @racket[hyphenate]. It will recursively hyphenate the text strings, while leaving the tags and attributes alone, as well as non-hyphenatable material (like character entities and CDATA). @examples[#:eval my-eval (hyphenate '(p "strangely" (em "formatted" (strong "snowmen"))) #\-) (hyphenate '(headline [[class "headline"]] "headline") #\-) (hyphenate '(div "The (span epsilon) entity:" epsilon) #\-) ] Don't send raw HTML or XML through @racket[hyphenate]. It can't distinguish tags and attributes from textual content, so everything will be hyphenated, thus goofing up your file. But you can easily convert your HTML or XML to an X-expression, hyphenate it, and then convert back. @examples[#:eval my-eval (define html "Hello") (hyphenate html #\-) (xexpr->string (hyphenate (string->xexpr html) #\-)) ] If you're working with HTML, be careful not to include any @code{