Another Franklin M. Liang's hyphenation algorithm Javascript implementation

Datetime:2016-08-23 03:13:07          Topic: JavaScript  Algorithm           Share

hyphen

This is an implementation of Franklin M. Liang's hyphenation algorithm in Javascript. It relies on fast programming technics to reduce execution time. The accuracy of hyphenation results depends on the quality of predefined patterns collections, that are different for every language. This implementation uses patterns collections from TeX website http://www.ctan.org/ .

var initialText = "A certain king had a beautiful garden"
var hyphenate = createHyphenator(hyphenationPatternsEnGb);

hyphenatedText = hyphenate(initialText);
// A cer-tain king had a beau-ti-ful garden

Demo

JSFiddle

Algorithm description

PDF Document

Install

npm install hyphen

or

bower install hyphe

Configuration

There is a couple of settings you can use to configure hyphenator function:

  • To change the default soft hyphen \u00AD character to something else, use hyphenChar: "-"
var hyphenator = createHyphenator(hyphenationPatternsEnGb, {hyphenChar:'-'})
  • To enable the console output of the hyphenation process, use debug: true
var hyphenator = createHyphenator(hyphenationPatternsEnGb, {debug:true})

Supported languages

  • Afrikaans
  • Ancient Greek
  • Armenian
  • Assamese
  • Bahasa Indonesia
  • Basque
  • Bengali
  • Bulgarian
  • Catalan
  • Chinese pinyin syllables
  • Church Slavonic
  • Classical Latin
  • Coptic
  • Croatian
  • Czech
  • Danish
  • Dutch
  • Esperanto
  • Estonian
  • Ethiopic
  • Finnish
  • French
  • Friulan
  • Galician
  • GB English
  • Georgian
  • German
  • Gujarati
  • Hindi
  • Hungarian
  • Icelandic Plain
  • Interlingua
  • Irish
  • Italian
  • Kannada
  • Kurmanji
  • Latin
  • Latvian
  • Lithuanian
  • Liturgical Latin
  • Malayalam
  • Marathi
  • Modern Monotonic
  • Modern Polytonic
  • Mongolian
  • Norwegian
  • Norwegian Bokmal
  • Norwegian Nynorsk
  • Occitan
  • Oriy
  • Panjabi
  • Piedmontese
  • Polish
  • Portuguese
  • Romanian
  • Romansh
  • Russian
  • Sanskrit and Prakrit
  • Serbian Cyrillic
  • Serbocroatian Cyrillic
  • Serbocroatian Latin
  • Slovak
  • Slovenian
  • Spanish
  • Swedish
  • Swiss-German
  • Tamil
  • Telugu
  • Thai
  • Turkish
  • Turkmen
  • Ukrainian
  • Upper Sorbian
  • US English
  • Welsh

Hyphenation in CSS

The CSS hyphens property is intended to add hyphenation support to modern browsers without Javascript:

p {
  hyphens: auto;
}

It is part of the CSS Text Level 3 specification. The browser compatibility list can be found on the related MDN page .

Alternatives

Check out other well-known Javascript implementations of hyphenation algorithm:

  • Hyphenator.js Javascript that implements client-side hyphenation of HTML-Documents.
  • Hypher A fast and small JavaScript hyphenation engine.

License

MIT





About List