Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed Aug 6, 2017
2 parents 72dcc1e + 0521eb0 commit fa43f57
Show file tree
Hide file tree
Showing 45 changed files with 8,670 additions and 6,973 deletions.
6,062 changes: 3,128 additions & 2,934 deletions Hyphenator.js

Large diffs are not rendered by default.

131 changes: 56 additions & 75 deletions Hyphenator_Loader.js
@@ -1,7 +1,7 @@
/** @license Hyphenator_Loader 5.1.0 - client side hyphenation for webbrowsers
* Copyright (C) 2015 Mathias Nater, Zürich (mathiasnater at gmail dot com)
/** @license Hyphenator_Loader 5.2.0 - client side hyphenation for webbrowsers
* Copyright (C) 2017 Mathias Nater, Zürich (mathiasnater at gmail dot com)
* https://github.com/mnater/Hyphenator
*
*
* Released under the MIT license
* http://mnater.github.io/Hyphenator/LICENSE.txt
*/
Expand All @@ -11,13 +11,13 @@
* @description Checks if there's CSS-hyphenation available for the given languages and
* loads and runs Hyphenator if there's no CSS-hyphenation
* @author Mathias Nater, <a href = "mailto:mathias@mnn.ch">mathias@mnn.ch</a>
* @version 5.1.0
* @version 5.2.0
* @namespace Holds all methods and properties
*/

/* The following comment is for JSLint: */
/*jslint browser: true */
/*global Hyphenator: false */
/*jslint browser: true multivar: true, single: true*/
/*global window Hyphenator*/

var Hyphenator_Loader = (function (window) {
'use strict';
Expand All @@ -43,66 +43,6 @@ var Hyphenator_Loader = (function (window) {
return r;
},

/**
* @name Hyphenator-checkLangSupport
* @description
* Checks if hyphenation for all languages are supported:
* If body is present (i.e. DOMContentLoaded) a hidden div is added to the body and the height of probably hyphenated text is measured.
* Else a fake body is inserted and used instead of the 'real' body. It will later be removed.
* @type {function()}
* @return {bool}
* @private
*/
checkLangSupport = function () {
var shadowContainer,
shadow,
shadows = [],
lang,
i,
r = true,
bdy = window.document.getElementsByTagName('body')[0],
fakeBdy = false;
if (!bdy) {
fakeBdy = createElem('body');
}
shadowContainer = createElem('div');
shadowContainer.style.MozHyphens = 'auto';
shadowContainer.style['-webkit-hyphens'] = 'auto';
shadowContainer.style['-ms-hyphens'] = 'auto';
shadowContainer.style.hyphens = 'auto';
shadowContainer.style.fontSize = '12px';
shadowContainer.style.lineHeight = '12px';
shadowContainer.style.wordWrap = 'normal';
shadowContainer.style.visibility = 'hidden';

for (lang in languages) {
if (languages.hasOwnProperty(lang)) {
shadow = createElem('div');
shadow.style.width = '5em';
shadow.lang = lang;
shadow.style['-webkit-locale'] = "'" + lang + "'";
shadow.appendChild(window.document.createTextNode(languages[lang]));
shadowContainer.appendChild(shadow);
shadows.push(shadow);
}
}
if (fakeBdy) {
fakeBdy.appendChild(shadowContainer);
window.document.documentElement.appendChild(fakeBdy);
} else {
bdy.appendChild(shadowContainer);
}
for (i = 0; i < shadows.length; i += 1) {
r = (shadows[i].offsetHeight > 12) && r;
}
if (fakeBdy) {
fakeBdy.parentNode.removeChild(fakeBdy);
} else {
bdy.removeChild(shadowContainer);
}
return r;
},

/**
* @name Hyphenator-loadNrunHyphenator
* @description Loads Hyphenator.js and runs it with the given configuration
Expand All @@ -117,28 +57,69 @@ var Hyphenator_Loader = (function (window) {
script = createElem('script');
script.src = path;
script.type = 'text/javascript';
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
script.onreadystatechange = function () {
if (!done && (!script.readyState || script.readyState === "loaded" || script.readyState === "complete")) {
done = true;

Hyphenator.config(config);
Hyphenator.run();

// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
script.onreadystatechange = null;
script.onload = null;
if (head && script.parentNode) {
head.removeChild(script);
}
}
};
script.onload = script.onreadystatechange;
head.appendChild(script);
},

runner = function () {
var allLangsSupported = checkLangSupport();
if (!allLangsSupported) {
loadNrunHyphenator(config);
/**
* @name Hyphenator-checkLangSupport
* @description
* Checks if hyphenation for all languages are supported:
* If body is present (i.e. DOMContentLoaded) a hidden div is added to the body and the height of probably hyphenated text is measured.
* Else a fake body is inserted and used instead of the 'real' body. It will later be removed.
* @type {function()}
* @return {bool}
* @private
*/
checkLangSupport = function () {
var shadowContainer,
shadow,
lang,
fakeBdy = createElem('body');
shadowContainer = createElem('div');
shadowContainer.style.visibility = 'hidden';

fakeBdy.appendChild(shadowContainer);
window.document.documentElement.appendChild(fakeBdy);

for (lang in languages) {
if (languages.hasOwnProperty(lang)) {
shadow = createElem('div');
shadow.style.MozHyphens = 'auto';
shadow.style['-webkit-hyphens'] = 'auto';
shadow.style['-ms-hyphens'] = 'auto';
shadow.style.hyphens = 'auto';
shadow.style.width = '5em';
shadow.style.lineHeight = '12px';
shadow.style.border = 'none';
shadow.style.padding = '0';
shadow.style.wordWrap = 'normal';
shadow.style['-webkit-locale'] = "'" + lang + "'";
shadow.lang = lang;
shadow.appendChild(window.document.createTextNode(languages[lang]));
shadowContainer.appendChild(shadow);
if (shadow.offsetHeight === 12) {
loadNrunHyphenator(config);
break;
}
}
}

fakeBdy.parentNode.removeChild(fakeBdy);
};

return {
Expand All @@ -153,7 +134,7 @@ var Hyphenator_Loader = (function (window) {
languages = langs;
path = p;
config = configs || {};
runner();
checkLangSupport();
}
};
}(window));
74 changes: 72 additions & 2 deletions README.md
@@ -1,5 +1,75 @@
# Hyphenator.js

This repository replaces https://code.google.com/p/hyphenator/
- This repository replaces https://code.google.com/p/hyphenator/
- Demo: http://mnater.github.io/Hyphenator/

Please see the projects page on http://mnater.github.io/Hyphenator/ for instructions and further reading.
Note: Hyphenator.js has somewhat grown old. Have a look at its successor: https://github.com/mnater/Hyphenopoly

## Overview

Hyphenator.js is a free open source Javascript library that automatically hyphenates text on websites. It comes in handy as a _polyfill_ for legacy browsers that don't support CSS 3 hyphenation at all or for modern browsers that do hyphenation, but do not provide hyphenation dictionaries for a particular language.

Hyphenator.js …

* can be [included by the creator of the website](https://github.com/mnater/Hyphenator/blob/wiki/en_HowToUseHyphenator.md#using-hyphenator-on-your-website).
* can be used [as bookmarklet on any website](https://github.com/mnater/Hyphenator/blob/wiki/en_HowToUseHyphenator.md#using-hyphenator-as-a-bookmarklet).
* is [unobtrusive](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript).
* steps behind CSS 3 hyphenation if supported ([how to use Hyphenator_Loader](https://github.com/mnater/Hyphenator/blob/wiki/en_HowToUseHyphenator.md#hyphenator_loaderjs)).
* runs on the client in order that the HTML source of the website may be served clean and svelte and that it can respond to text resizings by the user.
* is highly configurable and has a [well-documented API](https://github.com/mnater/Hyphenator/blob/wiki/en_PublicAPI.md#public-api).
* relies on Franklin M. Liangs hyphenation algorithm ([PDF](http://www.tug.org/docs/liang/liang-thesis.pdf)) commonly known from LaTeX and OpenOffice.
* supports a [large set of different languages](https://github.com/mnater/Hyphenator/blob/wiki/en_AddNewLanguage.md#what-we-have-now).
* provides services for [customizing, merging and packing script and patterns](http://mnater.github.io/Hyphenator/mergeAndPack.html).
* also wraps URLs and Email adresses.
* is free software licensed under [MIT License](http://mnater.github.io/Hyphenator/LICENSE.txt) (Version 5.0.0 and above).

## Quick links

* [Code](https://github.com/mnater/Hyphenator)
* [Download](https://github.com/mnater/Hyphenator/releases/latest)
* [Documentation](https://github.com/mnater/Hyphenator/blob/wiki/en_TableOfContents.md#table-of-contents)

## Quick guide

1. [Download](https://github.com/mnater/Hyphenator/releases/latest) the recent version of Hyphenator.js
2. Use [mergeAndPack.html](http://mnater.github.io/Hyphenator/mergeAndPack.html) to configure and minify Hyphenator.js and hyphenation patterns.
3. Prepare your .html documents (i.e. add `hyphenate`-classes, set `lang` and add Hyphenator.js)
4. Test it!

Get [detailed instructions](https://github.com/mnater/Hyphenator/blob/wiki/en_HowToUseHyphenator.md#using-hyphenator-on-your-website).

## The bad parts

As with most things, there's a downside, too. Consider the following drawbacks before using Hyphenator.js:

* Hyphenator.js and the hyphenation patterns are quite large. Good compression and caching is vital.
* Automatic hyphenation can not be perfect: it may lead to misleading hyphenation like leg-ends (depends on the pattern quality)
* There's no support for special (aka non-standard) hyphenation (e.g. omaatje->oma-tje)
* There's no way for Javascript to influence the algorithm for laying out text in the browser. Thus we can't control how many hyphens occur on subsequent lines nor can we know which words have actually been hyphenated. Hyphenator.js just hyphenates all of them.

## Philosophy

### There is text and there is beautiful text

This beauty becomes manifest in content and representation. I'm firmly convinced that all written text (well, most of it) deserves fine typography, that we deserve it. While hyphenation is just one of many tesserae that forms the appearance of text, it may be an important one.

### There is code and there is sound code

In code there is readability, maintainability, performance and genius – and some constraints of technology. As a hobbyist programmer I often feel like a hobbit surrounded by wizards who campaign for these values. But being an agile hobbit gives me the freedom to find my own way through the woods (thankfully free from evil in this area). I'm constantly in search of the most performant path to circumvent the constraints of technology while maintaining readability and maintainability of my code. Sometimes this path is illuminated by a wizard<sup>[1](#fn1)</sup>.

## Issues and Requests

Each release is tested in various browsers with an [automated testsuite](./testsuite/). Nevertheless, there will always be bugs. Please don't hesitate to [submit them](https://github.com/mnater/Hyphenator/issues).

If you have a special use case and Hyphenator.js doesn't provide a solution, feel free to [submit a feature request](https://github.com/mnater/Hyphenator/issues).

And please, be patient. Hyphenator.js is a hobby of mine and sometimes other things have precedence…

* * *

(1) Some of my coding wizards are:

* Franklin Mark Liang for his beautiful [hyphenation algorithm](http://www.tug.org/docs/liang/)
* [Brendan Eich](https://brendaneich.com/) for making JavaScript a programming language (and [Douglas Crockford](http://www.crockford.com) for finding The Good Parts of it)
* Vyacheslav Egorov for his [deep insights to V8](http://mrale.ph/)
* Bram Stein for his [initiative on web typography](http://stateofwebtype.com)
4 changes: 2 additions & 2 deletions bower.json
@@ -1,6 +1,6 @@
{
"name": "Hyphenator",
"version": "5.1.0",
"name": "hyphenator",
"version": "5.2.0",
"main": "Hyphenator.js",
"ignore": [
"favicon.ico"
Expand Down

0 comments on commit fa43f57

Please sign in to comment.