Skip to content

Commit

Permalink
runkit example
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed May 27, 2018
1 parent 180cd33 commit 4b99248
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
37 changes: 36 additions & 1 deletion README.md
Expand Up @@ -9,8 +9,9 @@ The package consists of the following parts:
- _hyphenEngine.wasm_ (~1KB uncompressed): wasm code for creating pattern trie and finding hyphenation points.
- _hyphenEngine.asm.js_ (~7KB uncompressed, ~1KB minified and compressed): fallback for clients that don't support wasm.
- _pattern.hpb_ (sizes differ! e.g. en-us.hpb: ~29KB): space saving binary format of the hyphenation patterns (including their license).
- _hyphenopoly.module.js_: the node module

# Usage
# Usage (Browser)
Place all code for Hyphenopoly at the top of the header (immediatly after the `<title>` tag) to ensure ressources are loaded as early as possible.
You'll have to insert two script blocks. In the first block provide the initial configurations for Hyphenopoly_Loader as inline script. In the second block load Hyphenopoly_Loader.js as external script.
Also, don't forget to enable CSS hyphenation.
Expand Down Expand Up @@ -87,6 +88,40 @@ If the browser supports all required languages the script deletes the `Hyphenopo
## enable CSS-hyphenation
Hyphenopoly by default hyphenates elements (and their children) with the classname `.hyphenate`. Don't forget to enable CSS-hyphenation for the classes eventually handled by Hyphenopoly.

# Usage (node)

Install:
````
npm i hyphenopoly
````

````javascript
"use strict";

const hyphenopoly = require("hyphenopoly");

const hyphenator = hyphenopoly.config({
"require": ["de", "en-us"],
"hyphen": "",
"exceptions": {
"en-us": "en-han-ces"
}
});

async function hyphenate_en(text) {
const hyphenateText = await hyphenator.get("en-us");
console.log(hyphenateText(text));
}

async function hyphenate_de(text) {
const hyphenateText = await hyphenator.get("de");
console.log(hyphenateText(text));
}

hyphenate_en("hyphenation enhances justification.");
hyphenate_de("Silbentrennung verbessert den Blocksatz.");
````

# Automatic hyphenation
The algorithm used for hyphenation was developped by Franklin M. Liang for TeX. It works more or less like this:

Expand Down
22 changes: 22 additions & 0 deletions example.js
@@ -0,0 +1,22 @@
const hyphenopoly = require("hyphenopoly");

const hyphenator = hyphenopoly.config({
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
});

async function hyphenate_en(text) {
const hyphenateText = await hyphenator.get("en-us");
console.log(hyphenateText(text));
}

async function hyphenate_de(text) {
const hyphenateText = await hyphenator.get("de");
console.log(hyphenateText(text));
}

hyphenate_en("hyphenation enhances justification.");
hyphenate_de("Silbentrennung verbessert den Blocksatz.");
37 changes: 20 additions & 17 deletions nodetest.js
@@ -1,22 +1,25 @@
/* eslint-env node */
"use strict";

const time = process.hrtime();
const Hyphenopoly = require("./hyphenopoly.module");
const hyphenopoly = require("hyphenopoly");

const textHyphenators = Hyphenopoly.config({
"require": ["de"],
//"require": ["de", "en-us"],
"hyphen": "•"
});
const hyphenator = hyphenopoly.config({
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
});

textHyphenators.then(
//textHyphenators.get("de").then(
function ff(hyphenateText) {
console.log(hyphenateText("Silbentrennung verbessert den Blocksatz."));
console.log(`${process.hrtime(time)[1] / 1e6}ms`);
},
function err(e) {
console.log(e);
}
);
async function hyphenate_en(text) {
const hyphenateText = await hyphenator.get("en-us");
console.log(hyphenateText(text));
}

async function hyphenate_de(text) {
const hyphenateText = await hyphenator.get("de");
console.log(hyphenateText(text));
}

hyphenate_en("hyphenation enhances justification.");
hyphenate_de("Silbentrennung verbessert den Blocksatz.");
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -140,5 +140,6 @@
"lint": "eslint Hyphenopoly_Loader.js Hyphenopoly.js hyphenopoly.module.js",
"prepare": "npm run minify",
"minify": "sh ./tools/minify.sh"
}
},
"runkitExampleFilename": "example.js"
}

0 comments on commit 4b99248

Please sign in to comment.