diff --git a/hyphenate/.gitignore b/hyphenate/.gitignore index 013cb2ff..442dc953 100644 --- a/hyphenate/.gitignore +++ b/hyphenate/.gitignore @@ -13,3 +13,4 @@ Icon # Files that might appear on external disk .Spotlight-V100 .Trashes +scribblings/hyphenate.html diff --git a/hyphenate/scribblings/hyphenate.html b/hyphenate/scribblings/hyphenate.html deleted file mode 100644 index b58e94e6..00000000 --- a/hyphenate/scribblings/hyphenate.html +++ /dev/null @@ -1,4 +0,0 @@ - -Hyphenate
Hyphenate
1 Installation & updates
2 Interface
hyphenate
hyphenatef
unhyphenate
3 License & source code
6.0.0.1

Hyphenate

Matthew Butterick <mb@mbtype.com>

A simple hyphenation engine that uses the Knuth–Liang hyphenation algorithm originally developed for TeX. This implementation is a port of Ned Batchelder’s Python version. 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 Butterick’s Practical Typography (which I made with Racket & Scribble). Though support for CSS-based hyphenation in web browsers is still iffy, soft hyphens work reliably well. But putting them into the text manually is a drag. Thus a module was born.

1 Installation & updates

At the command line: -

raco pkg install hyphenate

After that, you can update the package from the command line: -

raco pkg update hyphenate

2 Interface

 (require hyphenate) package: hyphenate

procedure

(hyphenate text    
  [joiner    
  #:exceptions exceptions    
  #:min-length length])  string?
  text : string?
  joiner : (or/c char? string?) = (integer->char 173)
  exceptions : (listof string?) = empty
  length : (or/c integer? false?) = 5
Hyphenate text by calculating hyphenation points and inserting joiner at those points. By default, joiner is the soft hyphen (Unicode 00AD = decimal 173). Words shorter than #:min-length length will not be hyphenated. To hyphenate words of any length, use #:min-length #f.

The REPL displays a soft hyphen as \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.

Examples:

> (hyphenate "ergo polymorphic")

"ergo poly\u00ADmor\u00ADphic"

> (hyphenate "ergo polymorphic" #\-)

"ergo poly-mor-phic"

> (hyphenate "ergo polymorphic" #:min-length 13)

"ergo polymorphic"

> (hyphenate "ergo polymorphic" #:min-length #f)

"er\u00ADgo poly\u00ADmor\u00ADphic"

Because the hyphenation is based on an algorithm rather than a dictionary, it makes good guesses with unusual words:

Examples:

> (hyphenate "scraunched strengths" #\-)

"scraunched strengths"

> (hyphenate "Racketcon" #\-)

"Rack-et-con"

> (hyphenate "supercalifragilisticexpialidocious" #\-)

"su-per-cal-ifrag-ilis-tic-ex-pi-ali-do-cious"

Using the #:exceptions keyword, you can pass hyphenation exceptions as a list of words with hyphenation points marked with regular hyphens ("-"). If an exception word contains no hyphens, that word will never be hyphenated.

Examples:

> (hyphenate "polymorphic" #\-)

"poly-mor-phic"

> (hyphenate "polymorphic" #\- #:exceptions '("polymo-rphic"))

"polymo-rphic"

> (hyphenate "polymorphic" #\- #:exceptions '("polymorphic"))

"polymorphic"

Knuth & Liang were sufficiently confident about their algorithm that they originally released it with only 14 exceptions: associate[s], declination, obligatory, philanthropic, present[s], project[s], reciprocity, recognizance, reformation, retribution, and table. Admirable bravado, but it’s not hard to discover others that need adjustment.

Examples:

> (hyphenate "wrong: columns signage lawyers" #\-)

"wrong: columns sig-nage lawyers"

> (hyphenate "right: columns signage lawyers" #\-
  #:exceptions '("col-umns" "sign-age" "law-yers"))

"right: col-umns sign-age law-yers"

Overall, my impression is that the Knuth–Liang algorithm is more likely to miss legitimate hyphenation points (i.e., generate false negatives) than create 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:

Example:

> (hyphenate "adder")

"adder"

This is the right result. If you used adder to mean the machine, it would be hyphenated add-er; if you meant the snake, it would be ad-der. Better to avoid hyphenation than to hyphenate incorrectly.

Don’t send raw HTML through hyphenate. It can’t distinguish HTML tags and attributes from textual content, so it will hyphenate everything, which will goof up your file.

Example:

> (hyphenate "<body style=\"background: yellow\">Hello world</body>")

"<body style=\"back\u00ADground: yel\u00ADlow\">Hel\u00ADlo world</body>"

Instead, send your textual content through hyphenate before you put it into your HTML template. Or convert your HTML to an X-expression and process it selectively (e.g., with match).

procedure

(hyphenatef text    
  pred    
  [joiner    
  #:exceptions exceptions    
  #:min-length length])  string?
  text : string?
  pred : procedure?
  joiner : (or/c char? string?) = (integer->char |#x00AD|)
  exceptions : (listof string?) = empty
  length : (or/c integer? false?) = 5
Like hyphenate, but only words matching pred are hyphenated. Convenient if you want to prevent hyphenation of certain sets of words, like proper names:

Examples:

> (hyphenate "Brennan Huff likes fancy sauce" #\-)

"Bren-nan Huff likes fan-cy sauce"

> (define uncapitalized? (λ(word) (let ([letter (substring word 0 1)])
  (equal? letter (string-downcase letter)))))
> (hyphenatef "Brennan Huff likes fancy sauce" uncapitalized?
   #\-)

"Brennan Huff likes fan-cy sauce"

It’s possible to do fancier kinds of hyphenation restrictions that take account of context, like not hyphenating the last word of a paragraph. But hyphenatef only operates on words. So you’ll have to write some fancier code. Separate out the hyphenatable words, and then send them through good old hyphenate.

procedure

(unhyphenate text [joiner])  string?

  text : string?
  joiner : (or/c char? string?) = (integer->char 173)
Remove joiner from text using string-replace.

A side effect of using hyphenate is that soft hyphens (or whatever the joiner is) will be embedded in the output text. If you need to support copying of text, for instance in a GUI application, you’ll probably want to strip out the hyphenation before the copied text is moved to the clipboard.

Examples:

> (hyphenate "ribbon-cutting ceremony")

"rib\u00ADbon-cut\u00ADting cer\u00ADe\u00ADmo\u00ADny"

> (unhyphenate (hyphenate "ribbon-cutting ceremony"))

"ribbon-cutting ceremony"

Use this function cautiously — if joiner appeared in the original input to hyphenate, the output from unhyphenate won’t be the same string.

Examples:

> (hyphenate "ribbon-cutting ceremony" #\-)

"rib-bon-cut-ting cer-e-mo-ny"

> (unhyphenate (hyphenate "ribbon-cutting ceremony" #\-) #\-)

"ribboncutting ceremony"

It’s also possible that soft hyphens could appear in your input string. Certain word processors allow users to insert soft hyphens in their text.

Examples:

> (hyphenate "True\u00ADType typefaces")

"True\u00ADType type\u00ADfaces"

> (unhyphenate (hyphenate "True\u00ADType typefaces"))

"TrueType typefaces"

> (hyphenate (unhyphenate "True\u00ADType typefaces") #\-)

"True-Type type-faces"

3 License & source code

This module is licensed under the LGPL.

Source repository at http://github.com/mbutterick/hyphenate. Suggestions & corrections welcome.

 
\ No newline at end of file