• Stars
    star
    564
  • Rank 79,014 (Top 2 %)
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast and small JavaScript hyphenation engine

Hypher

A small and fast JavaScript hyphenation engine. Can be used in Node.js and as a jQuery plugin.

Node.js

Hypher can be installed from NPM:

npm install hypher hyphenation.en-us

You can then use it in your program by creating an instance of Hypher and giving it a language object:

var Hypher = require('hypher'),
    english = require('hyphenation.en-us'),
    h = new Hypher(english);

// returns ['hy', 'phen', 'ation']
h.hyphenate('hyphenation');

See examples/node/ for a full example on how to use Hypher. The hyphenate method does not support hyphenated compound words. These should be split into individual words before being passed to the hyphenation engine and reassembled afterwards by the caller. You can also use the hyphenateText method to hyphenate a string of text. The hyphenateText method does support compound words and returns a string with inserted soft hyphens (\u00AD.)

// returns 'Hy|phen|ation is use|ful when cen|ter jus|ti|fy|ing a text.' where `|` is a soft hyphen
h.hyphenateText('Hyphenation is useful when center justifying a text.');

The hyphenateText method takes an optional second parameter minLength which is the minimum length a word should have to be considered for hyphenation (defaults to 4.) Note that an instance of the Hypher class should only be created once for each language object.

The language object should contain:

{
  // The minimum number of unhyphenated characters at the left of each word. (required)
  leftmin: <number>, 

  // The minimum number of unhyphenated characters at the right of each word. (required)
  rightmin: <number>,

  // A comma separated list of hyphenation exceptions. Custom hyphenations
  // can be specified using '\u2027' (hyphenation point) as hyphenation
  // character. List items are case-insensitive. (Optional)
  exceptions: <string>,

  // A patterns object (required)
  patterns: {}
}

Language patterns can be found in the patterns repository.

jQuery

To use the jQuery plugin include dist/jquery.hypher.js in your HTML document together with any number of language pattern files from the dist/browser directory in the patterns repository. It is important that you include jquery.hypher.js before any language pattern files.

<script src="jquery.hypher.js"></script>
<script src="en-us.js"></script>

This will extend jQuery with a hyphenate method. Given the following HTML:

<p>Hyphenation is <em>important</em></p>

You can hyphenate the text content of the p element like so:

$('p').hyphenate('en-us');

The hyphenate method only works on the text content of the elements it is called on, so in the above example the word "important" will not be hyphenated. To also include the text content of the em element, simply include it in your selector:

$('p, em').hyphenate('en-us');

This naturally also applies to your own classes:

$('p.hyphenate, em, a').hyphenate('en-us');

This will hyphenate only p with class hyphenate and em and a elements.

Ender

Assuming you have Ender installed you can either add Hypher and a hyphenation pattern to your library by using the command line ender build hypher hyphenation.en-us, or include them in your dependencies in your package.json:

"dependencies": {
  "hypher": "*",
  "hyphenation.en-us": "*"
}

and build your library as usual with ender build .. Then---as in jQuery---hyphenate the selected elements:

$('p').hyphenate('en-us');

The examples/ender/ directory contains an example project using Ender and Hypher.

License

Hypher is licensed under the three clause BSD license (see BSD.txt.)

See also

Contributors

  • Laurens Meurs - improvements to the exception list

More Repositories

1

fontfaceobserver

Webfont loading. Simple, small, and efficient.
JavaScript
4,235
star
2

typeset

TeX line breaking algorithm in JavaScript
JavaScript
980
star
3

trmix

apply CSS based on your browser's text rendering engine
JavaScript
500
star
4

homebrew-webfonttools

Homebrew formulae for font tools
Ruby
359
star
5

fontloader

A fontloader polyfill
JavaScript
324
star
6

xsltjson

XSLTJSON - Convert XML to JSON using XSLT
XSLT
305
star
7

jlayout

JavaScript layout algorithms
JavaScript
283
star
8

funcy

An experiment in adding functional pattern matching to JavaScript
JavaScript
247
star
9

url-template

A JavaScript URI template implementation (RFC 6570 compliant)
JavaScript
179
star
10

opentype

An OpenType, TrueType, WOFF, and WOFF2 parser in JavaScript
JavaScript
133
star
11

sfnt2woff-zopfli

WOFF utilities with Zopfli compression
C
126
star
12

promis

A small embeddable Promise polyfill
JavaScript
97
star
13

postcss-scale

PostCSS plugin to scale values from one range to another.
HTML
80
star
14

bit-array

JavaScript implementation of bit arrays.
JavaScript
78
star
15

hyphenation-patterns

Hyphenation patterns for use with Hypher
JavaScript
74
star
16

stateofwebtype

Up-to-date data on support for type and typographic features on the web.
JavaScript
65
star
17

junify

JUnify ― JavaScript Unification Library
JavaScript
48
star
18

text-overflow

jQuery Text Overflow plugin
JavaScript
43
star
19

jsizes

jQuery CSS size properties plugin
JavaScript
37
star
20

characterset

A library for creating and manipulating character sets
JavaScript
29
star
21

css-font-parser

A parser for the CSS font values
JavaScript
26
star
22

jslint

JSLint: The JavaScript Quality Tool, command line version (Node.js)
JavaScript
25
star
23

datrie

A JavaScript Double Array Trie
JavaScript
21
star
24

unicode-tokenizer

Unicode Tokenizer following the Unicode Line Breaking algorithm
JavaScript
20
star
25

nanofont

A nano font for testing font format support
Makefile
19
star
26

node-typekit

A minimal Typekit API client in Node.js
JavaScript
19
star
27

knockout.selection

A selection binding for Knockout.js
JavaScript
19
star
28

javascript

Various JavaScript projects & tools.
JavaScript
17
star
29

knockout.dragdrop

A HTML5 drag and drop binding for Knockout.
JavaScript
16
star
30

text-align

jQuery Text Alignment plugin
JavaScript
13
star
31

tpo

Next generation of browser typesetting
JavaScript
13
star
32

closure-compiler-inline

A Closure Compiler fork with more control over function inlining
Java
11
star
33

calcdeps

A Node.js port of Google Closure library calcdeps.py
JavaScript
11
star
34

js-preprocess

JavaScript Preprocessor
JavaScript
9
star
35

column-selector

jQuery Column Selector
JavaScript
9
star
36

fonzie

A tiny @font-face loader
JavaScript
8
star
37

phantomjs-typekit

A simple demo of using Typekit with PhantomJS
JavaScript
8
star
38

epub2ts

ePub to Treesaver conversion
JavaScript
8
star
39

php-typekit

A PHP client for the Typekit API
PHP
7
star
40

shp2json

Simple tool to convert Shapefiles (GIS) to JSON
JavaScript
6
star
41

hyphenation-justification-vf

JavaScript
5
star
42

nanoserver

A simple web server for development
JavaScript
5
star
43

emfont

A font with a single character filling the entire em-box
HTML
5
star
44

ui

C++/OpenGL User Interface library
5
star
45

jslint-core

JSLint: The JavaScript Code Quality Tool packaged as a CommonJS module
JavaScript
5
star
46

mocha-browserstack

A Mocha reporter that can be used to run Mocha tests automatically on BrowserStack
JavaScript
4
star
47

sfnt2woff

C
4
star
48

node-browserstack

A Node.js client for the BrowserStack API (v3 and screenshot)
JavaScript
4
star
49

website

bramstein.com website source
JavaScript
3
star
50

unicode-data-parser

JavaScript
3
star
51

markup

JavaScript
2
star
52

closure-dom

JavaScript
2
star
53

ui-test

C++/OpenGL User Interface library test project
2
star
54

closureloader

Load code using the Closure library dependency syntax in Node.js
JavaScript
2
star
55

cssvalue

Parsers (and generators) for common CSS values.
JavaScript
2
star
56

thesis

Master Thesis: "Visualizations on the Web"
2
star
57

sowt-test

Automated browser tests for State of Web Type
JavaScript
2
star
58

closure-fetch

JavaScript
1
star
59

detect-writing-script

Detect the writing script given an array of codepoints.
JavaScript
1
star
60

ui-demo

C++/OpenGL User Interface library demo
1
star
61

font-weight-test

Test case for font-weight fallback behaviour
Makefile
1
star
62

amd-to-closure

Transform AMD modules to Closure Compiler dependencies
JavaScript
1
star
63

fontformatdetection

Detect browser support for font formats using feature detection
JavaScript
1
star