Skip to content

Commit

Permalink
Update to version 1.0.0, register all pattern-files and add some logi…
Browse files Browse the repository at this point in the history
…c to it
  • Loading branch information
marcwieland95 committed Aug 27, 2015
1 parent dd87767 commit 0fd97f5
Show file tree
Hide file tree
Showing 37 changed files with 194 additions and 67 deletions.
10 changes: 3 additions & 7 deletions @examples/extended.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<html lang="en-us">
<head>
<title>Hyphenation extended</title>
<script>
// This is just a fix - will be removed in future versions
Hyphenator = [];
Hyphenator.languages = [];
</script>

<style>
/**
Expand Down Expand Up @@ -124,9 +119,10 @@ <h2 class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel v

</article> <!-- / CONTENT -->


<!--
<script src="../patterns/en-us.js"></script>
<script src="../patterns/de.js"></script>
-->

<!--<script src="http://zeptojs.com/zepto.min.js"></script>-->
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
Expand All @@ -136,7 +132,7 @@ <h2 class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel v
/* Some scripts here */
$('.first_column p, .second_column p, h2').hyphenate({
lang: 'en',
//hypenChar: true,
hypenChar: true,
//exceptions: 'Flatla|nd, bec|ause'
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion @examples/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<html lang="en-us">
<html lang="en">
<head>
<title>Hyphenation example</title>

Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Hypher for jQuery

A small and fast JavaScript hyphenation engine for jQuery (and also Zepto.js).
The core is based on the code of Bram Stein (@bramstein), which is build for jQuery and Node.js, but has some pitfalls in the jQuery version.
Hypher is the fastest javascipt based hypenation engine and is very lightweight. It's also very easy to use.

We are in a stable beta version. So feel free to use it. We're looking forward to some improvements.
The core is based on the code of Bram Stein (@bramstein), which is build for jQuery and Node.js, but has some pitfalls in the jQuery version.

##Setup
Have a look inside the `@example` folder.
Expand All @@ -21,12 +21,12 @@ Zepto.js
```

###2. Include language pattern
Make sure to include the right pattern for your desired languaga. You can use Hypher with multiple languages.
The patterns are inside the patterns folder. Also be sure to set the language code either in the settings or embeed it into the "lang" attribute inside the HTML-tag.
Make sure to include the right pattern for your desired language. You can use Hypher with multiple languages.
The patterns are inside the patterns folder (easiest way to do: copy the whole patterns-folder in your project). Be sure to set the language code either in the settings or embeed it into the "lang" attribute inside the HTML-tag.
You can use the language code (ISO 639-1) or also the WordPress Locale, which mostly includes the country. All patterns are compatible with the following list, see [Internationalization](http://wpcentral.io/internationalization/).

###3. Include Hypher
Include the main file after the language patterns. (`dist/jquery.hypher.js`)
Include the main file. (`dist/jquery.hypher.js`)
``` html
<script src="dist/jquery.hypher.js"></script>
```
Expand All @@ -39,7 +39,7 @@ Select the text you'd like to hyphenate and then call the function.
</script>
```

… if you customize some options, do it like this
… if you like to customize it a little, do it like this:

``` html
<script>
Expand All @@ -52,7 +52,7 @@ Select the text you'd like to hyphenate and then call the function.
```

##Customization
Your available settings in Hypher
Your available settings in Hypher:

| Option | Default | Type | Description
|--------|---------|------|------------
Expand Down Expand Up @@ -82,9 +82,10 @@ Be sure to disable the autoload settings inside hyphers function call.
```

##Changelog
`0.3.0` / `26.08.2015`
`1.0.0` / `27.08.2015`
- Autoload pattern files
- Improve patterns locale
- Register all pattern-files and add some logic to it
- Minor tweaks inside settings

`0.2.0` / `21.02.2015`
Expand Down
83 changes: 74 additions & 9 deletions dist/jquery.hypher.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*!
* hypher-for-jquery
* Version: 0.2.0
* Version: 1.0.0
* A fast and small JavaScript hyphenation engine for jQuery
* Author: @bramstein, @marcwieland95
* Site: http://http://www.bramstein.com//
* Site: http://http://www.bramstein.com/, http://marcwieland.ch/
* Licensed under the BSD license
*/
;(function($) {
Expand All @@ -13,9 +13,11 @@
* @param {Object} parent
* @param {Object} options
* @param {Object} nodeValue
* @param {String} language
*/
function Hypher(parent, options, nodeValue, language) {

// Set default options
var defaults = {

// {String} Set the correct language for your content
Expand Down Expand Up @@ -44,8 +46,72 @@

};

// Merge default with user option
this.options = $.extend({}, defaults, options);

// Asign pattern code to locale
this.langCode = {
// Register every pattern file here
// 'pattern-filename': ['locale', 'locale', etc.]
'be': [ 'be', 'be_BY', 'be-by', 'bel' ], // Belarusian
'bn': [ 'bn', 'bn_BD', 'bn-bd' ], // Bengali
'ca': [ 'ca' ], // Catalan
'cs': [ 'cs', 'cs_CZ', 'cs-cz' ], // Czech
'da': [ 'da', 'da_DK', 'da-dk' ], // Danish
'de': [ 'de', 'de_DE', 'de_CH', 'de_AT', 'de-de', 'de-ch', 'de-at' ], // German
'el-monoton': [ 'el', 'el-monoton' ], // Modern Monotonic Greek
'el-polyton': [ 'el-polyton' ], // Modern Polytonic Greek
'en-gb': [ 'en_GB', 'en-gb' ], // British English
'en-us': [ 'en', 'en_US', 'en-us' ], // US English
'eo': [ 'eo' ],
'es': [ 'es', 'es_ES', 'es-es' ], // Spanish
'et': [ 'et', 'et_EE', 'et-ee' ], // Estonian
'fi': [ 'fi', 'fi_FI', 'fi-fi' ], // Finish
'fr': [ 'fr', 'fr_FR', 'fr-fr', 'fr_BE', 'fr-be' ], // French
'grc': [ 'grc' ], // Ancient Greek
'gu': [ 'gu', 'gu_IN', 'gu-in' ], // Gujarati
'hi': [ 'hi', 'hi_IN', 'hi-in' ], // Hindi
'hu': [ 'hu', 'hu_HU', 'hu-hu' ], // Hungarian
'hy': [ 'hy' ], // Armenian
'it': [ 'it', 'it_IT', 'it-it' ], // Italian
'kn': [ 'kn' ], // Kannada
'la': [ 'la' ], // Latin
'lt': [ 'lt', 'lt_LT', 'lt-lt' ], // Lithuanian
'lv': [ 'lv' ], // Latvian
'ml': [ 'ml', 'ml_IN', 'ml-in' ], // Malayalam
'nb-no': [ 'nb', 'no', 'nb_NO', 'nb-no', 'nn_NO', 'nn-no' ], // Norwegian
'nl': [ 'nl', 'nl_NL', 'nl-nl', 'nl_BE', 'nl-be' ], // Dutch
'or': [ 'or', 'ory' ], // Oriya
'pa': [ 'pa', 'pa_IN', 'pa-in' ], // Panjabi
'pl': [ 'pl', 'pl_PL', 'pl-pl' ], // Polish
'pt': [ 'pt', 'pt_PT', 'pt-pt', 'pt_BR', 'pt-br' ], // Portuguese
'ro': [ 'ro', 'ro_RO', 'ro-ro' ], // Romanian
'ru': [ 'ru', 'ru_RU', 'ru-ru' ], // Russian
'sk': [ 'sk', 'sk_SK', 'sk-sk' ], // Slovak
'sl': [ 'sl', 'sl_SI', 'sl-si' ], // Slovenian
'sr-cyrl': [ 'sr-cyrl' ], // Serbian cyrillic
'sr-latn': [ 'sr-latn', 'sr', 'sr_RS', 'sr-rs' ], // Serbian latin
'sv': [ 'sv', 'sv_SE', 'sv-se' ], // Swedish
'ta': [ 'ta', 'ta_IN', 'ta-in', 'ta_LK', 'ta-lk' ], // Tamil
'te': [ 'te' ], // Telugu
'tr': [ 'tr', 'tr_TR', 'tr-tr' ], // Turkish
'uk': [ 'uk', 'uk_UK', 'uk-uk' ], // Ukrainian
};

Hypher.prototype.include = function(arr,obj) {
return (arr.indexOf(obj) != -1);
};

// Search for locale and set proper pattern file
for (var key in this.langCode) {
if (this.langCode.hasOwnProperty(key)) {
if (this.include(this.langCode[key], this.options.lang)) {
this.options.lang = key; // set current pattern code
break;
}
}
}

// Prepare url for request
if(this.options.path.substr(-1) === '/') {
this.options.path = this.options.path.substr(0, this.options.path.length - 1);
Expand All @@ -63,7 +129,7 @@
dataType: 'script',
cache: true,
error: function (xhr, ajaxOptions, thrownError) {
console.log('Pattern file can\'t be loaded - check path');
console.log('Pattern file can\'t be loaded - check path (' + this.options.path + ')');
}
});

Expand All @@ -78,7 +144,7 @@

// Autoload is on, but pattern still isn't available
if (typeof Hyphenator === 'undefined') {
console.log('Pattern isn\'t included - maybe pattern file doesn\'t exists or path linkes to wrong directory wrong');
console.log('Pattern isn\'t included - maybe pattern file doesn\'t exists or path linkes to wrong directory - check path (' + this.options.path + ')');
}

// Set variables
Expand All @@ -89,20 +155,20 @@
* @const
*/
// Load content from patterns when noting set in settings
if (!this.options.leftMin) this.options.leftMin = Hyphenator.languages[language].leftmin;
if (!this.options.leftMin) this.options.leftMin = Hyphenator.languages[this.options.lang].leftmin;

/**
* @type {!number}
* @const
*/
// Load content from patterns when noting set in settings
if (!this.options.rightMin) this.options.rightMin = Hyphenator.languages[language].rightmin;
if (!this.options.rightMin) this.options.rightMin = Hyphenator.languages[this.options.lang].rightmin;

/**
* @type {!Object.<string, !Array.<string>>}
*/
// Load content from patterns when noting set in settings
if (!this.options.exceptions) this.options.exceptions = Hyphenator.languages[language].exceptions;
if (!this.options.exceptions) this.options.exceptions = Hyphenator.languages[this.options.lang].exceptions;

this.exceptionsObject = {};

Expand All @@ -117,7 +183,7 @@
/**
* @type {!Hypher.TrieNode}
*/
this.trie = this.createTrie(Hyphenator.languages[language].patterns);
this.trie = this.createTrie(Hyphenator.languages[this.options.lang].patterns);

// Return hypenation to replace TextNode
this.hypens = this.hyphenateText(nodeValue);
Expand Down Expand Up @@ -295,7 +361,6 @@
if (options.lang !== undefined) {
language = options.lang;
} else {
//language = $('html').attr('lang');
language = document.getElementsByTagName('html')[0].getAttribute('lang');
}

Expand Down
Loading

0 comments on commit 0fd97f5

Please sign in to comment.